0
0
Microservicessystem_design~10 mins

Routing and load balancing 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 select the next server using round-robin load balancing.

Microservices
next_server = servers[[1] % len(servers)]
Drag options to blanks, or click blank then click option'
Acurrent_index + 1
Blen(servers)
Ccurrent_index
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(servers) always picks server 0.
Using current_index repeats the same server.
2fill in blank
medium

Complete the code to route requests based on URL path prefix.

Microservices
if request.path.startswith([1]):
    route_to = 'serviceA'
else:
    route_to = 'serviceB'
Drag options to blanks, or click blank then click option'
A'/login/'
B'serviceA'
C'/api/v1/'
D'/home/'
Attempts:
3 left
💡 Hint
Common Mistakes
Using service names instead of URL prefixes.
Using unrelated paths like '/login/' when routing API calls.
3fill in blank
hard

Fix the error in the load balancer health check function.

Microservices
def is_healthy(server):
    response = ping(server)
    return response.status_code == [1]
Drag options to blanks, or click blank then click option'
A500
B404
C302
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Using 404 which means not found.
Using 500 which means server error.
4fill in blank
hard

Fill both blanks to implement weighted load balancing selection.

Microservices
total_weight = sum(server['weight'] for server in servers)
random_pick = random.uniform(0, [1])
current = 0
for server in servers:
    current += server['weight']
    if current >= [2]:
        selected = server
        break
Drag options to blanks, or click blank then click option'
Atotal_weight
Brandom_pick
Clen(servers)
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(servers) instead of total weight.
Comparing to zero instead of the random pick.
5fill in blank
hard

Fill all three blanks to complete the request routing with fallback.

Microservices
try:
    response = [1](request)
except [2]:
    response = [3](request)
Drag options to blanks, or click blank then click option'
Aprimary_service.handle
BTimeoutError
Cbackup_service.handle
DValueError
Attempts:
3 left
💡 Hint
Common Mistakes
Catching the wrong exception type.
Using the backup service in the try block instead of except.