Bird
Raised Fist0

Leaky Bucket Algorithm - Smoothing Traffic Bursts to Steady Rate

HLDmediumInterview freq: medium
Choose your preparation mode4 modes available
🎯
Leaky Bucket Algorithm - Smoothing Traffic Bursts to Steady Rate
45 min · senior
senior

You are designing a traffic shaping system for a 50 million daily active users (DAU) startup that handles high-volume API requests. The system must smooth sudden traffic bursts into a steady output rate to downstream services, preventing overload and ensuring consistent latency. The system must operate under strict constraints including encrypted traffic, offline delivery support, and P99 latency under 200ms.

  • All traffic must be encrypted end-to-end using TLS 1.3
  • Support offline delivery: requests can be buffered and retried without loss
  • P99 end-to-end latency must be under 200ms including smoothing delay
  • System must handle peak burst rates up to 10x average traffic
  • Horizontal scalability to support 50M DAU with 100K requests/sec average
📋
Guided Steps
1/5
Step 1
Traffic Ingestion and Buffering
How will you ingest and buffer incoming requests to handle bursts without losing data?
💡 Hint
Consider a distributed, durable, partitioned message queue that supports high throughput and ordering guarantees.
Use Apache Kafka with topic partitioning keyed by client_id to buffer incoming requests, ensuring durability and ordered processing per client.
Mid-level: Names Kafka for buffering. Senior: Explains client_id partition key ensures ordering and load distribution, and why RabbitMQ or Redis Streams would not scale to 100K req/sec with durability.
⚠️Common Wrong Answer
Using Redis as a simple queue - Redis lacks strong durability guarantees and can lose data on failover under high load.
Step 2
Leaky Bucket Rate Limiting Implementation
How will you implement the leaky bucket algorithm to smooth bursts into a steady output rate?
💡 Hint
Use a token bucket or leaky bucket algorithm implemented in a distributed worker that reads from Kafka partitions and emits at a fixed rate.
Implement leaky bucket logic in a horizontally scalable Kafka consumer group using Java with Guava RateLimiter per partition to emit requests at a steady rate.
Mid-level: Describes rate limiting with Guava RateLimiter. Senior: Explains why per-partition consumer ensures ordering and rate control, and why a centralized rate limiter or Redis-based counters would cause bottlenecks or race conditions.
⚠️Common Wrong Answer
Centralized rate limiter service - creates a single point of failure and does not scale with 50M DAU traffic.
Step 3
Handling Offline Delivery and Retries
How will the system handle offline clients and ensure no request loss during retries?
💡 Hint
Use Kafka's offset management and idempotent producers to guarantee exactly-once processing and allow clients to resume from last acknowledged offset.
Leverage Kafka's consumer offset commits and idempotent producers to track processed requests and enable clients to retry from last known offset without duplication.
Mid-level: Mentions Kafka offset commits. Senior: Explains idempotent producer configuration and how offset management prevents duplicates and data loss during client offline periods.
⚠️Common Wrong Answer
Stateless consumers without offset tracking - leads to duplicate processing or lost requests on client reconnect.
Step 4
Ensuring Low Latency and Encryption
How will you ensure P99 latency under 200ms while maintaining end-to-end encryption?
💡 Hint
Use TLS 1.3 termination at ingress with minimal overhead and optimize Kafka consumer batch sizes and commit intervals for low latency.
Terminate TLS 1.3 at ingress load balancers (e.g., Envoy) with session resumption, and tune Kafka consumer batch size to 100 messages and commit interval to 50ms to meet latency SLA.
Mid-level: Mentions TLS termination and Kafka tuning. Senior: Explains trade-offs between batch size and latency, TLS session resumption benefits, and why offloading encryption to ingress is better than in-app TLS.
⚠️Common Wrong Answer
Terminating TLS inside application workers - increases CPU load and latency, risking P99 SLA breaches.
Step 5
API Design for Client Interaction
Design the 3 core API endpoints for clients to submit requests, check status, and receive acknowledgments.
💡 Hint
Consider REST or gRPC endpoints with request IDs, status polling or WebSocket subscriptions, and acknowledgment callbacks.
Expose REST POST /submitRequest with requestId, GET /requestStatus?requestId for polling, and WebSocket /ackChannel for push acknowledgments using JSON over TLS.
Mid-level: Names REST endpoints. Senior: Explains trade-offs between polling and WebSocket for acknowledgments, idempotency via requestId, and TLS for encryption.
⚠️Common Wrong Answer
Using HTTP polling only - causes excessive load and latency spikes at 50M DAU.
🔌
API Design Challenge
Sketch 3 core API endpoints for the Leaky Bucket system: how clients submit requests, how clients check request status, and how the system acknowledges processed requests.
  • How does the client send requests to be rate-limited and buffered?
  • How can clients poll or subscribe to know when their requests are processed?
  • How does the system confirm successful processing or retries?
