Sobes.tech
Middle+

How to ensure no duplicates when switching between machines (one degrades, the other takes over)?

sobes.tech AI

Answer from AI

To avoid duplication of processing when switching between machines (failover), it is necessary to ensure consistency and uniqueness of task processing. Main approaches:

  • Leader election — only one machine in the cluster is considered active and processes tasks. When the leader degrades, another node becomes the leader.

  • Using distributed locks — for example, via Redis, ZooKeeper, or etcd, to guarantee that a task is executed by only one instance.

  • Idempotency of operations — so that re-running the same task does not lead to errors or duplicate effects.

  • Storing state in a shared storage — so that when switching, the new machine can understand which tasks have already been completed.

Example with Redis for task locking:

func acquireLock(client *redis.Client, key string, ttl time.Duration) (bool, error) {
    ok, err := client.SetNX(ctx, key, "locked", ttl).Result()
    return ok, err
}

// When switching, attempt to acquire the lock; if unsuccessful — the task is already being executed

Thus, switching occurs without duplication, as only one machine holds the lock and processes tasks.