Skip to the content.

In distributed systems like ride-hailing apps:

If you don’t enforce this, due to concurrency or network delays, multiple drivers may accept the same ride or one driver may end up with overlapping rides.

Why ZooKeeper for locking?

ZooKeeper is often used as a distributed coordination service. One of its main features is distributed locks. Here’s why it fits:

  1. Strong consistency (CP system in CAP theorem)

    • ZooKeeper uses consensus protocols (like Zab, similar to Paxos).
    • This ensures that if two drivers try to acquire the same ride simultaneously, only one will succeed.
  2. Ephemeral nodes for lock ownership

    • A client (driver service) creates an ephemeral znode to acquire a lock.
    • If the client disconnects (e.g., driver app crashes), ZooKeeper automatically deletes the znode, releasing the lock.
  3. Sequential nodes for fairness

    • ZooKeeper can create ephemeral sequential znodes, ensuring a queue.
    • Example: Drivers trying to grab a ride get sequential nodes (lock-1, lock-2…), and only the lowest node gets the lock.
  4. Failure detection

    • If a driver’s session dies, ZooKeeper notices it and frees the ride lock.
  5. Cluster-wide coordination

    • Multiple ride-matching servers (distributed across data centers) can all use ZooKeeper as a single source of truth for locks.

Ride example with ZooKeeper lock


👉 So, ZooKeeper helps by:


⚡Now, in modern systems, people also use etcd or Consul (both inspired by ZooKeeper) because they’re lighter and easier to operate.

1. What are etcd and Consul?

etcd

Consul


2. Why are they used instead of Zookeeper?

ZooKeeper is older (came from the Hadoop ecosystem). etcd and Consul are newer and solve the same coordination problem with simpler APIs and modern features.

Feature ZooKeeper etcd Consul
Consensus Algorithm Zab (custom) Raft (simpler, well understood) Raft
API Style Java client, watch mechanism HTTP/JSON + gRPC API HTTP/JSON API
Ease of Use Harder to operate, JVM-based Lightweight, easy deployment (Go binary) Easy to deploy, lots of tooling
Main Use Cases Distributed locks, leader election Kubernetes cluster state, config, locks Service discovery, config, service mesh
Learning Curve Steep Moderate Moderate
Failure Handling Complex recovery, manual effort Raft makes recovery simpler Raft + built-in health checks

3. Why people moved from ZooKeeper to etcd/Consul?

  1. Operational Simplicity

    • ZooKeeper requires a JVM, config tuning, and is tricky to run.
    • etcd and Consul are just Go binaries, easier to start and manage.
  2. Modern APIs

    • ZooKeeper: requires client libraries (Java-centric originally).
    • etcd/Consul: simple HTTP/REST or gRPC APIs → easy for microservices in any language.
  3. Consensus Algorithm (Raft > Zab)

    • Raft is easier to understand, debug, and implement compared to Zab.
    • Raft became the industry standard for distributed consensus.
  4. Ecosystem Integration

    • etcd is the backbone of Kubernetes → almost every modern cloud-native platform depends on it.
    • Consul integrates well with HashiCorp tools (Terraform, Vault, Nomad) and also provides service mesh (like Istio).

4. Locking with etcd or Consul

Just like ZooKeeper, both support distributed locking:

Example (etcd lock flow):


✅ In short:


1. Redis Locking (Redlock, SETNX, etc.)

Redis can implement distributed locks using:

So yes — you can use Redis for ride-driver locking (e.g., “only one driver accepts ride”).


2. Problems with Redis for locks

The community debates a lot here. Redis is fast, but not strongly consistent like ZooKeeper/etcd/Consul.


3. Redis vs Zookeeper/etcd/Consul

Feature Redis (Redlock/SETNX) ZooKeeper / etcd / Consul
Primary Role Cache / data store Coordination / consensus
Lock Guarantees Best-effort (can fail on crash, partition) Strong consistency & correctness
Speed Very fast (in-memory) Slower but consistent
Failure Safety Weak (failover may cause duplicates) Strong (consensus ensures correctness)
Use Case Fit Short-lived, low-risk locks (e.g. leader election in cache invalidation) Critical locks (payments, ride allocation, DB migrations)

4. When to Use Redis for Locks?

✅ Good for:

❌ Not good for:


🔑 Final Takeaway

That’s why for ride-hailing (driver/ride matching) → you’d prefer ZooKeeper/etcd/Consul, not Redis.