In my project, I'm going to create lots of mini games. Those mini games can last 30 seconds to 1 week (the duration of the game is decided when it is created and is fixed).

The number of concurrent games can be large (more than 1 game per user and we hope to have many users of course :-)).

Nothing really happens during the game. They are composed of just a winning condition and end time.

Once a game reaches its end, I need the server to check the winning condition (using a remote service) and finalize the game (decide who won and give points). During the time the game takes place, the server does not need to manage it.

You can think of the games as bets. Someone places a bet that something will happen in 30 seconds and after 30 seconds I need to check if that something really happened and update the system.

Latency of the game finalization should be very fast, especially for short games. Not more than a second or two. I.e, for a game of 30 seconds, I should be able to update if it won or lost a second or two after it ended.

I thought about persisting each game to a row in a sql table and schedule a task to run when it ends and finalize it.

For example, a user starts a game of 1 minute. The server will create the game in the db and schedule a task to run in 1 minute (and pass it the game's id). When the task triggers, it will finalize the game and update its status in the db.

Are there other ways to solve this problem? Maybe something that does not require having a task per game? I thought also about having 1 task that will check for all finished games but for short games (e.g. 30 secs) the polling time must be very short and I'm not sure how well it can scale.

  • How will your system know the result of each game once it is finished? Will the players/game call the remove service and provide the results?
    – Roman Mik
    Commented Dec 18, 2014 at 19:41
  • @RomanMik The system will use a remote service to know the result. It will send the details of the game and the service will decide the result.
    – daramasala
    Commented Dec 18, 2014 at 19:48
  • @Snowman My question is not about how to implement scheduled jobs. I want to know if scheduled jobs are the right tool for my problem.
    – daramasala
    Commented Dec 18, 2014 at 19:49
  • @daramasala sorry, I misunderstood the question.
    – user22815
    Commented Dec 18, 2014 at 19:56
  • Why not just put a timer in the game and have it report an automatic loss if the timer expires? Commented Dec 29, 2014 at 6:05

3 Answers 3


Since you mentioned scale-ability and speed is important to you, I would like to propose the Message Queue as the architecture for your application. You will need to research examples in your technology of choice. Here's a Wiki for the general description (http://en.wikipedia.org/wiki/Message_queue).

The idea is simple. Clients (your games) send messages(game results) to the Queue Manager. The Manager dispatches these messages to subroutines that process them. As your application grows you can add more managers to process messages. Therefore, scale horizontally. As you add more games (that most likely will have different result formats and subroutines to process those results...i.e chess, poker or Go) you can add more subroutines that can process those results. Here you scale vertically.

  • I think my use case is a bit different. The games do not have event. You can imagine it is a bet on something. So someone creates a bet that in 30 seconds from now, some value will be X. I want to start a task in 30 seconds that will check the value and decide if the bet won.
    – daramasala
    Commented Dec 18, 2014 at 20:24
  • The same idea applies to your edit. Instead of the game sending the results to the queue, it sends its registration request and expiration time. The queue manager assigns it to a handler, and handler will wait for the game to end and then invoke the post game procedure to calculate the score.
    – Roman Mik
    Commented Dec 18, 2014 at 20:36
  • This sounds a lot like the solution I proposed with the message queue being used to communicate with the component that does the scheduling. Still, it requires a task (or handler) per game so it doesn't really add anything new (besides an implementation detail).
    – daramasala
    Commented Dec 20, 2014 at 15:02

I'm pretty sure there is some canonical representation of my problem. For example, you can think of the games as tasks that need to start at a specific time (the game's end time). I guess this is some kind of scheduling problem.

For my case, I have 2 solutions.

  1. The "naive" solution - I have a task that wakes up every second or so, loads all the games that need to be finalized from the db and finalizes them. Only one task handles all the games which means that when there are a lot of games, latency will increase. But at least it degrades slowly and doesn't just fall apart. It also does not depend on the number of concurrent games, only on the number of games that end at each second.

  2. Since I'm using Azure, I thought about using the Storage Queue. Messages in the Azure storage queue can have an "invisibility" time, so I can put each game in the queue and set it to be invisible until it is time for it to be finalized. Then I can have as many threads as I like reading games from the queue and finalizing them. But I'm not sure this the queue is optimized for such a use case.

  • This is not an answer to the question. You should edit the question to incorporate it. However, doing so would make it even more "too broad" than it already is.
    – msw
    Commented Jan 18, 2015 at 7:28

I would go for the approach of using table,however I would do a slight modification to the way they are processed. Instead of spinning up a task for each and every row I would create number of monitoring tasks, and they would execute a SQL against current table to find out which games needs finalizing at given point, and based on number of monitoring tasks they would distribute the number of rows among themselves based on some sort of sorting and taking certain amount of rows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.