Event Sourcing for Context
Event sourcing stores all context changes as immutable events. Current state is derived by replaying events. This provides complete history, audit capability, and temporal queries.
Event Format
Context Event Structure
{
"event_id": "uuid-v4",
"event_type": "context.preference.updated",
"aggregate_id": "user-123",
"aggregate_type": "user-context",
"timestamp": "2024-01-15T10:30:00Z",
"sequence": 42,
"payload": {
"preference": "theme",
"old_value": "light",
"new_value": "dark"
},
"metadata": {
"actor": "user-123",
"source": "web-app",
"correlation_id": "req-abc"
}
}
Event Types
Define granular event types for each context change. More specific events enable richer projections. Balance granularity with complexity.
Event Store Implementation
Storage Requirements
Event stores have specific needs: append-only writes, ordered reads per aggregate, optimistic concurrency, efficient stream reading.
ECM Protocol Binding
Map ECM operations to event store operations:
- CREATE → Initial event(s)
- UPDATE → Change event(s)
- DELETE → Deleted event
- READ → Replay events to build state
Projections
Building Projections
Projections transform event streams into read models. Multiple projections from same events. Optimized for specific query patterns. Rebuilt from events if needed.
Projection Types
Current state projection for standard context read. Historical projections for point-in-time queries. Aggregate projections for analytics. Cross-aggregate projections for relationships.
Snapshotting
Snapshot Strategy
For long event streams, snapshots optimize replay. Snapshot every N events. Snapshot on significant state changes. Load snapshot, then replay subsequent events.
Snapshot Management
Balance snapshot frequency with storage cost. Retain snapshots for recovery period. Garbage collect old snapshots.
Protocol Extensions
ECM extensions for event sourcing:
- x-ecm-event-id: Originating event reference
- x-ecm-as-of: Temporal query timestamp
- x-ecm-version: Aggregate version number
- x-ecm-causation: Causal event chain
Conclusion
Event sourcing provides powerful capabilities for context management: complete history, audit trails, temporal queries, and debugging. ECM Protocol extensions enable interoperable event-sourced context systems.