AlgoViz

Webhooks

easy 24 min

A 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

status of 123?Our shopPayment company

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

Our systemsigned POSTsaveevent idmarkPayment companythe senderEndpointverify, save, 200Event storeunique event idQueueWorkerbusiness logic

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

    critical

    The URL is public. Without a signature check anyone who learns it can mark orders as paid.

    Wrong
    event = json.loads(body)ship(event["order_id"])
    Right
    if not signature_ok(body, header, secret, now()):    return 400
  • Doing the work before answering

    critical

    The sender gives up after a few seconds (GitHub allows 10, Slack 3). It retries while you are still working, so the work runs twice.

    Wrong
    fulfil(order)      # 20 secondsreturn 200
    Right
    save(event); enqueue(event.id)return 200
  • Signing the parsed JSON

    major

    Printing JSON again can change spacing or key order. The bytes differ, so every honest event is refused.

    Wrong
    hmac(json.dumps(json.loads(body)))
    Right
    hmac(raw_body)
  • Checking for duplicates with a read, then a write

    major

    Two deliveries can read at the same moment, both see nothing, and both do the work. Let a unique key decide.

    Wrong
    if not db.exists(event.id):    db.insert(event.id)
    Right
    new = 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.