Day 30: Command Sourcing Pattern
Day 30: Command Sourcing Pattern
Welcome back, engineers! Today, we're diving deeper into the ledger system's core, building upon our understanding of Event Sourcing. If Day 29 was about recording the facts of what happened, today is about meticulously capturing the intentions of what was requested. We're talking about the Command Sourcing pattern β a powerful, yet often misunderstood, sibling to Event Sourcing, especially critical in high-stakes environments like banking.
The Engineering of Global Ledger Systems: High-Throughput Financial Core Architectures in Java
Module: Section 4: Persistence and Command Sourcing
Week: Section 4: Persistence and Command Sourcing
Lesson Title: Day 30: Command Sourcing Pattern (Day 30)
Why Command Sourcing? Beyond Just Events.
In our last session, we established Event Sourcing as the bedrock for financial truth, where every state change is an immutable event. This is fantastic for auditing and rebuilding state. But what if the "truth" you need to capture isn't just what happened, but also what was attempted?
Imagine a banking system processing 100 million requests per second. A user attempts to transfer funds. This intention, this TransferFundsCommand, hits your system. Before any business logic runs, before any validation, before any funds are moved, what if you could record that exact intention?
This is where Command Sourcing shines. It's the practice of persisting the incoming Command itself as a first-class citizen, before it's processed by your business logic.
Why is this critical for banking and ledger systems?
Unbreakable Audit Trail of Intent: Regulatory bodies don't just want to know what transactions succeeded; they also want to know every attempt, even failed ones. If a system rejected a transfer due to insufficient funds, the
TransferFundsCommanditself, along with its rejection reason, becomes part of the immutable historical record. This is a goldmine for compliance, fraud detection, and dispute resolution.Robustness in Distributed Systems: In ultra-high-scale, distributed environments, network partitions, service failures, or transient errors are a reality. If a
Commandis persisted before processing, you gain idempotent retry capabilities without external queue mechanisms. A system can pick up a previously persisted command that failed to process and try again, guaranteeing "at-least-once" command execution semantics, which can be critical for financial operations.Decoupling Command Receipt from Processing: This pattern allows you to separate the act of receiving a command from the act of processing it. The
Command Gatewaycan quickly persist the command and acknowledge receipt, offloading the potentially long-running or complex processing to an asynchronousCommand Handler. This significantly improves system throughput and responsiveness under heavy load.Replaying Intentions: Just as Event Sourcing allows replaying events to rebuild state, Command Sourcing allows replaying commands to simulate scenarios, test new business logic against historical inputs, or even recover from data corruption by reprocessing all commands.
How it Fits: Command Sourcing in the Overall System
Think of Command Sourcing as a powerful pre-processor layer to your Event Sourced system.
Command Gateway: The entry point for all commands. It receives a command (e.g.,
DepositCommand,WithdrawCommand).Command Store: A highly available, append-only store where commands are immediately persisted. This is the heart of Command Sourcing.
Command Bus: Dispatches the persisted commands to appropriate handlers.
Command Handler: Contains the business logic for a specific command. It loads the aggregate, applies the command, performs validation, and if successful, emits one or more
Events.Event Store: Where the events (facts) generated by the command handler are persisted (as per Event Sourcing).
Aggregate: The consistency boundary that encapsulates business logic and state, evolving through events.
Data Flow:
Keep going with the full lesson
The rest of this lesson β plus its implementation guide, runnable scripts and source code β is included with the course or an All Access plan. Create a free account to start the free lessons in every course.
Already a member? Sign in