Load Balancing

Runs on your machine

Distributes requests over workers and mirrors, with a canary deployment.

Spreads the requests to an API over three workers and sends downloads to one of two mirror servers. Proxy forwards a request to a worker and passes its response on; Redirect sends the client to a mirror, so downloads do not pass through this server.

A priority decides where a request may go: requests with an X-Canary header prefer the third worker, which can run a new version you want to test with some real traffic first. Everything else goes to the other two workers, which share the load.

Runs on your machine only. It forwards requests to worker processes running on your machine.

To run it, create a console app (dotnet new console), add GenHTTP (dotnet add package GenHTTP.Full), replace Program.cs with the code below and start it with dotnet run - it answers at http://localhost:8080/.

Program.cs29 lines
using GenHTTP.Engine.Internal;

using GenHTTP.Api.Infrastructure;
using GenHTTP.Api.Protocol;

using GenHTTP.Modules.Layouting;
using GenHTTP.Modules.LoadBalancing;
using GenHTTP.Modules.Practices;

var canary = new ByteString("X-Canary");

// the workers, e.g. instances of your API, run on ports 5001 to 5003
var workers = LoadBalancer.Create()
                          .Proxy("http://localhost:5001/")
                          .Proxy("http://localhost:5002/")
                          .Proxy("http://localhost:5003/", request => request.Header.Headers.ContainsKey(canary) ? Priority.High : Priority.Low);

var mirrors = LoadBalancer.Create()
                          .Redirect("https://mirror1.example.com/")
                          .Redirect("https://mirror2.example.com/");

var app = Layout.Create()
                .Add("api", workers) // GET /api/
                .Add("downloads", mirrors); // GET /downloads/setup.exe

await Host.Create()
          .Handler(app)
          .Defaults()
          .RunAsync();

Try it

With the sample running on your machine:

  1. Call a worker

    502, unless workers run on ports 5001 and 5002

    curl -i http://localhost:8080/api/
  2. Call the canary worker

    502, unless a worker runs on port 5003

    curl -i -H "X-Canary: 1" http://localhost:8080/api/
  3. Download from a mirror

    Redirects to one of the mirrors.

    curl -i http://localhost:8080/downloads/setup.exe