Server Process Redundancy and Race Conditions v1.0
Server Process Redundancy and Race Conditions v1.0
$current_user_point = $user->point;
$points_needed = $voucher->price_point;
// Check if the current user point is above points needed for redeem
if ($current_user_point >= $points_needed) {
// Decrease the user's point
$user_point_after = $current_user_point - $points_needed;
$user->point = $user_point_after;
$user->save();
...
The most feasible on Fresh Project The most feasible on Running Project
1. Queue Process 1. Update … Where …
2. Update … Where … 2. Throttle
3. Throttle 3. Queue Process
4. Locking Table / Row 4. Locking Table / Row
Summary
Race Condition by Concurrent Requests is an Edge-Case that can be happened by mistake or on
purpose.
The probability of this Edge-Case is slim if the system is not too complicated and not takes a long
time on each process. On the heavy process (need 3rd party API, or run many heavy queries),
probability of Race Condition will depends on time it takes from Get the Data to Update the
Data on Database.
Queue Job is the best practice to prevent Race Condition, but will severely decrease the
performance of the Application/System since the process will not be real-time (best case is semi-
real-time).
Throttle is not considered a good solution, but will eliminate most Race Condition if done right.
So, yeah…
Thank You