mpwjames
6 months ago

Atomic Database Row Selection

Posted 6 months ago by mpwjames

I have a model say Apple.

Then I have about 150 simultaneous requests coming to my Laravel app endpoint say AssignAppleToBushel

In that request I select the oldest apple and assign it to the Bushel model's apple_id field.

The problem I'm having is due to the high number of requests some Bushels are grabbing the same Apple

How can I make sure that the one I grab will not be grabbed by anything else.

I've tried things like:

  • lockForUpdate (which i don't believe I'm using correctly)
  • Database transactions.
  • Randomizers to select the 50 oldest apples and randomly select one from there.

Whats the best solution for this case?

Thanks!

Please sign in or create an account to participate in this conversation.