Scrivio Interview library
ScenarioFoundations7 min read · updated 12 November 2026

How do you rotate the database password of a running Spring Boot service with HikariCP without downtime?

We make sure two valid credentials exist during the change: create the new password or a second database user, roll it out to the service, and only revoke the old one once no connection uses it. HikariCP keeps its existing connections, which stay logged in, and uses the new password only for connections it opens later, so we either restart pods gradually or update the pool at runtime and let connections recycle, with maxLifetime shorter than the credential's lifetime. The password always comes from a secret store at start-up, never from the image or Git.

Why this question is asked

Rotating a database password sounds trivial until we try it on a service that handles traffic all day. Change the password in the database first and every new connection fails; change it in the service first and the same happens in the other direction. Interviewers ask this scenario because the answer shows whether we understand how a connection pool holds connections and how to overlap old and new credentials safely. In this answer, we will recap how HikariCP uses credentials, walk through the two-credential rotation for PostgreSQL and MySQL, see how the pool picks up the new password, look at dynamic credentials with Spring Cloud Vault, and finish with maxLifetime and where the password should live.

1A Quick Recap of How HikariCP Uses the Password

HikariCP is the connection pool Spring Boot uses by default. It opens a set of physical database connections, keeps them open, and lends them to the application for each transaction. The username and password are used only at the moment a physical connection is opened, when the database authenticates it. After that, the connection stays logged in for as long as it lives.

So two facts shape the whole rotation. Changing a password in the database does not disconnect sessions that are already open, in both PostgreSQL and MySQL. And HikariCP opens new connections continuously, whenever one reaches its maxLifetime, which is 30 minutes by default, or fails, so at any time the pool may need to log in again with whatever credentials it has.

2Why Changing One Side First Breaks Things

If we run ALTER ROLE app_rw PASSWORD 'new' first, existing connections keep working, but within minutes HikariCP retires old connections and tries to open new ones with the old password, which now fails. If we deploy the new password to the service first, its new connections fail because the database still expects the old one. Either way we get a slowly growing error rate. The fix is an overlap: for a while, both the old and the new credential must be accepted.

3Overlapping Credentials in PostgreSQL and MySQL

PostgreSQL allows only one password per role, so the overlap uses two login roles that are members of the same group role holding the privileges. We rotate by alternating between them. MySQL 8 has dual passwords built in: an account can keep its current password as a secondary one while a new primary is set.

SQL
-- PostgreSQL: two logins, one set of privileges
CREATE ROLE app_rw NOLOGIN; -- holds the grants
CREATE ROLE app_rw_a LOGIN PASSWORD 'old' IN ROLE app_rw;
CREATE ROLE app_rw_b LOGIN PASSWORD 'new' IN ROLE app_rw; -- step 1: prepare b
-- step 2: deploy the service with app_rw_b; step 3 when nothing uses a:
ALTER ROLE app_rw_a PASSWORD NULL; -- a can no longer log in
-- MySQL 8: dual passwords on one account
ALTER USER 'orders_svc'@'10.20.%' IDENTIFIED BY 'new' RETAIN CURRENT PASSWORD; -- both work
-- deploy the new password, wait until no session uses the old one, then:
ALTER USER 'orders_svc'@'10.20.%' DISCARD OLD PASSWORD; -- only new works
Step throughRotating without downtime
  1. Create new credentialsecond login or MySQL RETAIN CURRENT PASSWORD
  2. Store it in the secret storethe old one stays valid
  3. Roll it out to the servicegradual restart or runtime pool update
  4. Old login still in use?pg_stat_activity or processlist
    YesWait for recycling, check againNoRevoke old credential

4 steps in order. Play them, or step through them one at a time.

Both credentials are valid during the whole rollout.

Notice the order in both cases: add the new credential, move the service, remove the old credential. Between step 1 and step 3, any connection the pool opens succeeds, whichever credential it uses. Before step 3 we check that no sessions still use the old login, with pg_stat_activity in PostgreSQL by usename, or the processlist in MySQL. Because privileges sit on the group role in PostgreSQL, both logins always have identical rights.

4Getting the New Password Into the Pool

The simplest way is a rolling restart: Kubernetes replaces pods one by one, each new pod reads the new password at start-up and creates a fresh pool, while the remaining pods keep serving with the old credential that is still valid. With enough replicas and a readiness probe, users see nothing.

Without a restart, HikariCP can update the credentials of a running pool through its configuration MXBean, and then we ask it to retire the existing connections gently. Soft eviction marks connections to be closed when they are next returned to the pool, so no transaction is interrupted.

