Design a Webhook Dispatcher
🪝 What is a webhook dispatcher? It is the part of a platform that tells other systems when something happens. A customer registers a URL and says which events they care about, such as "order paid". When an order is paid, the platform sends an HTTP request with the details to that URL. If the customer's server is down, the dispatcher tries again later, and if it keeps failing, it sets the event aside so nobody loses it.
Payment providers, code hosts and chat apps all have one. As an interview problem it is a good fit for 45 minutes: the happy path is short, and the interesting parts are the ones that show judgment: what happens when a receiver fails, how we schedule retries without a busy loop, when we give up, and how the receiver can trust a request. We cover the four pieces most interviewers ask about: subscriptions, signing, retries with backoff, and dead letters.
Requirements
"Design a webhook system. Customers subscribe a URL to event types. When an event happens, we POST it to every subscribed URL. Make it reliable."
"Make it reliable" is the part to dig into. Reliable against what? Slow receivers, receivers that are down for an hour, receivers that are gone forever, and attackers who send fake events to a customer's URL. Each one leads to a different piece of the design.
Clarifying Questions
You: How does a subscription say which events it wants? Exact names, or patterns?
Interviewer: A list of exact event types, like order.paid. A subscription can also use * to get everything.
You: What counts as a successful delivery?
Interviewer: Any 2xx status code. Everything else is a failure, including redirects.
You: How often should we retry, and for how long?
Interviewer: Up to a configurable number of attempts. Wait a base delay after the first failure and double the wait each time, so a struggling server is not flooded.
You: What happens when a delivery runs out of attempts?
Interviewer: It becomes a dead letter. Customers can fix their server and then replay a dead delivery, which starts it over.
You: How does the receiver know a request really came from us?
Interviewer: Each subscription has a secret. Sign every request with it, and include the time so an old request cannot be replayed later.
You: If a customer unsubscribes while a retry is waiting, do we still send it?
Interviewer: No. Drop anything still pending for that subscription.
You: Should I read the clock, or is time passed in?
Interviewer: Pass the time in. Something calls deliver(now) regularly, like a timer, and sends whatever is due.
The last answer quietly decides the shape of the design. There is no background thread inside our class and no sleep: deliver(now) is a pure step that sends what is due at now. That makes every behavior, including backoff, easy to test with plain numbers.
💡 Interview tip: When a problem involves waiting (retries, timeouts, expiry), ask whether you may pass time in. A method like
deliver(now)turns "wait 40 seconds" into "the delivery is due atnow + 40", and you can trace the whole design on a whiteboard without a clock.
Final Requirements
- A subscription has a URL, a secret and a list of event types (
*means all). Subscribing returns an id; unsubscribing deactivates it. - Publishing an event creates one delivery for every active subscription that wants its type. Each delivery is due immediately.
deliver(now)sends every delivery that is due, earliest first (ties by delivery id), and reports each request.- A 2xx response means success. Any other response is a failure.
- After the
k-th failure, the delivery is due again afterbase_delay * 2^(k-1). Aftermax_attemptsfailures it becomes a dead letter. - Dead letters can be listed and replayed. A replayed delivery starts again with a fresh set of attempts.
- Every request carries a signature made from the subscription's secret, the request time and the body.
- Pending deliveries of an unsubscribed subscription are dropped; its dead letters cannot be replayed.
Out of scope
- Real HTTP calls, TLS and timeouts (an
HttpClienthides them; we talk about timeouts in Extensibility). - Storing deliveries in a database so they survive a crash (an Extensibility question).
- A customer-facing UI, delivery logs and metrics.
- Ordering guarantees between events (also in Extensibility).