Event
Event-driven architecture reference — event sourcing, pub/sub, CQRS, event buses, and stream processing. Use when designing event systems, implementing pub/s...
Documentation & Preview
#!/usr/bin/env bash
event — Event-Driven Architecture Reference
Powered by BytesAgain | bytesagain.com | hello@bytesagain.com
set -euo pipefail
VERSION="1.0.0"
cmd_intro() { cat << 'EOF' === Event-Driven Architecture Overview ===
Event-Driven Architecture (EDA) is a software design pattern where the flow of the program is determined by events — significant changes in state that the system needs to know about.
Core Concepts: Event An immutable record of something that happened "OrderPlaced", "UserRegistered", "PaymentReceived" Producer Component that creates/emits events Consumer Component that receives and processes events Broker Middleware that routes events (Kafka, RabbitMQ) Topic Named channel for a category of events Subscription Binding between consumer and topic
Event vs Command vs Query: Event Past tense, fact, immutable "OrderWasPlaced" Command Imperative, request, may fail "PlaceOrder" Query Request for data, no mutation "GetOrderStatus"
Benefits: ✓ Loose coupling (producers don't know consumers) ✓ Scalability (consumers scale independently) ✓ Extensibility (add consumers without changing producers) ✓ Resilience (async processing, buffering) ✓ Audit trail (events are a log of everything) ✓ Real-time reactions
Tradeoffs: ✗ Eventual consistency (not immediately consistent) ✗ Debugging complexity (distributed, async) ✗ Event ordering challenges ✗ Schema evolution management ✗ Idempotency requirements ✗ Monitoring and observability overhead
When to Use EDA: ✓ Multiple systems need to react to the same action ✓ Real-time processing requirements ✓ Need to decouple microservices ✓ Audit trail / compliance requirements ✓ Systems that process at different speeds
When NOT to Use: ✗ Simple CRUD applications ✗ Strong consistency required (banking transactions) ✗ Synchronous response needed ✗ Small team / simple architecture EOF }
cmd_patterns() { cat << 'EOF' === Event-Driven Patterns ===
- Publish/Subscribe (Pub/Sub):
Producer → Topic → [Consumer A, Consumer B, Consumer C]
- One-to-many communication
- Consumers subscribe to topics of interest
- New consumers can be added without changing producers
- Use: notifications, fan-out, broadcasting
- Event Sourcing:
Store all state changes as a sequence of events Current state = replay all events from the beginning Events: OrderCreated → ItemAdded → ItemAdded → OrderSubmitted State: {items: [A, B], status: "submitted"} Use: audit trails, temporal queries, undo capability
- CQRS (Command Query Responsibility Segregation):
Separate models for reading and writing: Write Side: Command → Event → Event Store Read Side: Event → Projection → Read Database Use: different optimization for reads vs writes
- Saga Pattern:
Coordinate distributed transactions across services:
Choreography (event-based): OrderService → OrderCreated → PaymentService PaymentService → PaymentProcessed → InventoryService InventoryService → ItemReserved → ShippingService (Each service reacts to events and emits events)
Orchestration (command-based): SagaOrchestrator sends commands to each service Manages compensating transactions on failure More control, single point of coordination
- Event Notification:
Light events that signal something happened Consumer queries source for full data if needed {type: "OrderPlaced", orderId: "123"} Use: when event payload would be too large
- Event-Carried State Transfer:
Events contain all data consumer needs No need to call back to source {type: "OrderPlaced", order: {id, items, total, customer}} Use: when consumer needs autonomy, offline capability
- Dead Letter Queue (DLQ):
Failed events → DLQ for manual review Prevents poison messages from blocking processing Alert operators when DLQ grows EOF }
cmd_design() { cat << 'EOF' === Event Design ===
Event Naming: Format: <Entity><PastTenseVerb> ✓ OrderPlaced, PaymentReceived, UserRegistered ✓ InventoryAdjusted, ShipmentDelivered ✗ PlaceOrder (command, not event) ✗ OrderEvent (too vague) ✗ order_data (not descriptive)
Event Schema: { "eventId": "evt_abc123", // Unique ID (UUID) "eventType": "OrderPlaced", // Event type name "version": 1, // Schema version "timestamp": "2024-01-15T10:30:45.123Z", // ISO 8601 "source": "order-service", // Producing service "correlationId": "req_xyz789", // Request correlation "causationId": "evt_prev456", // What caused this event "data": { // Event-specific payload "orderId": "ord_123", "customerId": "cust_456", "items": [...], "totalAmount": 99.99, "currency": "USD" }, "metadata": { // Non-business data "userId": "usr_789", "traceId": "trace_abc", "environment": "production" } }
Schema Evolution Strategies:
- Backward Compatible (safest):
- Add new optional fields
- Never remove fields
- Never change field types
- Old consumers handle new events fine
- Schema Registry:
- Central registry of event schemas (Confluent, Apicurio)
- Validate events against schema before publishing
- Enforce compatibility rules
- Track schema versions
- Versioned Events:
- OrderPlaced.v1, OrderPlaced.v2
- Consumers subscribe to specific version
- Upcasting: transform old events to new format
- Downcasting: transform new events for old consumers
- Consumer-Driven Contracts:
- Consumers declare what fields they need
- Producer ensures those fields are present
- Extra fields ignored by consumers
Event Size Guidelines: Small (< 1 KB) Notifications, signals Medium (1-10 KB) Entity state changes (most events) Large (10-100 KB) Batch operations, full document Too large (> 1 MB) Use claim check pattern (store data externally)
Claim Check Pattern: For large payloads:
- Store large data in blob storage (S3, GCS)
- Event contains reference: {dataUrl: "s3://bucket/key"}
- Consumer fetches data from storage
- Keeps events small and broker efficient
EOF }
cmd_brokers() { cat << 'EOF' === Event Broker Comparison ===
Apache Kafka: Type: Distributed log / streaming platform Delivery: At-least-once (exactly-once with transactions) Ordering: Per-partition ordering guaranteed Retention: Configurable (time or size based, or forever) Throughput: Very high (millions msg/sec per cluster) Consumer model: Pull-based, consumer groups Use: High-throughput streaming, event sourcing, log aggregation Ecosystem: Kafka Streams, ksqlDB, Kafka Connect
RabbitMQ: Type: Message broker (AMQP) Delivery: At-least-once (with confirms) Ordering: Per-queue FIFO Retention: Messages deleted after consumption Throughput: Moderate (tens of thousands msg/sec) Consumer model: Push-based, acknowledgments Patterns: Direct, topic, fanout, headers exchanges Use: Task queues, RPC, traditional messaging
NATS: Type: Cloud-native messaging Delivery: At-most-once (Core), at-least-once (JetStream) Ordering: Per-subject (JetStream) Throughput: Very high, low latency Footprint: Lightweight (single binary, <20MB) Use: Microservices, IoT, edge computing Modes: Pub/Sub, Request/Reply, Queue Groups
Apache Pulsar: Type: Distributed messaging + streaming Delivery: At-least-once, effectively-once Ordering: Per-partition Retention: Tiered storage (hot + cold) Features: Multi-tenancy, geo-replication native Use: Multi-tenant platforms, hybrid cloud
AWS Services: SQS Queue service, at-least-once, auto-scaling SNS Pub/sub notifications, push to SQS/Lambda/HTTP EventBridge Event bus, rules-based routing, schema registry Kinesis Real-time streaming (Kafka-like) MSK Managed Kafka
Choosing a Broker: Need replay? → Kafka, Pulsar Simple queuing? → RabbitMQ, SQS Lowest latency? → NATS Serverless? → EventBridge, SQS + Lambda Multi-tenant? → Pulsar Already on AWS? → SQS + SNS or EventBridge Team expertise? → Use what you know EOF }
cmd_sourcing() { cat << 'EOF' === Event Sourcing Deep Dive ===
Core Idea: Instead of storing current state, store all state changes (events). Current state is derived by replaying events.
Traditional: UPDATE orders SET status='shipped' WHERE id=123 Event Sourcing: APPEND {type: "OrderShipped", orderId: 123, timestamp: ...}
Event Store: Append-only log of events, organized by aggregate/stream.
Stream: order-123 Event 1: OrderCreated {customer: "Alice", items: [...]} Event 2: ItemAdded {item: "Widget", qty: 2} Event 3: PaymentReceived {amount: 49.98} Event 4: OrderShipped {carrier: "FedEx", tracking: "..."}
Operations: Append(streamId, events, expectedVersion) // Optimistic concurrency Read(streamId) → [events] // Load all events Read(streamId, fromVersion) → [events] // Load from version N
Event Store Implementations: EventStoreDB Purpose-built, Greg Young's creation PostgreSQL Using events table + optimistic locking DynamoDB Event table with stream ID + version Kafka Topic per aggregate type Marten .NET library on PostgreSQL
Projections (Read Models): Events → Projection → Read Database
Example Projection: OrderCreated → INSERT INTO order_summary (id, customer, total) ItemAdded → UPDATE order_summary SET total = total + amount OrderShipped → UPDATE order_summary SET status = 'shipped'
Types: Live: Process events in real-time Catch-up: Replay from beginning, then switch to live One-time: Build report by replaying specific events
Snapshots: Problem: Replaying 10,000 events is slow Solution: Periodically save aggregate state as a snapshot Snapshot at event 9000: {state: {...}} Load snapshot, then replay events 9001-10000 Only for performance — events remain source of truth
When to snapshot: Every N events (e.g., every 100) When load time exceeds threshold On aggregate version milestones
Temporal Queries: "What was the order state at 3pm yesterday?" → Replay events up to that timestamp Impossible with traditional CRUD (state was overwritten)
Pitfalls: ✗ Schema evolution is harder (old events live forever) ✗ Event store can grow very large ✗ Debugging requires understanding event sequence ✗ Not suitable for all domains (simple CRUD overkill) ✗ Projections can fall behind or break ✗ Eventual consistency between write and read models EOF }
cmd_delivery() { cat << 'EOF' === Delivery Guarantees ===
At-Most-Once: Fire and forget. Message may be lost, never duplicated. Implementation: Send without waiting for acknowledgment. Use: Metrics, telemetry, non-critical notifications Trade-off: Fastest, but unreliable
At-Least-Once: Message is delivered one or more times. May be duplicated. Implementation: Retry until acknowledged, consumer deduplicates. Use: Most business events (with idempotent consumers) Trade-off: Reliable but requires idempotency handling
Exactly-Once: Message is delivered exactly one time. No loss, no duplicates. Implementation: Extremely difficult in distributed systems. Reality: Usually "effectively once" via idempotency + dedup.
Achieving Effectively-Once:
Idempotent Consumers: Processing the same event multiple times produces same result. Techniques:
- Deduplicate by event ID (track processed IDs)
- Use database upserts (INSERT ON CONFLICT)
- Design operations to be naturally idempotent
Transactional Outbox Pattern: Problem: How to atomically update DB AND publish event? Solution:
- Write