Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chandram97's avatar

Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction

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):

  1. Web Load Balancer
  2. Under Web Load balancer I have two Web Servers, which hosts react and Laravel
  3. When User Request reaches Web LB it will go to one of Web Server and loads react Application for user.
  4. 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

0 likes
8 replies
Tray2's avatar

Deadlock means that another process is locking the record you are trying to update in the database.

I take it you are using database transactions.

They work something like this

  1. Select the record(s) that you need to update and lock them
  2. Update the record(s)
  3. Commit or rollback the changes
  4. Release the lock

While the record is locked another process or user can't update it until the process or user locking it does a rollback or a commit.

There are a few ways to solve this but it requires a bit more insight into your application.

3 likes
chandram97's avatar

@tray2 I know about dead locks but any how thank you for the info.

I am not sure what you mean by Database transactions

I am using laravel Default Functions. I have not done any customization on Login and Logout. Yes here will some internal business logics like user_activity and some Database verifications for data exists or not. Apart from that here is no customization on Login and Logout.

Below are the one I am using:

Auth::login($user, true); // For login
//For logout 
 Auth::logout(); 
Session::flush();

Also you mentioned you need more insight of my application. Can you let me know what exactly you want to know.

Tray2's avatar

By default Laravel doesn't use transaction but rather do the insert or update and then commit the changes. If you use the trait DatabaseTransactions you can do more updates or inserts before it commits it to the database.

If you haven't enabled Database transactions then you can ignore the above workflow.

It clearly has something to do with the sessions table.

What happens if you don't do the session flush? Does it remove the deadlock?

chandram97's avatar

@tray2 Sorry for the delay, Actually we are setting some variables in session on some api calls. If I use Auth::logout() it is not clearing those session variables. So I am using session::flush()

So you have any suggesion in this case.

Tray2's avatar

Try by just unsetting those values instead of flushing everything.

// Forget a single key...
$request->session()->forget('name');

// Forget multiple keys...
$request->session()->forget(['name', 'status']);
chandram97's avatar

Tried that and monitorred for a day. Still same issue.

I have moved session->forget to top of Auth::logout()

Also facing same issue

anmolssm's avatar

@tray2 , i am also having a same issue. I tried the solution as you suggested. But do you have any other options?

Please suggest..

Snapey's avatar

it's quite possible that you are experiencing an issue with database sessions that has long since been fixed

@anmolssm if you are also on 5.8 then I suggest getting onto a supported version as the first step

Please or to participate in this conversation.