LearnThatStack Ace your next interview
API Design · question
Question 85 of 110

How do you implement microservices communication patterns?

expert Pro
← All API Design questions

The full answer covers the core mechanics, when this pattern wins, and the trade-offs interviewers most often probe. It includes a code sample. Read it once at your own pace, then try to recall the structure from memory before the interview. Pro also includes this question's interview lens - the likely follow-up probes and what you can say in the room.

Full answer + code samples + AI explanations

Unlock to read the complete answer for this premium question.

Guided practice for API Design One question at a time. Answer out loud, get graded, see what you missed.
Related concept

Async APIs and webhooks: when the work takes longer than the request

Do not keep the connection open. Return 202 with a job the client can poll, or send a webhook to whoever needs to know and handle every delivery problem.

For this question · chapter 3 of 3

3/3 Webhooks invert the call
webhooksWebhooks invert the callat least once, in any orderAnyoneno signatureYour APIevt_b70 order.createdhook url/hooks/ordersattempts2 of 6secretwhsec_7c1Their server200evt_b71 order.updatedevt_b70 order.createdGET /orders/88 shippedsignatureverifiedretry queueattempt 1500attempt 2delivereddead letter after 6 triesarrived out of orderdeliveries35applied24event ids seenevt_9f3evt_a02evt_b71evt_b704 ids
webhooksWebhooks invert the callAnyoneno signatureYour APIevt_b70 order.createdhook url/hooks/ordersattempts2 of 6secretwhsec_7c1Their server200evt_b71 order.updatedevt_b70 order.createdGET /orders/88 shippedsignatureverifiedseen idsevt_9f3evt_a02evt_b71evt_b70retry queueattempt 1500attempt 2delivereddeliveries35applied24

Webhooks invert the call

  1. Returning 202 with a job to poll is for your own long-running work. With a webhook, you become the caller, because someone else needs to know that something happened. Their server, the consumer, gives you a URL to POST to, /hooks/orders, and gets a signing secret back from you. The signing secret is the only thing that lets the consumer tell your calls apart from anyone else's.
  2. A webhook brings back every problem that the job resource (the job the client polls) let you avoid, but with the roles reversed. A payment succeeds, so your API becomes the client and sends a delivery to the consumer's URL. The delivery includes the event id evt_9f3 and an HMAC signature of the body. This time, the consumer has to handle those problems.
  3. Anyone can send a POST to a public webhook URL, so only the signature proves that a delivery came from you. The consumer computes the same signature with the shared secret, so the consumer accepts and records the event evt_9f3. A forged POST from somewhere else has no signature at all, so the consumer returns 401. Compare the received and recomputed signatures in constant time: the check takes the same time whether the two signatures match or not.
  4. Their server records this delivery, then fails with a 500. So their outage becomes work for your retry queue, which tries again after 1 second, then 5, then 25. Exponential backoff multiplies the wait after each failure, so your retries do not cause a second outage on their server. After six attempts, the delivery becomes a dead letter: your queue sets it aside and stops retrying it.
  5. At-least-once delivery is the guarantee you can actually make. It means the consumer sometimes gets the same event twice. Here the retry arrives after the consumer's first write already worked, so the consumer now has the same event id twice. No amount of care on your side removes the risk of a duplicate.
  6. An idempotent consumer applies each event only once, no matter how many times the event arrives. The consumer's only protection is the set of event ids it has already seen, in practice one table with a unique column. The table already holds evt_a02, so the consumer drops the second copy and applies only 2 of the 3 deliveries. Say the term idempotent consumer before the interviewer does.
  7. Treat the event as a doorbell: a signal that something changed, not the new data itself. One retry makes order.created arrive after order.updated. So the consumer ignores the data in both payloads and fetches /orders/88, which returns shipped. The current state is the same whichever event arrives first. So fetching the current state means the order the events arrive in no longer matters.
Chapter 3 · step 1 of 7

Returning 202 with a job to poll is for your own long-running work. With a webhook, you become the caller, because someone else needs to know that something happened. Their server, the consumer, gives you a URL to POST to, /hooks/orders, and gets a signing secret back from you. The signing secret is the only thing that lets the consumer tell your calls apart from anyone else's.

1 of 7 Use ← → or swipe See the concept: all 3 chapters in it

© LearnThatStack - diagrams may not be republished without permission.

Want a quick review of the fundamentals? See the API Design cheatsheet.

← Back to all API Design questions
Pro · $10/mo

90 of 110 API Design answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime