Bird
Raised Fist0
Microservicessystem_design~10 mins

Eventual consistency in Microservices - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to represent the basic idea of eventual consistency in microservices.

Microservices
service_state = 'stale'
# After some time, the state becomes [1]
Drag options to blanks, or click blank then click option'
Ainconsistent
Bimmediate
Cconsistent
Dpermanent
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing 'immediate' because it sounds fast
Confusing 'inconsistent' as the final state
2fill in blank
medium

Complete the code to show how a microservice might handle updates asynchronously for eventual consistency.

Microservices
def update_data(data):
    # Send update to message queue
    message_queue.[1](data)
Drag options to blanks, or click blank then click option'
Apublish
Breceive
Cdelete
Dprocess
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'receive' which is for consuming messages
Using 'delete' which removes data
3fill in blank
hard

Fix the error in the code that processes messages to update the local state eventually.

Microservices
def process_message(msg):
    # Update local state
    local_state = [1]
Drag options to blanks, or click blank then click option'
Amsg
Bmsg.data
Cmsg.process()
Dmsg.update()
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning the whole message object instead of its data
Calling non-existent methods on the message
4fill in blank
hard

Fill both blanks to complete the code that checks if the local state is eventually consistent with the source.

Microservices
if local_state [1] source_state:
    print('States are consistent')
else:
    print('States are [2]')
Drag options to blanks, or click blank then click option'
A==
B!=
Cconsistent
Dinconsistent
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' in the if condition
Confusing 'consistent' and 'inconsistent' in the print statements
5fill in blank
hard

Fill all three blanks to complete the code that implements a retry mechanism for eventual consistency.

Microservices
def retry_update(data, attempts=3):
    for i in range(attempts):
        success = send_update(data)
        if success:
            return [1]
    return [2]

result = retry_update(new_data)
print('Update [3]' if result else 'Update failed')
Drag options to blanks, or click blank then click option'
ATrue
BFalse
Csucceeded
Dfailed
Attempts:
3 left
💡 Hint
Common Mistakes
Returning False on success
Printing 'failed' when update succeeded

Practice

(1/5)
1. What does eventual consistency mean in microservices?
easy
A. Data updates will be visible to all parts of the system after some delay
B. Data is always instantly consistent across all services
C. Data is never synchronized between services
D. Data updates happen only during system maintenance

Solution

  1. Step 1: Understand the meaning of eventual consistency

    Eventual consistency means data changes are not immediate but will propagate over time.
  2. Step 2: Compare options with the definition

    Only Data updates will be visible to all parts of the system after some delay correctly states that data updates become visible after some delay, matching eventual consistency.
  3. Final Answer:

    Data updates will be visible to all parts of the system after some delay -> Option A
  4. Quick Check:

    Eventual consistency = delayed data visibility [OK]
Hint: Eventual means "eventually", not instantly [OK]
Common Mistakes:
  • Confusing eventual consistency with immediate consistency
  • Thinking data never syncs
  • Assuming updates only during maintenance
2. Which of the following is a correct way to implement eventual consistency in microservices?
easy
A. Use synchronous HTTP calls between services for every update
B. Use asynchronous event messaging to propagate changes
C. Block all reads until all writes complete
D. Disable communication between services

Solution

  1. Step 1: Identify communication style for eventual consistency

    Eventual consistency relies on asynchronous communication to allow updates to propagate over time.
  2. Step 2: Evaluate options

    Only Use asynchronous event messaging to propagate changes uses asynchronous event messaging, which fits eventual consistency. Others use synchronous or block reads, which do not.
  3. Final Answer:

    Use asynchronous event messaging to propagate changes -> Option B
  4. Quick Check:

    Asynchronous messaging = eventual consistency [OK]
Hint: Eventual consistency needs async events, not sync calls [OK]
Common Mistakes:
  • Choosing synchronous calls which block updates
  • Blocking reads causing poor availability
  • Ignoring communication between services
3. Consider a microservice system where Service A updates data and publishes an event. Service B listens and updates its copy asynchronously. What is the expected state of Service B immediately after Service A's update?
medium
A. Service B has stale data until it processes the event
B. Service B rejects the update
C. Service B has the updated data instantly
D. Service B crashes due to inconsistency

Solution

  1. Step 1: Understand asynchronous event propagation

    Service B updates data only after receiving and processing the event from Service A, which takes time.
  2. Step 2: Determine Service B's state immediately after Service A's update

    Since event processing is asynchronous, Service B still holds old data until it processes the event.
  3. Final Answer:

    Service B has stale data until it processes the event -> Option A
  4. Quick Check:

    Async update means stale data initially [OK]
Hint: Async updates cause temporary stale data [OK]
Common Mistakes:
  • Assuming instant data sync
  • Thinking services reject updates
  • Believing system crashes on inconsistency
4. A microservice system uses event-driven updates but sometimes Service B never receives events from Service A, causing stale data. What is the best fix?
medium
A. Switch to synchronous calls only
B. Ignore the problem as eventual consistency tolerates it
C. Implement event retry and dead-letter queues
D. Stop Service B from reading data

Solution

  1. Step 1: Identify problem cause

    Missing events cause stale data because messages are lost or not delivered.
  2. Step 2: Choose solution to ensure event delivery

    Implementing retries and dead-letter queues helps guarantee events reach Service B or are logged for manual handling.
  3. Final Answer:

    Implement event retry and dead-letter queues -> Option C
  4. Quick Check:

    Retries fix lost events = better consistency [OK]
Hint: Use retries and dead-letter queues for reliable events [OK]
Common Mistakes:
  • Switching to sync calls losing scalability
  • Ignoring lost events causing stale data
  • Disabling reads instead of fixing events
5. You design a microservices system with eventual consistency. Service A updates inventory and publishes events. Service B updates order status based on inventory events. How do you ensure order status eventually matches inventory without blocking user requests?
hard
A. Store all data in a single database to avoid events
B. Make Service B synchronously call Service A for every order update
C. Block user requests until all services are consistent
D. Use asynchronous event processing with idempotent handlers and retries

Solution

  1. Step 1: Understand requirements for eventual consistency and availability

    The system must update order status eventually without blocking user requests, so async processing is needed.
  2. Step 2: Choose design that supports async updates safely

    Using asynchronous event processing with idempotent handlers and retries ensures updates happen reliably and without blocking.
  3. Step 3: Evaluate other options

    Synchronous calls or blocking requests reduce availability; single database removes microservices benefits.
  4. Final Answer:

    Use asynchronous event processing with idempotent handlers and retries -> Option D
  5. Quick Check:

    Async + idempotent + retries = safe eventual consistency [OK]
Hint: Async with retries and idempotency ensures safe updates [OK]
Common Mistakes:
  • Blocking user requests hurting availability
  • Using sync calls causing tight coupling
  • Ignoring idempotency causing duplicate updates