I have a Background service microservice in dot net and I am deploying it to Kubernetes as a container. If I have two pods then each pod will perform the same database operation at the same time, how can I avoid this? Please help
I am expecting, if my one pod is accessing a database and fetching few records, then my another pod should not fetch the same records, if they fetch the same records, then they would perform same operation twice in the different pods
I see three solutions :
UPDATE lock SET worker_id="1234", date_time=CURRENT_TIMESTAMP() WHERE lock="database_lock" AND (worker_id="1234" OR date_time < CURRENT_TIMESTAMP() - 30000);
. If the row can be updated, the worker has successfully "acquired" the lock and can process the database. If not, it remains idle. In both cases, it will try to acquire the lock again in 10 seconds. The lock can be acquired by a worker if it already has the lock, or it the lock has been acquired more than 30 seconds ago. So a worker that acquires a lock will normally keeps it until it its death.I suggest the first solution (https://en.wikipedia.org/wiki/KISS_principle).