What is the Commanded library and event sourcing in Elixir?
Answer
Commanded is an Elixir framework for building CQRS (Command Query Responsibility Segregation) and event-sourced applications. In event sourcing, state is derived by replaying an immutable sequence of events. Core concepts: Aggregate: domain object holding state, handling commands, emitting events. Command: intent to change state — defstruct [:account_id, :amount]. Event: fact that occurred — defstruct [:account_id, :amount]. Aggregate implementation: def execute(%Account{balance: balance}, %Withdraw{amount: amount}) when balance >= amount, do: %MoneyWithdrawn{amount: amount}; def apply(%Account{} = account, %MoneyWithdrawn{amount: amount}), do: %{account | balance: account.balance - amount}. Projections: Ecto-based read models updated by listening to events. Event handlers: react to events (send notifications, update caches). EventStore: persistent event log (PostgreSQL). Commanded with EventStore provides a complete event sourcing stack that leverages Elixir's immutability and concurrency strengths.
More Elixir Questions
View all →- Advanced What is the Elixir macro system and how does metaprogramming work?
- Advanced What is Elixir's GenServer deep internals and message queue management?
- Advanced What is Broadway in Elixir and how does it handle data pipelines?
- Advanced What is GenStage in Elixir?
- Advanced How does Elixir handle cluster formation and distributed state?