Bird
Raised Fist0
Microservicessystem_design~10 mins

Routing and load balancing in Microservices - Scalability & System Analysis

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
Scalability Analysis - Routing and load balancing
Growth Table: Routing and Load Balancing at Different Scales
UsersRequests per Second (RPS)Routing ComplexityLoad Balancer SetupNetwork Traffic
100 users~50 RPSSimple routing, single load balancerOne load balancer instanceLow, easily handled by single server
10,000 users~5,000 RPSMultiple microservices, routing rules growMultiple load balancers with health checksModerate, requires monitoring
1,000,000 users~500,000 RPSComplex routing, service discovery neededDistributed load balancers, global traffic managementHigh, needs optimized network and CDN
100,000,000 users~50,000,000 RPSHighly dynamic routing, multi-region failoverHierarchical load balancing, edge routing, global DNSVery high, requires advanced network infra
First Bottleneck

At small scale, the load balancer server CPU and memory become the first bottleneck because it must handle all incoming requests and route them correctly. As traffic grows, the routing logic and service discovery can slow down, causing delays. At medium scale, network bandwidth and latency between load balancers and microservices become critical. At large scale, global routing and failover complexity cause bottlenecks if not properly distributed.

Scaling Solutions
  • Horizontal Scaling: Add more load balancer instances behind a DNS or anycast IP to distribute traffic.
  • Service Discovery: Use dynamic service registries to keep routing updated without manual config.
  • Caching: Cache routing decisions or DNS lookups to reduce latency.
  • Sharding: Partition traffic by user region or service type to reduce load per balancer.
  • CDN and Edge Routing: Offload static content and route users to nearest data center.
  • Global Load Balancing: Use DNS-based or geo-aware load balancing for multi-region failover.
Back-of-Envelope Cost Analysis

Assuming 1 million users generate ~500,000 RPS:

  • Each load balancer can handle ~5,000 concurrent connections and ~10,000 RPS.
  • Number of load balancers needed: 500,000 / 10,000 = 50 instances minimum.
  • Network bandwidth: If average request size is 10 KB, total bandwidth = 500,000 * 10 KB = ~5 GB/s (~40 Gbps).
  • Storage is minimal for routing but logs and metrics storage grows with traffic.
Interview Tip

Start by explaining the role of routing and load balancing in microservices. Discuss how traffic grows and what breaks first. Then, describe scaling strategies step-by-step: horizontal scaling, service discovery, caching, and global load balancing. Use real numbers to show understanding of capacity and bottlenecks.

Self Check

Your database handles 1000 QPS. Traffic grows 10x. What do you do first?

Answer: Since the database is the bottleneck at 1000 QPS, and traffic grows to 10,000 QPS, the first step is to add read replicas and implement caching to reduce direct database load before scaling application servers or load balancers.

Key Result
Routing and load balancing scale by adding more load balancer instances, using service discovery for dynamic routing, and distributing traffic globally to avoid bottlenecks in CPU, memory, and network bandwidth.

Practice

(1/5)
1. What is the main purpose of routing in a microservices architecture?
easy
A. To store data persistently across services
B. To monitor the health of microservices
C. To encrypt communication between services
D. To send requests to the correct microservice based on rules

Solution

  1. Step 1: Understand routing role

    Routing directs incoming requests to the right microservice based on predefined rules like URL paths or headers.
  2. 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.
  3. Final Answer:

    To send requests to the correct microservice based on rules -> Option D
  4. Quick Check:

    Routing = directing requests [OK]
Hint: Routing directs requests to the right service [OK]
Common Mistakes:
  • Confusing routing with data storage
  • Mixing routing with security or monitoring
  • Thinking routing balances load
2. Which of the following is a correct syntax for defining a load balancer rule that forwards requests to multiple instances evenly?
easy
A. round_robin: [instance1, instance2, instance3]
B. loadbalance = {instance1; instance2; instance3}
C. balance->instances(instance1, instance2, instance3)
D. forward: instance1 | instance2 | instance3

Solution

  1. Step 1: Identify common load balancing syntax

    Round robin is a standard load balancing method cycling through instances evenly, often expressed as a list.
  2. 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.
  3. Final Answer:

    round_robin: [instance1, instance2, instance3] -> Option A
  4. Quick Check:

    Round robin uses list syntax [OK]
Hint: Look for standard list syntax with round robin keyword [OK]
Common Mistakes:
  • Using semicolons instead of commas
  • Incorrect assignment operators
  • Using arrows or pipes incorrectly
3. Given the following pseudo-code for a load balancer using weighted routing:
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?
medium
A. 6
B. 8
C. 4
D. 2

Solution

  1. 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.
  2. Step 2: Calculate expected requests for serviceA

    Out of 8 requests, serviceA should get (3/4)*8 = 6 requests on average.
  3. Final Answer:

    6 -> Option A
  4. Quick Check:

    Weighted share = 6 requests [OK]
Hint: Multiply total requests by service weight fraction [OK]
Common Mistakes:
  • Ignoring weights and dividing requests equally
  • Confusing total weight with individual weights
  • Calculating requests for serviceB instead
4. A load balancer is configured with the following rule:
if (instance.isHealthy()) {
  forwardRequest(instance)
} else {
  skipInstance(instance)
}
However, requests are still being sent to unhealthy instances. What is the most likely cause?
medium
A. Instances are overloaded but still marked healthy
B. Health check logic is not integrated with the load balancer
C. Load balancer is using round robin instead of weighted routing
D. Routing rules are missing URL path matching

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Health check logic is not integrated with the load balancer -> Option B
  4. Quick Check:

    Health check integration = key [OK]
Hint: Check if health logic is actually used by load balancer [OK]
Common Mistakes:
  • Assuming routing method affects health checks
  • Confusing overload with health status
  • Ignoring missing integration of health logic
5. You need to design a routing and load balancing system for a microservices app that handles both user requests and background jobs. User requests must be routed based on URL paths, and load balanced evenly. Background jobs should be routed to a separate set of instances with weighted load balancing. Which architecture best fits this requirement?
hard
A. Use DNS-based routing to split traffic, then apply round robin load balancing on all instances
B. Deploy two separate load balancers, one for user requests with weighted balancing, another for jobs with round robin
C. Use a single load balancer with path-based routing directing to two target groups; one uses round robin, the other weighted balancing
D. Route all traffic to a single instance that forwards requests internally based on type

Solution

  1. 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.
  2. 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.
  3. 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 C
  4. Quick 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]
Hint: Combine path routing with separate load balancing per target group [OK]
Common Mistakes:
  • 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