Skip to content
Learn Netverks
0

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

laravel redis rate limiting

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:

  1. 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)?

  2. Are there any edge cases or pitfalls I should be aware of when using Redis for this use case specifically?

  3. 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.

Comments on this question (0)

Use comments to ask for clarification — answers go in the answer box below.

Log in to comment on this question.

0

You have a user $request->user() .. just write directly limit to users table and use it in your limiter without Redis or other storages

Casey Patel · 0 rep · 9 hours ago

Your answer