📊
Scoring Rubric
▼ Show
Must Have - 60%
  • Use Kafka with partition key for buffering and ordering
  • Implement leaky bucket in distributed Kafka consumers with per-partition rate limiting
  • Handle offline delivery with Kafka offset commits and idempotent producers
  • Design APIs with requestId for idempotency and support status checking
  • Ensure TLS 1.3 encryption with minimal latency overhead
Good to Have - 30%
  • Tune Kafka consumer batch size and commit interval for latency
  • Use WebSocket for push acknowledgments to reduce polling load
  • Explain horizontal scalability via Kafka partitions and consumer groups
Bonus - 10%
  • Discuss failure modes like Kafka broker failure and recovery
  • Explain trade-offs between leaky bucket and token bucket algorithms
Using Redis as a queue - leads to data loss under failover and insufficient durability, risking lost requests during traffic bursts
Centralized rate limiter service - creates bottleneck and single point of failure, unable to scale to 50M DAU traffic
Stateless consumers without offset tracking - causes duplicate or lost requests on client reconnect or failure recovery
Terminating TLS inside application workers - increases CPU load and latency, risking P99 SLA breaches
HTTP polling only for status - causes excessive load and latency spikes at 50M DAU due to massive reconnects and polling frequency
✅
Model Answer
▼ Reveal

The system uses Apache Kafka with client_id partitioning to buffer incoming requests durably and in order. Distributed Kafka consumer groups implement the leaky bucket algorithm using Guava RateLimiter per partition to smooth bursts into a steady output rate. Kafka's offset management and idempotent producers guarantee exactly-once processing and support offline retries. TLS 1.3 is terminated at Envoy ingress with session resumption to minimize encryption overhead and meet P99 latency under 200ms. The API exposes REST endpoints for request submission and status polling, and a WebSocket channel for real-time acknowledgments, all secured over TLS.

  • Use Apache Kafka with client_id partition key to ensure ordered, durable buffering and horizontal scalability
  • Implement leaky bucket rate limiting in distributed Kafka consumers with Guava RateLimiter per partition to avoid bottlenecks
  • Leverage Kafka offset commits and idempotent producers to guarantee no data loss and support offline retries
  • Terminate TLS 1.3 at Envoy ingress with session resumption to minimize latency overhead and meet P99 SLA
  • Design REST APIs with requestId for idempotency and status polling, and WebSocket for real-time acknowledgments to reduce polling load
  • Apache Kafka with client_id partition key for durable, ordered buffering of requests
  • Kafka consumer groups implementing leaky bucket with Guava RateLimiter per partition for steady rate emission
  • Kafka offset management and idempotent producers to guarantee exactly-once processing and support offline retries
  • TLS 1.3 termination at Envoy ingress with session resumption to minimize encryption overhead and latency
  • REST API endpoints with requestId for idempotency, status polling, and WebSocket for real-time acknowledgments
  • Tuning Kafka consumer batch size and commit interval to meet P99 latency under 200ms
  • Horizontal scalability by partitioning Kafka topics and consumer groups to handle 100K req/sec average with 10x burst capacity
  • Avoiding centralized rate limiters to prevent bottlenecks and single points of failure

Practice

(1/5)
1. A web application experiences increasing CPU load on a single database server, causing query latency spikes. Which scaling approach best addresses this bottleneck without changing the application code?
easy
A. Distribute the database load by sharding data across multiple servers (horizontal scaling)
B. Add more CPU cores and RAM to the existing database server (vertical scaling)
C. Use a caching layer in front of the database to reduce read load
D. Switch from SQL to NoSQL database to improve write throughput

Solution

  1. Step 1: Identify the bottleneck as CPU and memory on a single DB server

    Vertical scaling adds resources to the existing server to handle more load without architectural changes.
  2. Step 2: Evaluate horizontal scaling and other options

    Horizontal scaling requires application changes for sharding; caching reduces reads but not CPU-bound writes; switching DB types is a different trade-off.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Vertical scaling adds resources to one server without code changes [check]