Java
HikariDataSource ds = (HikariDataSource) dataSource;
ds.getHikariConfigMXBean().setUsername("app_rw_b"); // used for connections opened from now on
ds.getHikariConfigMXBean().setPassword(newPassword);
ds.getHikariPoolMXBean().softEvictConnections(); // old connections close when returned
// within seconds every connection in the pool has logged in as app_rw_b

A small component that watches the secret store and calls this code is what people mean by a refreshable DataSource. Check that your HikariCP version supports changing the username at runtime; if it does not, rotate only the password with MySQL dual passwords, or fall back to rolling restarts.

5Dynamic Credentials With Spring Cloud Vault

A secrets manager can take rotation off our hands entirely. With the database secrets engine in HashiCorp Vault, for example, the service asks Vault for credentials at start-up, Vault creates a new database user with a lease, a time limit such as 24 hours, and the service renews the lease while it runs. Spring Cloud Vault can fetch these credentials and put them into spring.datasource.username and password before the DataSource is created, and it renews the lease in the background.

There is a catch: renewing a lease extends the same user, but when the lease reaches its maximum lifetime Vault issues a new user, and the running pool must be given it, through a refresh like the one above or a restart. Check your Spring Cloud Vault version for how it handles this rotation, because the behaviour has changed over versions.

6MaxLifetime and Where the Password Lives

Whatever the method, we set HikariCP's maxLifetime below the lifetime of the credential, for example 30 minutes for credentials valid for hours, so that connections are recycled long before their login is revoked and the pool never holds connections for a user that no longer exists. It should also be a few seconds shorter than any connection time limit enforced by the database or network.

The password itself never goes into the container image, the application.yml in Git, or a Helm values file in plain text. Those copies live for years and spread to every clone and registry. Instead the service reads it at start-up from a secret store or a mounted secret, so rotation means changing one place.

YAML
spring:
datasource:
username: ${DB_USERNAME} # injected from the secret store
password: ${DB_PASSWORD}
hikari:
max-lifetime: 1800000 # 30 minutes, below the credential lifetime
keepalive-time: 300000

7Follow-up questions

What happens to transactions in progress during the rotation?

Nothing. Existing connections stay authenticated even after the old password is removed, and soft eviction only closes a connection after it is returned to the pool, so running transactions finish normally on their connection.

Can we rotate by changing the password and restarting all pods at once?

Only if we accept a short outage while every pod restarts, and a long one if the new password is wrong, because then no pod can connect. The overlap approach lets us detect a bad credential on the first pod while the others still work.

How often should we rotate?

Static passwords are commonly rotated every 30 to 90 days and immediately after any suspected leak or staff change. With dynamic credentials the question disappears, since each credential lives only hours or days.

8Conclusion

In this answer, we rotated a database password without downtime by keeping two valid credentials during the change: two login roles in PostgreSQL or a retained secondary password in MySQL. HikariCP keeps existing connections and uses the new credential only for new ones, so we roll pods gradually or update the pool through its MXBean and soft-evict old connections. Spring Cloud Vault can supply dynamic credentials with leases, maxLifetime must stay below the credential's lifetime, and the password is always read from a secret store, never from the image or Git.

What to say in the interviewAbout 80 seconds aloud

Open with

The key idea is that both the old and the new credential must work for a while. HikariCP only uses the password when it opens a physical connection, and existing connections stay logged in even after the password changes, but the pool keeps opening new ones as connections reach maxLifetime.

Then, if they want more

So if I change the password in the database first, new connections start failing within minutes.

In PostgreSQL, which allows one password per role, I keep two login roles that are both members of a group role with the grants: I set a password on the unused one, put it in the secret store, roll the pods gradually so each new pod starts with it, and once pg_stat_activity shows nothing using the old login, I remove its password.

In MySQL 8 I can use dual passwords: ALTER USER with RETAIN CURRENT PASSWORD, deploy, then DISCARD OLD PASSWORD. Instead of restarts, I can also update the pool at runtime through HikariCP's config MXBean and call softEvictConnections, so connections are replaced as they are returned. Spring Cloud Vault can go further with dynamic credentials and leases.

In every case maxLifetime should be shorter than the credential's lifetime, and the password comes from the secret store at start-up, never from the image or Git.

About this answer

Last updated
12 November 2026
Versions it names
MySQL 8
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.
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 "How do you rotate the database password of a running Spring Boot service with HikariCP...": https://scrivio-interview-library.netlify.app/databases/database-security/ff5a902a08/