Your event cache expires and a hundred requests arrive together. What protects Postgres?
After this you can answer: Your event cache expires and a hundred requests arrive together. What protects Postgres?
Say it in 30 seconds
Section titled “Say it in 30 seconds”The deeper answer
Section titled “The deeper answer”A single-flight map stores the running promise for one key and removes it on success or failure. Another process has its own map, so a Redis lock coordinates across tasks. The lock lasts five seconds and uses a random ownership token; its release script deletes it only if that token still matches.
A lock holder checks the cache again before rebuilding because another task may have filled it between the first read and lock acquisition. Non-holders wait fifty milliseconds before each of five fresh-value rechecks, then read Postgres themselves. That bounds added latency at the cost of allowing extra work under a slow rebuild.
The lock is suitable for rebuilding disposable cache data. It is unsuitable for the seat invariant: expiry or Redis failure can permit two holders. For production overload, combine cache coordination with admission control, bounded database work and alerts on the failure path.
Counter-questions
Section titled “Counter-questions”Does single-flight work when Redis is down?
Why compare the ownership token on release?
Why allow waiters to query Postgres?
Where in MiniTickets
Section titled “Where in MiniTickets”Read Caching, then inspect the events-cache piece →.
apps/api/src/services/single-flight.ts → singleFlightRead this book online: https://book.theversehub.in/