Modern Laravel applications experience asynchronous workflows, event sourcing and delayed execution. Laravel has a capable queue system to handle delay event handling or storing a historical log of all changes. 

What Is Delayed Event Processing in Laravel?

Delayed event processing in Laravel is intentionally delaying a specific event listener for a fixed time. This is a useful practice for offloading time-consuming tasks like emails, data sync with third-party services or creating background reports. 

How to Implement Delayed Event Processing in Laravel?

Problem Statement: Sending a welcome email 10 minutes after a user signs up when you have a UserRegistered event.

Step 1: Create the Event and Listener

bash

php artisan make:event UserRegistered
php artisan make:listener SendWelcomeEmail --event=UserRegistered

Step 2: Implement the Listener with Delay

In your SendWelcomeEmail listener:

PHP

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class SendWelcomeEmail implements ShouldQueue
{ use InteractsWithQueue; public $delay = 600; // Delay in seconds (600 = 10 minutes) public function handle(UserRegistered $event) { Mail::to($event->user->email)->send(new WelcomeMail($event->user)); }
}

The $delay property automatically instructs Laravel to delay the job execution.

Step 3: Dispatch the Event

PHP

event(new UserRegistered($user));

Step 4: Configure the Queue (Optional but Recommended)

In .env:

env

QUEUE_CONNECTION=redis

Start the queue worker:

bash

php artisan queue:work

What Is Event Sourcing in Laravel?

Event sourcing is a pattern where every change in the application’s state is stored as a sequence of events. Instead of saving just the current state in the database, you store each action, then rebuild the current state by replaying those events.

Laravel doesn’t include event sourcing out of the box, but you can use packages like Spatie’s Laravel Event Sourcing.

Example: Event Sourcing with Spatie

Step 1: Install the Package

bash

composer require spatie/laravel-event-sourcing

Step 2: Create an Event

bash

php artisan make:storable-event ProductAddedToInventory

This event can store data like:

PHP

class ProductAddedToInventory extends StoredEvent
{ public function __construct(public string $productId, public int $quantity) {}
}

Step 3: Projector to Handle Event Logic

bash

php artisan make:projector InventoryProjector

PHP

class InventoryProjector extends Projector
{ public function onProductAddedToInventory(ProductAddedToInventory $event) { $product = Product::find($event->productId); $product->increment('quantity', $event->quantity); }
}

Step 4: Dispatch the Event

event(new ProductAddedToInventory($productId, 50));

This event is stored, and replayed later if needed to rebuild the product inventory state.

Use Cases for Delayed Events and Event Sourcing

Use CaseApproach
Send follow-up emails laterDelayed Event
Schedule background jobsDelayed Listener
Audit logs of changesEvent Sourcing
Rebuild historical stateEvent Sourcing
Third-party API sync delayQueued + Delayed

Best Practices

  • Use Redis or SQS for production-ready queues.
  • Always monitor delayed jobs using Laravel Horizon.
  • In event sourcing, ensure events are immutable and well-versioned.

Separate event storage from your core business logic for scalability.