php - How to prevent two user get same unique key? -


i creating system generate unique keys. works now. but, haven't tested many users. users may click button , unique number, simple that.

but, how prevent multiple users getting same unique keys,if press button in same time (even in ms scale)? button on client side, must in end.

this unique key looks like:
19/xxxxxx-abc/xyz

the xxxxxx auto increment number 000001 999999. have code didn't know if it's reliable enough handle issue.

    $autoinc = $this->mpenomoran->get_surat($f_nomor)->jumlah_no+1; //count data in table , added 1      $no_1   = date('y')+2;     $no_2   = str_pad($autoinc, 6, '0', str_pad_left);     $no_3   = "-abc/xyz";      $nomor      = $no_1."/".$no_2.$no_3;       $returned_nomor     = $nomor;                 $success =  array ('nomor' => $returned_nomor); //sent unique keys user's view 

it seems don't want come out , tell platform this, or limitations platform are.

the first thing jumps out format limited year, 999999 total unique keys. odd, presumably understand limit, , need put in code deal hitting maximum number.

approaches

redis based

this simple redis server using incr. since incr atomic, have solution creating key named year + 2, should not exist, , using incr on there on out.

you need utilize php redis client, , there variety of them strengths , weaknesses each i'm not going go into.

redis great caching, if @ possible first thing into.

mysql based

there few different solutions using mysql. involved, i'll outline them because don't want spend time writing novel.

note: you need translate these appropriate php code (mysqli or pdo) noted, parameters passed, transactions started etc.

mysql - create own sequence generator

  1. create table named "sequence" basic structure:

    name varchar(2) pk nextval unsigned int default 1 engine=innodb

the underlying query this:

begin_trans; select nextval sequence name = '$no_1' update; update sequence set nextval = nextval + 1; end_trans; 

this code emulates serialized oracle style sequence. safe concurrency standpoint, because mysql lock row briefly, increment upon completion.

mysql - autoincrement on multi-value pk

this comes caveats.

it incompatible replication. underlying table must myisam

name varchar(2) pk lastval unsigned int auto_increment pk engine=myisam 

your underlying query be:

insert sequence (name) values ('$no_1') 

this depends on mysql supporting multi-column key 2nd column auto_increment. it's behavior such acts sequence each unique name.

you use relevant api's built-in approach getting mysql last_insert_id(). example pdo

other alternatives

you use semaphores, files locking, , sorts of other ideas create sequence generator work in monolithic (one server everything) environment. mysql , redis serve cluster, more robust options standpoint.

the important thing whatever do, test out using load tester siege or boom generate multiple requests @ web level.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -