Requirements (functional + non‑functional):
- Receive partner HTTP(S) POSTs reliably at scale, verify authenticity, prevent replay, ensure idempotent processing, surface failures for retries/alerting, and detect suspicious activity.
High-level architecture:
Partner → API Gateway (TLS, rate limiting, mTLS optional) → Webhook Receiver Service → Validation Layer → Processing Queue → Worker(s) → Persistent Store / Downstream systems → DLQ / Alerting / Monitoring
Authentication & verification
- Transport: enforce TLS 1.2+ and HSTS. Offer optional mTLS for high-trust partners.
- Message signature: require partner to sign payload with HMAC-SHA256 using a shared secret; include headers:
- X-Partner-Id: partner identifier
- X-Signature: v1=hex(hmac_sha256(secret, timestamp + "." + body)), v=version
- X-Timestamp: unix seconds (or ISO8601)
- X-Idempotency-Key: UUID (optional)
- Key management: support key versioning (kid) in header, rotate keys periodically, store secrets encrypted (KMS).
Signature verification steps
- Validate partner exists and retrieve active secret by partner id and kid.
- Ensure |now - X-Timestamp| <= allowed window (e.g., 300s).
- Compute expected = HMAC(secret, timestamp + "." + body); constant-time compare to X-Signature.
- If mismatch → 401 and log.
Replay protection & idempotency
- Timestamp window prevents old messages. Additionally store recent signature hashes or nonce in a fast store (Redis) with TTL = window.
- Idempotency: require or accept X-Idempotency-Key. Persist processed message keys (combination of partner id + idempotency-key or signature hash) in durable store (Redis for short TTL or RDBMS for long-lived dedupe) and return cached result if duplicate.
- If no idempotency key, use signature hash as dedupe key.
Failure handling & retries
- Receiver should validate and enqueue quickly (respond 2xx on accept). Use durable queue (Kafka/SQS) for processing.
- If downstream processing fails: retry with exponential backoff; after N attempts, send message to DLQ and generate alert.
- For synchronous validation failures (bad signature/timestamp), respond 4xx without retry.
- Provide partner-facing retry guidelines and a webhook replay endpoint for manual replays (authenticated).
Monitoring & suspicious activity detection
- Metrics: total requests, successful verifications, failed verifications, replay attempts, duplicate deliveries, processing failures, processing latency, queue depth.
- Logs: structured logs with partner id, request id, signature status (do not log raw secrets or full payloads).
- Alerts: thresholds for spikes in failed verifications, repeated bad signatures from same partner, sudden increase in traffic or replay attempts.
- Automated actions: throttle/rate‑limit or temporarily suspend partner keys on repeated failures; require rekeying or mTLS for high-risk partners.
- Audit trail: immutable storage of webhook receipts (hash of payload + metadata) for forensic analysis.
Operational considerations
- Scale: API Gateway autoscale + horizontally scaled receivers; Redis/KV for dedupe with consistent hashing; workers scale by queue partitions.
- Security: rotate keys, KMS encryption, least privilege for services, WAF for common threats, protect logs for PII.
- Documentation & onboarding: provide sample signing code (HMAC example), test endpoints, and key rotation process.
- Example HMAC snippet (pseudo): expectedSig = HMAC_SHA256(secret, timestamp + "." + body); compare const-time.
This design balances security, reliability and operability: fast validation at edge, durable asynchronous processing, strong replay/idempotency controls, and monitoring + automated mitigation for suspicious activity.