What if your system could magically handle millions of users without breaking a sweat?
Why Routing and load balancing in Microservices? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you run a busy restaurant with many customers arriving at once. You try to seat each guest yourself, deciding who goes to which table and waiter. As more guests arrive, it becomes chaotic and slow.
Manually directing each customer wastes time and causes mistakes. Some tables get overcrowded while others stay empty. Waiters get overwhelmed or idle. This leads to unhappy guests and lost business.
Routing and load balancing act like a smart host and manager. They automatically guide customers to the right tables and balance the workload evenly among waiters. This keeps service smooth and guests happy.
sendRequest(server1, data) sendRequest(server1, data) sendRequest(server2, data)
loadBalancer.routeRequest(data) # Automatically picks best serverIt enables systems to handle many users efficiently and reliably without manual effort.
Popular websites like online stores use load balancing to spread visitors across many servers, so pages load fast even during sales.
Manual routing causes delays and errors under heavy load.
Routing and load balancing automate traffic distribution smartly.
This improves system speed, reliability, and user experience.
Practice
Solution
Step 1: Understand routing role
Routing directs incoming requests to the right microservice based on predefined rules like URL paths or headers.Step 2: Differentiate routing from other functions
Storing data, encrypting communication, and monitoring are separate concerns handled by databases, security layers, and monitoring tools respectively.Final Answer:
To send requests to the correct microservice based on rules -> Option DQuick Check:
Routing = directing requests [OK]
- Confusing routing with data storage
- Mixing routing with security or monitoring
- Thinking routing balances load
Solution
Step 1: Identify common load balancing syntax
Round robin is a standard load balancing method cycling through instances evenly, often expressed as a list.Step 2: Evaluate options for correct syntax style
round_robin: [instance1, instance2, instance3] uses a clear list with round_robin keyword, matching common config styles. Others use invalid or uncommon syntax.Final Answer:
round_robin: [instance1, instance2, instance3] -> Option AQuick Check:
Round robin uses list syntax [OK]
- Using semicolons instead of commas
- Incorrect assignment operators
- Using arrows or pipes incorrectly
weights = {"serviceA": 3, "serviceB": 1}
requests = 8
for i in range(requests):
target = weighted_choice(weights)
print(target)
What is the expected number of requests routed to serviceA?Solution
Step 1: Understand weighted routing concept
Weights define how many times a service should receive requests relative to others. ServiceA has weight 3, serviceB has weight 1, total weight is 4.Step 2: Calculate expected requests for serviceA
Out of 8 requests, serviceA should get (3/4)*8 = 6 requests on average.Final Answer:
6 -> Option AQuick Check:
Weighted share = 6 requests [OK]
- Ignoring weights and dividing requests equally
- Confusing total weight with individual weights
- Calculating requests for serviceB instead
if (instance.isHealthy()) {
forwardRequest(instance)
} else {
skipInstance(instance)
}
However, requests are still being sent to unhealthy instances. What is the most likely cause?Solution
Step 1: Analyze health check integration
The code shows a health check condition, but if the load balancer does not actually use this logic, unhealthy instances may still receive traffic.Step 2: Evaluate other options for relevance
Round robin vs weighted routing does not affect health checks. Overload does not mark instances unhealthy. URL path matching is unrelated to health status.Final Answer:
Health check logic is not integrated with the load balancer -> Option BQuick Check:
Health check integration = key [OK]
- Assuming routing method affects health checks
- Confusing overload with health status
- Ignoring missing integration of health logic
Solution
Step 1: Identify routing needs for user requests and jobs
User requests require path-based routing to separate them from background jobs, which need different load balancing strategies.Step 2: Choose architecture supporting both routing and load balancing rules
A single load balancer with path-based routing can direct traffic to two target groups. One group uses round robin for user requests, the other weighted for jobs, meeting all requirements efficiently.Final Answer:
Use a single load balancer with path-based routing directing to two target groups; one uses round robin, the other weighted balancing -> Option CQuick Check:
Path-based routing + mixed balancing = Use a single load balancer with path-based routing directing to two target groups; one uses round robin, the other weighted balancing [OK]
- Using weighted balancing for user requests instead of round robin
- Splitting with DNS which lacks path awareness
- Routing all traffic to one instance causing bottlenecks
