Communication

Webhooks

Understanding webhooks for event-driven integrations.

3 min readConcepts · System Design

What are Webhooks?

Webhooks are HTTP callbacks that notify external systems when an event occurs. Instead of polling for changes, the server pushes data to a registered URL when something happens.


Polling vs Webhooks

Polling vs WebhooksFlowchart
AspectPollingWebhooks
DirectionClient → ServerServer → Client
LatencyDepends on intervalNear real-time
EfficiencyWasteful (many empty responses)Efficient (only on events)
ComplexitySimpleRequires endpoint

How Webhooks Work

How Webhooks WorkSequence diagram

Webhook Payload

{
  "id": "evt_123abc",
  "type": "payment.completed",
  "created": 1710432000,
  "data": {
    "object": {
      "id": "pay_456def",
      "amount": 2000,
      "currency": "usd",
      "status": "succeeded"
    }
  }
}

Security

Signature Verification

Signature VerificationFlowchart
const crypto = require('crypto');

function verifyWebhook(payload, signature, secret) {
  const expected = crypto
    .createHmac('sha256', secret)
    .update(payload)
    .digest('hex');

  return crypto.timingSafeEqual(
    Buffer.from(signature),
    Buffer.from(expected)
  );
}

Best Practices

PracticeWhy
Verify signaturesPrevent spoofed events
Use HTTPSEncrypt in transit
Validate timestampsPrevent replay attacks
Use secrets rotationLimit exposure

Reliability

Retry Strategy

Most webhook providers retry on failure:

Attempt 1: Immediate
Attempt 2: 5 minutes
Attempt 3: 30 minutes
Attempt 4: 2 hours
Attempt 5: 24 hours

Handling on Your End

Handling on Your EndFlowchart

Key rule: Return 200 quickly, process asynchronously.


Common Webhook Providers

ProviderEvents
Stripepayment.succeeded, invoice.paid
GitHubpush, pull_request, issues
Twiliomessage.received, call.completed
Shopifyorders/create, products/update
Slackmessage, reaction_added

Building a Webhook System

As a Provider

As a ProviderFlowchart
ComponentPurpose
Event storeRecord all events
Subscription tableURL + event types per subscriber
Delivery queueReliable, ordered delivery
Retry schedulerExponential backoff
Dead letter queueFailed after max retries

Webhooks vs Other Patterns

PatternBest For
WebhooksCross-service notifications, third-party integrations
WebSocketsReal-time bidirectional communication
SSEServer-to-client streaming
Message QueuesInternal service-to-service
PollingSimple, no public endpoint

Interview Tips

  • Compare webhooks vs polling (efficiency, latency)
  • Explain signature verification (HMAC)
  • Discuss reliability: quick ACK + async processing
  • Mention retry strategy and idempotency
  • Know real-world examples (Stripe, GitHub)

Finished reading?

Mark it done to track your progress in Concepts.