跳转到内容

Load Balancing

此内容尚不支持你的语言。

MiniGateway supports multiple load balancing algorithms to distribute requests across upstream targets.

AlgorithmDescriptionBest For
round-robinDistribute evenly in rotationEqual-capacity backends
least-connectionsRoute to server with fewest active requestsVarying load patterns
hashConsistent hashing based on request keySession affinity
health-awareCombine least-connections with health checksProduction deployments

Targets can have different weights for weighted distribution:

{
"upstreamId": "upstream_123",
"target": "http://server-a:5000",
"weight": 3 // Receives 3x more requests
}

With weights [A:3, B:1] and round-robin:

  • Requests: A→A→A→B→A→A→A→B…

The load balancer tracks target health:

  • Healthy - Successfully responding
  • Unhealthy - Failed responses or timeouts

Unhealthy targets are automatically excluded from selection until they recover.

You can create load balancers programmatically:

import { createLoadBalancer } from "@minigateway/core";
const lb = createLoadBalancer("round-robin", {
targets: [
{ url: "http://server-a:5000", weight: 1 },
{ url: "http://server-b:5000", weight: 2 },
],
});
const target = lb.select();

Complete upstream with targets:

// Create upstream
{
"name": "backend-pool",
"algorithm": "health-aware"
}
// Add targets
{
"upstreamId": "upstream_xyz",
"target": "http://10.0.0.1:5000",
"weight": 1
}
{
"upstreamId": "upstream_xyz",
"target": "http://10.0.0.2:5000",
"weight": 2
}