Cost-Based Rate Limiting
What you'll learn: How to rate limit based on the computational or resource cost of operations, not just raw request counts.
The Core Problem
Traditional rate limiting treats all requests equally—whether you're fetching a tiny profile picture or running a complex analytics query, each counts as "1 request." But in reality, some operations consume vastly more resources (CPU, memory, database queries, I/O) than others.
Cost-based rate limiting charges users differently based on what their requests actually cost your system.
How It Works
Instead of counting requests, you assign a cost weight to each operation:
- Simple read operation: 1 credit
- Complex search query: 10 credits
- Report generation: 50 credits
- Video transcoding: 500 credits
Users then have a credit budget per time window rather than a request count. A user might burn through their budget with 100 simple reads or just 2 expensive reports.
Why This Matters
Fair resource allocation: Power users running expensive operations don't get the same treatment as those making lightweight calls. This prevents abuse where someone could DoS your system with 100 computationally-expensive requests while staying under a "100 requests/minute" limit.
Better monetization: You can offer tiered pricing that reflects actual infrastructure costs, not arbitrary request counts.
Analogy
Think of a buffet with two pricing models: "pay per plate" vs "pay by weight." Cost-based rate limiting is like paying by weight—it doesn't matter if you take one plate or three, what matters is whether you loaded up on heavy prime rib or light salad.
Key Takeaway: Cost-based rate limiting tracks resource consumption instead of request counts, ensuring expensive operations are charged proportionally to their actual system impact.