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

avrahamm's avatar

Laravel Race Conditions tutorial

Hello Laracasts

I can't find a Race conditions, locks and DB transaction Laravel tutorial.

Can you please suggest one?

Thank you,

Avraham

0 likes
3 replies
jlrdw's avatar

If using MySql, the manual covers it.

https://dev.mysql.com/doc/c-api/8.0/en/getting-unique-id.html

And clearly states:

Quote

For LAST_INSERT_ID(), the most recently generated ID is maintained in the server on a per-connection basis. It is not changed by another client. It is not even changed if you update another AUTO_INCREMENT column with a nonmagic value (that is, a value that is not NULL and not 0). Using LAST_INSERT_ID() and AUTO_INCREMENT columns simultaneously from multiple clients is perfectly valid. Each client will receive the last inserted ID for the last statement that client executed.

Unquote

If mass inserting data, the laravel documentation covers upserts.

https://laravel.com/docs/9.x/eloquent#upserts

1 like
avrahamm's avatar

@jlrdw Thank you for your answer.

I followed the links you kindly suggested.

I am reading the https://dev.mysql.com/doc/refman/8.0/en/sql-transactional-statements.html

section may be relevant.

Yet, In my case https://laracasts.com/discuss/channels/laravel/db-transaction-and-race-conditions

it is about updating locked row from another asynchronous code section/"thread".

Is there more Laravel style tutorial uses DB:transaction, sharedLock

while trying to update some shared DB table locked rows?

Running in async manner with race conditions?

Thank you.

Please or to participate in this conversation.