Safe Webhook Consumer Design for Multi-Tenancy
This design outlines a robust, scalable architecture for consuming webhooks from external providers (e.g., Stripe, GitHub, Twilio) in a multi-tenant SaaS environment. It prioritizes fairness (equitable resource sharing), isolation (tenant separation), and abuse controls (mitigating DoS, spam, or malicious payloads). The system handles high-volume, variable traffic while ensuring 99.99% uptime and sub-second P99 latency for critical events.
1. High-Level Architecture
code Copy code
[External Providers] --> [API Gateway] --> [Validation Service] --> [Event Router] --> [Tenant Queues]
|
v
[Worker Pools] <--> [Shared Services: Cache, DB, Analytics] <--> [Monitoring & Alerting]
Components :
Component Tech Stack Purpose API Gateway Envoy/Kong/NGINX Ingress, TLS termination, rate limiting, routing. Validation Service Go/Node.js microservice Signature verification, schema validation, deduping. Event Router Kafka/SQS/RabbitMQ Fan-out to tenant-specific queues/topics. Worker Pools Kubernetes Jobs + Keda autoscaler Async processing with per-tenant scaling. Shared Services Redis (cache), Postgres (metadata), Elasticsearch (logs). Monitoring Prometheus + Grafana + Sentry.
Flow :
Webhook hits /v1/webhooks/{tenant_id}/{provider}/{event_type} (e.g., /v1/webhooks/acme/stripe/invoice.paid).
Gateway applies global/tenant rate limits.
Validation service checks auth, queues valid events.
Workers consume from isolated queues, process idempotently.
ACK/NACK with retries (exponential backoff, dead-letter queues).
2. Tenant Isolation
Prevent "noisy neighbor" issues and lateral movement.
Network Isolation :
Kubernetes namespaces per tenant tier (e.g., tenant-gold, tenant-silver).
Network policies: Workers only access their namespace's queue/DB shards.
mTLS between services; tenant-scoped certs.
Data Isolation :
Queue Partitioning : Kafka topics partitioned by tenant_id (e.g., webhooks.acme, webhooks.beta). Use consumer groups per worker pool.
Database Sharding : Row-level tenancy in Postgres ( tenant_id index + RLS). Event payloads stored encrypted (AES-256-GCM).
Memory/Process Isolation : Container limits (CPU: 100m-2 cores, Mem: 128Mi-4Gi per pod, based on tenant tier).
Payload Handling :
Deserialize in sandboxed workers (e.g., Firecracker VMs for untrusted code).
No direct tenant code execution; use safe handlers (e.g., JSONata for transformations).
Secrets Management :
Per-tenant webhook secrets in Vault/K8s Secrets, scoped to namespaces.
3. Fairness and Resource Allocation
Ensure equitable CPU, memory, and throughput sharing.
4. Abuse Controls
Detect and mitigate DoS, replays, malformed payloads, or malicious tenants.
5. Reliability and Observability
6. Security & Compliance
Encryption : Payloads at-rest (EBS encryption), in-transit (TLS 1.3).
Auditing : All events logged to ES with tenant context (GDPR-compliant purge).
SOC2/ISO27001 : Role-based access; secrets rotation every 90d.
Edge Cases :
Zero tenants: Graceful degrade.
Mega-tenant: Dedicated cluster shard.
Implementation Roadmap
MVP (2 weeks) : Gateway + Kafka + Basic Workers.
Beta (4 weeks) : Rate limits + Validation + K8s isolation.
Prod (8 weeks) : ML abuse + Autoscaling + Monitoring.
This design scales to 1M+ events/day across 10k tenants, with <0.01% cross-tenant impact. Cost: ~$0.001/event at scale. Customize quotas via YAML configs for flexibility.
Questions? Let's iterate! 🕵️♂️