Quick revision
The short answer
The service read the password once at startup and handed it to HikariCP, so after rotation Hikari keeps opening new connections with the old password and fails, while connections opened before rotation keep working until they are retired. A rotation without errors keeps old credentials valid for a while with the alternating users strategy, and makes the application fetch current credentials itself, through the AWS Advanced JDBC Wrapper or the Secrets Manager JDBC driver, or a scheduled reload, with maxLifetime shorter than the time the old password stays valid. IAM database authentication with short-lived tokens is the other route, and either way we test rotation in staging and alert on authentication failures after rotation events.
What to remember
In this answer, we saw that Hikari keeps using the password it was given at startup, so after single-user rotation every replacement connection fails while old ones slowly retire. The alternating users strategy keeps old credentials valid for a full period, a Secrets Manager-aware driver or a scheduled reload lets the application pick up new credentials with maxLifetime as the safety bound, and IAM database authentication removes passwords entirely. Regular rotation tests in staging and alerts after rotation events keep it reliable.
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.
The service read the password once at startup and handed it to HikariCP, so after rotation Hikari keeps opening new connections with the old password and fails, while connections opened before rotation keep working until they are retired. A rotation without errors keeps old credentials valid for a while with the alternating users strategy, and makes the application fetch current credentials itself, through the AWS Advanced JDBC Wrapper or the Secrets Manager JDBC driver, or a scheduled reload, with maxLifetime shorter than the time the old password stays valid. IAM database authentication with short-lived tokens is the other route, and either way we test rotation in staging and alert on authentication failures after rotation events.
Why this question is asked
Turning on automatic rotation is a good security step, and this failure is what often greets the team the next morning: a wave of authentication errors that disappears when someone restarts the pods. It teaches a real lesson about how connection pools and secrets interact, which is why interviewers like it. In this answer, we will recap how Secrets Manager rotation works, replay what happens inside Hikari during rotation, compare the two rotation strategies, look at three ways for the application to pick up new credentials, and see how to test and monitor rotation so it stays boring.
1A Quick Recap of Rotation and the Pool
Secrets Manager rotation is run by a Lambda function on a schedule, for example every 30 days. For a single-user secret it creates a new password, sets it on the database user, and marks the new secret version AWSCURRENT, while the old version becomes AWSPREVIOUS. From that moment the database accepts only the new password for new logins.
HikariCP, the connection pool in Spring Boot, opens connections with the username and password it was given when the DataSource was created. Spring Cloud AWS loaded the password into spring.datasource.password at startup, Spring Boot built the HikariDataSource from it, and nothing ever tells Hikari that the value changed. PostgreSQL does not close existing sessions when a password changes, so connections already open keep working.
2Replaying What Happens During Rotation
Let us follow one pod through a rotation at 02:00. Hikari's maxLifetime is the default 30 minutes, so it retires and replaces every connection within half an hour.
| Time | Pool state | Result |
|---|---|---|
| 01:59 | 10 connections, old password | all requests succeed |
| 02:00 | rotation sets new password in DB | existing sessions unaffected |
| 02:05 | a connection reaches maxLifetime | new login with old password fails |
| 02:30 | most connections retired, none replaced | requests time out waiting for connections |
| 08:00 | pods restarted, new password read | errors stop |
So the errors are not immediate, which makes the cause harder to spot. They start as Hikari retires connections and tries to open replacements, and they grow until the pool is empty and every request waits connectionTimeout for a connection that can never be created. The restart fixes it only because startup reads the secret again.
3Fix 1: Rotate With Alternating Users
Secrets Manager offers two rotation strategies for databases. Single-user rotation changes the password of the one user, which invalidates the old password instantly, as we saw. The alternating users strategy uses two database users with the same privileges, for example orders_app and orders_app_clone. Each rotation changes the password of the user that is not currently in use and then switches the secret to point at it.
The effect is that the credentials the application holds stay valid for a whole rotation period after the switch. A pod that still uses orders_app with its old password keeps logging in successfully until the next rotation, 30 days later, changes that user's password. That gives every pod a long window to pick up the new secret. This strategy needs a separate superuser-style secret that the rotation function uses to manage the two users.
- Secret points to user Aapp logs in as A
- Rotation: new password for user BA is untouched
- Secret now points to user BAWSCURRENT = B, AWSPREVIOUS = A
- Pods still on A keep workinguntil the next rotation
- Pods pick up Breload, reconnect or restart
- Next rotation changes Anobody uses A by then
6 steps in order. Play them, or step through them one at a time.
4Fix 2: Let the Application Fetch Current Credentials
Even with alternating users, the application should pick up new credentials on its own. The cleanest way is a driver that knows about Secrets Manager. The AWS Advanced JDBC Wrapper wraps the PostgreSQL driver and has a Secrets Manager plugin: it reads the secret when opening a connection, caches it, and on an authentication failure fetches the secret again and retries. The older AWS Secrets Manager JDBC driver does the same with a special URL scheme. Either way Hikari no longer holds a password at all.
Property names vary between wrapper versions, so we check the plugin documentation. If we cannot change the driver, a scheduled job can reload the secret and push the new password into the running pool: HikariCP exposes setUsername and setPassword on its HikariConfigMXBean for this, which we verify for our Hikari version. New connections then use the new value, and maxLifetime makes sure old connections are replaced within that time. With alternating users the old password stays valid for a full rotation period, so a maxLifetime of 30 minutes and a reload every few minutes leave a large safety margin.
5Fix 3: No Passwords at All With IAM Authentication
RDS and Aurora PostgreSQL also support IAM database authentication. Instead of a password, the client generates a signed authentication token from its IAM role, valid for 15 minutes, and uses it as the password when opening a connection. The AWS Advanced JDBC Wrapper's IAM plugin generates a fresh token for every new connection, so there is nothing to rotate and nothing stored. The token is only checked at login, so long-lived connections are fine, and maxLifetime keeps re-authentication regular. The costs are a limit on new connections per second that IAM authentication can sustain, so very high connection churn needs care, and a role setup per database user. It works especially well with RDS Proxy.
6Testing Rotation and Watching It
Rotation that runs once a month in production and never anywhere else will break again. We run it in staging on a short schedule, for example daily, with load running, and check that no request fails. In production we alert on database authentication failures, from the PostgreSQL log or from application errors containing password authentication failed, in the hour after each rotation event, which Secrets Manager reports through EventBridge and CloudTrail. A failed rotation is then a page, not a morning surprise.
7Follow-up questions
What does Hikari maxLifetime have to do with credential rotation?
maxLifetime is the longest a connection may live before Hikari closes it and opens a new one, 30 minutes by default. During rotation it decides two things. It sets how soon a pool that has learned the new credentials stops using sessions opened with the old ones. And it sets when the pool must open new connections, which is the moment the credentials it holds are tested. The rule is that maxLifetime, plus the time it takes the application to pick up the new secret, must be shorter than the period during which the old credentials remain valid. With alternating users that period is a whole rotation interval; with single-user rotation it is zero, which is why that strategy breaks pools.
Why do existing connections keep working after the password changes?
PostgreSQL checks the password only during login. An established session is already authenticated, so changing the user's password does not end it. That is why the failures only start as Hikari replaces connections.
Is restarting pods after each rotation an acceptable fix?
It works but it is fragile: it needs coordination with the rotation schedule, causes a burst of reconnections, and fails if a pod misses the restart. Self-refreshing credentials remove the coupling.
8Conclusion
In this answer, we saw that Hikari keeps using the password it was given at startup, so after single-user rotation every replacement connection fails while old ones slowly retire. The alternating users strategy keeps old credentials valid for a full period, a Secrets Manager-aware driver or a scheduled reload lets the application pick up new credentials with maxLifetime as the safety bound, and IAM database authentication removes passwords entirely. Regular rotation tests in staging and alerts after rotation events keep it reliable.
Open with
The application read the password once at startup and Hikari keeps it. Rotation changes the password in the database, and PostgreSQL leaves existing sessions alone, so nothing fails at first.
Then, if they want more
But as Hikari retires connections at maxLifetime and opens new ones, each login uses the old password and fails, until the pool is empty and requests time out. Restarting works only because startup reads the secret again. To rotate without errors I would do two things.
First, use the alternating users strategy: two database users, and each rotation changes the password of the one not in use and then switches the secret to it. The credentials a pod holds stay valid for a full rotation period, so there is time to pick up the new ones. Second, make the application refresh credentials itself.
The cleanest way is the AWS Advanced JDBC Wrapper with its Secrets Manager plugin, or the Secrets Manager JDBC driver, which fetch the current secret when connecting and refetch on an authentication failure.
Without a driver change, a scheduled job can reload the secret and set the new password through Hikari's config MXBean, and maxLifetime must be shorter than the time the old password remains valid.
An alternative is IAM database authentication, where the driver plugin generates a 15-minute token from the role for each new connection, so there is no password to rotate. Finally I would run rotation daily in staging under load, and alert on authentication failures in the hour after each production rotation.
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
- AWS documentation
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 "A Spring Boot service with a HikariCP pool reads its PostgreSQL password from AWS...": https://scrivio-interview-library.netlify.app/spring-boot/aws/ce01c9cb92/