Skip to main content

Services

Services define how to reach backend servers and handle load balancing.

What is a Service?

A service configures how Traefik forwards requests to your backend servers. Each service defines:
  • Servers: One or more backend server addresses
  • Load Balancing: Algorithm to distribute requests
  • Health Checks: Monitoring to remove unhealthy servers
  • Sticky Sessions: Route clients to the same server

HTTP Services

HTTP services use the loadBalancer configuration to distribute requests across backend servers.

Basic Service Configuration

Service Configuration Options

array
required
List of backend server URLs.
string
Load balancing algorithm: wrr (default), p2c, or hrw.
object
Health check configuration to monitor server availability.
object
Sticky session configuration using cookies.
boolean
Forward the Host header to backend servers (default: true).

Load Balancing Algorithms

Traefik supports three load balancing strategies with different characteristics.

Weighted Round Robin (WRR)

Default algorithm using Earliest Deadline First (EDF) scheduling. Implemented in pkg/server/service/loadbalancer/wrr/wrr.go.
Distribute requests evenly across all servers:
Each server receives ~33% of traffic.
WRR uses O(log n) selection time with a heap-based implementation for efficient server selection.

Power of Two Choices (P2C)

Selects two random servers and chooses the one with fewer active requests. Implemented in pkg/server/service/loadbalancer/p2c/p2c.go.
When to use P2C:
  • High-concurrency applications
  • Servers with varying response times
  • Distributed deployments without central coordination
  • Better “herd behavior” than least connections
P2C provides O(1) selection time by randomly sampling two servers instead of checking all servers.

Highest Random Weight (HRW)

Rendezvous hashing that routes clients consistently to the same server based on client IP. Implemented in pkg/server/service/loadbalancer/hrw/hrw.go.
When to use HRW:
  • Cache-heavy applications (consistent routing to same server)
  • Session affinity without sticky cookies
  • Minimal disruption when adding/removing servers
HRW uses client IP for routing. Requests from the same IP always go to the same backend (unless server is unhealthy).

Health Checks

Health checks automatically remove unhealthy servers from load balancing rotation.

Basic Health Check

Health Check Options

string
required
Health check endpoint path (relative URL).
duration
Frequency of health checks for healthy servers (default: 30s).
duration
Frequency of health checks for unhealthy servers (default: same as interval).
duration
Maximum duration to wait for health check response (default: 5s).
string
Override URL scheme for health checks (http or https).
string
Hostname to use in Host header for health checks.
integer
Override port for health check endpoint.
string
HTTP method for health checks (default: GET).
integer
Expected HTTP status code (default: accept any 2xx or 3xx).
object
Custom headers to send with health check requests.
boolean
Follow redirects during health checks (default: true).
string
Health check mode: http (default) or grpc.

Advanced Health Check Examples

Use dedicated health check port and path:
Send authentication headers with health checks:
Use gRPC health check protocol:
Traefik uses gRPC health check v1 protocol and expects SERVING status.
Check unhealthy servers more frequently:
Healthy servers that become unhealthy are automatically removed from rotation. Once they recover (returning 2xx-3xx status), they’re added back automatically.

Sticky Sessions

Sticky sessions route requests from the same client to the same backend server using cookies.

Basic Sticky Sessions

string
Cookie name. Default is SHA1 hash abbreviation (e.g., _1d52e).
boolean
Set Secure flag (HTTPS only). Default: false.
boolean
Set HttpOnly flag (no JavaScript access). Default: false.
string
SameSite attribute: none, lax, strict, or empty. Default: empty.
integer
Cookie expiration in seconds. Default: 0 (session cookie). Negative values create expired cookies.
string
Cookie domain for subdomain sharing.
If a server becomes unhealthy, requests are forwarded to a new healthy server and the cookie is updated.

Server Options

Individual server configuration options:

Server Weight

Preserve Path

Preserve URL path when forwarding to backends:
preservePath is ignored when health checks are configured.

TCP Services

TCP services forward raw TCP connections to backend servers.
TCP services use address (host:port) instead of url. Load balancing algorithm is always round-robin.

UDP Services

UDP services forward UDP packets to backend servers.

Weighted Services

Combine multiple services with weighted traffic distribution:

Mirroring Services

Mirror traffic to another service for testing:
Mirrored requests are fire-and-forget. Responses from mirrored services are ignored.

Real-World Examples

Complete service with health checks and sticky sessions:
Route traffic between two service versions:
Gradually shift traffic by adjusting weights: 90/10, 50/50, 0/100.
Multiple services with different configurations:

Next Steps

Configure EntryPoints

Set up network entry points for incoming traffic

Create Routers

Define routing rules to connect requests to services