Bird
Raised Fist0

Zookeeper for Leader Election and Distributed Configuration

HLDmediumInterview freq: common
Choose your preparation mode4 modes available
🎯
ZooKeeper for Leader Election and Distributed Configuration
45 min · senior
senior

You are designing a distributed coordination service for a 50M DAU startup that requires reliable leader election and distributed configuration management. The system must support multiple data centers, provide strong consistency guarantees, and maintain P99 latency under 200ms for all operations. Security is critical, so all data in transit and at rest must be encrypted. The system should also handle network partitions gracefully and support offline client operation with eventual sync.

  • Support 50 million daily active users with up to 100,000 concurrent clients
  • P99 latency for leader election and config reads/writes under 200ms
  • End-to-end encryption for data in transit and at rest
  • Multi-datacenter replication with strong consistency
  • Offline client support with eventual synchronization
  • Graceful handling of network partitions without split-brain
📋
Guided Steps
1/5
Step 1
Choosing the Coordination Service Technology
Which technology will you use for leader election and distributed configuration, and why?
💡 Hint
Consider a system that provides strong consistency, ephemeral nodes, and watches for event notifications.
Use Apache ZooKeeper because it provides ephemeral znodes for leader election, strong consistency via Zab protocol, and watch mechanisms for configuration change notifications.
Mid-level: Names ZooKeeper as the coordination service. Senior: Explains ZooKeeper's Zab consensus protocol, ephemeral nodes for leader election, and watch-based event notifications for config changes.
⚠️Common Wrong Answer
Using a distributed database like Cassandra for leader election - it lacks ephemeral nodes and watch notifications, leading to stale leadership info and higher latency.
Step 2
Ensuring Strong Consistency and Low Latency
How will you ensure strong consistency and keep P99 latency under 200ms for leader election and config operations?
💡 Hint
Think about ZooKeeper's quorum size, read/write paths, and how to optimize for latency without sacrificing consistency.
Configure ZooKeeper ensemble with an odd number of nodes (e.g., 5) to tolerate failures and use read requests from followers with sync guarantees; writes go through leader with Zab consensus ensuring linearizability within 200ms P99 latency.
Mid-level: Mentions quorum and leader-based writes. Senior: Details Zab protocol phases, read-from-follower optimization with sync, and tuning session timeouts to balance latency and failure detection.
⚠️Common Wrong Answer
Using a single ZooKeeper node to reduce latency - causes single point of failure and breaks availability under node failure.
Step 3
Handling Multi-Datacenter Replication and Network Partitions
How will you design multi-datacenter replication to maintain strong consistency and avoid split-brain during network partitions?
💡 Hint
Consider ZooKeeper's leader election and quorum requirements across data centers and how to handle WAN latency.
Deploy separate ZooKeeper ensembles per data center with cross-ensemble leader election coordination using a global consensus system like etcd or Raft-based proxy; avoid active-active writes across DCs to prevent split-brain.
Mid-level: Suggests multi-DC ZooKeeper clusters. Senior: Explains why active-active multi-DC writes break Zab consistency, and how a global consensus proxy or leader arbitration prevents split-brain and maintains strong consistency.
⚠️Common Wrong Answer
Running a single ZooKeeper ensemble spanning multiple DCs - leads to high WAN latency and availability degradation during partitions.
Step 4
Implementing End-to-End Encryption
How will you implement encryption for data in transit and at rest in the ZooKeeper system?
💡 Hint
Think about TLS for client-server and server-server communication, and encryption of persistent storage.
Enable TLS for all client-server and server-server communication in ZooKeeper; use encrypted disks or filesystem-level encryption for ZooKeeper data directories to secure data at rest.
Mid-level: Mentions TLS for client connections. Senior: Details mutual TLS for server-server communication, certificate rotation, and encryption of transaction logs and snapshots on disk.
⚠️Common Wrong Answer
Only encrypting client-server traffic - leaves inter-server communication and data at rest vulnerable to interception or compromise.
Step 5
Supporting Offline Client Operation and Eventual Sync
How will clients operate offline and synchronize configuration changes once reconnected?
💡 Hint
Consider ZooKeeper session semantics, watches, and how to handle missed events during offline periods.
Use ZooKeeper session timeouts with ephemeral nodes to detect offline clients; clients cache last known config version and upon reconnect perform a sync operation using ZooKeeper's versioned znodes to fetch missed updates atomically.
Mid-level: Describes session timeouts and ephemeral nodes. Senior: Explains versioned znodes, watch re-registration on reconnect, and conflict-free sync using ZooKeeper's atomic update guarantees.
⚠️Common Wrong Answer
Clients polling ZooKeeper repeatedly for changes - causes high load and inconsistent state due to missed events.
🔌
API Design Challenge
Sketch 3 core API endpoints for the ZooKeeper-based system: leader election, configuration read/write, and client session management.
  • How does a client participate in leader election and get notified of leadership changes?
  • How does a client read and update configuration atomically with consistency guarantees?
  • How does the system manage client sessions and detect failures or offline states?
