Four cutover stages: old only, mirror writes to new, new first with old as fallback, new only

You want to move a bucket's traffic to a new S3 endpoint and nobody should notice. Copying the files is the easy part (see moving a bucket with rclone). The hard part is the hours while apps keep writing.

One nginx in front of both endpoints can do it with four small config files. We ran it with a client writing and reading the whole time, through every switch, and counted what broke. Short version: nothing failed, but two things quietly went wrong, and both are easy to fix if you know to look.

The four stages

StageReads go toWrites go tonginx pieces
1. Beforeoldoldproxy_pass
2. Mirroroldold, and a copy to newmirror, mirror_request_body on
3. Flipnew, old if new says 404new, and a copy to oldproxy_intercept_errors on, error_page 404 = @old
4. Donenewnewproxy_pass

Stage 3 keeps a copy of every write going back to old, so you can flip back without losing anything. Each stage is one file and a graceful <code>nginx -s reload</code>, which does not drop open connections.

The config

text
# stage 2: reads and writes to old, every write copied to new
location / {
  mirror /_mirror;
  mirror_request_body on;
  proxy_set_header Host $http_host;
  proxy_pass http://OLD;
}
location = /_mirror {
  internal;
  if ($request_method !~ ^(PUT|POST|DELETE)$) { return 204; }
  proxy_set_header Host $http_host;
  proxy_pass http://NEW$request_uri;
}

# stage 3: new first, old as fallback, writes copied back to old
location / {
  mirror /_mirror;
  mirror_request_body on;
  proxy_set_header Host $http_host;
  proxy_intercept_errors on;
  error_page 404 = @old;
  proxy_pass http://NEW;
}
location @old { proxy_set_header Host $http_host; proxy_pass http://OLD; }
# /_mirror in stage 3 points at OLD
  • The if inside /_mirror keeps reads from being mirrored: GETs return 204 at once, only writes go on.
  • $request_uri carries the query string, which matters for multipart parts (see below).
  • Host must pass through untouched. It is part of the SigV4 signature, so a proxy that rewrites it gets SignatureDoesNotMatch.

The test

  • Two S3 gateways (rclone 1.76.0 serve s3) on one Mac, behind nginx 1.31 on loopback. Both accept the same access key, because a proxy cannot re-sign a request.
  • 2,000 objects (1 to 64 KB) created on old first. Then 4 client workers (boto3, retries off, so one failure is one failure) ran for the whole test: write a new object, read back two random older ones, check the SHA-256.
  • Switches between stages happened while the workers ran. Between stages we copied the backlog with rclone copy old new --ignore-existing.

Result: zero failed requests

StageWritesReadsErrorsWrong contentPUT median / p95GET median / p95
1 old only1,0202,040002.7 / 6.1 ms1.5 / 3.0 ms
2 mirror on1,1252,250003.1 / 8.0 ms1.7 / 3.8 ms
2 new gateway down 6 s8471,694003.1 / 7.5 ms1.7 / 3.9 ms
2 mirror on again341682003.7 / 9.3 ms1.9 / 4.3 ms
3 new first, old fallback1,2102,420003.1 / 8.6 ms1.8 / 4.3 ms
4 new only9501,900003.4 / 8.4 ms1.9 / 3.7 ms

5,493 live writes plus the 2,000 starting objects, 10,966 reads, 0 errors and 0 hash mismatches. At the end all 7,493 acknowledged objects were on new with the right content. The mirror added about 0.4 ms to the median PUT on loopback; a real network will differ, so measure on your own link.

Trap 1: the mirror is best effort

nginx ignores the response of a mirrored request. If new is down or errors, the client still gets a 200 from old and never hears about it. We stopped the new gateway for 6 seconds in stage 2:

MomentObjects on oldObjects on newOnly on old
Before the outage (after the first backfill)3,9933,9930
After the 6 s outage5,3064,584722
After rclone copy --ignore-existing5,3305,3300

The client saw no error for any of the 721 small writes in that window. That is why a compare-and-copy pass before every flip is not optional. Run it, and run it again right before stage 4: <code>rclone check old: new:</code> lists what differs.

Trap 2: multipart uploads do not mirror

We uploaded a 20 MiB file with boto3 in 8 MiB parts through the stage 2 proxy. It landed on old. It never appeared on new. (It is one of the 722.) Each gateway hands out its own UploadId when the upload starts, so the mirrored UploadPart and CompleteMultipartUpload calls name an ID that new has never heard of, and nginx throws away the error.

  • Large files need the compare-and-copy pass, or a short freeze on big uploads during stage 2.
  • Plan on every object over your SDK's multipart threshold (8 MiB in boto3) being a candidate for that pass.

The fallback: what a read costs

In stage 3 we deleted 40 objects from new on purpose, then read them. All 40 came back correct from old. A missing key is a different cost: the log line for a key that exists nowhere shows two upstream attempts, <code>new/404</code> then <code>old/404</code>, and the client gets 404.

text
GET /bkt/absent/key 404 up=127.0.0.1:18482 : 127.0.0.1:18481/404 : 404

Apps that probe for keys that often do not exist pay for two requests each until you drop the fallback.

Trap 3: dropping the fallback too early

In a first run we went to stage 4 without the last check. The 40 objects we had deleted from new were still missing. The workers kept reading random keys, and <b>14 of 1,871 reads</b> in stage 4 picked one of those 40 and got NoSuchKey. A second run with the check and one more <code>rclone copy</code> before stage 4 had 0 errors. In real life the 40 are whatever the mirror dropped.

Order of work

  1. Start stage 2 (mirror on) and watch the old and new object counts.
  2. Backfill the history with rclone copy. Compare. Repeat until only in-flight objects differ.
  3. Flip to stage 3. Keep writes mirrored back to old for as long as you want a way back.
  4. Run the compare again, copy what is missing, and only then move to stage 4.
  5. Last: point the clients' DNS or endpoint setting straight at new, and retire the proxy and old.

Where this does not work

  • Different credentials. The same signed request goes to both sides. If old and new use different access keys or hostnames in the signature, new will refuse the copy. A signing proxy or dual-write in the app is the way then. We did not test either.
  • Lua routing. OpenResty was not installed here, so nothing in this article needs it; we did not test Lua-based routing.
  • Real network and real providers. The lab is loopback with a simple S3 server. Numbers about latency are for comparison between stages only.

Keep an eye on retries under 503 SlowDown while a mirror doubles your write requests for a while.

S3-compatible buckets with scoped access keys and no egress fees. Copy a bucket across, then cut over.

Explore tport.space

Test record and sources