Design quota enforcement + API usage tracking system (with Redis, Kafka, CDC, etc.)

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2024-03-01

Problem Statement

You are tasked with designing a Quota Enforcement and API Usage Tracking System for a large-scale SaaS platform. The platform exposes APIs to thousands of clients (apps, partners, internal services). Each client has usage quotas (daily, monthly, per-second) that must be enforced in real time, while also providing accurate usage analytics for billing, monitoring, and anomaly detection.


Functional Requirements


1.Quota Enforcement (Real-Time):
•Enforce per-client quotas (e.g., 1000 requests/day, 100 requests/min).
•Support hierarchical quotas (per-user, per-organization, global).
•Reject requests that exceed quota with appropriate error response.
2.Usage Tracking (Streaming + Storage):
•Track API usage events (client ID, endpoint, timestamp, status).
•Ensure accurate accounting (exactly-once or idempotent tracking).
•Store usage history for billing, dashboards, and audits.
3.Quota Configuration & Updates:
•Admins should be able to configure or update quotas dynamically.
•Support CDC (Change Data Capture) to propagate quota updates in real-time (e.g., from MySQL → Kafka → Redis).
4.Monitoring & Reporting:
•Provide dashboards for API usage (per client, per endpoint, per time window).
•Trigger alerts when clients approach quota limits or show abnormal traffic spikes.


Non-Functional Requirements


•Low Latency: Quota check should add <5 ms overhead per request.
•Scalability: Handle millions of requests per second.
•Fault Tolerance: Ensure usage tracking works even if downstream systems (Kafka, DB) are temporarily down.
•Consistency:
•Strong consistency needed for quota checks.
•Eventual consistency acceptable for reporting/analytics.
•Extensibility: Ability to add new quota dimensions (per region, per API type) easily.


Core Components


•API Gateway / Proxy Layer: Intercepts API calls, performs real-time quota checks.
•In-Memory Store (Redis/Redis Cluster): Maintains counters for quotas (per-client, per-window).
•Event Pipeline (Kafka):
•Streams all API usage events for durability & analytics.
•Supports replay for correcting inconsistencies.
•CDC Layer (Debezium/MySQL Binlog → Kafka): Propagates quota configuration changes in real time.
•Usage Storage:
•Hot storage (Redis, Cassandra) for fast lookups.
•Cold storage (S3, BigQuery, Snowflake) for historical analytics.
•Monitoring + Alerts: Grafana/Prometheus for metrics; anomaly detection on Kafka streams.
•Admin Service + UI: Manage quota rules, update limits, and view reports.


Example Flow


1.Quota Enforcement
•API Gateway → Redis atomic counter (INCR with TTL/window).
•If counter exceeds quota → reject request.
2.Usage Tracking
•Every API call emits an event → Kafka.
•Consumers update usage DB + dashboards.
•Quota changes (from MySQL) flow via CDC → Kafka → Redis sync.


Key Trade-offs to Discuss


•Where to enforce quota? (API Gateway vs service-level).
•Counter granularity? (sliding window vs fixed interval).
•Data freshness vs cost? (real-time Redis vs eventual consistency DB).
•Duplicate event handling? (exactly-once semantics with Kafka vs idempotency in consumers).


⚡ This problem blends real-time quota enforcement (Redis), streaming analytics (Kafka), and CDC-based configuration sync — making it an excellent HLD interview problem.

Solution

Comments