ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/proxies/tasks/task-12-3-collapsed
TASK

Implementation

Implement collapsed forwarding for cache misses to prevent thundering herd:

When a cached item expires and multiple requests arrive:

  1. First request goes to the origin (is "collapsed")
  2. Subsequent requests for the same key wait
  3. When origin responds, cache the response
  4. Return the cached response to all waiting requests

This prevents the backend from being overwhelmed when popular items expire.

Sample Test Cases

Collapse concurrent requestsTimeout: 5000ms
Input
{
  "src": "c0",
  "dest": "n1",
  "body": {
    "type": "init",
    "msg_id": 1,
    "node_id": "n1",
    "node_ids": [
      "n1"
    ]
  }
}
Expected Output
{"src":"n1","dest":"c0","body":{"type":"init_ok","in_reply_to":1,"msg_id":0}}

Hints

Hint 1
Similar to deduplication but for cache misses
Hint 2
Only one request goes to origin
Hint 3
Queue waiters until response arrives
OVERVIEW

Theoretical Hub

Thundering Herd Problem

When a popular cached item expires, thousands of requests might simultaneously miss the cache and hit the backend. This can overwhelm the backend and cause cascading failures.

Collapsed Forwarding

Collapsed forwarding allows only one request through to the origin while others wait. When the response arrives, it is cached and returned to all waiters. CDNs like Varnish implement this.

Key Concepts

collapsed forwardingthundering herdcache miss handling
main.py
python
Implement Collapsed Forwarding - Proxies | Build Distributed Systems