A popular cache key expires. In the next 50 milliseconds, 500 requests for that key arrive, every one of them misses, and all 500 stampede your database for the same row. This is the thundering herd (or cache stampede), and the fix is request coalescing: when several callers ask for the same key at the same time, one of them does the work and the rest wait for its result.
You are given an upstream object with a single slow method:
upstream.fetch(key) # slow; returns the value for key
Implement Coalescer:
__init__(self, upstream)get(self, key) — return the value for key. If a fetch for that key is already in flight, do not start a second one: wait for the in-flight fetch and return its result.Requirements:
upstream.fetchConstraints:
threading primitives (Lock, Event, Condition)get is called from many threads simultaneouslyOfficial solution locked
Give the problem a real attempt before peeking — revealing it affects your credit for this problem for 4 hours.