Imagine your API crashing under load, with thousands of requests from a single IP in the logs. On one of our projects, a third‑party service got stuck in a loop, sending 10,000 requests per minute. Without rate limiting, it took down the database and caused 4 hours of downtime. We deployed limits at both Nginx and application levels—the problem never returned. Such cases are our daily bread.
We implement rate limiting for web application APIs on any stack—from Laravel to NestJS—with Redis backend, tier‑based limits, and proper response headers. Below are battle‑tested approaches to help you avoid downtime and protect your infrastructure.
How to Choose a Rate Limiting Algorithm
The choice depends on the traffic pattern. Sliding Window averages requests over a moving window, avoiding the Fixed Window vulnerability: when the counter resets every N seconds, a client can send 100 requests at the end of the window and 100 at the start of the next—effectively 200 per second. Sliding Window prevents this. Token Bucket accumulates tokens at a refill rate, allowing bursts up to the bucket size—ideal for integrations with irregular traffic. Leaky Bucket queues requests with a fixed drain rate, providing the smoothest load but potentially causing delays.
| Algorithm | Accuracy | Burst Protection | Implementation Complexity | Recommended Scenario |
|---|---|---|---|---|
| Fixed Window | Low | No | Low | Simple tiered plans |
| Sliding Window | High | Yes | Medium | General‑purpose APIs |
| Token Bucket | Medium | Yes | Medium | Partner burst traffic |
| Leaky Bucket | High | No | High | Real‑time systems |
For most web applications, Sliding Window with Redis is optimal. It provides even throttling without spikes at window edges. If your API handles burst traffic (e.g., from partner integrations), choose Token Bucket. For real‑time systems with a steady stream, Leaky Bucket is best.
Why Combine Nginx and Application Rate Limiting?
Nginx acts as the first line of defense: it can block obvious anomalies (e.g., more than 1000 requests per second from a single IP) at the server level, without burdening the application. The application then provides flexible per‑user and per‑tier limits. This two‑layer protection is more effective than any single‑layer implementation. You can configure Nginx with the limit_req module for general protection, and set finer rules in the application.
Implementation in Laravel and NestJS
Laravel — using Throttle middleware and RateLimiter:
// app/Providers/RouteServiceProvider.php RateLimiter::for('api', function (Request $request) { $user = $request->user(); if (!$user) return Limit::perMinute(30)->by($request->ip()); return match($user->plan) { 'enterprise' => Limit::perMinute(1000)->by($user->id), 'pro' => Limit::perMinute(300)->by($user->id), default => Limit::perMinute(60)->by($user->id), }; }); Route::middleware(['auth:sanctum', 'throttle:api'])->group(function () { Route::apiResource('articles', ArticleController::class); }); NestJS — using the @nestjs/throttler module with Redis storage:
import { ThrottlerModule, ThrottlerGuard } from '@nestjs/throttler'; import { ThrottlerStorageRedisService } from 'nestjs-throttler-storage-redis'; ThrottlerModule.forRoot({ throttlers: [ { name: 'short', ttl: 1000, limit: 10 }, { name: 'medium', ttl: 60000, limit: 300 }, { name: 'long', ttl: 3600000, limit: 5000 }, ], storage: new ThrottlerStorageRedisService(redisClient), }); | Aspect | Laravel | NestJS | Nginx |
|---|---|---|---|
| Limit flexibility | High (per user, plan) | High (per route group) | Low (only by IP) |
| Performance | Medium (PHP) | High (Node.js) | Maximum (C) |
| Centralized storage | Redis | Redis | No (or external module) |
| Retry-After headers | Automatic | Automatic | Manual |
Distributed Rate Limiting with a Lua Script
For multi‑server applications, we use a centralized Redis and an atomic Lua counter:
-- sliding_window.lua local key = KEYS[1] local now = tonumber(ARGV[1]) local window = tonumber(ARGV[2]) local limit = tonumber(ARGV[3]) redis.call('ZREMRANGEBYSCORE', key, 0, now - window) local count = redis.call('ZCARD', key) if count < limit then redis.call('ZADD', key, now, now .. math.random()) redis.call('EXPIRE', key, window / 1000) return 1 end return 0 This script guarantees atomicity and works in distributed architectures. We use it in high‑load projects.
Bypass Strategies: What Not to Limit
Some requests must bypass limits: internal services (IP whitelist), webhook endpoints, health checks /health. Implement via a condition in the RateLimiter:
RateLimiter::for('api', function (Request $request) { if ($request->ip() === config('services.internal_ip')) { return Limit::none(); } // ... }); Common Mistakes When Implementing Rate Limiting
In 70% of projects we've audited, Fixed Window is used without considering bursts—allowing clients to bypass the limit. Another 20% ignore Retry-After headers, leaving clients unaware of when to retry. Limiting health checks causes false monitoring alerts. Ignoring distributed storage leads to inconsistent counters across servers.
What’s Included in a Turnkey Implementation
- Analysis of current architecture and traffic profile.
- Algorithm selection (we recommend Sliding Window + Redis).
- Configuration of limits per endpoint and tier.
- Integration with Nginx as the first line of defense.
- Addition of
X-RateLimit-*andRetry-Afterheaders. - Monitoring of 429 responses (Grafana + alerts).
- Documentation and team training.
Timeline
Basic implementation with Redis and Sliding Window: 1–2 days. Extended with Nginx, Lua scripts, and monitoring: 3–4 days. The cost is calculated individually after analyzing your project. We evaluate your project in 1 day—contact us for a consultation. Order rate limiting implementation, and your API will withstand any load.
We have implemented rate limiting for 20+ projects of varying complexity. Rest assured, after our deployment, you will forget about overload issues.







