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

Haresh's avatar

How to handle concurrent requests

I'm developing API for quiz game in which there are 3 tables:

1)Games: various games stored here

2)Player: Players involved in a various game (game_id foreign_key) with its turn number

3)answer: Here the user's answer will be store

Now when the game starts question will be displayed to the player with turn_number 1 and the player will submit an answer. After that next question will be displayed to the player with turn_number 2 and the player will submit an answer.

Now, We have one lifeline that if the next player not submitted his/her answer than the previous player can undo their choice. So if a player with turn 2 not submitted their answer than a player with turn 1 can undo his/her choice and can change their answer.

But my issue is when a player with turn 1 click undo and a player with turn 2 submit an answer at the same time. Because of this, my data is becoming inconsistent.

I want to decide which request came first and when undo request is in progress no one can submit an answer or when submit answer request is in progress no one can undo.

How can I handle this situation? Please help.

Note: When undo/submit answer there will be a change in multiple tables, not one table.

0 likes
3 replies
drewdan's avatar

Sounds like you ought to keep the connection open and listen to events using a broadcaster like pusher. You'd be able to listen on the front end for a event and your script could disable a button or reload the page when it detects certain events.

https://laravel.com/docs/7.x/broadcasting

Have a look at that and see if it fits what you want

1 like
Haresh's avatar

My system not like Socket. We are developing game with Simple API so i need solution to not update a value of database while another user updating it. And there are multiple table i was dealing with when user submits or undo their choice.

drewdan's avatar

I am not well versed in this, but, iirc, pessimistic locking might be the solution you need: https://laravel.com/docs/master/queries#pessimistic-locking

You can lock certain rows in the table to prevent one http call overiding another. I believe this will work on a first in first out basis on your http calls.

The one thing this cannot account for is the connection speed of your users. If one user fires of a request, and a second user fires off a request, the second user may have a quicker connection and the request may hit the server before the first user. I have no idea how you would account for such a thing

Please or to participate in this conversation.