Secrets Rotation Strategy for Application Credentials Without Downtime
Quick answer
Quick answer: rotate secrets without downtime by letting two credentials be valid at the same time. Create the new credential, roll it out to every consumer,...
Quick answer: rotate secrets without downtime by letting two credentials be valid at the same time. Create the new credential, roll it out to every consumer, verify it works, and only then revoke the old one. Make applications able to reload secrets without a restart, automate the process, and keep a faster emergency path for suspected leaks.
Why Does Rotation Cause Outages?
Rotation fails when the credential changes in one place before every consumer has the new value. If you change a database password and then update the application, there is a window where the application uses a password that no longer works. With many instances, services, and scheduled jobs, it is easy to miss one, and the failure shows up hours later when that job runs.
Because of this risk, teams often avoid rotating, and long-lived credentials accumulate. The longer a secret lives, the more places it has been copied, and the more likely an old copy has leaked through a laptop, a log file, or a former employee's access. Regular rotation limits that exposure, but only if it is safe enough to do routinely.
The solution is to design rotation as an overlap, not a swap. At no point should the system depend on a single instant when every component changes together. Instead, old and new credentials coexist while the rollout proceeds and then the old one is retired deliberately.
How Does the Dual-Credential Pattern Work?
The pattern works for database users, API keys, signing keys, and service tokens, as long as the receiving system can accept two valid credentials at once. The steps are the same in every case:
- Create a new credential alongside the existing one, with the same permissions and a clear identifier.
- Store it in your secrets manager and distribute it to consumers through your normal deployment path.
- Roll out the consumers gradually and confirm through logs and metrics that they authenticate with the new credential.
- Check that nothing still uses the old credential, using the provider's usage or audit information where available.
- Revoke the old credential and monitor for authentication failures.
When the target system supports only one password per account, such as many database setups, use two accounts that alternate. You rotate by setting a new password on the inactive account, switching consumers to it, and later changing the password of the account that was just retired. Each rotation moves the active role to the other account.
For signing keys, publish the new public key before you start signing with the new private key, and keep the old public key available until all tokens signed with it have expired. That lets verifiers accept both during the transition.
How Should Applications Handle New Secrets?
Applications should be able to pick up a new credential without a full restart. If a secret is read once at startup and stored forever, rotation requires restarting everything. Better options include reading secrets from a mounted file and watching it for changes, fetching them from a secrets manager periodically, or retrying once with a freshly fetched secret when an authentication error occurs.
func getDB() (*sql.DB, error) {
db, err := open(currentSecret())
if err != nil || db.Ping() != nil {
refreshSecret() // fetch latest from the secrets manager
return open(currentSecret()) // retry once with the new value
}
return db, nil
}
Connection pools deserve attention. Existing connections authenticated with the old credential may stay open after rotation, while new ones use the new credential. That is usually fine during the overlap, but make sure the pool eventually recycles connections, through a maximum lifetime, so retiring the old credential does not cut live sessions unexpectedly.
Where possible, prefer short-lived credentials issued on demand, since they expire automatically and reduce the need for manual rotation. Whether your platform supports this depends on the services involved, so check their documentation.
What About Automation and Incidents?
Manual rotation does not survive turnover and busy periods. Automate it with scheduled jobs or built-in features of your secrets management system, and test the process regularly, including the failure path. Keep an inventory of where each secret is used so nothing is forgotten, and log rotation events for audit.
Have a separate emergency procedure for suspected leaks. It follows the same overlap logic but compresses the timeline, rotating immediately, revoking the old credential quickly, and reviewing access logs for misuse. Practicing it in advance makes a real incident much less chaotic. Never place secrets in source control, container images, or logs, since those copies cannot be rotated away.
Frequently Asked Questions
How often should I rotate secrets?
It depends on sensitivity, compliance requirements, and how automated rotation is. Frequent rotation is only practical when it is safe and automatic, so build that first.
Can I rotate a database password without downtime?
Yes, using two accounts that alternate, or a system that supports multiple valid credentials. Roll consumers to the new one, then retire the old one.
Do applications need to restart after rotation?
Not if they reload secrets dynamically. Otherwise, a rolling restart picks up the new value. Reloading avoids brief capacity dips.
What if I think a secret has leaked?
Rotate immediately using your emergency process, revoke the old credential, and review logs for suspicious use. Then find how it leaked and fix that path.
Summary
Rotate with an overlap: create, distribute, verify, then revoke. Build applications that reload secrets, prefer short-lived credentials, automate rotation, and rehearse emergency procedures. Check your secrets manager and service documentation for supported rotation features.