Mark Ku's Blog

Building a Custom Load Balancer and Multi-purpose Gateway with Microsoft's Open-Source YARP Reverse Proxy

Introduction

If you've ever worked with load balancers, you've probably heard of F5 Networks and their well-known F5 load balancer. But a single unit costs roughly NT$200,000-300,000, which is a hefty bill for small and medium-sized businesses. If you want to bring in a load balancer but the budget is tight, Microsoft's open-source YARP is a solid option for rolling your own software load balancer.

Benefits of a software load balancer

  • Horizontal scaling (Scale Up) of servers
  • API gateway with dynamic API routing
  • Rolling deployments
  • Maintenance page during downtime
  • Conditional rules to mitigate DDoS attacks
  • Routing search engine bots directly to an SSR site

What is load balancing?

It's the practice of distributing workload across multiple machines to maximize throughput, minimize response time, and avoid overloading any single server (which would otherwise stop responding to clients) — making the best possible use of network connections, CPU, memory, disk I/O, and other resources.

In the cloud era, load balancing makes horizontal scaling (Scale Up) effortless. Suppose a small e-commerce site normally only needs a 6-core/12-thread CPU. During Double 11 or anniversary sales, the server is often overloaded and can't keep up with traffic. You can simply rent extra cloud VMs from Azure, GCP, or AWS, add them to the load balancer's pool, and elastically scale to serve more users.

My network topology

Diagram of a load balancing and reverse proxy network architecture
Diagram of a load balancing and reverse proxy network architecture

Sticky session mechanisms in load balancing

Most load balancers offer two ways to keep a client tied to the same backend: Sticky Cookie and Custom Header. YARP supports both.

In a multi-server setup, to make sure a user stays on the same server, the load balancer (LTM) attaches a cookie to the response. On subsequent requests, the LTM reads that cookie and routes the user back to the original server.

Custom Header

With browsers tightening third-party cookie restrictions, more and more sites — especially those that may be embedded — can't rely on cookies. With this approach, the LTM passes a sticky-session token via a custom response header. The frontend stores it and sends it back on the next request, keeping the user pinned to the same backend server.

I mostly referenced this great post and forked the project, fixing a few bugs along the way

Article Project

Download and run the project, log in to the admin panel with password "password" (requires .NET Core 6 SDK and runtime)

Next, here are some common rule configurations

1. Forward all traffic for a whole site

1. Server Groups > Add a new server group

YARP gateway server group configuration panel
YARP gateway server group configuration panel

2. Routes > Add

YARP gateway UI for editing route configuration, including methods and path Pick the server group you just created and configure as shown: Path = {**catch-all} Transforms 1 PathPattern = {**catch-all} Transforms 2 RequestHeadersCopy = true

2. Forwarding a specific API route

1. Server Groups > Add a new server group

Gateway settings UI displaying server group configuration fields
Gateway settings UI displaying server group configuration fields

2. Routes > Add

Pick the server group you just created and configure as shown: Path = /api/{**remider} Transforms 1 PathPrefix = /api/v1/{**remider} Gateway route configuration UI with API path transform

3. SignalR forwarding

See this article for the configuration.

4. LTM sticky session configuration

YARP session affinity cookie configuration settings UI
YARP session affinity cookie configuration settings UI

2. Custom Header sticky sessions

YARP session affinity and cookie configuration form with custom header policy
YARP session affinity and cookie configuration form with custom header policy

5. Make sure to enable active health checks. When YARP detects a downed instance, it will stop dispatching requests to that server.

UI section with HealthCheck and Active Health Check toggle enabled
UI section with HealthCheck and Active Health Check toggle enabled

Official documentation

All the admin-panel actions above map to YARP's configuration file. If you're interested, take a look at the docs for the full picture. Official documentation

Author

Mark Ku

擁有 10+ 年經驗的資深軟體工程師,現為 AI 應用 Builder,專注於大型平台架構與簡化複雜系統設計,從電商系統到訂閱與收費平台,結合 AI Agent、AI 整合與自動化開發,打造高效率且可持續演進的產品技術基礎。Read More

Found this useful?

The author's free tools, daily podcasts and newsletter are all here.

Mark Ku · This article is licensed under CC BY 4.0. Credit the author and link back to the original when reusing it.

Comments

Subscribe to Newsletter

Subscribe to get new posts delivered instantly — never miss a tech share.

By submitting, you agree to receive emails. You can anytime.

Popular Posts

View all
Mark Ku
··602

Oracle Cloud Always Free Tier: Linux Host and Static IP for a $0 Cloud Solution

Oracle Cloud Always Free Tier: Linux Host and Static IP for a $0 Cloud Solution
Mark Ku
··492

Say Goodbye to Postman's Fee Trap! A Hands-on Guide to Bruno, the Open-Source Git-Native API Testing Powerhouse.

Say Goodbye to Postman's Fee Trap! A Hands-on Guide to Bruno, the Open-Source Git-Native API Testing Powerhouse.
Mark Ku
··334

A Free, Open-Source, Notion-like Knowledge Base — A Complete Guide to Deploying and Backing Up Outline Wiki

A Free, Open-Source, Notion-like Knowledge Base — A Complete Guide to Deploying and Backing Up Outline Wiki
Mark Ku
··268

Training Your Own AI Voice: Hardware Requirements, Open-Source Model Comparison, and LoRA Fine-Tuning

Training Your Own AI Voice: Hardware Requirements, Open-Source Model Comparison, and LoRA Fine-Tuning
Mark Ku
··218

Building an Efficient API Management Platform: Deploying Kong Gateway from Scratch - Part 1

Building an Efficient API Management Platform: Deploying Kong Gateway from Scratch - Part 1
Mark Ku
··217

Setting Up Samba on Ubuntu to Share Folders with Windows 11

Setting Up Samba on Ubuntu to Share Folders with Windows 11