Overview & goals
Design a global, low-latency (<5ms added) per-tenant rate limiter for 10k RPS peak (multi-region), scalable to 100k tenants, with burst handling and graceful degradation under datastore partitions. I propose a hybrid edge-local + centralized coordination approach using token-bucket at the edge and Redis for global quota enforcement and sync.
Algorithm choices
- Edge token-bucket (local, in-memory): immediate checks for low latency and burst absorption. Each API gateway instance keeps per-tenant tokens and refill logic.
- Periodic global reconciliation using a distributed counter (Redis) to enforce long-term quotas and prevent cross-region abuse.
- For strict global windows, use Redis LUA scripts (atomic) implementing sliding-window counters or a global leaky-bucket.
Data stores & topology
- Redis Cluster (sharded, persistence AOF/RDB) per-region for fast atomic ops; enable Redis Raft or Cluster with replicas for high availability.
- Config store (etcd) for tenant policies, burst sizes, risk posture.
- Use consistent-hash assignment of tenant -> shard to scale to 100k tenants.
Eventual consistency & correctness
- Primary enforcement at edge => correctness is optimistic; edges periodically (e.g., every 100–500ms) sync token usage to Redis.
- For strong enforcement (high-risk tenants), perform synchronous Redis check on each request (costly) — use risk-based policy.
- Use Redis LUA scripts for atomic decrements when doing synchronous checks; fallback to edge-only when Redis partitioned.
Graceful degradation
- Detect datastore partition via healthchecks/latency.
- Tiered fail mode:
- Fail-closed for critical/high-risk tenants (deny if uncertain).
- Fail-open for low-risk tenants (allow but mark telemetry and apply heavier post-facto throttles).
- Queue background reconciliation and rate-backoff if central counters resume.
Scaling & tenancy
- Shard tenants across Redis slots; push tenant policy to gateways via etcd watch to avoid per-request config fetch.
- Edge memory ~ O(active tenants per node). For 100k tenants, use many gateway instances and evict inactive tenant entries LRU.
Monitoring & metrics
- Per-tenant and per-region metrics: allowed, denied, dropped (due to datastore), sync lag, Redis latency, token consumption rate.
- Dashboards (Prometheus + Grafana), alerts for >1% failopen, Redis errors, sync lag >500ms.
- Audit logs for security investigations: decisions, tenant id, policy version, enforcement mode.
Trade-offs
- Hybrid reduces latency but accepts temporary cross-region quota overshoots; mitigated by reconciliation and risk-based synchronous checks.
- Strong consistency is possible at cost of latency; use selective strictness for high-risk tenants.
This design balances security posture, low latency, and operational resilience appropriate for a Security Architect: enforceable policies, risk-tiered strictness, and clear observability for incident response.