In this section, I will review how many requests we can handle in Laravel based on my experience.
I worked on a Laravel project for a campaign. In this campaign, we wanted users come to our website and sign up for it.
On the first day of the campaign, numerous users unexpectedly came to sign up, and the server came down.
After this problem occurred, I researched and came across a concept called Throttle.
With Throttle, also known as Rate Limiter, we can specify how many requests are allowed to come to the program in a certain period.
For example, we have a form, and the user can only register this form ten times in 1 minute, and if it is more than ten times, he must face HTTP error 429, "Too Many Requests." This means more than the number of requests allowed in a period. For this, Laravel has an internal middleware called Throttle, which we can be used by setting it on the routes:
Route::post('login', '...')->middleware('throttle:10,1');
The part "throttle:10,1" means that the user can send ten requests to the specified route in 1 minute. The request for more than the specified value will not be checked and will be blocked with status code 429.
There is another way;
Every Laravel project inside the app/Http/Kernel.php
file has a default throttle limit for all API routes:
protected $middlewareGroups = [
...
'api' => [
'throttle:60,1',
],
];
burst inserts
Another reason that may cause problems, as I mentioned in the previous article, is the connection with the database.
Well, it is clear that when you create a panel for registration or user login, you must have a connection with the database, the issue you must pay attention to is controlling this connection.
For example, if you want to insert the information of 10,000 users in the table, you will probably face a problem and get the Too Many Request error.
The proposed solution for large and sudden insertions in a short period of time is to use the queue.
By changing the configuration of this file, we can control how many requests must be handled per minute.
Top comments (0)