Mahmoud Al-HabashOpen to new projects
  • WorkWork
  • ServicesServices
  • LabLab
  • WritingWriting
  • ContactContact
Amman--:--عربي
Writing/DevOps & cloud

Event-Driven Architecture: Building Loosely Coupled Systems

Design event-driven systems with message queues, event sourcing, and CQRS — patterns used by Netflix and LinkedIn at massive scale.

22 February 20261 min readDevOps & cloudاقرأ بالعربية

On this page

  1. Core Concepts
  2. Laravel Events Implementation
  3. CQRS Pattern
  4. Benefits

Event-driven architecture decouples services by communicating through events. When a user places an order, the Order service emits "OrderPlaced", and Inventory, Payment, and Notification services react independently. LinkedIn processes 7 trillion events per day.

Core Concepts

Event: A fact that something happened ("OrderPlaced", "UserRegistered")
Producer: Service that emits events
Consumer: Service that reacts to events
Broker: Middleware that routes events (RabbitMQ, Kafka, SQS)

Laravel Events Implementation

php
class OrderPlaced
{
    public function __construct(
        public readonly Order $order,
        public readonly User $user,
    ) {}
}

class SendOrderConfirmation
{
    public function handle(OrderPlaced $event): void
    {
        Mail::to($event->user)->queue(new OrderConfirmationMail($event->order));
    }
}

class UpdateInventory implements ShouldQueue
{
    public $tries = 3;
    public $backoff = [10, 60, 300];

    public function handle(OrderPlaced $event): void
    {
        foreach ($event->order->items as $item) {
            Product::where('id', $item->product_id)
                ->decrement('stock', $item->quantity);
        }
    }

    public function failed(OrderPlaced $event, Throwable $e): void
    {
        Log::critical('Inventory update failed', [
            'order_id' => $event->order->id,
        ]);
    }
}

// Dispatch
OrderPlaced::dispatch($order, $user);

CQRS Pattern

Command Side (Write):
  → Validates → Updates database → Emits events

Query Side (Read):
  → Reads from optimized, denormalized read models
  → Updated by event listeners

Benefits

  • Loose coupling — services don't know about each other
  • Scalability — consumers scale independently
  • Resilience — failed consumers retry without blocking
  • Audit trail — events provide a natural activity log
All articles

Keep reading

  • Docker for Developers: From Zero to Containerized Applications28 February 20261 min read
  • CI/CD Pipelines with GitHub Actions: Automate Everything22 February 20261 min read
  • Clean Code Principles: Write Code That Humans Can Read22 February 20261 min read