0
\$\begingroup\$

At scale for multi-users, what is the proper way to implement timer updates?

I'm making a game where Users have buildings to upgrade which involve timers (Yes I know there are plenty of questions related to front-end: back-end this is not the same)

Every building exists in a table, with a completed field for a timestamp when we level up a building. I'm curious what the most common methods are to update the database when the timer runs out.

Many articles on stack exchange talk about setting up Cron jobs or having a game loop to poll against all stored timers. Still, I don't see those solutions scaling in a game with ~1000+ users nor useful in a browser-based Nodejs game relying on event updates over a game loop.

One solution I thought looked interesting was using RabbitMQ timed queues, so AMQP pushes a MySQL update at the scheduled time, but it sort of feels like a roundabout 'hack' of solving the problem compared to a more traditional method.

\$\endgroup\$
  • \$\begingroup\$ Most answers we have on this topic say do not set up a cron job or polling loop. Just update the timer state lazily the next time you read it. That way your timer servicing cost is proportionate to the client queries you answer (ie. the least you can do and still fulfill your client requests), rather than the number of timers you have running in total. \$\endgroup\$ – DMGregory Jun 14 at 19:56
  • \$\begingroup\$ @DMGregory problem is say the building gives you +1gold per hour, if you don't log in for 6h after the timer completes that's work not completed. \$\endgroup\$ – ChrisW Jun 14 at 21:08
  • \$\begingroup\$ No, that's work that you tally and apply retroactively at the time of login. If your server code is leaving that gold earning unaccounted for, your server code has a bug, and we can help you fix that bug if you walk us through it. \$\endgroup\$ – DMGregory Jun 14 at 21:09
  • \$\begingroup\$ Brilliant, thanks! Make this a post and I'll flag it as an answer! \$\endgroup\$ – ChrisW Jun 15 at 1:14
  • \$\begingroup\$ Want to write one up yourself? I think you have a better idea of where the problem was, and how you'll change your approach with this information. \$\endgroup\$ – DMGregory Jun 15 at 1:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.