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

dewaadiperdana's avatar

Testing Race Condition on Laravel API

I have an API that generates initial data and saves it to the database which has a unique column and it's manually generated by the service. But somehow, in certain request, the API throw QueryException that says duplicate key: ... because the service attempt to write the same generated id to the unique column. I assume that the race condition causes this issue. And I've read about pesimistic locking on the laravel docs and I did implement it in my code. But just some curiosity and I need to make sure that the issue was caused by race condition.

So my question is how do I reproduce this case for the sake of testing, or what kind of approach should I take to simulate this race condition thing on my laravel API?

Thanks

0 likes
0 replies

Please or to participate in this conversation.