Quick revision
The short answer
Vault's database secrets engine creates a new PostgreSQL user for each lease, with the grants and TTL defined by a role, and the service asks for credentials at startup through Spring Cloud Vault's database backend. Spring Vault renews the lease until its maximum TTL; after that, new credentials must be fetched and the HikariCP pool rotated, by updating its username and password and soft-evicting connections, or by recreating the DataSource. Hikari's maxLifetime should be below the credential TTL, and a shared owner role is needed so objects created by migrations are not owned by short-lived users.
What to remember
In this answer, we followed Vault's database engine creating a PostgreSQL user per lease and Spring Cloud Vault passing it to Hikari at startup. Spring Vault renews the lease until max TTL, after which new credentials must be fetched and the pool rotated through the Hikari MXBean with soft eviction, or by recreating the DataSource. We set maxLifetime below the TTL so connections cycle, checked how open connections behave in the database, and used a shared owner role so migrations do not create objects owned by short-lived users.
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.
Vault's database secrets engine creates a new PostgreSQL user for each lease, with the grants and TTL defined by a role, and the service asks for credentials at startup through Spring Cloud Vault's database backend. Spring Vault renews the lease until its maximum TTL; after that, new credentials must be fetched and the HikariCP pool rotated, by updating its username and password and soft-evicting connections, or by recreating the DataSource. Hikari's maxLifetime should be below the credential TTL, and a shared owner role is needed so objects created by migrations are not owned by short-lived users.
Why this question is asked
A shared database password lives forever, is copied into many places, and when it leaks we cannot tell which service leaked it. Dynamic credentials fix this: every instance gets its own user that expires on its own. Interviewers ask this design question because the hard part is not getting the first credential, it is what happens hours later when it expires while a connection pool is full of connections. In this answer, we will walk through the flow, the lease lifecycle, how PostgreSQL treats open connections, how to rotate the Hikari pool, the ownership problem, and what to test.
1The Flow From Startup to a Connection
An operator configures the database secrets engine once: a connection to PostgreSQL with an admin user, and a role, say orders-app, with creation statements that define the new user's grants and a default TTL such as 1 hour with a max TTL of 24 hours. The service authenticates to Vault, for example with Kubernetes auth, and Spring Cloud Vault reads database/creds/orders-app. Vault runs the creation statements, returns a username such as v-k8s-orders-app-x7Qa and a password with a lease, and Spring Cloud Vault puts them into spring.datasource.username and spring.datasource.password before Hikari starts.
6 messages in order. Play them, or step through them one at a time.
2Renewal Until the Maximum TTL
Spring Vault's lease container renews the lease in the background before it expires, so the same user keeps working for up to the max TTL, 24 hours in our example. At that point Vault will not renew any more and will revoke the lease when it ends, which runs the revocation statements and typically drops the user.
So the application must get new credentials before the max TTL arrives. Spring Vault's lease container can be told to treat the lease as rotating: when the lease is about to expire, it requests a new secret at the same path and publishes an event with the new credentials. Our code listens for that event and gives the pool the new user. Check your version for the exact event and rotation API names; recent versions publish a SecretLeaseCreatedEvent for the new secret.
3What Happens to Open Connections
Here the database matters. In PostgreSQL, VALID UNTIL is checked only when a user logs in, so connections already open keep working after the password expires. Whether they survive revocation depends on the revocation statements: dropping a role that still has sessions may fail or leave them running, and some teams add a call that terminates the user's backends. Other databases behave differently, so we test the exact behaviour rather than assuming it.
Either way, we do not want connections living for days with an old identity. Hikari's maxLifetime closes each connection after a fixed age, 30 minutes in our configuration, and replaces it with a fresh one. Setting it well below the credential TTL guarantees that connections cycle long before the credentials behind them disappear, and the new ones are opened with whatever credentials the pool currently holds.
4Rotating the Hikari Pool
When new credentials arrive, the simplest safe approach is to update the pool's credentials through its MXBean and then soft-evict existing connections. Soft eviction marks connections to be closed as soon as they are returned to the pool, so in-flight transactions finish normally and new connections use the new user.
The alternative is to build a new DataSource with the new credentials, swap it in behind a delegating DataSource and close the old one after a grace period. That is cleaner when other components cache connections, but more code. Whichever we choose, we test it under load: run traffic, force rotation, and check that no request fails.
5The Ownership Problem
There is a trap that appears only on the first schema migration. In PostgreSQL, a table belongs to the user who created it. If Flyway or Liquibase runs as today's short-lived user, the new tables are owned by v-k8s-orders-app-x7Qa. Tomorrow that user is dropped, and either the DROP fails because it owns objects, or tomorrow's user cannot alter the table it needs to migrate.
The fix is a shared, long-lived owner role that cannot log in, orders_owner in our creation statements. Every dynamic user is a member of it, and migrations run SET ROLE orders_owner first, for example through Flyway's init SQL, so all objects are owned by the stable role. A common refinement is to give migrations their own Vault role with DDL rights, and the running application a role with only read and write grants.
6Follow-up questions
What happens if Vault is down when a pod starts?
The pod cannot obtain database credentials, so with spring.cloud.vault.fail-fast=true it fails to start, its readiness probe never passes, and Kubernetes restarts it with back-off. Running pods are unaffected until their leases need renewal, so a short Vault outage does not cause an incident, but a long one blocks deployments and scaling and eventually breaks running pods when credentials expire. That is why Vault must run highly available, with long enough max TTLs to cover a realistic outage, and we must never fall back to a shared static password silently.
Why not just use a long TTL and skip rotation?
Because the point is short-lived, per-instance identity. A 30-day credential is almost a static password. A TTL of hours plus rotation limits the window of a leak and makes audit logs point to a single instance.
How many database users does this create?
One per lease, so roughly one per pod per rotation period. PostgreSQL handles that easily, but it is worth monitoring the role count and that revocation actually cleans up.
7Conclusion
In this answer, we followed Vault's database engine creating a PostgreSQL user per lease and Spring Cloud Vault passing it to Hikari at startup. Spring Vault renews the lease until max TTL, after which new credentials must be fetched and the pool rotated through the Hikari MXBean with soft eviction, or by recreating the DataSource. We set maxLifetime below the TTL so connections cycle, checked how open connections behave in the database, and used a shared owner role so migrations do not create objects owned by short-lived users.
Open with
With the database secrets engine, Vault has an admin connection to PostgreSQL and a role, say orders-app, that defines creation statements and a TTL, for example one hour with a 24-hour maximum.
Then, if they want more
At startup the service authenticates to Vault, Spring Cloud Vault's database backend reads database/creds/orders-app, Vault creates a new user with those grants, and the username and password go into the datasource properties before Hikari starts. Spring Vault's lease container then renews the lease until the max TTL.
After that it cannot be renewed, so we need new credentials and must rotate the pool.
I would configure the lease as rotating, listen for the event with the new secret, set the new username and password on Hikari's config MXBean, and soft-evict connections so in-flight work finishes and new connections use the new user; recreating the DataSource behind a delegating wrapper is the alternative.
In PostgreSQL, open connections usually keep working after the password expires, since it is only checked at login, so I set Hikari's maxLifetime well below the credential TTL, like 30 minutes, to force connections to cycle. I would test rotation under load.
And I would fix ownership: every dynamic user joins a shared non-login owner role, and migrations SET ROLE to it, otherwise tables end up owned by users that get dropped.
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 Boot 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 "You want each Spring Boot service to get short-lived PostgreSQL credentials from Vault's...": https://scrivio-interview-library.netlify.app/spring-boot/spring-projects/fd02d8df19/