To understand why we move from mutable rows to an append-only event log, you have to stop thinking about **"what is the current state?"** and start thinking about **"what happened?"** ### The Shift: From "State" to "History" In a traditional inventory database, you have a row: `product_id: 123, quantity: 50`. When a sale happens, you perform an `UPDATE` command: `SET quantity = 40`. **The Problem:** You have overwritten the history. If a warehouse manager asks, "Why is the stock 40?" you can’t answer it from that row. You have to hope you have a separate, fragile audit table. **The Append-Only Solution:** Instead of updating, you append an event: 1. `ProductStockIncreased(id: 123, amount: 50)` 2. `ProductSold(id: 123, amount: 10)` The "current state" (40) is simply the result of replaying or aggregating these events. This is called **Event Sourcing**. **Why it’s better for a Junior Engineer:** 1. **Auditability:** You have a perfect, immutable record of every transaction. 2. **Debugging:** You can "time travel." If a bug caused a stock error, you can replay events in a local environment to see exactly where the state diverged. 3. **Decoupling:** Multiple services can "listen" to the log. The Shipping service and the Accounting service can both react to the `ProductSold` event without you having to write complex triggers in your database. --- ### Three Failure Modes and How to Detect Them When you abandon mutable state, you shift the complexity from "database locking" to "event processing." #### 1. The "Poison Pill" Event An event is appended to the log that is malformed or triggers an unhandled exception in your consumer service (e.g., a schema change that the consumer doesn't expect). Every time the service tries to process this event, it crashes and restarts, creating a loop. * **How to detect:** **Consumer Lag Monitoring.** Monitor the "lag" (the gap between the last event produced and the last event processed). If the lag grows indefinitely and your error logs show a constant stream of exceptions for the same offset, you have a poison pill. #### 2. Eventual Consistency Drift In an event-sourced system, the "read model" (the database that shows the current quantity) is updated asynchronously. If your event processor crashes after appending an event but before updating the read model, the user sees "50" in the app, but the event log says "40." * **How to detect:** **Checksum/Reconciliation Jobs.** Run an asynchronous "heartbeat" process that periodically calculates the state from scratch by replaying the log and compares it against the "current state" table. If the values don't match, fire an alert. #### 3. Duplicate Event Processing (Idempotency Failure) If your service crashes *after* updating the read model but *before* committing the offset to the event log, it will replay the same event upon restart, potentially subtracting inventory twice. * **How to detect:** **Duplicate Sequence Detection.** Every event should have a unique `event_id` or `sequence_number`. Your "current state" table should track the `last_processed_event_id`. If your consumer receives an event with an ID that is less than or equal to the one already stored in your table, you have detected a replay. Log this as a "Non-Idempotent Collision" event.
