0
0
Microservicessystem_design~10 mins

Idempotent event consumers 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 ensure the event consumer processes each event only once.

Microservices
def process_event(event):
    if event.id in [1]:
        return "Already processed"
    # process the event
    processed_events.add(event.id)
Drag options to blanks, or click blank then click option'
Aevent_queue
Bevent_log
Cpending_events
Dprocessed_events
Attempts:
3 left
💡 Hint
Common Mistakes
Using a queue or list instead of a set for membership checks.
2fill in blank
medium

Complete the code to atomically check and mark an event as processed to ensure idempotency.

Microservices
def handle_event(event):
    with lock:
        if event.id in [1]:
            return
        [2].add(event.id)
    process(event)
Drag options to blanks, or click blank then click option'
Aprocessed_events
Bpending_events
Cevent_log
Devent_queue
Attempts:
3 left
💡 Hint
Common Mistakes
Not using a lock leading to race conditions.
Using different data structures for check and add.
3fill in blank
hard

Fix the error in the event consumer code to prevent duplicate processing in distributed systems.

Microservices
def consume(event):
    if event.id not in [1]:
        process(event)
        processed_events.add(event.id)
Drag options to blanks, or click blank then click option'
Aprocessed_events
Bpending_events
Cevent_queue
Devent_log
Attempts:
3 left
💡 Hint
Common Mistakes
Checking the wrong data structure causing duplicate processing.
4fill in blank
hard

Fill both blanks to implement a distributed lock and idempotent event processing.

Microservices
def process_event(event):
    with [1](event.id):
        if event.id in [2]:
            return
        processed_events.add(event.id)
        handle(event)
Drag options to blanks, or click blank then click option'
Adistributed_lock
Blocal_lock
Cprocessed_events
Dpending_events
Attempts:
3 left
💡 Hint
Common Mistakes
Using a local lock in distributed systems.
Checking the wrong data structure.
5fill in blank
hard

Fill all three blanks to implement idempotent event processing with event deduplication and logging.

Microservices
def consume_event(event):
    if event.id in [1]:
        log("Duplicate event ignored: " + event.id)
        return
    [2].add(event.id)
    process(event)
    [3](event)
Drag options to blanks, or click blank then click option'
Aprocessed_events
Bpending_events
Clog_event
Devent_log
Attempts:
3 left
💡 Hint
Common Mistakes
Using different sets for check and add.
Not logging processed events.