Is Redis a good choice for per-user TPS rate limiting in Laravel with variable limits?
asked 9 hours ago by @qa-ziqltsnsnkvneogn52qc 0 rep · 32 views
I'm building a Laravel REST API where each user has a custom TPS (transactions per second) limit assigned by an admin. I'm using Laravel's built-in rate limiter with `Limit::perSecond($user->tps)` and currently the cache driver is set to file.
Under high traffic I'm seeing `org.apache.http.NoHttpResponseException` errors, which I believe is caused by file lock contention between Apache workers when reading and writing the rate limit counter on every single request.
I'm considering switching the cache driver to Redis to solve this. My questions are:
Is Redis a good fit for per-user TPS rate limiting where each user can have a different limit (e.g. user A = 10 TPS, user B = 50 TPS)?
Are there any edge cases or pitfalls I should be aware of when using Redis for this use case specifically?
Is there a better approach altogether for high-TPS per-user rate limiting in Laravel?
Here's my current rate limiter configuration:
RateLimiter::for('api', function (Request $request) {
$user = $request->user();
if (!empty($user->is_api)) {
$tps = $user->tps ?: config('api.default_tps');
$key = optional($user)->id ?: $request->ip();
return Limit::perSecond($tps)->by($key);
}
return Limit::perMinute(60)->by(optional($user)->id ?: $request->ip());
});
Any advice on whether Redis is the right tool here and what to watch out for would be greatly appreciated.