{"id":1068,"date":"2025-05-14T10:47:49","date_gmt":"2025-05-14T10:47:49","guid":{"rendered":"https:\/\/www.cmarix.com\/qanda\/?p=1068"},"modified":"2026-02-05T12:06:26","modified_gmt":"2026-02-05T12:06:26","slug":"laravel-delayed-event-processing-using-queue-system","status":"publish","type":"post","link":"https:\/\/www.cmarix.com\/qanda\/laravel-delayed-event-processing-using-queue-system\/","title":{"rendered":"How To Implement Delayed Event Processing Or Event Sourcing Using Laravel\u2019s Queue System?"},"content":{"rendered":"\n<p>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.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Is Delayed Event Processing in Laravel?<\/h2>\n\n\n\n<p>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.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Implement Delayed Event Processing in Laravel?<\/h2>\n\n\n\n<p><strong>Problem Statement:<\/strong> Sending a welcome email 10 minutes after a user signs up when you have a UserRegistered event.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 1: Create the Event and Listener<\/h4>\n\n\n\n<p>bash<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan make:event UserRegistered\nphp artisan make:listener SendWelcomeEmail --event=UserRegistered<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Step 2: Implement the Listener with Delay<\/h4>\n\n\n\n<p>In your SendWelcomeEmail listener:<\/p>\n\n\n\n<p>PHP<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use Illuminate\\Contracts\\Queue\\ShouldQueue;\nuse Illuminate\\Queue\\InteractsWithQueue;\n\nclass SendWelcomeEmail implements ShouldQueue\n{\n    use InteractsWithQueue;\n\n    public $delay = 600; \/\/ Delay in seconds (600 = 10 minutes)\n\n    public function handle(UserRegistered $event)\n    {\n        Mail::to($event->user->email)->send(new WelcomeMail($event->user));\n    }\n}<\/code><\/pre>\n\n\n\n<p>The $delay property automatically instructs Laravel to delay the job execution.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 3: Dispatch the Event<\/h4>\n\n\n\n<p>PHP<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>event(new UserRegistered($user));<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Step 4: Configure the Queue (Optional but Recommended)<\/h4>\n\n\n\n<p>In .env:<\/p>\n\n\n\n<p>env<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>QUEUE_CONNECTION=redis<\/code><\/pre>\n\n\n\n<p>Start the queue worker:<\/p>\n\n\n\n<p>bash<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan queue:work<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">What Is Event Sourcing in Laravel?<\/h2>\n\n\n\n<p>Event sourcing is a pattern where <strong>every change in the application&#8217;s state is stored as a sequence of events<\/strong>. Instead of saving just the current state in the database, you store each action, then rebuild the current state by replaying those events.<\/p>\n\n\n\n<p>Laravel doesn&#8217;t include event sourcing out of the box, but you can use packages like <strong>Spatie&#8217;s Laravel Event Sourcing<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Event Sourcing with Spatie<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Step 1: Install the Package<\/h4>\n\n\n\n<p>bash<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>composer require spatie\/laravel-event-sourcing<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Step 2: Create an Event<\/h4>\n\n\n\n<p>bash<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan make:storable-event ProductAddedToInventory<\/code><\/pre>\n\n\n\n<p>This event can store data like:<\/p>\n\n\n\n<p>PHP<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ProductAddedToInventory extends StoredEvent\n{\n    public function __construct(public string $productId, public int $quantity) {}\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Step 3: Projector to Handle Event Logic<\/h4>\n\n\n\n<p>bash<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan make:projector InventoryProjector<\/code><\/pre>\n\n\n\n<p>PHP<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class InventoryProjector extends Projector\n{\n    public function onProductAddedToInventory(ProductAddedToInventory $event)\n    {\n        $product = Product::find($event->productId);\n        $product->increment('quantity', $event->quantity);\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Step 4: Dispatch the Event<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>event(new ProductAddedToInventory($productId, 50));<\/code><\/pre>\n\n\n\n<p>This event is stored, and replayed later if needed to rebuild the product inventory state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Use Cases for Delayed Events and Event Sourcing<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Use Case<\/strong><\/td><td><strong>Approach<\/strong><\/td><\/tr><tr><td>Send follow-up emails later<\/td><td>Delayed Event<\/td><\/tr><tr><td>Schedule background jobs<\/td><td>Delayed Listener<\/td><\/tr><tr><td>Audit logs of changes<\/td><td>Event Sourcing<\/td><\/tr><tr><td>Rebuild historical state<\/td><td>Event Sourcing<\/td><\/tr><tr><td>Third-party API sync delay<\/td><td>Queued + Delayed<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practices<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <strong>Redis or SQS<\/strong> for production-ready queues.<\/li>\n\n\n\n<li>Always <strong>monitor<\/strong> delayed jobs using <strong>Laravel Horizon<\/strong>.<\/li>\n\n\n\n<li>In event sourcing, ensure events are <strong>immutable<\/strong> and <strong>well-versioned<\/strong>.<\/li>\n<\/ul>\n\n\n\n<p>Separate <strong>event storage<\/strong> from your core business logic for scalability.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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.&nbsp; 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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1073,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[13,3],"tags":[],"class_list":["post-1068","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","category-web"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1068","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/comments?post=1068"}],"version-history":[{"count":6,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1068\/revisions"}],"predecessor-version":[{"id":1076,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1068\/revisions\/1076"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media\/1073"}],"wp:attachment":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media?parent=1068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/categories?post=1068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/tags?post=1068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}