0
0
Microservicessystem_design~10 mins

Event sourcing pattern in Microservices - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Astates
Blogs
Cevents
Dsnapshots
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing events with snapshots or current states.
Using logs as a generic term instead of events.
2fill in blank
medium

Complete 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'
Aapply_event
Bsave_event
Cdelete_event
Dlog_event
Attempts:
3 left
💡 Hint
Common Mistakes
Using functions that do not modify the state.
Confusing event application with event storage.
3fill in blank
hard

Fix 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'
Astate
Bevents
Csnapshot
Dlog
Attempts:
3 left
💡 Hint
Common Mistakes
Appending events to the wrong variable.
Using a variable that does not exist.
4fill in blank
hard

Fill 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'
A%
B==
C>
Dcreate_snapshot
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators.
Confusing snapshot creation with event storage.
5fill in blank
hard

Fill 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'
Asnapshot
B0
Clen(snapshot_events)
Dinitial_state
Attempts:
3 left
💡 Hint
Common Mistakes
Starting replay from the beginning every time.
Ignoring snapshots and replaying all events.