Bird
Raised Fist0

Leaky Bucket Algorithm - Smoothing Traffic Bursts to Steady Rate

HLDmediumInterview freq: medium
Choose your preparation mode4 modes available
▶
Request Trace
Step 1/6
API Gateway

Receives client request and forwards to Rate Limiter Service

API Gateway accepts HTTP request and routes it downstream

Protocol:HTTP/HTTPS
BeforeClient request arrives at API Gateway, no rate limiting applied
AfterRequest forwarded to Rate Limiter Service for rate check
Rate Limiter Service

Checks leaky bucket counters in Redis to decide request allowance

Reads current bucket state and updates counters if allowed

Protocol:Redis GET/SET
BeforeRate Limiter has no current request state for this client
AfterBucket counters updated; request marked allowed or denied
Redis Cluster

Stores and updates leaky bucket counters and timestamps

Maintains per-client counters to smooth traffic bursts

Protocol:Redis GET/SET
BeforeBucket counters reflect previous request rates
AfterCounters incremented or reset according to leaky bucket logic
Rate Limiter Service

Forwards allowed requests to Downstream Services or rejects them

Sends allowed requests downstream; rejects excess requests

Protocol:HTTP/HTTPS
BeforeRequest allowance decision made based on bucket state
AfterAllowed requests sent downstream; denied requests dropped
Downstream Services

Processes allowed requests and sends metrics data

Executes business logic and pushes metrics asynchronously

Protocol:Metrics Push (e.g., HTTP or gRPC)
BeforeRequest received from Rate Limiter Service
AfterRequest processed; metrics sent to Monitoring & Metrics
Monitoring & Metrics

Aggregates rate limiting and traffic metrics for observability

Collects data on request rates, denials, and system health

Protocol:Metrics ingestion protocol
BeforeNo new metrics for this request cycle
AfterUpdated metrics reflect current traffic and rate limiting
🏗️
Architecture Diagram
Architecture Diagram
┌───────────────┐
│ API Gateway   │
└───────┬───────┘
        │ HTTP/HTTPS
        ▼
┌───────────────────────┐
│ Rate Limiter Service  │
│ (Leaky Bucket Logic)  │
└───────┬───────────────┘
        │ Redis GET/SET
        ▼
┌───────────────┐
│ Redis Cluster │
│ (Leaky Bucket │
│  counters)    │
└───────┬───────┘
        │ Allow/Deny
        ▼
┌───────────────────┐
│ Downstream Svcs   │
└───────┬───────────┘
        │ Metrics Push
        ▼
┌──────────────────────┐
│ Monitoring & Metrics  │
└──────────────────────┘

This architecture implements the leaky bucket algorithm to smooth traffic bursts by enforcing a steady request rate. Incoming requests first hit the API Gateway, which forwards them to the Rate Limiter Service. The Rate Limiter queries Redis Cluster to check and update per-client counters representing the bucket state. Redis acts as a fast, centralized store for counters and timestamps, enabling distributed rate limiting. Allowed requests proceed to Downstream Services for business logic processing. These services asynchronously push metrics to Monitoring & Metrics for observability. This design decouples rate limiting logic from business services, uses Redis for low-latency state management, and ensures bursts are smoothed to a steady flow, preventing overload downstream.

Primary Focus

✓

Practice

(1/5)
1. In the driver matching flow, at step 4 the Matching Service queries the Surge Pricing Service for the current multiplier. What happens if the Surge Pricing Service responds with a stale multiplier due to cache lag?
easy
A. The Matching Service rejects the request and retries immediately
B. The Matching Service blocks until fresh surge data is available
C. The Matching Service falls back to a default multiplier of 1.0
D. The Matching Service uses the stale multiplier, causing temporary incorrect pricing

Solution

  1. Step 1: Trace Matching Service behavior at step 4

    Matching Service queries Surge Pricing Service for multiplier to calculate fare.
  2. Step 2: Understand stale data impact

    If surge multiplier is stale due to cache lag, Matching Service proceeds with returned value, causing temporary fare mispricing but no blocking or retries.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Matching Service tolerates stale surge multiplier for availability [check]
Hint: Matching tolerates stale surge data to avoid blocking [check]
Common Mistakes:
2. If the TTL eviction mechanism is disabled in a cache system, what failure occurs when stale data remains in the cache?
medium
A. Stale entries remain indefinitely, causing clients to read outdated data
B. Cache automatically switches to LRU eviction, which is slower
C. Cache entries are evicted based on frequency instead, causing unpredictable behavior
D. Cache evicts entries too aggressively, causing high miss rates

Solution

  1. Step 1: Understand TTL role

    TTL ensures entries expire after a fixed time to prevent stale data.
  2. Step 2: Trace failure without TTL

    Without TTL, stale entries never expire, so clients may read outdated data indefinitely.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Disabling TTL causes stale data to persist indefinitely [check]
Hint: No TTL means stale data never expires, causing outdated reads [OK]
Common Mistakes:
3. For a large-scale code repository system supporting millions of users, which branching strategy best balances performance and user flexibility?
medium
A. Use a centralized locking mechanism to serialize all branch creation and merges
B. Use lightweight branch pointers referencing immutable commits with eventual consistency
C. Use full branch copies stored separately for each user to avoid conflicts
D. Use synchronous replication of branch metadata across all servers before allowing merges

Solution

  1. Step 1: Analyze branching strategies for scale

    Lightweight branch pointers referencing immutable commits allow fast branch creation and merges without locking, supporting high concurrency.
  2. Step 2: Evaluate other options

    Centralized locking serializes operations, hurting performance; full branch copies waste storage and complicate merges; synchronous replication adds latency and reduces availability.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Immutable commits + lightweight pointers -> scalable branching [check]
Hint: Immutable commits enable lightweight, scalable branches [check]
Common Mistakes:
4. For intent classification in the bot, which architecture best balances latency and model update frequency given a high volume of diverse user queries?
medium
A. Use a single large LLM instance serving all queries synchronously
B. Run multiple LLM replicas behind a load balancer with synchronous inference
C. Deploy a lightweight intent classifier for fast routing, with LLM fallback for complex queries asynchronously
D. Use batch processing of queries offline to update intents periodically

Solution

  1. Step 1: Analyze latency and update frequency constraints

    High volume requires low latency for common intents and flexibility for complex queries.
  2. Step 2: Evaluate architectures

    A single large LLM (A) has high latency and update overhead. Multiple replicas (C) improve throughput but still synchronous latency is high. Batch offline (D) delays updates and user experience. Lightweight classifier with async LLM fallback (B) balances fast routing and complex understanding.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Lightweight classifier + async LLM fallback balances latency and accuracy [check]
Hint: Fast lightweight routing, async LLM fallback for complexity [check]
Common Mistakes:
5. At 10x scale, which Pastebin component is most likely to fail first and why?
hard
A. The relational database, because complex joins for analytics become a bottleneck
B. The analytics event ingestion pipeline, because Kafka partitions saturate and cause backpressure
C. The Expiration Worker, because scanning all snippets for TTL expiry becomes too slow
D. The snippet storage layer, because key-value stores cannot shard keys efficiently at scale

Solution

  1. Step 1: Analyze scaling bottlenecks

    Expiration Worker scans all snippets to delete expired ones; at 10x scale, scan latency grows linearly.
  2. Step 2: Compare other components

    Relational DB (A) is not primary storage; key-value stores (C) shard well; Kafka (D) scales with partitioning but expiration scanning is a linear bottleneck.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Expiration scanning is linear and slows down first at scale [check]
Hint: Expiration scanning scales linearly, fails first at 10x [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