Hint: Vertical scaling adds power to one machine [check]
Common Mistakes:
2. For a real-time ML inference system requiring minimal latency and high availability during model updates, which architecture is preferable and why?
medium
A. Monolithic model server that loads new models sequentially, blocking requests during reload
B. Single model server with periodic batch reloads and request queueing during reload
C. Blue-green deployment with two identical model serving clusters switching traffic atomically
D. Client-side model loading with periodic updates pushed to clients

Solution

  1. Step 1: Evaluate architectures for latency and availability

    Monolithic (A) and single server batch reload (C) block requests during reload, increasing latency. Client-side loading (D) adds complexity and inconsistent versions.
  2. Step 2: Understand blue-green deployment benefits

    Blue-green (B) runs two clusters; new model loads on inactive cluster, then traffic switches atomically, ensuring zero downtime and minimal latency impact.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Blue-green enables atomic switch with zero downtime [check]
Hint: Blue-green switches traffic atomically, avoiding reload blocking [OK]
Common Mistakes:
3. For a proximity service requiring low-latency queries and frequent updates of business locations, which spatial indexing approach is better and why?
medium
A. Use Geohash-based indexing because it supports efficient prefix queries and simpler updates
B. Use a full R-tree index because it guarantees minimal overlap and tight bounding rectangles
C. Use a grid-based fixed cell index because it provides exact bounding box queries without hierarchy
D. Use a Quadtree because it supports fast insertions and deletions with balanced trees

Solution

  1. Step 1: Compare update and query costs

    Quadtree (Use a Quadtree because it supports fast insertions and deletions with balanced trees) can become unbalanced and expensive to update frequently. R-tree (Use a full R-tree index because it guarantees minimal overlap and tight bounding rectangles) is complex and costly to maintain with frequent writes.
  2. Step 2: Evaluate Geohash benefits

    Geohash (Use Geohash-based indexing because it supports efficient prefix queries and simpler updates) encodes locations into fixed-length strings allowing efficient prefix queries and simpler incremental updates, ideal for low-latency and frequent writes.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Geohash supports efficient prefix queries and simpler updates [check]
Hint: Geohash enables fast prefix queries and simpler frequent updates [OK]
Common Mistakes:
4. If the health monitoring component of the GSLB fails silently, what is the most likely failure observed by end users?
medium
A. Users are routed to unhealthy data centers causing request failures or timeouts
B. Traffic is evenly distributed regardless of data center health, causing overload
C. DNS queries fail to resolve, causing complete service outage
D. Users experience increased latency due to rerouting through backup data centers

Solution

  1. Step 1: Understand health monitor role

    The health monitor informs GSLB which data centers are healthy; without it, GSLB cannot avoid unhealthy endpoints.
  2. Step 2: Analyze failure impact

    Without health info, GSLB routes users to unhealthy data centers causing failures (Users are routed to unhealthy data centers causing request failures or timeouts). Increased latency (Users experience increased latency due to rerouting through backup data centers) or DNS failures (DNS queries fail to resolve, causing complete service outage) are indirect or unlikely. Even traffic distribution (Traffic is evenly distributed regardless of data center health, causing overload) ignores health but is less precise than routing to unhealthy nodes.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Silent health monitor failure -> routing to unhealthy data centers [check]
Hint: No health data -> users sent to unhealthy data centers [check]
Common Mistakes:
5. Without Kafka in the chat system, what exactly breaks when Alice sends a message to Bob?
medium
A. Alice's message is immediately delivered but Bob's client cannot decrypt it
B. Alice's send blocks until Bob's server responds; if Bob's server crashes, the message is lost with no retry
C. Messages are duplicated because no deduplication queue exists
D. Alice's client crashes due to lack of message buffering

Solution

  1. Step 1: Understand Kafka's role in decoupling and durability

    Kafka buffers messages asynchronously, preventing blocking and enabling retries.
  2. Step 2: Trace failure without Kafka

    Without Kafka, Alice's server must synchronously contact Bob's server. If Bob's server is slow or crashes, Alice's send blocks or message is lost with no retry.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Without Kafka, synchronous send blocks and loses messages on Bob's server crash [check]
Hint: No Kafka -> synchronous send blocks and loses messages [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