There are a few overlapping concerns when it comes to using Redis for rate limiting in a Laravel application.
By default, Laravel will use your default CACHE_STORE
for rate limiting.
So if you have your application using Redis by default for caching, then rate limiting will also use Redis.
But in the docs, there is a configuration snippet that also shows how to explicitly set a different cache store for rate limiting. If you're just scanning the docs and not reading them closely, you might think you need to set that as well, but it's only required in the specific scenario of overriding the default cache store.
If you set it unnecessarily, it can cause you grief in testing.
For example, if your test environment swaps out CACHE_STORE
to use an in-memory array store instead of Redis, but you also have that extra cache.limiter
configuration key set to redis
, then your tests will try to use Redis for rate limiting, even though the rest of your application is using the in-memory array store.
Second, there's an additional middleware optimization available if you're using Redis for your rate limiter implementation.
You can specify using the ThrottleRequestsWithRedis
middleware instead of the default ThrottleRequests
middleware.
While both middleware will work, this optimized middleware is tuned specifically for Redis, eliminating unnecessary Redis commands that the default middleware would use with the general cache store interface.
Here again though, just be aware that if you set that middleware on your app instance, it will also try to use it in tests, which may not be what you want.
Here to help,
Joel
P.S. Would you like help dealing with an issue in your Laravel test suite? That's a great use for a Get Unstuck call!