Prompt
How do I rotate database passwords without downtime?
Latest observation
To rotate database passwords without downtime, the key idea is: allow both the old and new credentials to work during a transition period, then gradually move traffic over, and only revoke the old password after all clients have switched.
Here’s the general pattern.
1) Know what kind of auth your database supports
Different databases handle rotation differently:
- PostgreSQL / MySQL: usually one password per user, so true overlap is limited unless you use multiple users, proxy/auth layer, or dual-auth capability via your app infrastructure.
- MongoDB: similar user/password model, often handled with staged credential updates.
- Cloud-managed DBs: may support easier secret rotation via IAM, tokens, or proxies.
- Kerberos / IAM-based auth: can rotate more cleanly than static passwords.
2) Use a “dual credential” strategy
If the DB itself only supports one password per user, use one of these approaches:
Option A: Two DB users with equivalent permissions
- Create a second user, e.g.
app_user_v2. - Grant the same permissions as the current user.
- Update your application to support both credentials:
- try new credential first, or
- maintain two connection configs temporarily.
- Roll out the app change.
- Once all instances are using the new user, remove the old user.
This is the most common no-downtime approach.
Option B: Connection pool / proxy layer
If you have a proxy like:
- PgBouncer
- RDS Proxy
- ProxySQL
- Cloud SQL connector/proxy
you can rotate the upstream credentials in the proxy while clients keep connecting to the proxy. This reduces blast radius and can make rotation seamless.
Option C: Token-based or ephemeral auth
Where possible, use:
- IAM auth
- short-lived tokens
- certificate-based auth
These are much easier to rotate safely than long-lived passwords.
3) Roll out the new secret before revoking the old one
The safe sequence is:
- Generate new password or create new DB user
- Store it in your secret manager
(AWS Secrets Manager, GCP Secret Manager, Vault, Azure Key Vault, etc.) - Deploy app/config changes so new instances use the new credential
- Reload credentials gracefully in running services
- restart pods one by one
- use config reload if supported
- drain old connections if needed
- Wait until old connections drain
- Disable/revoke old credential
4) Make applications reload secrets safely
A common failure mode is rotating the secret but keeping long-lived app processes using the old value.
To avoid that:
- keep DB connections short-lived if possible
- use connection pools with controlled refresh
- support runtime secret reload
- do rolling restarts instead of big-bang restarts
- set a reasonable connection TTL
5) Watch out for cached connections
Even after changing passwords, existing DB connections may stay valid until disconnected. That’s useful for no downtime, but it also means:
- apps may continue using the old password until they reconnect
- old connections can hide failed rotation until later
Mitigations:
- rotate during low traffic
- reduce pool max lifetime
- restart instances gradually
- verify new logins are working before revoking old ones
6) Test the new credential before deprecating the old one
Before cutting over:
- confirm a fresh connection works with the new secret
- check permissions are identical
- verify all app paths work
- monitor auth failures and connection errors
7) Practical rotation recipe
A solid generic workflow:
- Create new DB user/password or update secret.
- Add new credential to secret manager.
- Deploy application version that can read the new secret.
- Roll out instances gradually.
- Confirm traffic is using new connections.
- Keep old credential valid for a grace period.
- Disable old credential after confidence window passes.
8) If you need true zero-downtime, design for it
The best long-term setup is:
- use a secret manager
- use multiple app credentials or a proxy
- support dynamic secret reload
- avoid embedding passwords in configs/images
- use short-lived authentication where possible
Example pattern for Kubernetes
If your app runs in Kubernetes:
- store DB creds in Secret Manager
- sync them to a Kubernetes Secret
- mount secret as env var or file
- deploy app with rolling updates
- set readiness probes so pods only receive traffic once they can connect
- keep old and new credentials valid during rollout
- revoke old credential afterward
Example pattern for PostgreSQL
Since PostgreSQL doesn’t support two passwords on one role:
- create
app_user_v2 - grant same privileges as
app_user - add both credentials temporarily in app or proxy
- roll pods to use
app_user_v2 - verify all connections migrated
- drop
app_useror change its password later
If you want, I can give you:
- a step-by-step runbook,
- a PostgreSQL-specific rotation plan, or
- an AWS/GCP/Azure-managed DB example.