Skip to main content

Command Palette

Search for a command to run...

How Load Balancers Route

How Load Balancers Route Traffic Based on Request

Updated
β€’4 min read

1. Problem Statement

Imagine this setup:

  • A user opens your website

  • Another user calls your API

  • Someone else loads images

But your load balancer sends everything to the same backend server.

What happens?

  • API requests slow down image delivery

  • Static content consumes application resources

  • Backend becomes overloaded and inefficient

Even worse:

Everything looks β€œUP”… but performance is inconsistent.

πŸ‘‰ The problem is simple: Not all traffic is the same β€” but it’s being treated the same.

2. Concept Explanation

What is Content Switching?

Content Switching is a technique where a load balancer:

πŸ‘‰ Makes routing decisions based on the request itself

Instead of blindly forwarding traffic, it inspects the request and decides:

  • Where should this go?

  • Which backend is best suited?

Simple Analogy

Think of a sorting system at a courier facility:

  • Packages arrive at one entry point

  • Labels are checked

  • Items are routed to different destinations

Similarly:

  • /api β†’ API servers

  • /images β†’ image servers

  • /app β†’ web servers

πŸ‘‰ The load balancer becomes intelligent, not just a traffic distributor.

3. Types / Variations

1. URL Path-Based Routing (Most Common)

  • /api/* β†’ API backend

  • /images/* β†’ image server

  • /app/* β†’ web server

πŸ‘‰ Simple and widely used in real-world deployments

2. Host-Based Routing (High-Level)

πŸ‘‰ Useful for multi-domain architectures

3. Header-Based Routing (Brief)

  • Based on:

    • User-Agent

    • Custom headers

    • Cookies

πŸ‘‰ Used in advanced scenarios (A/B testing, version routing)

4. How It Works Internally

Let’s break it down step by step:

  1. Client sends request

    • Example: GET /api/users
  2. Load Balancer receives request

  3. Load Balancer inspects:

    • URL path

    • Host header

    • Other attributes

  4. Policy is evaluated

    • Match: /api/*
  5. Decision is made

    • Route to API backend
  6. Request is forwarded to correct server

πŸ‘‰ This happens in milliseconds, for every request.

5. Diagram

Flow:

Client β†’ Load Balancer β†’

  • /api β†’ API Server

  • /images β†’ Image Server

  • /app β†’ Web Server

6. Real-World Example

Scenario: Single Domain, Multiple Services

Your application:

  • example.com/app β†’ Web UI

  • example.com/api β†’ Backend APIs

  • example.com/images β†’ Static content

Instead of:

❌ One backend handling everything

You design:

βœ… Separate backends optimized for each workload

Microservices Architecture

Modern apps follow this pattern:

  • API services scale independently

  • Static content served via optimized nodes

  • Frontend handled separately

πŸ‘‰ Content switching enables clean architecture + better performance

7. Common Issues / Pitfalls

1. Incorrect Routing Rules

  • Wrong match condition

  • Traffic goes to wrong backend

πŸ‘‰ Result: broken application behavior

2. Overlapping Policies

Example:

  • /api/*

  • /api/v1/*

πŸ‘‰ Which one should match first?

πŸ‘‰ Order matters β€” always.

3. Performance Overhead

  • Every request is inspected

  • Complex rules = more processing

πŸ‘‰ Keep policies simple and efficient

8. Try It Yourself (MANDATORY)

Try it yourself πŸ‘‡

πŸ‘‰ Open Full Visualizer (Visualizer Link)

9. Key Takeaways

  • Not all traffic is equal

  • Load balancers can make intelligent decisions

  • Content switching improves performance and scalability

  • Proper rule design is critical

  • It’s the foundation for modern architectures

10. Conclusion

Content Switching transforms a load balancer from:

πŸ‘‰ A simple traffic distributor to πŸ‘‰ An intelligent traffic router

It allows you to:

  • Separate workloads

  • Improve performance

  • Design scalable systems

And most importantly:

πŸ‘‰ Build architecture that matches real-world traffic behavior

11. Series Continuity

Previously: πŸ‘‰ SSL Termination β€” how encryption is handled efficiently

Next: πŸ‘‰ Responder & Rewrite Policies β€” modifying traffic on the fly

12. Final Thought

A good load balancer distributes traffic. A great load balancer understands traffic.

πŸ‘‰ Content Switching is where that intelligence begins.

13. Practical: NetScaler Hands-on

13.1 Mini Lab

  • Create 3 backend services:

    • Web server

    • API server

    • Image server

  • Configure Content Switching vServer

  • Route based on URL path

13.2 Variation / Experiment

  • Add rule: /test β†’ new backend

  • Break a rule intentionally

  • Observe incorrect routing

πŸ‘‰ This is how you truly learn behavior

13.3 Commands (3–5 max)

add cs vserver cs_vsrv HTTP 10.0.0.10 80

add cs policy api_policy "HTTP.REQ.URL.STARTSWITH(\"/api\")" api_lb_vsrv
add cs policy img_policy "HTTP.REQ.URL.STARTSWITH(\"/images\")" img_lb_vsrv

bind cs vserver cs_vsrv -policyName api_policy -priority 10
bind cs vserver cs_vsrv -policyName img_policy -priority 20
7 views