Performance: Why event-driven architecture decouples code
This affects how quickly the app responds to user actions and how smoothly it runs by reducing direct dependencies between code parts.
Jump into concepts and practice - no test required
<?php
// Controller fires event, listeners handle tasks asynchronously
public function store(Request $request) {
$user = User::create($request->all());
event(new UserRegistered($user));
return response()->json(['status' => 'success']);
}
// Listener sends email asynchronously
use IlluminateContractsQueueShouldQueue;
class SendWelcomeEmail implements ShouldQueue {
public function handle(UserRegistered $event) {
NotificationService::sendWelcomeEmail($event->user);
}
}<?php
// Controller directly calls service methods
public function store(Request $request) {
$user = User::create($request->all());
NotificationService::sendWelcomeEmail($user);
return response()->json(['status' => 'success']);
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Tightly coupled direct calls | N/A | N/A | Blocks response, delays input handling | [X] Bad |
| Event-driven decoupled calls | N/A | N/A | Non-blocking, faster response, better input handling | [OK] Good |
event(new UserRegistered($user));
class SendWelcomeEmail implements ShouldQueue { public function handle(UserRegistered $event) { Mail::send(...); }}