writing

Four copies at lunch, one at 3 a.m.

Until this week a Gagarin service ran as one copy, so a lost node meant a restart. Now a service can run up to five, either a fixed number or a range that follows CPU. You pay for each copy by the minute, and only while it runs. This post covers how the range decides, what it costs, and the three things in your code that a second copy will break.

Until this week every service on Gagarin ran as exactly one container. When the node under it went away, the service restarted on another node. Your users saw that as an outage. A burst of traffic got the size you picked and nothing more. To get more CPU you had to resize, and you paid the bigger rate around the clock, including the 3 a.m. hours when the service handles three requests a minute.

Now you can ask for more copies:

sh
gg deploy shop/api:8080 api:v4 --replicas 3     # always three
gg deploy shop/api:8080 api:v4 --replicas 1-4   # one to four, by CPU

gg ship takes the same flag, and so does the MCP server's deploy tool, so you can also just tell your agent "run the api as 1 to 4 copies". A service can run up to five copies.

How a range decides

A range adds an autoscaler that watches the average CPU of your copies. It holds that average at 70% of one copy's CPU limit: 350m at S, 700m at M and 1.4 CPU at L. When the average goes above that, the autoscaler adds a copy right away. When the service needs fewer copies, it waits five minutes before it removes one, so a short quiet spell doesn't drop a copy that the next burst would need back.

We measure against the limit and not the request on purpose. An S requests 100m of CPU and can use up to 500m. Most autoscalers measure "70% utilisation" against the request, which at S comes to 70m. That would add a copy whenever an S service did anything at all. Measured against the limit, the threshold means the same thing at every size, so we can state it in one sentence.

Why 70%? It is low enough that a second copy starts before the first one is pinned at its limit. It isn't lower because you pay for every copy.

What it costs

Each copy that runs is billed by the minute at its size's rate. Two copies of an M cost exactly twice what one costs. A range bills its minimum all the time, and each extra copy only for the minutes it runs.

"Runs" has a narrow meaning. Billing starts when a copy's container starts. A copy that waits for a node, or is still pulling its image, costs you nothing. During a deploy, the rollout briefly starts one extra copy, and during a scale-down old copies take a moment to stop. Neither is billed, because the platform did that, not you. A meter counts running copies every minute, and it never counts more than your range allows.

Here is an API at size M with --replicas 1-4. Say it runs four copies for three busy hours a day, two copies for six hours, and one copy for the other fifteen:

copiesper month
fixed, --replicas 11 all day$30.66
fixed, --replicas 44 all day$122.64
range, --replicas 1-44 / 2 / 1 over the dayabout $50

The range handles the lunch peak that one copy couldn't, and costs you 40% of what four fixed copies would. Your own traffic will be shaped differently, so check the console's Metrics tab for your service's CPU over the last 30 days before you pick a range.

What a second copy buys besides speed

Once a service can run more than one copy, the scheduler tries to put the copies on different nodes. That is a preference, not a rule. On a quiet day, when the pool has a single node, both copies run on it and the platform doesn't buy a machine just to separate them.

Once a service always runs at least two copies, it also gets a disruption budget. When we drain a node for an upgrade, its copies leave one at a time, and the service never drops to zero.

So if you want failover, the number that matters is the minimum. 1-5 scales under load, but at 3 a.m. it is one copy on one node, so losing that node is a restart, the same as before. 2-5 costs a second copy all the time, and in return it survives losing a node.

Three things a second copy breaks

The platform treats copies as interchangeable. Requests are spread across them, and nothing sends the same user back to the same copy. Code that was written for a single instance can break in a few ways:

  1. State in memory. Sessions in a Map, a rate limiter that counts in a variable, or a cache that you write to and read back on the next request now hold only a third of their data, depending on which copy a request lands on. Move that state to valkey or postgres.
  2. A scheduler inside the process. A node-cron, an APScheduler or a time.Ticker loop that sends the daily digest now sends it once per copy. Run it as a Gagarin job, or take a lock in the database before the work.
  3. Files on local disk. Each copy has its own filesystem, so an upload written to one copy can't be read from the others. Put the file in an s3 bucket. A service with a volume runs one copy anyway, because a disk can be mounted in only one place, and Gagarin refuses --replicas 3 together with --volume.

What stays single

Resources stay at one copy: postgres, qdrant, valkey and the rest. So do jobs and any service with a volume. Copies help only with services that hold no state of their own, which is also why they were the simplest place to start.

This affects one shared resource in particular. Five copies of your API all connect to one postgres, and every copy opens its own connection pool. The next post works through that arithmetic: Five copies, sixty connections.

Things worth knowing

  • A deploy without --replicas keeps the range the service already has, the same way it keeps the size. A redeploy can't drop three copies to one just because the flag wasn't repeated.
  • A project you stop and then resume starts at its minimum.
  • gg status adds a REPLICAS column once anything in the project can run more than one copy. READY shows how many copies are up right now, and the console shows the same as ready/desired.
  • A recipe stores its services' ranges, so whoever installs it gets the same ranges. The recipe's price is worked out at each range's minimum, with a note that copies added under load are billed by the minute.

Run gg upgrade to get the flag. Then pick the service that struggles at lunchtime and give it a range.