Docs/Servers/Replicas and routing

Replicas and routing

4 min read
On this page

You can run one environment on more than one of your own servers. Dokwe deploys each release to every replica and splits incoming traffic between them.

Replicas use servers you've connected with bring your own server. Each replica is one environment running on one server.

How replicas work#

Every environment with replicas has one primary and any number of replicas:

  • The primary is the server you picked as the environment's deployment target. There is always exactly one primary, and you can't remove it.
  • A replica is any other connected server you add. A server can host a given environment only once.

Each replica has a status of healthy, degraded or down. It also has a region tag, a traffic weight, and a switch that controls whether it gets traffic.

Add a replica#

  1. Set a deployment target#

    Open the environment and choose a deployment target first. This becomes the primary. Until you do, the High Availability section reads "The primary must be set before you can add replicas."

  2. Add servers#

    In High Availability, click + Add replica and pick a server. You can only add a server once it has sent a heartbeat. Servers still waiting for their first heartbeat are rejected.

  3. Deploy#

    Adding a replica doesn't deploy anything yet. Your next deploy will include the new server. The dashboard confirms this with "Replica added. Next deploy will fan out."

You can also choose replica servers for staging and production when you create a project. Replicas use the same host port as the environment.

How deploys reach replicas#

Deploys go out one server at a time, not all at once:

  1. The primary deploys first.
  2. Once the primary is live, Dokwe deploys to each enabled replica in turn. Each one builds from source on its own server, using the same commit, environment variables and domains.
  3. Each replica runs its own health check before it reports success.

Each replica has 5 minutes to finish. If it doesn't, Dokwe marks it failed. A failed replica doesn't stop the rollout, so the remaining replicas still deploy. Dokwe doesn't roll back the replicas that already succeeded. If you need to, roll back on purpose with rollbacks.

Replicas you've drained (switched out of the pool) are skipped during deploys.

Because replicas deploy one at a time, a broken build usually shows up on the primary first. Watch the primary's logs before the rest of the rollout finishes.

Health from heartbeats#

Your servers send Dokwe a heartbeat every 15 seconds. If a server misses heartbeats for 45 seconds, Dokwe marks it offline. Every 15 seconds, Dokwe copies each server's state to its replicas:

Server statusReplica status
Onlinehealthy
Pending (no heartbeat yet)degraded
Offline or errordown

So a replica can take up to about 15 seconds after its server goes offline to show down.

Region tags#

Dokwe tags each replica with a region, based on its server's public IP. It looks the IP up in MaxMind's GeoLite2-Country database. The tag is set when you add the replica. A background job then fills in any tags still missing.

Today a region is a continent code such as NA, EU or AS. If the address can't be located, the region stays empty.

Load balancing#

Dokwe sends each environment's domains to all of its replicas that are both enabled and have a host port. Requests are spread across them like this:

  • Weighted round robin. Every replica has a weight from 1 to 100, with a default of 1. A replica with a higher weight gets a larger share of requests. Change it with the Weight slider.
  • Sticky sessions. Sticky sessions are always on. The first response sets an HTTP-only cookie named fd_lb_<project-slug>, so a visitor keeps going to the same replica. This helps apps that keep session state in memory.
  • Drain. Switch a replica from IN POOL to DRAINED to stop sending it traffic without removing it.

Replicas marked down (their server stopped sending heartbeats) are taken out of rotation automatically, and routing is refreshed as soon as a replica's status changes. If every replica is down, Dokwe keeps routing to all of them rather than serving an error.

The load balancer doesn't run its own HTTP health checks: a replica leaves rotation when its server's agent goes quiet, not when your app returns errors. To take a healthy server out of traffic, drain its replica.

Location-aware routing, which sends each visitor to their nearest healthy replica, is coming. For now, traffic is shared by weight across all enabled replicas, whatever the region tags say.

Visitor location headers#

Requests that reach your app through Dokwe's router include the visitor's location in these headers:

HeaderExample
X-Country-CodeUS
X-Country-NameUnited States
X-Continent-CodeNA

Dokwe takes the visitor's IP from X-Forwarded-For, then X-Real-IP, then the connection address. A failed lookup never blocks a request. If the IP can't be found, the headers are left out, so your code should handle them being missing.

js
// Express example
app.get('/', (req, res) => {
  const country = req.get('X-Country-Code') ?? 'unknown';
  res.send(`Hello from ${country}`);
});

Native deploys that bind ports 80 and 443 themselves through services.web.domain in dokwe.yml bypass the router, so they don't get these headers.

Remove a replica#

Use the remove action on the replica's row. You can remove any replica except the primary. To move the primary, change the environment's deployment target.