Support the ongoing development of Laravel.io →
Database Queues
Last updated 2 years ago.
0
  1. You could design this process to run as one loop that gives away all of the "free" points that users have donated. Remove any user that has received > 1 point until all points are assigned. One advantage of this approach is that you could do it all in a single database transaction.

  2. Keep track of the points donated per day some other way, say in Redis or in a database table. Set your worker queue so that there is no concurrency, i.e., number_of_workers=1. On each worker run, give away a point, and note who received it. If number_received(today) > 1 then find someone else.

Anywhere you can avoid concurrency problems, you should.

0

Also, quick note on #2 and concurrency in general: this is what database transactions are for. Database transactions make sure your rules are consistently enforced, even if the point giveaway happens in real time. Something like:

BEGIN TRANSACTION; SELECT * FROM points_to_assign LIMIT 1 FOR UPDATE; SELECT * FROM assigned_points WHERE day = today() FOR UPDATE; -- grab random user for update, locking that record -- perform update -- make sure that assigned_points for that user is < 2 or ROLLBACK COMMIT;

This is a good candidate for a database stored procedure.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.