Frontend System Design · Lesson 50 of 86 · 4 min
Batching, Throttling and Debouncing API Calls
These are your core tools for managing network requests. In an interview, knowing which to use and why is non-negotiable.
Debounce vs. Throttle: The classic trade-off
Both techniques control how often a function executes, but they solve different problems. Mixing them up is a common red flag.
Debounce groups a burst of events into a single one. The function only runs after a specific period of inactivity. Think of an elevator door: it only closes after everyone has stopped trying to get in.
Use debounce when you only care about the final state. The classic use case is a typeahead search input. You don't want to fire an API request for every keystroke ("a", "ap", "app", "appl"); you want to fire one request for "apple" after the user has paused typing.
Throttle ensures a function runs at a maximum frequency. It guarantees execution at regular intervals, no matter how many times the event is fired. Think of a game loop running at 60 frames per second; it doesn't matter how fast the hardware is, the screen won't update more than 60 times a second.
Use throttle for continuous events where you need periodic updates. Examples include tracking scroll position for an infinite scroll feature, handling window resize events, or reporting mouse movement. You want updates while the action is happening, just not thousands of them.
In an interview, it's this simple:
- If the problem involves reacting to a user's pause (e.g., "search input"), your answer is debounce.
- If the problem involves handling a continuous stream of events (e.g., "scrolling", "resizing"), your answer is throttle.
Batching: Reducing network chatter
Batching is different. It's not about rate-limiting a single action; it's about bundling multiple distinct actions into a single network request.
Instead of sending ten small analytics events in ten separate API calls, you queue them up and send them all at once. This reduces HTTP overhead (fewer TCP handshakes, fewer headers) and can ease server load.
The trade-off is latency for the individual events. The first event you queue has to wait for either the batch to fill up or a timeout to expire. This is fine for non-critical data like analytics, but unacceptable for an action where the user is waiting for a response, like submitting a form.
The trap: What happens if the user closes the tab before the batch is sent? You lose the data. A robust implementation listens for the pagehide or visibilitychange events to flush the queue before the page unloads. For this, navigator.sendBeacon() is the right tool, as it's designed for this exact "fire-and-forget" scenario and won't be cancelled by page navigation.
How to implement debounce from scratch
You will almost certainly be asked to write a debounce function in a coding interview. You need to be able to do it from memory. The core is a closure that holds a reference to a timer.
function debounce<T extends (...args: any[]) => void>(
func: T,
delay: number
): (...args: Parameters<T>) => void {
let timeoutId: ReturnType<typeof setTimeout> | null = null;
return function(this: ThisParameterType<T>, ...args: Parameters<T>) {
// Preserve the `this` context from the call site.
const context = this;
// If the debounced function is called again, clear the previous timer.
if (timeoutId) {
clearTimeout(timeoutId);
}
// Set a new timer. The function will execute after `delay` ms of inactivity.
timeoutId = setTimeout(() => {
func.apply(context, args);
}, delay);
};
}
Key points to explain during the interview:
- Closure: The returned function has access to the
timeoutIdvariable from its parent scope. This is how the timer state persists between calls. clearTimeout: This is the heart of the debounce. Every new call cancels the previously scheduled execution.apply(context, args): This ensures the original function is called with the correctthiscontext and all of its arguments. A naivefunc(...args)would lose the context.
The follow-up question: "How do you prevent memory leaks in a React component using this?"
In a class component, you'd clear any pending timer in
componentWillUnmount. In a functional component, you return a cleanup function fromuseEffect. This is critical. If the component unmounts before the timer fires, you need to cancel it to prevent it from trying to update a non-existent component.
Choosing the right delay
An interviewer might ask, "What's a good delay for a search input?" There is no single right answer. It's a trade-off between responsiveness and efficiency.
- Too short (e.g., 50ms): You'll still make many unnecessary API calls for fast typists. The UI might feel busy and flicker.
- Too long (e.g., 1000ms): The application feels sluggish. The user stops typing and waits a full second for results to appear.
A reasonable starting point is 200-400ms. This range is short enough that it feels responsive to most users but long enough to batch keystrokes from an average typist. The important thing is to articulate the trade-off, not to state a magic number.
This diagram shows the logic for a debounced function. Each time the event fires, the timer is reset. The function only executes when the timer is allowed to complete.
Try it
Implement a throttle function from scratch. It's a different logic: instead of a timer you reset, you'll likely use a timestamp or a boolean flag to track whether you're in a "cooldown" period. Use it to log window.scrollY to the console, but no more than once every 200ms.
Next: Handling API Errors in the Frontend.
Lock it in — apply this lesson under interview conditions.