📊
Scoring Rubric
▼ Show
Must Have - 60%
  • Use of Apache ZooKeeper with ephemeral nodes and watches for leader election and config management
  • Explanation of Zab consensus protocol and quorum configuration for strong consistency and availability
  • Multi-datacenter replication strategy that avoids split-brain and maintains consistency
  • Implementation of end-to-end encryption including TLS and data at rest encryption
  • Client session management supporting offline operation and eventual synchronization
Good to Have - 30%
  • Details
Bonus - 10%
    ✅
    Model Answer
    ▼ Reveal

    This design uses Apache ZooKeeper as the coordination service to provide leader election and distributed configuration management with strong consistency and low latency. The system is deployed across multiple data centers with separate ZooKeeper ensembles coordinated by a global consensus proxy to avoid split-brain. End-to-end encryption is implemented using TLS for all communication and disk encryption for data at rest. Client sessions use ephemeral and versioned znodes to support offline operation and eventual synchronization.

    • Use Apache ZooKeeper for coordination due to ephemeral znodes and watch support
    • Configure ZooKeeper ensemble with 5 nodes to tolerate 2 failures and maintain P99 latency under 200ms
    • Deploy separate ZooKeeper clusters per data center with global leader arbitration via Raft proxy to avoid split-brain
    • Enable mutual TLS for all communication and encrypt data at rest using disk encryption
    • Implement client session management with ephemeral nodes and versioned znodes to support offline clients and atomic sync on reconnect
    • Apache ZooKeeper for coordination: ephemeral znodes for leader election and watches for config change notifications
    • Zab consensus protocol ensures strong consistency and linearizable writes with a quorum of 3/5 nodes
    • ZooKeeper ensemble configured with odd number of nodes (5) to tolerate 2 failures and maintain P99 latency under 200ms
    • Multi-datacenter deployment with separate ZooKeeper clusters per DC and global leader arbitration via Raft-based proxy to avoid split-brain
    • TLS encryption enabled for all client-server and server-server communication, plus disk encryption for data at rest
    • Client session management with ephemeral nodes and versioned znodes to support offline operation and atomic sync on reconnect
    • Use of ZooKeeper watches and re-registration to notify clients of configuration changes efficiently without polling
    • Tuning session timeouts and heartbeat intervals to balance failure detection latency and false positives

    Practice

    (1/5)
    1. For a global e-commerce platform requiring high availability and multi-region writes with eventual consistency, which key-value store choice is best and why?
    medium
    A. Use DynamoDB Global Tables for multi-region writes and automatic conflict resolution
    B. Use Redis with master-slave replication across regions for low latency
    C. Use Redis Cluster with cross-region sharding and manual conflict handling
    D. Use a relational database with cross-region synchronous replication

    Solution

    1. Step 1: Identify multi-region write and availability requirements

      Multi-region writes require conflict resolution and high availability without synchronous blocking.
    2. Step 2: Compare Redis and DynamoDB capabilities

      DynamoDB Global Tables support multi-region writes with built-in conflict resolution and eventual consistency; Redis lacks native multi-region write support.
    3. Final Answer:

      Option A -> Option A
    4. Quick Check:

      DynamoDB Global Tables -> multi-region writes with conflict resolution [check]
    Hint: DynamoDB Global Tables handle multi-region writes natively [check]
    Common Mistakes:
    2. For a system requiring globally unique IDs with high throughput and low latency, which approach is best considering the trade-offs of coordination overhead and ID ordering?
    medium
    A. Use a distributed consensus protocol (e.g., Paxos) to assign IDs synchronously
    B. Use a database auto-increment primary key with sharding
    C. Use a centralized Ticket Server allocating ID blocks to clients for local use
    D. Use client-generated UUIDs combined with a timestamp prefix

    Solution

    1. Step 1: Evaluate coordination overhead

      Consensus (A) ensures strong consistency but adds latency and limits throughput; DB auto-increment with sharding (D) risks collisions and complex coordination.
    2. Step 2: Assess ordering and uniqueness

      UUIDs (C) are unique but unordered and can cause issues in ordered systems; Ticket Server (B) balances uniqueness, ordering, and throughput by allocating ID blocks.
    3. Final Answer:

      Option C -> Option C
    4. Quick Check:

      Ticket Server -> high throughput, low latency, ordered IDs [check]
    Hint: Centralized block allocation beats consensus and UUIDs for throughput [check]
    Common Mistakes:
    3. If the CDN's cache invalidation mechanism fails to propagate updates, what is the exact failure experienced by end users?
    medium
    A. Users get 404 errors because the CDN deletes cached content prematurely
    B. Users receive outdated content until cache TTL expires, causing stale data display
    C. Origin servers become overloaded as CDN stops serving any cached content
    D. Users experience increased latency due to CDN routing loops

    Solution

    1. Step 1: Understand cache invalidation failure impact

      Failure to propagate invalidation means cached content remains stale beyond intended update.
    2. Step 2: Identify user experience as stale content served until TTL expiry

      Users see outdated data until cache naturally expires or is manually cleared.
    3. Final Answer:

      Option B -> Option B
    4. Quick Check:

      Cache invalidation failure -> stale content served, not errors or overload [check]
    Hint: Invalidation fails -> stale content served until TTL expires [OK]
    Common Mistakes:
    4. At 10x scale, which component in the web crawler system is most likely to become the first bottleneck, and why?
    hard
    A. URL Frontier, because it cannot shard URLs efficiently leading to queue contention
    B. Scheduler, because it cannot prioritize URLs fast enough under high throughput
    C. Politeness Policy Manager, because maintaining per-domain timers statefully limits horizontal scaling
    D. Duplicate Detection, because its in-memory hash set grows beyond memory capacity causing slow lookups

    Solution

    1. Step 1: Identify statefulness of Politeness Policy Manager

      It maintains per-domain timers and request schedules, which are stateful and hard to shard.
    2. Step 2: Compare with other components

      URL Frontier can shard URLs; Duplicate Detection can use scalable storage; Scheduler can be distributed but Politeness Policy requires domain-specific timing.
    3. Final Answer:

      Option C -> Option C
    4. Quick Check:

      Politeness Policy's stateful timers limit scaling first [check]
    Hint: Stateful timers in Politeness Policy block scaling first [OK]
    Common Mistakes:
    5. At 10x scale, which component fails first in a chat system and why?
    hard
    A. The chat servers fail first because WebSocket connections are stateful and cannot be resharded easily
    B. The load balancer fails first because it cannot distribute 10x more TCP connections
    C. The message queue fails first because Kafka partitions cannot be increased dynamically
    D. The database fails first because it cannot handle 10x more writes without sharding

    Solution

    1. Step 1: Identify stateful components limiting scale

      Chat servers maintain stateful WebSocket connections bound to server memory, limiting horizontal scaling.
    2. Step 2: Compare other components' scalability

      Databases can be sharded; Kafka partitions can be increased; load balancers scale horizontally. Stateful chat servers cannot easily move existing connections.
    3. Final Answer:

      Option A -> Option A
    4. Quick Check:

      Stateful WebSocket servers bottleneck scaling first [check]
    Hint: Stateful WebSocket servers block scaling first [check]
    Common Mistakes:
    Recommended

    Ready to prove it?

    Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

    10 questions~5 mineasy hard