Quick revision
The short answer
We rotate secrets regularly because rotation limits how long a leaked secret stays useful: a password that changes every 30 days gives an attacker at most 30 days, while one that never changes gives them forever. Rotation causes outages when the old secret is revoked before every consumer has switched to the new one, often because applications read secrets only at startup and keep the old value until they restart. The cure is to support two valid secrets during a transition, reload secrets at runtime where possible, and rehearse rotation in lower environments as part of normal release practice.
What to remember
In this answer, we saw that rotation limits how long a leaked secret is useful, and that it causes outages when the old secret is revoked before every consumer uses the new one, especially when applications read secrets only at startup. We fixed it with two valid secrets during a transition, runtime reload of secrets where possible or rolling restarts otherwise, and regular rotation rehearsals in lower environments so the procedure is routine.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We rotate secrets regularly because rotation limits how long a leaked secret stays useful: a password that changes every 30 days gives an attacker at most 30 days, while one that never changes gives them forever. Rotation causes outages when the old secret is revoked before every consumer has switched to the new one, often because applications read secrets only at startup and keep the old value until they restart. The cure is to support two valid secrets during a transition, reload secrets at runtime where possible, and rehearse rotation in lower environments as part of normal release practice.
Why this question is asked
Almost every team agrees that secrets should be rotated, and almost every team has a story about the rotation that took production down. That tension is why many secrets end up never rotated at all, which is the worst outcome. Interviewers ask this question to see whether we understand both sides: why rotation matters, and the mechanics that make it break. In this answer, we will recap what rotation means, see why it limits damage, reproduce the classic outage, and then go through the patterns that make rotation safe: overlapping validity, runtime reload and regular rehearsal.
1A Quick Recap of Secret Rotation
A secret is any value that grants access: a database password, an API key, an OAuth client secret, a signing key. Rotation means replacing it with a new value and then revoking the old one so it stops working. The two halves matter equally. Creating a new secret without revoking the old one changes nothing for an attacker who already holds the old value.
Rotation happens on a schedule (for example every 90 days for API keys) and immediately when a leak is suspected. The scheduled kind is what keeps the emergency kind from being a disaster, because a team that rotates every month knows exactly how to do it on the day it matters.
2Why Rotation Limits the Damage
Secrets leak in many quiet ways: a log line, a laptop backup, a copy in a CI variable, an employee who leaves with a note of a password. We rarely know when a leak happens. Rotation puts a ceiling on the damage from leaks we never detect: whatever was copied stops working at the next rotation.
So the rotation period is a risk choice. A secret that grants access to customer data and changes every 30 days limits an unknown leak to 30 days of exposure; one unchanged for five years may have been in an attacker's hands for most of them. Short-lived dynamic credentials take this to its conclusion, with lifetimes of hours.
3Reproducing the Classic Outage
Here is the usual failure. An operator changes the database password: the new password is written to the secret store, and the old one is removed from the database at the same moment. The service read the password once, at startup, into its DataSource. Existing pooled connections keep working, but whenever the pool opens a new connection, it uses the old password and fails.
6 messages in order. Play them, or step through them one at a time.
The consumer was never told, and the old value stopped working before it switched.
Notice that the outage does not start at the moment of rotation, which makes it confusing to diagnose. It starts when the pool replaces connections, perhaps 30 minutes later, or at the next deploy, or when traffic spikes and the pool grows. The same pattern happens with API keys revoked at a provider while the calling service still has the old key in its configuration.
4Fix 1: Two Valid Secrets During the Transition
The safe pattern is overlapping validity. Instead of replacing a secret in one step, we go through phases: create the new secret while the old one still works, move every consumer to the new one, verify that nothing uses the old one, and only then revoke it. Many systems support this directly: AWS IAM allows two access keys per user, most API providers allow a primary and a secondary key, and for databases we can rotate between two users (orders_a and orders_b), so the password of the user not currently in use is the one we change.
For signing keys the same idea appears as key ids: the token issuer starts signing with the new key while verifiers accept both, published together in a JWKS document, until tokens signed with the old key have expired.
- Create new secretold one still valid
- Publish to secret storeconsumers can now fetch it
- Consumers switchreload at runtime or rolling restart
- Any use of old secret?YesWait, find and fix the consumerNoRevoke old secret
- Old secret revokedrotation complete, record the date
5 steps in order. Play them, or step through them one at a time.
5Fix 2: Reload Secrets at Runtime
The other half of the problem is applications that only read secrets at startup. Where possible, the application should pick up a new value without a restart. Spring Cloud Vault can renew and rotate leases, Spring Cloud's @RefreshScope beans can be rebuilt when configuration changes, Kubernetes updates mounted secret files (not environment variables) in running pods after a short delay, and a small listener can update a Hikari pool's credentials through its MXBean and evict old connections.
When runtime reload is not practical, a rolling restart after the new secret is published achieves the same result, as long as the old secret stays valid until the restart finishes. The important thing is that the rotation procedure includes this step explicitly instead of assuming it happens by itself.
6Fix 3: Rehearse Rotation Before Production Needs It
Rotation that is done once a year is an emergency procedure every time. We make it routine by running it in lower environments as part of normal releases, for example rotating staging credentials automatically every week, so any service that cannot cope fails in staging, where it costs nothing. The rotation runbook or automation is then tested code, and the day a production key leaks, rotating it is a ten-minute job instead of a war room.
7Follow-up questions
Why do environment variables make rotation harder?
A process's environment variables are fixed when it starts and cannot be updated from outside, so a new value always needs a restart. Mounted files or a secret store client can deliver a new value to a running process.
Is it enough to rotate on a schedule?
No. We also rotate immediately when a leak is suspected, when someone with access leaves, or when a vendor reports a breach. The scheduled rotation is what makes the immediate one safe and fast.
8Conclusion
In this answer, we saw that rotation limits how long a leaked secret is useful, and that it causes outages when the old secret is revoked before every consumer uses the new one, especially when applications read secrets only at startup. We fixed it with two valid secrets during a transition, runtime reload of secrets where possible or rolling restarts otherwise, and regular rotation rehearsals in lower environments so the procedure is routine.
Open with
Rotation matters because most leaks are never detected. A password copied from a log or a laptop works until it changes, so rotating every 30 days limits an unknown leak to 30 days, and dynamic credentials limit it to hours.
Then, if they want more
Rotation also only counts when the old value is revoked. The reason it causes outages is ordering. The classic case is an operator changing the database password and invalidating the old one immediately, while the service read the password at startup and keeps it in its DataSource.
Existing connections keep working, so nothing breaks at first, but when the pool replaces connections half an hour later it uses the old password and every request starts failing, which is confusing to diagnose.
So I design rotation with overlapping validity: create the new secret while the old one still works, switch all consumers, check that nothing uses the old one, and only then revoke it. Many systems support two keys at once, and for databases we can alternate between two users.
On the application side, I prefer reading secrets at runtime, through Spring Cloud Vault, refreshable beans or mounted secret files, which Kubernetes updates in place, unlike environment variables. Where that is not possible, the procedure includes a rolling restart before revocation.
Finally, I rehearse it: rotating staging credentials automatically every week means a service that cannot cope fails in staging, and an emergency rotation in production becomes a routine ten-minute job.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Security reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Why should applications rotate secrets regularly, and why does rotation often cause...": https://scrivio-interview-library.netlify.app/spring-boot/secrets-and-crypto/5f8f2e464c/