Loading...
Loading...

Do you know YARP?

If you're developing modern and scalable applications, you've probably come across challenges related to load balancing, request routing, or even security and authentication issues for APIs. So, I want to introduce you to YARP!

What is YARP?

YARP (Yet Another Reverse Proxy) is an open-source project from Microsoft that acts as a reverse proxy made to be super configurable, extensible, and easy to use with .NET. As the name itself suggests, it's just another reverse proxy... but with the simplicity and robustness that Microsoft delivers. If you are in the world of C# development, this tool could be a powerful addition to your stack.

Before going into the uses, it's worth looking at my other article on the topic here, but if you don't want to read it here's a summary: a reverse proxy is a server that sits between clients and servers, receiving requests from clients and redirecting them to the appropriate servers. This not only helps balance the load among several servers but can also enhance security, hide the structure of internal servers, and optimize performance.

Common Uses of YARP

YARP is a real "helping hand" for many situations! Here are some common uses of this technology:

  1. Load Balancing: If your application needs to handle a large amount of traffic, YARP allows you to distribute requests among multiple servers, ensuring none of them become overloaded. This makes the system more resilient and scalable.

  2. Dynamic Routing: Want to direct specific requests to different services? With YARP, you can define rules to direct traffic based on the request content, such as specific URLs, headers, content types, etc.

  3. Proxy for Microservices: In microservice architectures, it's common for different services to have varying URLs and be scattered around. YARP simplifies orchestration and communication between these services, functioning as a "central hub" that manages the traffic between them.

  4. Authentication and Security: As an intermediary for requests, YARP can be configured to add layers of authentication and access control before requests reach the services, increasing the security of the application.

Shall we see a practical application?

Let's configure a simple reverse proxy using YARP. Imagine you have an application running at localhost:5001 and you want to expose an API running at localhost:5002 through a proxy, all centralized on a single port to facilitate access and routing.

Configuration in appsettings.json

First, let's set up the basic routing and clusters in the appsettings.json file:

{
  "ReverseProxy": {
    "Routes": {
      "authenticatedRoute": {
        "ClusterId": "authenticatedCluster",
        "Match": {
          "Path": "/secure-api/{**catch-all}"
        }
      }
    },
    "Clusters": {
      "authenticatedCluster": {
        "Destinations": {
          "secureBackend": {
            "Address": "http://localhost:5003/"
          }
        }
      }
    }
  }
}

Here we are telling YARP that any request to /api/* should be routed to the cluster1 cluster, which redirects requests to the http://localhost:5002/ address.

Configuring YARP in Code

Now let's move on to the code! Add the Yarp.ReverseProxy package to your project and configure the proxy in Startup.cs or Program.cs, depending on your project's structure:

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = WebApplication.CreateBuilder(args);

// Add YARP as a service
builder.Services.AddReverseProxy()
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));

var app = builder.Build();

// Configure the reverse proxy middleware
app.MapReverseProxy();

app.Run();

With this, when running the application on localhost:5001, any request to /api will be redirected to localhost:5002. And what's even cooler: you can add multiple rules and routes to appsettings.json, configuring different destinations for your requests!

Advantages and Customizations

The real magic of YARP lies in customization. In addition to the basic configurations, you can add middleware to handle authentication, caching, redirects, and more. Since it's based on ASP.NET Core, you can take advantage of everything you already know about the platform, including dependency injection and middleware pipelines.

Why should I adopt YARP?

YARP makes creating robust and scalable solutions hassle-free. It allows you, as a developer, to control the traffic flow of your application, add security, and increase performance with a simple and easy-to-understand configuration. Microsoft has provided a solution that resolves complex infrastructure problems with a simplicity that fits the .NET ecosystem.

For those working with APIs, microservices, load balancing, and traffic control, YARP is an extremely powerful addition!

Plus, you can solve many other "outside the box" problems with this tool. There are many other use cases that can't be explained in a single article. It’s worth checking out the sources below and seeking more information about YARP.

Sources

  1. Microsoft
  2. YT Channel Raw Coding (one of the best .NET content creators)
  3. Source Code Github Repo
  4. Official YARP Documentation