Quick revision
The short answer
With Vault's database secrets engine, each lease gives the service its own database user with a limited lifetime, and Spring Cloud Vault renews the lease until it reaches its max TTL, after which brand-new credentials are needed. Connections already open in the pool keep working until they are closed, but new connections need the new username and password, so we either refresh the pool's credentials or restart the instance in a controlled way. Setting Hikari's maxLifetime well below the credential lifetime keeps connections recycling, and we monitor renewal failures and test what happens when Vault is unavailable.
What to remember
In this answer, we followed a Vault dynamic database credential through its life. The database engine creates a user per lease, Spring Cloud Vault renews it until the max TTL, and then entirely new credentials are needed. Open pooled connections may keep working, but new ones need the new credentials, so we refresh the pool through Hikari's MXBeans or restart instances before expiry. Setting maxLifetime below the credential lifetime keeps connections recycling, and monitoring renewal failures plus deliberate failure tests make expiry a non-event.
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.
With Vault's database secrets engine, each lease gives the service its own database user with a limited lifetime, and Spring Cloud Vault renews the lease until it reaches its max TTL, after which brand-new credentials are needed. Connections already open in the pool keep working until they are closed, but new connections need the new username and password, so we either refresh the pool's credentials or restart the instance in a controlled way. Setting Hikari's maxLifetime well below the credential lifetime keeps connections recycling, and we monitor renewal failures and test what happens when Vault is unavailable.
Why this question is asked
Dynamic database credentials are one of Vault's best features: no shared password that lives for years, and every instance has its own user that disappears on its own. They also bring a new failure mode that surprises teams in production. The credentials expire, often at three in the morning, and the connection pool starts failing in ways that are hard to diagnose. Interviewers ask this design question to see whether we understand leases, how they meet the connection pool's lifecycle, and how to design so expiry is a non-event. In this answer, we will recap how the database engine works, follow a lease through renewal and expiry, see how HikariCP behaves, and then design the handling, monitoring and failure tests.
1A Quick Recap of Vault Dynamic Secrets
Vault's database secrets engine holds an admin connection to the database. When an application asks for credentials for a role, Vault runs the role's creation statements, for example CREATE ROLE with a random name and password and a GRANT on the schema, and returns that username and password together with a lease. A lease is Vault's promise about lifetime: it has a TTL (time to live), which can be extended by renewal, and a max TTL, which cannot be passed.
When a lease expires or is revoked, Vault runs the revocation statements and drops the user. So the credentials are unique per instance and self-destructing, which means a leaked password is useless within hours and the database audit log shows exactly which instance did what.
2The Lifecycle of a Lease
Spring Cloud Vault requests credentials at startup, through its database integration, and exposes them as spring.datasource.username and spring.datasource.password, so Spring Boot's DataSource is built with them. It also starts a lease container that renews the lease before each TTL runs out: with a 1-hour TTL, it renews every hour or so, and each renewal extends the same user.
But there is a catch: renewal stops at the max TTL. After 24 hours in our example, Vault refuses to extend the lease and the user will be dropped. Spring Cloud Vault can be configured to rotate, meaning it requests a new set of credentials before expiry and publishes an event, but the DataSource that was built at startup still holds the old username and password unless something updates it. Check your Spring Cloud Vault version for the exact lease settings and events it offers.
9 messages in order. Play them, or step through them one at a time.
Renewal only extends the same user; at max TTL the service needs entirely new credentials.
3What Happens to the Connection Pool
HikariCP opens physical connections with the credentials it was given and keeps them open for reuse. Database authentication happens only when a connection is opened, so an open connection usually keeps working even after the password would no longer be accepted. New connections are a different story: whenever Hikari replaces a connection, it logs in again with the username it has, and once Vault has dropped that user, the login fails.
There is one more detail. When Vault revokes a lease, the revocation statements can terminate existing sessions as well, depending on how the role is written (for PostgreSQL, dropping a role that still has sessions fails unless the statements end them first). So on expiry the pool may lose all its connections at once and be unable to open new ones, and every request fails with authentication errors until the instance gets new credentials.
4Designing the Refresh
There are two workable designs. The first is a controlled restart: we set the max TTL longer than our deployment and rolling-restart cadence (for example a max TTL of 7 days and a scheduled rolling restart every 3 days), so every instance always starts with fresh credentials and never reaches expiry. This is simple and robust, and Kubernetes makes rolling restarts cheap.
The second is a live refresh. When Spring Cloud Vault rotates and publishes the new credentials, a listener updates the pool. HikariCP lets us change the username and password through its config MXBean (HikariConfigMXBean.setUsername and setPassword) and then soft-evict existing connections, so each connection is replaced with one using the new user as it becomes idle. Old connections finish their current work first. This avoids restarts but is more code to own and test.
Whichever design we choose, we set Hikari's maxLifetime clearly below the credential lifetime, for example 30 minutes with a 1-hour TTL. maxLifetime retires each connection after that time, so the pool continually opens fresh connections. That has two benefits: connections never outlive their user by surprise, and any login problem appears quickly and gradually rather than all at once at hour 24.
5Monitoring and Testing Failure
Renewal is now a dependency on Vault's availability. We alert on lease renewal failures and on credentials getting close to max TTL without rotation, and we export pool metrics, such as Hikari's pending threads and connection timeouts, alongside them, so an authentication storm is traced to its cause in minutes.
Then we test the bad days on purpose in a lower environment. Set a very short max TTL, such as 10 minutes, and watch the service cross it. Block Vault for an hour and see whether existing connections keep serving and whether the service starts at all. Revoke a lease manually and see how fast the pool recovers. These tests usually reveal one wrong setting long before production would.
- Startup: fetch credentialsHikari built with user A
- Renew before TTLsame user A, until max TTL
- Renewal failed?YesAlert; keep serving on open connectionsNoContinue renewing
- Close to max TTL?YesRotate: fetch user BNoKeep renewing
- Update pool, soft-evictor rolling restart before expiry
- Old lease revokedno connection still uses user A
6 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Why not just use a very long TTL to avoid the problem?
A long TTL turns dynamic secrets back into long-lived passwords. The value of the design is that a leaked credential stops working soon. A few hours to a few days, combined with rotation or restarts, keeps that benefit.
Do database migrations with Flyway need special care?
Yes. Tables created by one dynamic user are owned by that user, and when it is dropped, ownership problems appear. A common fix is to grant a shared group role to each dynamic user and have migrations run SET ROLE to that group, or to run migrations with a separate long-lived migration role, so objects belong to a stable role.
What if Vault is down when an instance starts?
It cannot get credentials, so it should fail to start and the old instances keep running. That is why rolling restarts need Vault to be healthy, and why Vault itself runs highly available.
7Conclusion
In this answer, we followed a Vault dynamic database credential through its life. The database engine creates a user per lease, Spring Cloud Vault renews it until the max TTL, and then entirely new credentials are needed. Open pooled connections may keep working, but new ones need the new credentials, so we refresh the pool through Hikari's MXBeans or restart instances before expiry. Setting maxLifetime below the credential lifetime keeps connections recycling, and monitoring renewal failures plus deliberate failure tests make expiry a non-event.
Open with
Vault's database engine creates a real database user for each lease, with a TTL that can be renewed and a max TTL that cannot. Spring Cloud Vault fetches the credentials at startup, Boot builds the DataSource with them, and a lease container keeps renewing.
Then, if they want more
The tricky part is the max TTL: after that, renewal is refused and Vault drops the user, so the service needs new credentials. Hikari only authenticates when it opens a connection, so existing connections may keep working until they close, but every new connection fails with authentication errors, and revocation may end the existing sessions too.
I would design for it in one of two ways. The simple way is to set the max TTL longer than our rolling restart cadence, for example seven days with restarts every three, so instances never reach expiry.
The other way is live rotation: when Spring Cloud Vault fetches new credentials, a listener sets the new username and password on Hikari's config MXBean and soft-evicts connections so they reconnect as the new user.
In both cases I set Hikari maxLifetime well below the credential lifetime, say 30 minutes with a 1-hour TTL, so connections recycle continuously and problems show up early. I would alert on renewal failures and leases nearing max TTL, watch pool timeouts, and test in a lower environment with a 10-minute max TTL, a manual revocation and a Vault outage.
For migrations I would use a stable owner role so tables are not owned by temporary users.
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 "Design database credential handling for a Spring Boot service using Vault dynamic...": https://scrivio-interview-library.netlify.app/spring-boot/secrets-and-crypto/b90e7910c9/