symfony/rate-limiter
Symfony Rate Limiter component implementing token bucket rate limiting. Configure limiters via a factory and use reserve() to wait for tokens or consume() to attempt immediately. Supports pluggable storage like in-memory for controlling request/input/output rates.
API Security & Abuse Prevention:
/api/payments = 10 requests/minute) to prevent scraping or abuse of sensitive operations.SaaS Monetization & Tiered Access:
config() or environment variables.consume(1) for control group, consume(5) for treatment group) to test monetization strategies.Compliance & Auditing:
Cost Optimization & Scalability:
User Experience & Transparency:
Retry-After headers (RFC 6585) for API clients, improving developer experience.429 Too Many Requests) with Retry-After timestamps for self-service recovery.Microservices & Distributed Systems:
Roadmap Acceleration:
Adopt when:
Retry-After headers for API clients or RFC 6585 compliance.CompoundRateLimiterFactory.sleep() delays).throttle middleware and seeking a unified solution across APIs, queues, and CLI commands.config/rate_limits.php or environment variables.Look elsewhere:
express-rate-limit (Node.js), ulule/limiter (Go), or django-ratelimit (Python).InMemoryStorage (single-instance only).For Executives:
*"Symfony’s Rate Limiter is a turnkey solution to secure our APIs, prevent abuse, and enable scalable monetization—without custom development. It’s already integrated into Laravel’s core (throttle middleware), so adoption is seamless. Here’s the impact:
For Engineering Teams: *"This package replaces spaghetti rate-limiting code with a battle-tested, Symfony-backed solution. Key advantages:
InMemoryStorage for dev, RedisStorage for prod, or even a database—your choice.throttle middleware, Horizon queues, and Telescope.config() or env vars—no redeploys needed.
Implementation:composer require symfony/rate-limiter.config/rate_limits.php:
'login' => ['limit' => 5, 'interval' => '5 minutes'],
'api' => ['limit' => 100, 'interval' => 'hour'],
$limiter = app(RateLimiterFactory::class)->create('login');
if (!$limiter->consume(1)->isAccepted()) {
return response()->json(['error' => 'Too many attempts'], 429);
}
Next steps: Let’s prioritize this for the auth and payment flows in Q3. We’ll start with Redis storage and add database fallback for high-availability."*
For Security Teams: *"This addresses A03:2021 (Injection) and A07:2021 (Broken Authentication) with:
Retry-After headers: Reduces support overhead for throttled requests./reset-password, /admin). Example:// 3 attempts per 5 minutes for password resets
$limiter = app(RateLimiterFactory::class)->create('password_reset');
if (!$limiter->consume(1)->isAccepted()) {
event(new RateLimitExceeded($request));
return response()->json(['error' => 'Too many attempts'], 429)
->header('Retry-After', $limiter->getRetryAfter()->format('U'));
}
```"*
How can I help you explore Laravel packages today?