Hi,
I am getting Deadlock exception:
[2020-12-29 08:35:16] PRODUCTION.ERROR: SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction (SQL: update `sessions` set `payload` = abc, `last_activity` = 1609202116, `user_id` = 0000, `ip_address` = 0.0.0.0, `user_agent` = safari where `id` = sdfasd) {"userId":0000,"exception":"[object] (Illuminate\Database\QueryException(code: 40001): SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction (SQL: update `sessions` set `payload` = abc, `last_activity` = 1609202116, `user_id` = 0000, `ip_address` = 0.0.0.0, `user_agent` = safari where `id` = sdfasd) at /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664, Doctrine\DBAL\Driver\PDO\Exception(code: 40001): SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction at /var/www/html/laravel/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDO/Exception.php:18, PDOException(code: 40001): SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction at /var/www/html/laravel/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:141)
My Application architecture is (IN case if you need):
- Web Load Balancer
- Under Web Load balancer I have two Web Servers, which hosts react and Laravel
- When User Request reaches Web LB it will go to one of Web Server and loads react Application for user.
- if React calls any API, it will go via Web LB to one of the Web Server and then hits laravel.
Now when ever there is Update / Delete on Session table i am getting Dead Locks. Initially before 6 months we use to get 3-4 dead locks in both servers. But now the count of dead locks has increased minimum 50 maximum we show around 200.
I am using
- Laravel : 5.8
- Database: Mysql
- Laravel Config Session = database
Can anyone let me know how I can prevent this deadlocks to happen.
One My Staging Env:
I just I have one Server So I am not getting any deadlocks, need to reproduce the issue in staging so that i can test any solution provided is working fine or not