Rate Limiting
Introduction
Laravel includes a simple to use rate limiting abstraction which, in conjunction with your application's cache, provides an easy way to limit any action during a specified window of time.
If you are interested in rate limiting incoming HTTP requests, please consult the rate limiter middleware documentation.
Cache Configuration
Typically, the rate limiter utilizes your default application cache as defined by the default
key within your application's cache
configuration file. However, you may specify which cache driver the rate limiter should use by defining a limiter
key within your application's cache
configuration file:
1'default' => 'memcached',23'limiter' => 'redis',
1'default' => 'memcached',23'limiter' => 'redis',
Basic Usage
The Illuminate\Support\Facades\RateLimiter
facade may be used to interact with the rate limiter. The simplest method offered by the rate limiter is the attempt
method, which rate limits a given callback for a given number of seconds.
The attempt
method returns false
when the callback has no remaining attempts available; otherwise, the attempt
method will return the callback's result or true
. The first argument accepted by the attempt
method is a rate limiter "key", which may be any string of your choosing that represents the action being rate limited:
1use Illuminate\Support\Facades\RateLimiter;23$executed = RateLimiter::attempt(4 'send-message:'.$user->id,5 $perMinute = 5,6 function() {7 // Send message...8 }9);1011if (! $executed) {12 return 'Too many messages sent!';13}
1use Illuminate\Support\Facades\RateLimiter;23$executed = RateLimiter::attempt(4 'send-message:'.$user->id,5 $perMinute = 5,6 function() {7 // Send message...8 }9);1011if (! $executed) {12 return 'Too many messages sent!';13}
Manually Incrementing Attempts
If you would like to manually interact with the rate limiter, a variety of other methods are available. For example, you may invoke the tooManyAttempts
method to determine if a given rate limiter key has exceeded its maximum number of allowed attempts per minute:
1use Illuminate\Support\Facades\RateLimiter;23if (RateLimiter::tooManyAttempts('send-message:'.$user->id, $perMinute = 5)) {4 return 'Too many attempts!';5}
1use Illuminate\Support\Facades\RateLimiter;23if (RateLimiter::tooManyAttempts('send-message:'.$user->id, $perMinute = 5)) {4 return 'Too many attempts!';5}
Alternatively, you may use the remaining
method to retrieve the number of attempts remaining for a given key. If a given key has retries remaining, you may invoke the hit
method to increment the number of total attempts:
1use Illuminate\Support\Facades\RateLimiter;23if (RateLimiter::remaining('send-message:'.$user->id, $perMinute = 5)) {4 RateLimiter::hit('send-message:'.$user->id);56 // Send message...7}
1use Illuminate\Support\Facades\RateLimiter;23if (RateLimiter::remaining('send-message:'.$user->id, $perMinute = 5)) {4 RateLimiter::hit('send-message:'.$user->id);56 // Send message...7}
Determining Limiter Availability
When a key has no more attempts left, the availableIn
method returns the number of seconds remaining until more attempts will be available:
1use Illuminate\Support\Facades\RateLimiter;23if (RateLimiter::tooManyAttempts('send-message:'.$user->id, $perMinute = 5)) {4 $seconds = RateLimiter::availableIn('send-message:'.$user->id);56 return 'You may try again in '.$seconds.' seconds.';7}
1use Illuminate\Support\Facades\RateLimiter;23if (RateLimiter::tooManyAttempts('send-message:'.$user->id, $perMinute = 5)) {4 $seconds = RateLimiter::availableIn('send-message:'.$user->id);56 return 'You may try again in '.$seconds.' seconds.';7}
Clearing Attempts
You may reset the number of attempts for a given rate limiter key using the clear
method. For example, you may reset the number of attempts when a given message is read by the receiver:
1use App\Models\Message;2use Illuminate\Support\Facades\RateLimiter;34/**5 * Mark the message as read.6 *7 * @param \App\Models\Message $message8 * @return \App\Models\Message9 */10public function read(Message $message)11{12 $message->markAsRead();1314 RateLimiter::clear('send-message:'.$message->user_id);1516 return $message;17}
1use App\Models\Message;2use Illuminate\Support\Facades\RateLimiter;34/**5 * Mark the message as read.6 *7 * @param \App\Models\Message $message8 * @return \App\Models\Message9 */10public function read(Message $message)11{12 $message->markAsRead();1314 RateLimiter::clear('send-message:'.$message->user_id);1516 return $message;17}