Scaling and load balancing
Every feature on this page is opt-in. A site that uses none of these keys or commands deploys as before: one container per service, recreated on each deploy.
All keys go into a service's x-landfall in compose.yaml (Compose sites).
Replicas
Run more than one container of a service. Traefik balances requests over all of them.
services:
web:
build: .
deploy:
replicas: 3 # or the older scale: 3
x-landfall:
port: 3000lf scale changes the count without a new deploy:
# show each service's count, where it comes from, and its update mode
lf scale blog
# run 3 containers of web and 2 of worker
lf scale blog web=3 worker=2
# go back to the count in compose.yaml
lf scale blog web=default- The count goes into the release's
compose.override.yaml, sodocker compose up -don the server keeps it. - It stays for later deploys and rollbacks.
lf undoreverts it. - A count of 0 stops the service. Its data stays.
- A service with
container_nameor published ports (x-landfall.publish) runs only once. landfall refuses more. lf scale --jsonprints{site, services}: each service withreplicas,source(lf scale,compose.yamlordefault),update, andweightandsplitwhen it is in a traffic split.
Rolling deploys
By default a deploy recreates a changed service: it is down for a moment.
With update: rolling, the new containers start beside the old ones first.
x-landfall:
port: 3000
update: rollingOn each deploy, for each rolling service:
- The other services start first (a new database, a cache).
- If its configuration or image is unchanged, nothing happens.
- New containers start beside the running ones, as many as it should run.
- landfall waits until they run and, if they have a healthcheck, are healthy (up to 5 minutes).
- The old containers stop (30 seconds to finish) and are removed.
If the new containers do not get ready, they are removed, the old ones keep serving, and the release goes back.
- Give the service a healthcheck (in compose.yaml or the image). Without one, "ready" only means "started".
- A rolling service needs
x-landfall.port. It may not setcontainer_name, publish ports or have its own Traefik labels. lf scaleandlf weightnever roll a service: they change no configuration that needs new containers.
Load balancer options
x-landfall:
port: 3000
sticky: true # a cookie pins each client to one container; sticky: my_cookie names it
healthcheck: /health # Traefik sends nothing to a container that fails it
# or, with its own interval and timeout:
# healthcheck:
# path: /health
# interval: 10s
# timeout: 3s
ratelimit: 20 # requests per second per client IP
# or, with a burst:
# ratelimit:
# average: 20
# burst: 50
headers: # response headers; an empty value removes one
X-Frame-Options: DENY
compress: true # gzip and brotli
retry: 3 # retries a request when a container cannot be reached- Routes take the same keys:
routes: [{ port: 3001, subdomain: admin, ratelimit: 5 }]. - Middlewares run in this order:
allow,ratelimit, the login (lf auth),headers,compress,retry. - Traefik's
healthcheckis separate from Docker'shealthcheck:. Docker's decides when a container is ready; Traefik's decides where requests go.
Traffic split
Two or more services with weight on the same domains share their traffic by weight.
Use it to send a few requests to a new version (a canary).
services:
stable:
image: ghcr.io/me/app:1.4
x-landfall:
port: 3000
weight: 90
canary:
image: ghcr.io/me/app:1.5
x-landfall:
port: 3000
weight: 10# show the split
lf weight blog
# send everything to canary
lf weight blog stable=0 canary=100- Weights are 0 to 1000. A service gets its weight divided by the sum. At least one weight must be above 0.
lf weightchanges only Traefik's routing: no container restarts.lf undoreverts it.- The split is a file in
/opt/landfall/traefik/dynamic, read by Traefik's file provider. It needs landfall's own Traefik (lf traefik setup).lf ejectrefuses a site with a split. stickykeeps a client on one side of the split. Traefik skips an unhealthy side only when every side has ahealthcheck.- The services share one router, the one of the first service by name. Its
allow, middlewares and login apply to the whole split. Another service in the split may leave outallow,ratelimit,headers,compressandretry, or set them to the same values. Different values are an error. lf auth <site> --route <service>with any service of the split sets the login of the shared router.
Force a variant
match sends requests with a cookie or a header to one service of the split, whatever the weights.
services:
a:
image: ghcr.io/me/app:1.4
x-landfall:
port: 3000
weight: 100
b:
image: ghcr.io/me/app:1.5
x-landfall:
port: 3000
weight: 0 # no random share: only matching requests reach b
match:
cookie: variant=b # or header: X-Variant=b, or both# reach b on purpose
curl -H 'Cookie: variant=b' https://blog.example.com
curl -H 'X-Variant: b' https://blog.example.com- A request matches when it has the cookie or the header with that value. Names have letters, digits,
-and_. Values may also have dots. matchneedsweighton the same service. Withweight: 0only matching requests reach it: a preview for testers before any visitor sees it.- It is an extra Traefik router (
<site>_<service>-match). Its rule is longer, so Traefik tries it before the split's router. - It uses the split's middlewares:
allow, the login and the others still apply. A cookie never skips the login. - Your app sets the cookie, for example on an opt-in page. landfall only reads it.
lf weight <site>shows each service'smatch.
Compare the variants
lf weight --stats shows what each service of the split served, from Traefik's access log.
# the last 24 hours
lf weight blog --stats
# the last hour, as JSON
lf weight blog --stats --since 1h --jsonSERVICE WEIGHT REQUESTS MATCHED 4XX 5XX P50 P95
a 90 (90%) 9120 (91%) - 12 (0%) 3 (0%) 41ms 180ms
b 10 (10%) 893 (9%) 41 2 (0%) 14 (2%) 45ms 420ms
- REQUESTS is what reached the service, with its share of the split. MATCHED counts the requests its
matchsent. - 4XX and 5XX count status codes; the percentage is of the service's requests.
- P50 and P95 are response times, from a histogram: within about 20%.
--sincetakes30m,24hor7d, up to what the container log holds (lf log-limit).- The counts are read on the server with awk; only the sums cross ssh. Traefik must log JSON (
lf traefik access-logturns it on for your own Traefik).
A/B testing
The pieces above make a simple A/B test.
- Deploy two services of the same app on the same domains, with
weight(for example 50 and 50). - Add
sticky: trueto both services, so each visitor stays on one side. - Tell each side which one it is, for example with
environment: { VARIANT: b }, and send that to your analytics. - Give your team
matchto see either side on purpose. - Watch errors and response times with
lf weight <site> --stats. - Pick the winner:
lf weight blog a=0 b=100, then deploy it as the only service.
landfall splits traffic by chance and counts requests. It does not target users or measure conversions. Use a feature flag service or your analytics for that.
Autoscaling
lf autoscale changes a service's count by its CPU use, between a minimum and a maximum.
# run 2 to 8 containers of web, each at about 70% of one core
lf autoscale blog web=2-8
# aim at 50% of one core per container
lf autoscale blog web=2-8 --cpu 50
# show the rules, the counts and the log
lf autoscale blog
# measure once now and show what it did
lf autoscale blog --now
# stop autoscaling: web keeps the count it has
lf autoscale blog web=off- A crontab line runs
/opt/landfall/sites/<site>/autoscale.shevery minute. Nothing else runs on the server. - The script measures the service's containers with
docker stats: three samples, averaged. - It adds containers when they use more than 110% of the target. It adds enough to bring each one back to the target.
- It removes one container when one fewer would still use less than 90% of the target. It waits 5 minutes after a change before it removes one.
- It never goes below the minimum or above the maximum.
- It changes the count with
docker compose up --scale, the waylf scaledoes. The count goes intosite.jsonand the release'scompose.override.yaml. - Deploys, rollbacks and
docker compose up -don the server keep the last count. - It skips a run while a deploy or change of the site runs, and leaves a stopped site stopped.
- Each change goes into
/opt/landfall/sites/<site>/autoscale.log.lf autoscale <site>shows its end. - Turning it on or off, and changing a range, are undoable with
lf undo. The script's own changes are not journaled. lf scalerefuses a service that autoscales. Change its range withlf autoscaleinstead.- The script reads
site.jsonwith jq.lf autoscaleinstalls jq when the server has none. lf autoscale --jsonprints{site, services, cron, log}: each service withmin,max,cpuandreplicas.- On a group of servers,
lf autoscale <site>shows the rules once, each server's count and the total.--jsonreads the primary.-s <server>shows one server's log. See Several servers.
Limits
-
A traffic split,
matchand--statsneed landfall's own Traefik (lf traefik setup).matchneeds Traefik v3. -
lf restart <site> <service>restarts every container of the service at once. -
lf tunnelreaches one container of a service. -
Autoscaling measures only CPU. It scales one server's containers; it adds no servers.
-
An auto-deploy (
lf autodeploy) starts the count it had when its script was written. The next autoscale run corrects it. -
A rolling deploy stops old containers with SIGTERM. Requests still running on them can fail unless the app finishes them first.
retryhides most of these.