Implementing Event-Driven Patterns
1. Event Notification Pattern
| Aspect | Detail |
| Payload | Minimal — just IDs ("OrderPlaced: orderId=123") |
| Consumer Action | Calls back to source for full data if needed |
| Pros | Tiny events; producer keeps single source of truth |
| Cons | Adds sync coupling on lookup; producer must stay available |
Example: Notification Event
{ "type": "OrderPlaced", "orderId": "ord_123", "occurredAt": "2026-05-15T10:00:00Z" }
2. Event-Carried State Transfer Pattern
| Aspect | Detail |
| Payload | Full state needed by consumers |
| Consumer Action | Stores local replica; queries locally |
| Pros | Consumer autonomous, fast reads, resilient |
| Cons | Larger events; replicated data; eventual consistency |
3. Event Sourcing Pattern
| Concept | Detail |
| Source of Truth | Append-only stream of events; not current state |
| Current State | Derived by replaying events (or from snapshot) |
| Pros | Full audit, time travel, projections, retro-active queries |
| Cons | Schema evolution complex; learning curve; query needs CQRS |
| Tools | EventStoreDB, Axon, Kafka + KSQL, AWS DynamoDB Streams |
Example: Rebuild State from Events
public class Account {
private BigDecimal balance = BigDecimal.ZERO;
public static Account replay(List<Event> events) {
Account a = new Account();
events.forEach(a::apply);
return a;
}
private void apply(Event e) {
switch (e) {
case Deposited d -> balance = balance.add(d.amount());
case Withdrawn w -> balance = balance.subtract(w.amount());
default -> {}
}
}
}
4. Event Collaboration Pattern
| Aspect | Detail |
| Definition | Services collaborate by reacting to each other's events (choreography) |
| No Central Brain | Workflow emerges from event reactions |
| Pros | Loose coupling, easy to add new reactors |
| Cons | Hard to see end-to-end flow; needs strong tracing |
5. Event Store Pattern
| Property | Detail |
| Append-Only | Events never updated/deleted |
| Stream-Based | One stream per aggregate (e.g., order-123) |
| Optimistic Concurrency | Append with expected version; fail on mismatch |
| Subscriptions | Projections subscribe to streams to build read models |
6. Event Replay Pattern
| Use Case | How |
| Rebuild Read Model | Reset projection offset, replay all events |
| Bug Fix | Fix projection logic, replay to correct stored view |
| New Consumer | Bootstrap historical state from beginning of log |
| Forensics | Reconstruct state at any past time |
7. Event Ordering Pattern
| Strategy | Mechanism |
| Per-Partition (Kafka) | Same key → same partition → ordered |
| Single Consumer | One consumer per partition preserves order |
| Sequence Numbers | Producer stamps monotonic seq; consumer reorders/buffers |
| Vector Clocks | For causal ordering across multiple producers |
Warning: Global ordering across all events kills throughput. Choose the smallest ordering scope your business needs (usually per-aggregate).
8. Event Deduplication Pattern
| Strategy | Detail |
| Idempotency Key | Producer assigns unique event ID; consumer stores processed IDs |
| Inbox Table | Consumer records each event ID atomically with side-effect |
| Bloom Filter | Probabilistic dedupe for high volume |
| TTL | Expire seen IDs after time window safe to forget |
9. Event Streaming Pattern
| Aspect | Detail |
| Stream | Continuous, unbounded sequence of events |
| Processing | Stateful operators (map, filter, join, window) |
| Tools | Kafka Streams, Apache Flink, ksqlDB, Spark Structured Streaming |
| Use Cases | Real-time analytics, fraud detection, ETL pipelines |
Example: Kafka Streams Aggregation
StreamsBuilder b = new StreamsBuilder();
b.stream("orders.placed", Consumed.with(Serdes.String(), orderSerde))
.groupBy((k, v) -> v.customerId())
.windowedBy(TimeWindows.ofSizeWithNoGrace(Duration.ofMinutes(5)))
.count()
.toStream()
.to("orders.per-customer-5min");
10. Event-Driven State Machine Pattern
| Element | Detail |
| States | Enumerated lifecycle (DRAFT, PLACED, PAID, SHIPPED, DELIVERED, CANCELLED) |
| Transitions | Triggered by events; rejected if illegal |
| Persistence | State + version stored; or derived from event log |
| Tools | Spring Statemachine, Akka FSM, AWS Step Functions |