Cancellation and concurrency control · senior
Batching and rate limiting
A batcher collects items and sends them together when the batch is full or a timer expires, whichever comes first, and a rate limiter allows at most N calls per time window — both are closure-plus-timer utilities that Uber, Atlassian and Dream11 have asked seniors to write.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Batcher: buffer items; on add, push; if the buffer is full, flush now; else if no timer is running, start one. flush() clears the timer, takes the buffer, resets it and sends. Never run two timers.
- Flush when the page is hidden ('visibilitychange' to hidden, or 'pagehide') with navigator.sendBeacon or fetch(..., { keepalive: true }). 'unload' is unreliable.
- Retry failed batches with backoff, and cap the buffer size (drop oldest or sample) so memory can't grow forever.
- Rate limiter kinds: fixed window (counter reset every minute; allows bursts at the edges), sliding-window log (timestamps queue; drop ones older than the window), token bucket (refill rate plus burst size).
- API choice for extra calls: reject or drop them, or queue them and run them when allowed (return a promise).
- Throttle is a rate limiter of one call per wait; debounce waits for quiet.
The red pen
Common traps: mistake → fix
Mistake: Restarting the timer on every add
Fix: that is debounce; a batch timeout starts at the first buffered item.
Mistake: Losing buffered events when the tab closes
Fix: flush on visibilitychange or pagehide.
Mistake: A fixed window allows N calls
Fix: it can allow 2N calls across a window boundary.
Easy to hard
Questions interviewers ask, easy to hard
- Write a batcher that sends at N items or after T ms. (Uber, 2025)
- Add flush() and clear(). What happens when the tab closes?
- Write rateLimiter(fn, limit per minute). (Dream11, 2025)
- Compare fixed window, sliding window and token bucket.
- Design an analytics SDK: batching, retries, offline queue and sampling.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.