MATIH Platform is in active MVP development. Documentation reflects current implementation status.
8. Platform Services
Load Balancing

Load Balancing

The API Gateway manages load balancing through Kong upstreams. The LoadBalancerService supports multiple algorithms including round-robin, weighted distribution, consistent hashing (sticky sessions by header or cookie), and latency-based routing. Each upstream contains targets (backend instances) with configurable weights and health checking.


Algorithms

AlgorithmKong ValueDescription
ROUND_ROBINround-robinDistributes requests evenly across targets
CONSISTENT_HASHINGconsistent-hashingRoutes requests based on a hash key (header, cookie, IP)
LATENCYlatencyRoutes to the target with lowest latency (Kong Enterprise)

Upstream Configuration

UpstreamConfig Properties

PropertyTypeDefaultDescription
nameStringrequiredUnique upstream identifier
algorithmAlgorithmROUND_ROBINLoad balancing algorithm
slotsint10000Number of hash slots for consistent hashing
hashOnHashOnNONEWhat to hash on for consistent hashing
hashOnHeaderStringnullHeader name when hashOn is HEADER
hashOnCookieStringnullCookie name when hashOn is COOKIE
hashOnCookiePathString/Cookie path for consistent hashing
hashFallbackHashOnnullFallback hash strategy
healthchecksHealthcheckConfignullActive and passive health check config

HashOn Options

ValueDescription
NONENo hashing (use with round-robin)
CONSUMERHash on authenticated consumer
IPHash on client IP address
HEADERHash on a specific request header
COOKIEHash on a specific cookie
PATHHash on the request path
QUERY_ARGHash on a query parameter
URI_CAPTUREHash on a URI capture group

Preset Configurations

Round-Robin

Endpoint: POST /api/v1/gateway/upstreams/preset/round-robin

curl -X POST "http://localhost:8080/api/v1/gateway/upstreams/preset/round-robin?name=ai-upstream" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${TOKEN}" \
  -d '["ai-service-1:8000", "ai-service-2:8000", "ai-service-3:8000"]'

Weighted

Endpoint: POST /api/v1/gateway/upstreams/preset/weighted

curl -X POST "http://localhost:8080/api/v1/gateway/upstreams/preset/weighted?name=ai-upstream" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${TOKEN}" \
  -d '{"ai-service-1:8000": 60, "ai-service-2:8000": 30, "ai-service-3:8000": 10}'

Sticky Sessions (Header)

Endpoint: POST /api/v1/gateway/upstreams/preset/sticky-header

curl -X POST "http://localhost:8080/api/v1/gateway/upstreams/preset/sticky-header?name=ai-upstream&headerName=X-Session-ID" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${TOKEN}" \
  -d '["ai-service-1:8000", "ai-service-2:8000"]'

Sticky Sessions (Cookie)

Endpoint: POST /api/v1/gateway/upstreams/preset/sticky-cookie

curl -X POST "http://localhost:8080/api/v1/gateway/upstreams/preset/sticky-cookie?name=ai-upstream&cookieName=session_id" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${TOKEN}" \
  -d '["ai-service-1:8000", "ai-service-2:8000"]'

Least Connections

Endpoint: POST /api/v1/gateway/upstreams/preset/least-connections

curl -X POST "http://localhost:8080/api/v1/gateway/upstreams/preset/least-connections?name=ai-upstream" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${TOKEN}" \
  -d '["ai-service-1:8000", "ai-service-2:8000", "ai-service-3:8000"]'

Health Checks

Active Health Checks

Active health checks periodically probe targets to determine availability:

PropertyDefaultDescription
typehttpHealth check protocol
timeout1sProbe timeout
concurrency10Concurrent probe connections
httpPath/healthPath to probe
healthyInterval5sProbe interval for healthy targets
healthySuccesses2Successes needed to mark healthy
healthyHttpStatuses200, 302HTTP statuses considered healthy
unhealthyInterval5sProbe interval for unhealthy targets
unhealthyTcpFailures2TCP failures to mark unhealthy
unhealthyHttpFailures5HTTP failures to mark unhealthy
unhealthyTimeouts3Timeouts to mark unhealthy
unhealthyHttpStatuses429, 500, 503HTTP statuses considered unhealthy

Passive Health Checks

Passive health checks monitor real traffic responses to determine target health without additional probe requests.


Target Management

Add a Target

Endpoint: POST /api/v1/gateway/upstreams/:upstreamName/targets

curl -X POST http://localhost:8080/api/v1/gateway/upstreams/ai-upstream/targets \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${TOKEN}" \
  -d '{"target": "ai-service-4:8000", "weight": 100}'

Update Target Weight

Endpoint: PATCH /api/v1/gateway/upstreams/:upstreamName/targets/:targetAddress/weight

curl -X PATCH "http://localhost:8080/api/v1/gateway/upstreams/ai-upstream/targets/ai-service-4:8000/weight?weight=50" \
  -H "Authorization: Bearer ${TOKEN}"

Check Upstream Health

Endpoint: GET /api/v1/gateway/upstreams/:upstreamName/health

curl http://localhost:8080/api/v1/gateway/upstreams/ai-upstream/health \
  -H "Authorization: Bearer ${TOKEN}"

Response returns the health status of each target: HEALTHY, UNHEALTHY, or HEALTHCHECKS_OFF.