0%
API Design Interviews: REST, gRPC, GraphQL, Pagination, Idempotency, Versioning and Webhooks
Webhooks and Async APIs
Most of the APIs you design in an interview answer in a few hundred milliseconds: read a resource, create a record, return it. A surprising number of real products cannot work that way. A video has to be transcoded, which takes minutes. A bank transfer settles hours later. A data export walks millions of rows. A payment is approved by the card network, then disputed weeks later by the cardholder. In each case the interesting thing happens after the request that started it has long finished. This lesson is about the API surface for that gap: how the client learns that the work finished, how it learns about events it never asked for, and how you make those notifications trustworthy.
Interviewers bring this up in two ways. Sometimes the prompt itself is asynchronous, such as design an API for a video processing service. More often it comes up as a probe inside another design: your payments API is done, now how does the merchant find out the charge was refunded by the bank? A candidate who answers with a vague "we send a webhook" and stops there leaves most of the credit on the table. A candidate who can describe the event envelope, the signature, the retry policy, the ordering caveat and the catch up path shows that they have operated one of these systems.
Synchronous, asynchronous and the time budget
Start with a time budget. Clients, load balancers and proxies all have timeouts, often somewhere between thirty and sixty seconds, and mobile networks drop long connections regularly. A good rule is that an endpoint should finish well under its callers' timeout at the 99th percentile. If the work can take longer than a few seconds, or if its duration depends on something you do not control, such as a third party, it should not run inside the request.
That gives three shapes. Synchronous: the request does the work and returns the result, which is right for anything quick and bounded. Asynchronous with a handle: the request records the intent, starts the work in the background and returns immediately with something the client can use to follow progress. Event driven: nobody asked; something happened in the provider's world, such as a dispute or a subscription renewal, and the consumer needs to hear about it. The first two are request driven. The third is the reason webhooks exist.
The async request reply pattern
The standard shape for request driven async work is called async request reply. The client sends the request, the server validates it, persists a job and answers 202 Accepted. The body describes an operation resource, and the Location header points at it. The client then follows the operation until it reaches a terminal state.
Notice the details an interviewer will look for. The 202 means the request was accepted for processing, not that it succeeded, and you should say so. Validation errors are still returned synchronously as 400 or 422, because there is no point queuing a job that is malformed. The Idempotency-Key header protects against the client retrying the POST after a timeout and starting two exports; lesson five covers the storage and scope of those keys, so here you can just name it. The Retry-After header tells a polling client how long to wait before asking again.
The operation resource
The operation is a first class resource, and its schema deserves a moment on the whiteboard. It needs an id, a type, a status from a small closed set, timestamps, optional progress, and on completion either a result or an error. Keep the status set small: queued, running, succeeded, failed, canceled. Each terminal state should carry what the client needs next.
Two design choices matter. First, whether the result lives inside the operation or the operation points at a separate resource. For an export the result is a file, so a link is natural. For an order fulfilment job the result might be the order itself, in which case the operation holds a resource reference and the client fetches the order. Second, whether operations can be canceled. If they can, expose a custom action such as POST /v1/operations/op_81:cancel and define what happens to work already done. Cancel is best effort, and the response should return the operation with its current status rather than promising instant cancellation.
Operations should also expire. Keep finished operations for a documented period, perhaps seven days, then return 404 or 410. Without that, the operations table grows forever and old download links leak data.
Polling well
Polling has a bad reputation, but it is simple, works through every firewall, and puts the client in control of its own load. Its problems are cost and latency: most polls return nothing new, and the client only learns about a change on the next poll after it happens. You can make polling much better with a few rules. Return Retry-After and expect well behaved clients to honour it. Make the GET cheap: serve it from a fast store, support ETag and If-None-Match so an unchanged operation returns 304 with no body, and rate limit per client so a buggy loop cannot hammer you. Suggest backoff in your SDK, so a job that has been running for ten minutes is polled every thirty seconds rather than every second.
Long polling is a middle ground. The client sends GET /v1/operations/op_81?wait=30s and the server holds the request open until the status changes or the wait expires, then answers. It reduces empty responses and latency, at the cost of holding connections open on your side. It is a reasonable answer when the interviewer pushes on polling cost but the consumers cannot receive inbound HTTP.
What the interviewer is checking
In this part of an interview the interviewer wants to see that you recognise when work is too slow for a synchronous call, that you return 202 with a pollable handle instead of blocking, that the operation has a clear status model with terminal states, and that you have thought about retries of the initial request. The next question is almost always: can the client avoid polling? That leads to webhooks, which take the rest of this lesson.
Choosing between sync and async in the room
Candidates sometimes overcorrect and make everything asynchronous, which is its own mistake. Async adds a resource, a status model, a notification path and a second round trip for the client. If the work finishes in under a second at the 99th percentile, keep it synchronous and return the result. A good test is to ask what the client would do while waiting. If the honest answer is nothing, and the wait is short, a synchronous call is simpler for everyone.
There is a useful hybrid for work whose duration varies. The server tries to finish within a short budget, perhaps two seconds. If it does, it returns 200 or 201 with the result. If not, it returns 202 with the operation handle and keeps working. Clients must handle both responses, which your SDK can hide. This pattern is common for things like generating a thumbnail or running a fraud check that is usually instant but occasionally slow. Mention it when the interviewer gives a latency distribution with a long tail, because it shows you are designing for the actual numbers rather than for a single case.
Failure inside the operation
An operation that fails needs a useful error, not just status failed. Use the same structured error shape as the rest of your API, so the client's error handling code works unchanged: a stable machine readable code, a human message, and a flag or documented rule for whether retrying could help. A conversion that failed because the source file was corrupt will fail again if retried, while one that failed because a dependency timed out may succeed. The client cannot tell those apart unless you say so.
Also decide who retries. For transient internal failures, the server should usually retry the job itself a few times before declaring failure, because the server knows more about its own dependencies than the client does. Only after those internal retries are exhausted does the operation become failed. Document this, so clients do not layer their own aggressive retries on top and multiply load during an incident.
Partial results and progress
Some jobs produce useful output before they finish. A bulk import of fifty thousand rows can report how many rows succeeded, how many failed and where to download the failure report. Model that explicitly with counters on the operation rather than burying it in a message string. Progress should be a number between zero and one, or counts of done and total, and it should never go backwards, because clients render it as a bar and a jumping bar destroys trust. If you cannot estimate progress honestly, omit it rather than inventing a value.
When you return 202 Accepted, say out loud that it means accepted for processing, not succeeded, and show where the final outcome will appear. Interviewers often probe this exact distinction.