How to Rotate Environment Variables Without Breaking Production
Rotating a secret is simple. Doing it without an outage takes a plan.
Most teams rotate secrets only after something goes wrong: a laptop is lost, a contractor leaves, or a key shows up in a public repo. That is the wrong time to find out which services read DATABASE_URL and which ones have it cached.
This guide walks through a rotation that keeps production running.
Start with an inventory
Before you change anything, list where each variable is used:
- Which services read it (API, workers, cron jobs, edge functions)
- Which environments hold it (development, staging, production)
- Which external systems trust it (third-party webhooks, OAuth callbacks, payment keys)
If you cannot answer those questions in five minutes, you are not ready to rotate. Write the list down first.
Use the two-key pattern
Most secrets can run with two valid values for a short window. The pattern looks like this:
- Create the new credential in the provider (a new database user, a new API key).
- Add the new value to your environment variable store alongside the old one.
- Deploy services so they accept both values.
- Switch clients to the new value.
- Confirm nothing reads the old value.
- Revoke the old credential.
The overlap window is what prevents downtime. Keep it as short as your deploy pipeline allows, but do not skip it.
Roll out by environment
Rotate in order: development, staging, then production. Staging is where you find the service that hard-codes a value or caches it at boot.
For each environment, check:
- The service restarts or reloads and picks up the new value
- Health checks pass
- Error rates stay flat for at least one full traffic cycle
Keep the change in one place
The most common rotation failure is a value that changed in one spot and not another. A .env file on a teammate's machine, a CI secret, and a production dashboard can all hold different copies.
Store each variable once, per project and environment, and let every consumer pull from that source. When you rotate, you change one record. In Envly, that means updating the variable for the right environment and letting your services pull the new value with the CLI.
After the rotation
- Revoke the old credential and confirm the revocation in the provider's audit log.
- Remove the old value from any local
.envfiles and CI variables. - Record the date and reason. The next rotation will be faster.
Rotation is routine work when the variables live in one encrypted place and the steps are written down. Practice it on staging before you need it in a hurry.
