ℹ️ disclaimer
This article represented my mental model at the time of writing, but I’m always iterating on it.
Commands
A command is an intention to do, not something that has already happened in the system. Commands provide minimal but enough information for decision-making and can be sent synchronously or asynchronously.
Similar to events, commands should adhere to common principles and a structured format:
- use a verb-like syntax for their naming
- should include criteria, such as
command_id
for distinguishing them for idempotence - should include criteria, such as
command_type
for applying actions based on different types (on consumer side forcommandBus
implementation) - may include criteria, such as
expected_<entity>_version
for applying actions based on CAS (compare-and-swap) operations - may include criteria, such as
trace_id
for enabling tracing capabilities - …
As example, WithdrawUserWalletCommand
:
{
"command_id": "feabe53f-0444-4448-9dde-2f7d5e115da2",
"command_type": "withdraw_wallet",
"user_id": "1",
"wallet_id": "abc",
"amount": "100 USD",
"expected_wallet_version": 7,
"created_at": 1695309057,
...
}
Synchronous executions of commands
Commands can be executed in synchronous way using such ways like:
- Rest
- gRPC
- …
It’s common for commands to return the result of their execution, such as the last entity state or some outcome.
Asynchronous executions of commands
Commands also can be executed in asynchronous way via messages buses or brokers:
- RabbitMQ
- kafka
- …
In this scenario, the consumer may emit an event as a result of the command execution, indicating whether it was successful or not.
Conclusion
Commands play a pivotal role in Event-Driven Architecture (EDA), serving as the triggers for actions within a distributed system. They offer a means of conveying intent, maintaining system reliability through idempotency, and fostering decoupling between components. As the backbone of EDA, understanding the significance and best practices of commands is essential for building resilient and responsive event-driven systems