# Building a Load Balancer with YARP in NET

Listen to this Post

Scaling your API horizontally requires an efficient load balancer. YARP (Yet Another Reverse Proxy) is a powerful, extensible reverse proxy toolkit for .NET applications. Below is a step-by-step guide to setting up YARP for load balancing.

Step-by-Step Setup

1. Install Yarp.ReverseProxy

Add the YARP NuGet package to your .NET project:

dotnet add package Yarp.ReverseProxy 

2. Configure YARP in Your Application

Update `Program.cs` to include YARP:

var builder = WebApplication.CreateBuilder(args);

// Add YARP Reverse Proxy 
builder.Services.AddReverseProxy() 
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));

var app = builder.Build();

// Map reverse proxy routes 
app.MapReverseProxy();

app.Run(); 

3. Define Downstream Services in `appsettings.json`

{ 
"ReverseProxy": { 
"Routes": { 
"route1": { 
"ClusterId": "cluster1", 
"Match": { 
"Path": "{catch-all}" 
} 
} 
}, 
"Clusters": { 
"cluster1": { 
"Destinations": { 
"destination1": { 
"Address": "http://localhost:5001/" 
}, 
"destination2": { 
"Address": "http://localhost:5002/" 
} 
}, 
"LoadBalancingPolicy": "PowerOfTwoChoices" 
} 
} 
} 
} 

You Should Know:

YARP Load Balancing Policies

YARP supports multiple load-balancing strategies:

  • Random – Randomly selects a destination.
  • RoundRobin – Cycles through destinations in order.
  • LeastRequests – Picks the destination with the fewest active requests.
  • FirstAlphabetical – Selects the first destination alphabetically.
  • PowerOfTwoChoices (Default) – Picks two random destinations and selects the least loaded.

Extending YARP

  • Custom Middleware – Modify requests/responses before forwarding.
  • Health Checks – Automatically exclude unhealthy destinations.
  • Session Affinity – Ensure a user sticks to the same server.

Performance Tuning

  • Caching – Reduce backend load with response caching.
  • Compression – Enable GZIP/Brotli compression for faster transfers.
  • HTTP/2 Support – Improve throughput with multiplexed connections.

What Undercode Say

YARP is a modern, high-performance reverse proxy for .NET that simplifies horizontal scaling. Unlike traditional proxies (NGINX, HAProxy), YARP integrates seamlessly with .NET middleware, making it ideal for microservices and cloud-native apps.

Key Linux/Windows Commands for Load Balancing

  • Check Active Connections (Linux):
    netstat -tuln | grep -E '5001|5002' 
    
  • Test Load Balancer (Curl):
    curl http://localhost:5000/api/resource 
    
  • Monitor Performance (Windows):
    Get-NetTCPConnection -LocalPort 5000,5001,5002 
    
  • Stress Test (Linux):
    ab -n 1000 -c 100 http://localhost:5000/api/resource 
    

Expected Output:

A scalable, high-performance API with YARP load balancing, configurable policies, and extensible middleware for enterprise-grade reliability.

Further Reading:

References:

Reported By: Milan Jovanovic – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 TelegramFeatured Image