Rotation Without Downtime
If rotating a secret needs a maintenance window, it won't happen until there's an incident. Rotation must be a routine, automated, boring operation. The trick that makes it boring is the same everywhere: for a while, two credentials are valid at once.
The two-credential pattern
- Create the new credential while the old one still works (a second database user, a second API key, a new key version).
- Distribute it: update the secrets store; consumers pick it up on their next read, restart or lease renewal.
- Verify that every consumer uses the new one (connection logs, "last used" timestamps on API keys).
- Revoke the old one.
Swapping a single credential in place ("ALTER USER ... PASSWORD") breaks every consumer between the change and its next reload. The model shows both strategies with three linkly replicas that reload their configuration at different times:
RELOAD_AT = {"linkly-0": 2, "linkly-1": 5, "linkly-2": 9} # minute at which each replica re-reads the secret
def simulate(strategy):
valid = {"v1"} # credentials the database accepts
using = {r: "v1" for r in RELOAD_AT}
store = "v1"
failures = []
for minute in range(12):
if minute == 1: # rotation starts
store = "v2"
valid = {"v2"} if strategy == "in-place" else {"v1", "v2"}
if strategy == "two-credential" and minute == 10 and all(u == "v2" for u in using.values()):
valid = {"v2"} # old credential revoked after every consumer moved
for r, t in RELOAD_AT.items():
if minute == t:
using[r] = store
if using[r] not in valid:
failures.append(f"{minute}:{r}")
return failures
for s in ["in-place", "two-credential"]:
f = simulate(s)
print(f"{s:15} failed requests: {len(f):2} {f[:4]}{' ...' if len(f) > 4 else ''}")in-place failed requests: 13 ['1:linkly-0', '1:linkly-1', '1:linkly-2', '2:linkly-1'] ...
two-credential failed requests: 0 []
In-place rotation produced failed requests until the slowest replica reloaded. The two-credential pattern produced none, and revoked the old credential only after the last consumer moved.
How the tools implement it
| Secret | Two-credential mechanism |
|---|---|
| Vault KV v2 | versions: write v2, consumers read "latest"; old versions stay readable until you delete them (KV v2) |
| Vault dynamic database credentials | every consumer gets its own user; rotation is just lease expiry (Dynamic Database Credentials) |
| Vault transit keys | key versions: new data is encrypted with the latest version, old versions still decrypt, rewrap upgrades ciphertext (Transit) |
| AWS Secrets Manager | rotation Lambda with AWSPENDING → AWSCURRENT → AWSPREVIOUS staging labels; the "alternating users" strategy keeps two DB users |
| TLS certificates | issue the new certificate before the old one expires; cert-manager renews at two thirds of the lifetime |
| SOPS data keys | sops rotate re-encrypts with a new data key; recipients change with sops updatekeys (SOPS for Teams) |
| JWT signing keys | publish the new key in the JWKS first, sign with it later, remove the old key after the longest token lifetime |
Consumers must cooperate
Rotation only works when applications re-read secrets:
- read secrets from a file that an agent or the kubelet updates (Kubernetes updates mounted Secret volumes, not environment variables or
subPathmounts), - or watch for changes and reconnect (linkly reopens its database pool when
/run/secrets/dbchanges), - or restart on change (Vault Agent's
execmode, Reloader for Kubernetes, a rolling restart triggered by a checksum annotation).
An application that reads DB_PASSWORD from the environment at startup can only rotate by restarting, so the restart must be part of the rotation runbook.
Test yourself
Why does in-place rotation (changing the only password) cause errors?
- Databases reject new passwords
- Consumers still using the old value fail until they reload, because the old value stopped working immediately
- It doesn't cause errors
- The new password is too long
When is it safe to revoke the old credential in the two-credential pattern?
- Immediately after creating the new one
- After verifying that every consumer has switched to the new credential
- Never
- After one year