I’m trying to fix race conditions by applying Atomic Locks, like stated in the docs: https://laravel.com/docs/7.x/cache
My environment file is configured to use the database
driver and I have both cache
and cache_locks
table migrated.
I can see some contents being written in the cache
table but the cache_locks
is always empty.
This is my code logic to fix race conditions:
$lock = Cache::lock("{$myUniqueId}_lock", 5);
try
{
$lock->block(5);
// Perform some database operation
DB::statement("UPDATE my_table SET some_column = {$some_data}");
}
catch (LockTimeoutException $e)
{
throw new Exception('Error with atomic locks');
}
finally
{
$lock->release();
}
While performing a stress test on postman with 20 virtual users:
- I get the MySQL error
Integrity constraint violation: 1062 Duplicate entry '....' for key '....'
, meaning the locks are not being performed correctly - The table
cache_locks
is always empty, it doesn’t hold any value, I keep refreshing but I don’t see anything being written there - The
LockTimeoutException
is never thrown
What am I missing?