I've written a basic RESTful API in Sinatra/Ruby for handling betting markets. Due to the nature of fixed odds, I need to recalculate the current odds in the market over a specific interval, lets say five minutes. Normally you could put something like this a cron job, or run clockwork etc to fire off an event every five minutes, however my trouble is that I might be running hundreds or thousands of these markets at once and I don't want them all synced to the same clock if I can avoid it.
My first thought is to put an item into a delayed job queue for regenerating the odds at a specific interval, however I can guarantee that the job will run on time (or at all in the event of the queue server goin开发者_运维百科g down)
The most elegant way of handling this would be to just have a time stamp in the database and automatically recalculate odds once a new bet comes in, if the odds are past a certain time. The downside to this is that if the betting is slow, I'm constantly going to be rejecting the newest bet because I'm invalidating the odds before I'm placing it. Not good.
Any thoughts?
Not sure that I understand the background of your problem completely, but maybe this strategy can be useful:
Split all your N
markets into m
chunks, say by using modulo of dividing their ID by m
.
If your recalculation period is T
, run a recalculation every T/m
, recalculating only one chunk at a time.
精彩评论