I have code where 2 separate calls are made to it simultaneously. It writes to the database.
For example:
$x->KEY = 'ABC'; //Primary key
$x->TIME = date('His') . substr(microtime(), 2, 2); //Primary key
$x->save(); //saves the record in DB
Both the processes come from a external service. So I have no control over that. As you may see, the primary key involves TIME which is causing Duplicate entry errors and multiple executions happen at the same time.
I've tried putting in DB entries describing the process has been executed once, so that the 2nd process doesn't go through. However, this has led to only Duplicate DB errors.
I've also tried lagging the execution by a rand($microsecs), hoping that the 2 calls would choose random delay times, but that doesn't work all the time either.
Can you please suggest alternate ways to solve this problem?
Thank you!!