Hey DEV community! ๐ Ever had that moment when you realize your API could get overwhelmed by too many requests? I faced that exact issue in a recent Laravel project and decided to tackle it with Rate Limiting. Spoiler: It worked like a charm! In this post, Iโll break down what Rate Limiting is and show you how I applied it to my project with some handy code snippets. Letโs dive in! ๐
What Is Rate Limiting? (The Quick Version)
Rate Limiting is like putting a bouncer at your APIโs door: โOnly 10 requests per minute โ everyone else waits!โ I needed this in my project to keep things running smoothly.
Why Itโs a Game-Changer ๐
Hereโs why I decided to use it:
โ
Security: Stops bots from spamming my API (think brute-force attacks).
โ
Server Health: Keeps my server from choking on too many requests.
โ
Fairness: Ensures all users get a smooth experience, no matter the traffic.
Laravelโs Rate Limiting Superpowers ๐ฆธโโ๏ธ
Laravel makes Rate Limiting a breeze with some awesome tools. I experimented with two approaches in my project.
Approach 1: The Manual Way with RateLimiter
Laravelโs RateLimiter facade lets you control limits manually. Hereโs what I used:
-
hit: Tracks each request. Example:RateLimiter::hit('api', 60)logs a request for theapikey, resetting after 60 seconds. -
tooManyAttempts: Checks if the limitโs exceeded.RateLimiter::tooManyAttempts('api', 10)returnstrueif thereโs more than 10 requests.
Implementation:
$key = 'contacts:' . request()->ip();
RateLimiter::hit($key, 60);
if (RateLimiter::tooManyAttempts($key, 10)) {
$seconds = RateLimiter::availableIn($key);
return response("Whoa! Too many requests. Wait $seconds seconds!", 429);
}
This worked, but I found myself repeating this code everywhere โ not ideal. ๐
Approach 2: The Magic of throttle Middleware โจ
Then I discovered Laravelโs throttle middleware, and it was a game-changer! I set up a named limit using RateLimiter::for in RouteServiceProvider:
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;
protected function configureRateLimiting()
{
RateLimiter::for('contacts', function (Request $request) {
return $request->user()
? Limit::perMinute(20) // 20 for logged-in users
: Limit::perMinute(10)->by($request->ip()); // 10 for others
});
}
Then I applied it to my routes in routes/api.php:
Route::prefix('contacts')->middleware('throttle:contacts')->group(function () {
Route::get('/', [ContactController::class, 'index']);
Route::post('/', [ContactController::class, 'store']);
});
This approach kept my controller clean โ no manual checks needed! ๐
My Project: A Contacts API with Rate Limiting ๐ก๏ธ
For my project, I built a simple API to manage contacts (viewing and adding contacts). I wanted to make sure it could handle traffic without breaking, so I added Rate Limiting.
The Controller
Hereโs the controller I used:
namespace App\Http\Controllers;
use App\Models\Contact;
use Illuminate\Http\Request;
class ContactController extends Controller
{
public function index()
{
return response()->json(Contact::all());
}
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
'phone' => 'required|string|max:20',
]);
$contact = Contact::create($validated);
return response()->json($contact, 201);
}
}
Thanks to throttle middleware, I didnโt need to add any Rate Limiting logic here. If users hit the limit, Laravel automatically returns a โ429โ Too Many Requests response. ๐ฏ
Testing It Out ๐
I tested it with Postman:
-
Unauthenticated: 10
GETrequests worked fine; the 11th got a โ429โ error. - With a token (Sanctum): I hit 20 requests smoothly, but the 21st triggered the limit.
It was amazing to see how little code I needed to keep my API safe! ๐ก๏ธ
Key Takeaways ๐
Using Rate Limiting in my Laravel project was a total win. Itโs now a go-to tool for me whenever I build APIs. Hereโs what I learned:
โ
Use RateLimiter for fine-grained control, but throttle for simplicity.
โ
Protect your server and keep your users happy with just a few lines of code.
Have you tried Rate Limiting in your projects? Drop a comment โ Iโd love to hear your thoughts! If you want to dive deeper, check out my full setup on Medium. ๐
Top comments (0)