Back to Blog
Event SourcingArchitectureDatabaseCQRS

Event Sourcing Fundamentals

Understand event sourcing. From event stores to projections to rebuilding state from events.

B
Bootspring Team
Engineering
September 5, 2022
6 min read

Event sourcing stores state as a sequence of events rather than current values. Here's how to implement it effectively.

What is Event Sourcing

Traditional state storage: - Store current state - Update in place - History lost Event sourcing: - Store events that caused state changes - Derive current state from events - Complete history preserved Example: Instead of: { balance: 150 } Store events: 1. AccountOpened { balance: 100 } 2. MoneyDeposited { amount: 100 } 3. MoneyWithdrawn { amount: 50 } Current state derived: { balance: 150 }

Event Definition

Loading code block...

Event Store

Loading code block...

Aggregate

Loading code block...

Repository

Loading code block...

Projections

Loading code block...

Snapshots

Loading code block...

Best Practices

Events: ✓ Make events immutable ✓ Include all necessary data ✓ Use past tense naming ✓ Version event schemas Storage: ✓ Use append-only storage ✓ Implement optimistic concurrency ✓ Create snapshots for performance ✓ Archive old events Projections: ✓ Build for specific queries ✓ Handle idempotency ✓ Enable rebuilding ✓ Monitor projection lag

Conclusion

Event sourcing provides complete audit history and enables temporal queries. Start with clear event definitions, implement proper aggregate handling, and build projections for read performance. The pattern works well for domains where history and audit trails are important.

Share this article

Help spread the word about Bootspring

Related articles