Laravel: Rate-Limit API Routes with Throttle
Protect your Laravel API from abuse with the built-in throttle middleware — limit requests per minute per user or IP.
Send a queued job in Laravel after a delay, with retries and backoff, so slow work never blocks the request.
27 lines · Updated Nov 26, 2026
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class SendReportJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $tries = 3; // retry up to 3 times
public $backoff = [60, 300]; // wait 1 min, then 5 min
public function __construct(public int $userId) {}
public function handle(): void
{
// Build and send the report here.
}
}
// Dispatch it to run in 10 minutes:
// SendReportJob::dispatch($user->id)->delay(now()->addMinutes(10));
Some work should not happen right now: a reminder email one hour after signup, a follow-up message three days after purchase, cancelling an unpaid order after 30 minutes. This snippet dispatches a Laravel queued job with a delay and sensible retry settings, so delayed tasks run reliably in the background even if an external service fails temporarily.
ShouldQueue, so it runs on a queue worker instead of during the web request.->delay(now()->addHour()); the worker will not pick it up before that time.$tries and $backoff control how many attempts are made and how long to wait between them.failed() method can alert you when all attempts are used up.QUEUE_CONNECTION to database or redis — with sync, delays are ignored.php artisan queue:restart after every deploy so workers load the new code.Delayed job or scheduler? Use delayed jobs for "X time after an event", and the scheduler for fixed times like "every day at 9:00".
Related: Send WhatsApp messages · System design basics
Save the job as app/Jobs/SendReportJob.php and run php artisan queue:work (or a supervisor in production). Change $tries and $backoff to suit the job.
Share this snippet
Protect your Laravel API from abuse with the built-in throttle middleware — limit requests per minute per user or IP.
Generate a sitemap.xml from your database in Laravel — always up to date, no file to maintain by hand.
Send WhatsApp messages from Laravel through a provider API — order updates, OTPs and alerts in a few lines.