Performance: Queued notifications
This affects how quickly the page responds to user actions by offloading notification sending to background jobs, improving interaction speed.
Jump into concepts and practice - no test required
Notification::send($user, (new InvoicePaid($invoice))->delay(now()->addSeconds(5))); // or using ShouldQueue interface on notification class $user->notify((new InvoicePaid($invoice))->delay(now()->addSeconds(5)));
Notification::send($user, new InvoicePaid($invoice));
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous notification sending | N/A | N/A | Blocks server response delaying paint | [X] Bad |
| Queued notification sending | N/A | N/A | Server responds immediately, faster paint and interaction | [OK] Good |
notify() is called?
use Illuminate\Notifications\Notification;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
class InvoicePaid extends Notification implements ShouldQueue
{
use Queueable;
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage)->line('Invoice paid.');
}
}use Illuminate\Notifications\Notification;
use Illuminate\Bus\Queueable;
class OrderShipped extends Notification
{
use Queueable;
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage)->line('Order shipped.');
}
}class PaymentFailed extends Notification implements ShouldQueue
{
use Queueable;
public $tries = 3;
public function via($notifiable) { return ['mail']; }
}
B) class PaymentFailed extends Notification implements ShouldQueue
{
use Queueable;
public function via($notifiable) { return ['mail']; }
public function retryTimes() { return 3; }
}
C) class PaymentFailed extends Notification
{
use Queueable;
public $tries = 3;
public function via($notifiable) { return ['mail']; }
}
D) class PaymentFailed extends Notification implements ShouldQueue
{
use Queueable;
public function via($notifiable) { return ['mail']; }
public $maxRetries = 3;
}