Rows Rotation For Mysql Table Consumer Process in Round Robin Rows Rotation For Mysql Table Consumer Process in Round Robin sql sql

Rows Rotation For Mysql Table Consumer Process in Round Robin


Since you are open to use third party integrations, Redis is a good choice for distributed locking.

You can achieve the desired behaviour by making an entry related to each website in the redis.

setnx command is useful in locking,as it returns 0 if the value is already set.

Whenever a bot is process on a website, use redis command
setnx WEBSITE_NAME "BOTID"
with some expiry value.
You can set an expiry value of twice the average time it takes to complete processing a website.

If the response for the command is 1, i.e website is open for processing.Response 0 indicates that a bot is processing the website.

Upon completion of processing, BOT should remove the entry in redis.

Setting expiry removes the lock automatically,even in the event of bot failing to remove the lock from Redis.

Redis is single threaded in server side and executes the commands sequentially, hence it takes care of the concurrency.


The challenge here is to handle the various exceptions that will disrupt the expected flow and how to recover from them. In order to design the actual solution, you need to take into account average process times, how many bots working on how many websites and the severity of failure and whether it can be fixed as a side process. If the websites are within your control (not 3rd party sites), I would prefer to use instead a messaging (pub-sub) type of solution where your infrastructure notifies an agent on the website to handle the update, and the same agent ensures that only a single update is taking place at one time (per your requirement).

If this type of setup is not possible, then your next bet is to use something like what @Akina is suggesting, but also come up with a recovery action for every pitfall that might happen, including handling race conditions, bots timing out or returning incomplete tasks, websites returning unexpected responses, etc. This might get a bit tiring after a while if someone does not keep an eye on the process and adjust it to handle every unexpected surprise you're bound to see over the long term.