A production walkthrough of CQRS and Event Sourcing using NestJS, KurrentDB, PostgreSQL, and Drizzle ORM. Covers the full architecture: commands as intent-bearing verbs, aggregates as state machines with TypeScript exhaustiveness checking, immutable event streams, projections for read models, and snapshots for performance. Also addresses production realities tutorials skip: optimistic concurrency with PostgreSQL advisory locks, event schema versioning via upcasters, idempotency for at-least-once delivery, two flavors of projections (read model vs. async side effects), and correlation IDs for audit trails. Closes with an honest accounting of costs including eventual consistency, cross-aggregate query complexity, and onboarding overhead.

21m read timeFrom spin.atomicobject.com
Post cover image
Table of contents
A Motivating PuzzleThe Production StackThe Thesis, in One ParagraphCommands Describe Intent, not DataThe Aggregate is a State MachineEvents are What Actually Get SavedReconstructing the Present from the PastMaking Events QueryableThe Read Side is Mercifully BoringProduction Details that Tutorials SkipAn honest Accounting of the Costs7 Things Worth Taing AwayA Closing Thought
4.2K Impressions