Have only 1 pod process a task during startup

Issue

I have my deployment scaled across multiple pods. The requirement is that whenever the app starts up I want a piece of code to be executed. But I just want one pod to execute this code. How can this be achieved?

Right now I have a way – I set a flag in my DB and have my pods read the flag, which ever reads and locks the flag first will have to process the task.

Are there any drawbacks in my approach ? Is there any better way to do this?

Solution

I believe this is right approach. All pods need to have a way to understand if someone else is processing the task and doing through DB is best option. The only drawback is what if while the pod which picks it up, couldnt update the flag status. What would happen in that case?

The other option that i could think of is perhaps publishing a message to a message queue (a queue maintained outside of your pods, cloud platforms?). The idea is whenever a pod comes alive, they will check the queue and process it. Its similar to what database approach you have.

Answered By – Em Ae

Answer Checked By – Mildred Charles (GoLangFix Admin)

Leave a Reply

Your email address will not be published.