Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define the main storage in event sourcing.
Microservices
events = [] # This list stores all [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing events with snapshots or current states.
Using logs as a generic term instead of events.
✗ Incorrect
In event sourcing, all changes are stored as events in an append-only log.
2fill in blank
mediumComplete the code to rebuild the current state from events.
Microservices
state = initial_state for event in events: state = [1](state, event)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using functions that do not modify the state.
Confusing event application with event storage.
✗ Incorrect
The apply_event function updates the state by applying each event in order.
3fill in blank
hardFix the error in the event storage function to append new events.
Microservices
def store_event(event): [1].append(event)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Appending events to the wrong variable.
Using a variable that does not exist.
✗ Incorrect
New events must be appended to the events list to keep the event history.
4fill in blank
hardFill both blanks to create a snapshot after every 100 events.
Microservices
if len(events) [1] 100 == 0: snapshot = [2](state)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators.
Confusing snapshot creation with event storage.
✗ Incorrect
The modulo operator % checks if 100 events have occurred, then create_snapshot saves the current state.
5fill in blank
hardFill both blanks to implement event replay with snapshot optimization.
Microservices
def rebuild_state(): state = [1] for event in events[[2]:]: state = apply_event(state, event) # Apply remaining events return state # Return the latest state
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting replay from the beginning every time.
Ignoring snapshots and replaying all events.
✗ Incorrect
Start from the latest snapshot, replay events after the snapshot index len(snapshot_events).