KyoMel / Yogyakarta, Indonesia

Term 016016 / 344

Event Sourcing

Month
July 2025
Day
16
Source
2025/july-term.md

Definition:

Event Sourcing is a software architectural pattern where state changes are stored as a sequence of events, rather than just the current state. Each event represents a change in the system, allowing for complete reconstruction of the application’s state at any point in time.

Key Concepts:

  • Events - Immutable, timestamped records of state changes

  • Event Store - Database for persisting events

  • Event Handlers - Process events to update application state

  • CQRS - Often used with Command Query Responsibility Segregation for separating read and write operations

Example:

Bank Account with Event Sourcing

# Event Store
events = [
    {"type": "AccountCreated", "account_id": 1, "timestamp": "2025-07-16T10:00:00"},
    {"type": "Deposit", "account_id": 1, "amount": 100, "timestamp": "2025-07-16T10:01:00"},
    {"type": "Withdrawal", "account_id": 1, "amount": 50, "timestamp": "2025-07-16T10:02:00"}
]

# Event Handler
def apply_events(events):
    balance = 0
    for event in events:
        if event["type"] == "Deposit":
            balance += event["amount"]
        elif event["type"] == "Withdrawal":
            balance -= event["amount"]
    return balance

# Get current balance
current_balance = apply_events(events)
print(f"Current Balance: ${current_balance}")  # Output: $50

Keep reading