Sobes.tech
Middle

There are 5 instances of a service with a cron job. How to ensure that the task is executed only once, not five times?

sobes.tech AI

Answer from AI

To ensure that a task runs only once among 5 instances, a distributed locking or leadership mechanism must be implemented. One common approach is to use an external storage supporting atomic operations, such as Redis or a database.

Example with Redis and Python:

import redis
import time

redis_client = redis.Redis()
lock_key = "cron_task_lock"
lock_timeout = 60  # lock lifetime in seconds

# Attempt to acquire the lock
if redis_client.set(lock_key, "locked", nx=True, ex=lock_timeout):
    try:
        # Execute the task
        print("Task is being executed by one instance")
        # ... task code ...
    finally:
        redis_client.delete(lock_key)
else:
    print("Another instance is already executing the task")

Here, set with nx=True sets the key only if it does not exist, ensuring only one instance gets the lock. ex sets the key's lifetime to prevent perpetual locking in case of failure.

Alternatives:

  • Use distributed coordination systems like ZooKeeper or etcd.
  • Use specialized libraries for distributed tasks (e.g., Celery with a message broker).

Thus, the task is executed only by one instance, while others skip execution.