Per-Tenant Configuration Storage
What you'll learn: How to store and dynamically manage unique rate limit rules, quotas, and tiers for each tenant in a multi-tenant rate limiting system.
The Challenge
In a multi-tenant rate limiter, Tenant A might need 1,000 requests per minute while Tenant B needs 10,000. Each tenant may have different time windows, algorithms (token bucket vs sliding window), or even different limits per API endpoint. You need a flexible, fast way to store and retrieve these per-tenant configurations without hardcoding them.
Core Storage Design
Configuration Data Structure
Each tenant's configuration includes:
- Tenant ID (unique identifier)
- Rate limit values (e.g., 5000 requests/hour)
- Algorithm type (token bucket, sliding window, etc.)
- Time window (per minute, hour, day)
- Tier/plan (free, premium, enterprise)
- Custom rules per endpoint (optional overrides)
Storage Options
Redis as a configuration store:
Store tenant configs as Redis hashes or JSON strings with keys like config:tenant:{tenant_id}. This allows sub-millisecond lookups alongside your rate limit counters.
Relational database (PostgreSQL): For complex queries, audit trails, and durability. Cache heavily in Redis or local memory to avoid query overhead on every rate limit check.
Hybrid approach: Database as source of truth, Redis as read-through cache with TTL-based refresh.
Analogy
Think of it like a hotel key card system: each guest (tenant) has a card (config) programmed with specific access rules—some can use the gym 24/7, others only during business hours. The front desk database holds all rules; room card readers cache them for instant validation.
Key Takeaway:
Per-tenant configuration storage decouples rate limit policies from code, enabling dynamic, scalable management of diverse tenant requirements through fast-access key-value stores backed by persistent databases.