Anonim / 5 days, 5 hours ago | Download | Plain text | Reply |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | # Event Sourcing and CQRS: When to Mention Them in an Interview ## Event sourcing Instead of storing the current state of an entity, store the sequence of **events** that happened to it: `AccountOpened`, `MoneyDeposited(50)`, `MoneyWithdrawn(20)`. Current state is computed by replaying events (balance = 30). Benefits: - A complete audit log for free, which is why finance and ledger systems like it. - You can rebuild state as of any point in time, or build new views from old history. - Debugging: you can see exactly how an entity got into a bad state. Costs: - Replaying long histories is slow, so you store periodic **snapshots** and replay only newer events. - Changing event formats needs versioning ("upcasting" old events). - Queries like "all accounts with balance over 1000" need a separate read model. ## CQRS Command Query Responsibility Segregation separates the **write model** (validates commands and appends events) from one or more **read models** (projections optimized for queries: a SQL table for lists, a search index for text search, a cache for dashboards). Projections are updated asynchronously from the event stream. The read side is eventually consistent, so the UI must tolerate a short delay after a write (or read its own writes from the write side). ## When to bring it up Good fits: ledgers, order lifecycles, collaborative tools with history, systems that need audit trails. Poor fits: simple CRUD apps, where it adds a lot of complexity for little gain. Saying that out loud shows judgment. Disclosure: I'm part of the team at Linkjob AI (https://www.linkjob.ai/), an AI interview copilot. |