Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is event sourcing in system design?
Event sourcing is a design pattern where all changes to application state are stored as a sequence of events. Instead of saving only the current state, the system saves every change as an event, allowing the state to be rebuilt by replaying these events.
Click to reveal answer
beginner
How does event sourcing differ from traditional state storage?
Traditional storage saves only the current state, overwriting previous data. Event sourcing saves every change as an event, preserving the full history and allowing state reconstruction at any point in time.
Click to reveal answer
intermediate
What are the main benefits of using event sourcing?
Benefits include: full audit trail of changes, easier debugging by replaying events, ability to rebuild state after failure, and support for complex business logic with event replay.
Click to reveal answer
intermediate
What is an event store in event sourcing?
An event store is a specialized database that stores all events in order. It acts like a journal or log where each event is appended and never deleted, ensuring the history is complete and immutable.
Click to reveal answer
intermediate
How do you rebuild the current state in an event sourced system?
You rebuild the current state by replaying all stored events in order, applying each event to an initial empty state until you reach the latest state.
Click to reveal answer
What does event sourcing primarily store?
AOnly the latest state snapshot
BDatabase schema changes
CUser session data
DA sequence of all state changes as events
✗ Incorrect
Event sourcing stores every change as an event, not just the latest state.
Which of the following is a key advantage of event sourcing?
ANo need for backups
BFaster writes than traditional databases
CAbility to replay events to rebuild state
DSimpler database schema
✗ Incorrect
Replaying events allows rebuilding the state and auditing changes.
What is an event store?
AA database that stores events in order
BA cache for user data
CA tool for monitoring system health
DA backup storage system
✗ Incorrect
An event store is a database designed to store events sequentially and immutably.
How is the current state obtained in event sourcing?
ABy replaying all events from the start
BBy reading the last event only
CBy querying a snapshot only
DBy using a cache
✗ Incorrect
The current state is rebuilt by applying all events in order.
Which scenario best suits event sourcing?
ASimple static websites
BSystems needing full audit trails
CTemporary session storage
DSingle-use scripts
✗ Incorrect
Event sourcing is ideal for systems requiring detailed history and auditability.
Explain event sourcing and how it helps in rebuilding application state.
Think about how a bank keeps track of every transaction to know the current balance.
You got /3 concepts.
Describe the role and characteristics of an event store in event sourcing.
Imagine a diary where you only add new entries and never erase old ones.
You got /3 concepts.
Practice
(1/5)
1. What is the main idea behind event sourcing in system design?
easy
A. Store all changes as a sequence of events to reconstruct state
B. Store only the latest snapshot of data for quick access
C. Use events only for logging errors in the system
D. Send events to users as notifications without storing them
Solution
Step 1: Understand event sourcing concept
Event sourcing means saving every change as an event, not just the final data.
Step 2: Identify how state is managed
The current state is rebuilt by applying all stored events in order, not by snapshots alone.
Final Answer:
Store all changes as a sequence of events to reconstruct state -> Option A
Quick Check:
Event sourcing = store events to rebuild state [OK]
Hint: Event sourcing saves changes as events, not just snapshots [OK]
Common Mistakes:
Confusing event sourcing with snapshot-only storage
Thinking events are only for error logs
Believing events are just notifications
2. Which of the following is the correct way to represent an event in an event sourcing system?
Events should be structured data with type, timestamp, and data fields for clarity and processing.
Step 2: Compare options
{ "eventType": "UserCreated", "timestamp": "2024-06-01T12:00:00Z", "data": { "userId": 123 } } uses a clear JSON object with eventType, timestamp, and data, which is standard practice.
Hint: Events are structured objects with type, timestamp, and data [OK]
Common Mistakes:
Using unstructured strings for events
Confusing event data with SQL commands
Using arrays without keys for event details
3. Given these events in order: [{"eventType":"AddItem","data":{"itemId":1}}, {"eventType":"AddItem","data":{"itemId":2}}, {"eventType":"RemoveItem","data":{"itemId":1}}] What is the final state of the item list?
After all events, only item 2 remains in the list.
Final Answer:
[2] -> Option B
Quick Check:
Apply events sequentially = final list [2] [OK]
Hint: Apply events one by one to get final state [OK]
Common Mistakes:
Ignoring remove event
Applying events out of order
Assuming all added items remain
4. You notice that replaying all events to rebuild state is very slow. What is a common solution to improve performance in event sourcing?
medium
A. Store only the latest event for each entity
B. Delete old events after 1 day to reduce size
C. Use snapshots to save intermediate states periodically
D. Switch to storing only current state, no events
Solution
Step 1: Identify performance issue cause
Replaying all events from the start can be slow as event count grows.
Step 2: Choose common optimization
Snapshots save the full state at points in time, so replay starts from snapshot, reducing replay time.
Final Answer:
Use snapshots to save intermediate states periodically -> Option C
Quick Check:
Snapshots speed up event replay [OK]
Hint: Use snapshots to avoid replaying all events every time [OK]
Common Mistakes:
Deleting events breaks history and audit
Keeping only latest event loses full history
Abandoning events loses event sourcing benefits
5. You design an event sourcing system for a bank. Which approach best ensures data consistency and auditability when multiple transactions happen concurrently?
hard
A. Process events in random order to improve throughput
B. Allow events to overwrite each other without checks for speed
C. Store only final balances without event history to simplify design
D. Use optimistic concurrency control with event versioning and conflict detection
Solution
Step 1: Understand concurrency challenges in event sourcing
Concurrent transactions can cause conflicts if events overwrite each other or are applied out of order.
Step 2: Choose method to maintain consistency and audit
Optimistic concurrency control uses event version numbers to detect conflicts and prevent overwrites, preserving history and correctness.
Final Answer:
Use optimistic concurrency control with event versioning and conflict detection -> Option D