0
0
Microservicessystem_design~10 mins

Eventual consistency 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 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