Webhooks
easy 24 minA webhook is an HTTP request that another system sends to your URL when something happens, so you do not have to keep asking.
See it run
From asking again and again to being told once: receiving, checking, retrying and handling events twice.
Operations
Polling or a push
Two ways to learn that a payment went through: keep asking, or leave an address and be told.
Step 1 of 5
Ask
Calls
1
pending
Our app asks for the status of payment 123. The answer is pending.
Step 1: Ask
while status(payment) == "pending": sleep(5) # polling# webhook: no loop. The sender calls us.- Delay
- Up to 5 s
- Calls
- 1 every 5 s
- Webhook
- about one network trip, 1 call per event
Give the poll interval and the number of objects, then the calls per day. The number makes the choice for you.
In the real world
The same webhooks doing real jobs. Pick one and step through it.
The journey of one payment, from the click to the parcel.
Step 1 of 3
Start: the customer pays
The payment page belongs to the payment company. We are not in that conversation.
Step 1: Start: the customer pays
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Trusting any POST
criticalThe URL is public. Without a signature check anyone who learns it can mark orders as paid.
Wrongevent = json.loads(body)ship(event["order_id"])Rightif not signature_ok(body, header, secret, now()): return 400Doing the work before answering
criticalThe sender gives up after a few seconds (GitHub allows 10, Slack 3). It retries while you are still working, so the work runs twice.
Wrongfulfil(order) # 20 secondsreturn 200Rightsave(event); enqueue(event.id)return 200Signing the parsed JSON
majorPrinting JSON again can change spacing or key order. The bytes differ, so every honest event is refused.
Wronghmac(json.dumps(json.loads(body)))Righthmac(raw_body)Checking for duplicates with a read, then a write
majorTwo deliveries can read at the same moment, both see nothing, and both do the work. Let a unique key decide.
Wrongif not db.exists(event.id): db.insert(event.id)Rightnew = db.insert_if_absent(event.id) # unique keyif not new: return 200
Interview tips
How to explain it
- A webhook is an HTTP POST that another system sends to your URL when something happens.
- It replaces polling: one call per event instead of one per interval.
- Delivery is at least once, so the receiver removes duplicates.
Memory trick: HAIL
- H: check the HMAC.
- A: process asynchronously.
- I: be idempotent.
- L: log everything, without secrets.
Reach for it when
- Another company's system must tell yours something.
- Events are rare compared with how often you would poll.
- The receiver has a public server.
Look elsewhere when
- The receiver is a browser or a phone: use a real-time connection or push notification.
- Both services are yours: a queue is simpler.
- You must not miss anything: add a daily reconciliation job as well.