KatInterview

Single-Active Worker Coordination with Expiring Redis Locks

System DesignModel answer
MediumModel answerSystem Designleader-electionredisdistributed-systems

Last updated

Design a way for a fleet of identical workers to pick exactly one active instance using Redis expiring keys, covering failover, fencing, pauses, clock assumptions and Redis outages.

Choosing One Active Worker with Redis

A fleet of up to 50 identical, stateless service instances runs the same code. Exactly one of them should be active at any moment and run a singleton job (for example, a scheduler that fires periodic tasks, the sole writer for a data partition, or the only consumer of a queue). All other instances stay on standby and must be ready to take over.

Design a time-limited lease protocol that uses Redis to decide which instance is active. A single primary Redis endpoint is acceptable. You may use any Redis feature, such as SET NX PX, Lua scripts or Pub/Sub.

What your design must cover

  • Election: at most one active instance at any time.
  • Failover: if the active instance crashes or becomes unhealthy, another instance takes over within a bounded time.
  • Lease and renewal: activity is time-bounded and continuously extended while the holder is healthy.
  • Correctness: how you prevent or limit split brain, a dead holder blocking progress, dependence on clocks, and the effects of network glitches and long GC or stop-the-world pauses.
  • Operations: which metrics and logs you would add, and how the system behaves when Redis is unreachable.

Deliverables

  1. A precise description of the algorithm or protocol.
  2. The Redis data model: keys, values and TTLs.
  3. Behavior in edge cases and failure modes.

Constraints and assumptions

  • Up to about 50 instances, each running the same election loop.
  • One primary Redis endpoint. Redis is not a consensus (CP) system, so state clearly where that limits your safety guarantee.
  • Load on Redis is tiny (a few small keys, a low rate of acquire and renew calls). Throughput is not the concern; failover time and safety are.
  • Partitions, packet loss and process pauses are all in scope.
  • Having no active instance for a short time is acceptable. Having two instances doing conflicting work is not. Safety takes priority over availability.
  • Do not assume synchronized wall clocks across instances.

Questions worth raising with the interviewer

  • Does the singleton job write to an external system that could check a fencing token, or is it purely in-memory so that idempotency is the only protection?
  • What is the failover target after a hard crash? This sets the TTL.
  • How bad is a short overlap of two active instances: is the work idempotent, or would a stale action corrupt data?
  • Is Redis really a single primary, or is there an asynchronously replicated standby to reason about?
  • Is TTL-bounded takeover good enough, or is near-instant handover required?

Hints

Hint 1

One key holding a unique instance ID, created only if absent and with an expiry, gives you both mutual exclusion and automatic release after a crash. Think about what must be atomic when you extend or delete it.

Hint 2

Distinguish three renewal outcomes: renewed, definitely lost the lease, and could not reach Redis. Each requires a different reaction, and the third must make the instance stop acting before its own deadline passes.

Hint 3

A lease alone cannot stop a paused process from acting after it has lost leadership. Look for a monotonically increasing number that the downstream resource can check and use to reject stale writers.