Design a Webhook Delivery System

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2024-09-06

Problem Statement

Design a generic and scalable Webhook System** that allows a platform to notify external client systems (subscribers) in real-time when specific events occur.


For example:

•A payment service (like Stripe) notifies a merchant’s backend when a payment is successful.
•A CI/CD platform (like GitHub Actions) triggers a third-party server when a commit is pushed.


The system should support event publishing, reliable delivery, retry on failure, and subscriber management.


🎯 Functional Requirements:

1.Event Triggering
•Services should be able to publish events (e.g., ORDER_PLACED, USER_SIGNED_UP, etc.)
2.Webhook Registration
•Clients (third-party consumers) should register:
•Endpoint URL
•Event types they care about
•Authentication method (e.g., secret header)
3.Webhook Delivery
•Deliver events to registered endpoints asynchronously
•Support batching and retries on failure
4.Retry & DLQ
•Exponential backoff on failure
•Move to Dead Letter Queue (DLQ) after max retries
5.Security
•Authenticate outbound requests via secret tokens, HMAC, etc.
6.Monitoring & Logging
•Track delivery success/failure metrics
•Provide delivery status dashboard to clients


⚙️ Non-Functional Requirements:

•Low latency delivery (<1s ideally)
•High throughput (thousands of events/sec)
•Guaranteed ordering (optional or per subscriber)
•High availability and retry durability
•At-least-once delivery (no lost events)


🧱 Core Components:

ComponentResponsibility

Publisher API

Internal services use to emit events

Event Router/Queue

Pushes events to topic/queue for async processing

Webhook Registry

Stores subscriber info and delivery config

Delivery Workers

Pick events from queue and push to client endpoints

Retry Manager

Retries failed deliveries with backoff

DLQ Handler

Moves permanently failed messages for inspection

Logging/Monitoring

Tracks delivery metrics and errors

Security Layer

HMAC signing and validation for trusted delivery


📦 High-Level Flow:

1.Internal service (e.g., order-service) → emits an event to Publisher API
2.Event gets written to Kafka / Event Bus
3.Webhook Processor:
•Fetches all matching subscribers
•Sends payload to their endpoint via HTTP POST
•Signs payload with a shared secret
4.On delivery failure:
•Retry with exponential backoff
•After N retries, move to DLQ
5.All events and statuses are logged


🛢️ Database Schema (Simplified)


webhook_subscriptions

1bash 2CopyEdit 3id | client_id | event_type | endpoint_url | secret | is_active


event_logs

1lua 2CopyEdit 3event_id | event_type | payload | created_at | retry_count | status


dlq

1nginx 2CopyEdit 3event_id | reason | last_attempt_time | payload


🚀 Scalability and Reliability Strategies:

•Use Kafka or RabbitMQ as event bus
•Horizontal scaling of Delivery Workers
•Use Redis or DynamoDB to store deduplicated delivery state
•Enable rate limiting per subscriber
•Support multi-tenancy for client isolation


🔐 Security Considerations:

•HMAC-SHA256 signature headers for payloads
•HTTPS only delivery
•Optional OAuth-based delivery authentication
•Payload encryption at rest


🧰 Suggested Tech Stack:

LayerTools

Event Queue

Kafka / RabbitMQ / SQS

Worker Execution

Node.js / Go / Java

Database

PostgreSQL / DynamoDB

Retry Scheduler

BullMQ / Sidekiq / Celery

Monitoring

Prometheus + Grafana

DLQ Storage

S3 / DynamoDB / Elasticsearch

Security

Vault for key storage


🔁 Example Event (Webhook Payload):

1json 2CopyEdit 3POST /webhook 4Headers: 5 X-Signature: HMAC-SHA256=abc123... 6 7Body: 8{ 9 "event": "ORDER_PLACED", 10 "timestamp": 1658723423, 11 "data": { 12 "order_id": "ORD123", 13 "amount": 499.00 14 } 15}


✨ Optional Extensions:

•Webhook debugging UI (replay, inspect headers)
•Replay events from DLQ
•Throttling, IP whitelisting
•Subscription billing (charge based on events)


Solution

Comments