Scrivio Interview library
DesignIntermediate6 min read · updated 12 November 2026

Design short-lived database credentials for 100 services using a secret manager that creates a new database user per lease. What failure modes do you plan for?

Each service authenticates to the secret manager with its workload identity and receives a database user with a lease, for example 24 hours, which it renews well before expiry; the secret manager creates the user as a member of a group role that holds the privileges, and drops it when the lease ends, while a fixed owner role owns all objects. We plan for the secret manager being down at start-up, leases expiring while connections are open, and role-creation storms when many pods start at once, and mitigate with early renewal, maxLifetime shorter than the lease, jittered backoff and monitoring of renewals and failed logins.

Why this question is asked

Short-lived credentials are one of the most effective ways to make a stolen database password worthless. Instead of a password that lives for years, every service gets a fresh database user that disappears within a day. But the design moves a dependency into the critical path: if the secret manager or the role creation fails, services cannot connect at all. Interviewers ask this design question to see whether we plan for those failures, not only for the happy path. In this answer, we will recap how dynamic credentials work, design the lease lifecycle and the role structure, and then go through the failure modes one by one with their mitigations and monitoring.

1A Quick Recap of Dynamic Database Credentials

With dynamic credentials, a secret manager such as HashiCorp Vault holds an administrative connection to each database. When a service asks for credentials, it runs a creation statement that makes a new database user with a random name and password, and returns them with a lease. A lease is a time limit plus an id the service can use to renew it. When the lease ends or is revoked, the secret manager runs a revocation statement that removes the user. No person ever sees these passwords, and each one is used by exactly one service instance for a short time.

2The Lease Lifecycle for One Service

Each pod of a service authenticates to the secret manager with its workload identity, for example its Kubernetes service account token, so no bootstrap password is needed. It requests credentials for its role, gets a user valid for 24 hours, builds its connection pool, and renews the lease periodically. Leases also have a maximum lifetime; when renewal is no longer possible, the service requests a brand new user and switches its pool to it.

Step throughLease lifecycle of a dynamic database user
Service podSecret managerPostgreSQL
login with workload identity
credentials for role orders-rw
CREATE ROLE v-orders-x7k LOGIN ... IN ROLE orders_rw
user, password, lease 24 h
open pool connections as v-orders-x7k
renew lease at two thirds of its time
near max lifetime: request a new user
lease ended: revoke and DROP ROLE old user

8 messages in order. Play them, or step through them one at a time.

Renewal and replacement happen in the background while traffic continues.

3Grant Through a Group Role, Own Through a Fixed Role

The creation statement should make the dynamic user a member of a group role that already holds the service's privileges, rather than granting table privileges to each new user. That keeps creation fast, keeps all 100 services' grants in reviewed code, and makes every dynamic user of a service identical.

SQL
-- creation statement configured in the secret manager for orders
CREATE ROLE "{{name}}" WITH LOGIN PASSWORD '{{password}}' VALID UNTIL '{{expiration}}' IN ROLE orders_rw;
-- revocation statement
REVOKE orders_rw FROM "{{name}}";
DROP ROLE IF EXISTS "{{name}}";
-- fails if "{{name}}" owns any object: dynamic users must never create tables

Ownership is the trap. In PostgreSQL an object belongs to the role that created it, and a role that owns objects cannot be dropped. If a dynamic user ever creates a table, for example because a library auto-creates its schema, the revocation fails and the user is left behind, or worse, an expired user owns production tables. So all objects are owned by a fixed NOLOGIN owner role, migrations run with a separate migration credential that does SET ROLE to that owner, and runtime dynamic users have no CREATE privilege anywhere. Also note that VALID UNTIL only stops new logins; sessions already open keep working, which is why the pool must recycle connections before the user is dropped.

4Failure Mode: The Secret Manager Is Down

If the secret manager is unavailable when a pod starts, the pod cannot get credentials and cannot connect. For a single pod this is a failed start that Kubernetes retries. During a large outage it can block every deployment and every autoscaling event. We mitigate in layers: the secret manager runs highly available across zones; pods retry with exponential backoff and jitter rather than crash-looping and hammering it when it returns; and running pods are unaffected because they already hold credentials. Leases are long enough, 24 hours with renewal from two thirds of the way, that a secret manager outage of several hours does not expire anything. Some teams also let a pod keep its credentials across a process restart within the same lease, which avoids a new user for every crash.

5Failure Mode: Lease Expiry With Open Connections

If renewal fails for long enough, the lease reaches its end and the secret manager drops the user. Open connections may survive the drop or be terminated, depending on the revocation statements, but either way new connections fail and the pool slowly dies. The mitigation is to make expiry a planned event: HikariCP's maxLifetime is set far shorter than the lease, for example 30 minutes against 24 hours, the service requests a replacement user well before the maximum lifetime, and it switches the pool to the new user with soft eviction, so connections move over gradually. An alert fires when any pod's lease is within a few hours of expiry without successful renewal, which gives people time to act.

6Failure Mode: Role-Creation Storms

When a cluster restarts, or a big deployment rolls 2,000 pods at once, each pod asks for a new user at the same moment. Each request runs CREATE ROLE on the database, which writes to the shared role catalog, and thousands of them, plus the matching DROP ROLE calls later, cause catalog contention, slow logins and sometimes connection limit errors. We spread the load with jitter in start-up, limit deployment speed, and consider issuing credentials per service instance group rather than per process where the risk model allows. We also cap the creation rate in the secret manager so it queues rather than overwhelms the database, and run a cleanup job that finds orphaned dynamic users, ones whose lease no longer exists, and drops them.

Failure modes and their mitigations
FailureEffectMitigation
Secret manager down at start-upnew pods cannot connectHA, backoff with jitter, long leases
Renewal fails until lease endsuser dropped, pool diesearly renewal, maxLifetime << lease, alert
Thousands of pods start at onceCREATE ROLE storm, slow loginsjitter, rate limits, slower rollouts
Dynamic user created a tablerevocation fails, orphan ownerfixed owner role, no CREATE for runtime
Revocation fails silentlyorphaned users pile upcleanup job, count users per service

7Follow-up questions

What do you monitor?

Lease renewal success and failures per service, time remaining on the oldest lease, credential requests per minute, the number of dynamic users per database, and failed logins in the database logs. A rise in failed logins often shows a service using an expired user, and a growing user count shows revocation failing.

Why not issue one shared dynamic user per service instead of per pod?

It reduces role creation and is simpler, but a leaked credential then works for every pod of the service, and revoking it affects all of them at once. Per-pod users give finer audit and revocation; per-service users are a reasonable compromise for very large fleets, as long as leases stay short.

How do database migrations fit in?

Migrations run as a separate job with their own short-lived credential for a migration role that can SET ROLE to the fixed owner, so all objects are owned by the owner role and runtime users only inherit DML through the group role.

8Conclusion

In this answer, we designed short-lived credentials for 100 services. Each pod authenticates with its workload identity, gets a dynamic user with a 24 hour lease, renews early and switches to a new user before the maximum lifetime, while grants live on group roles and objects stay owned by a fixed owner role so users can always be dropped. We planned for a secret manager outage, lease expiry with open connections and role-creation storms, mitigated them with high availability, backoff with jitter, maxLifetime far below the lease and rate limits, and monitored renewals, user counts and failed logins.

What to say in the interviewAbout 90 seconds aloud

Open with

Each service pod authenticates to the secret manager with its workload identity, so there is no bootstrap secret, and gets a dynamic database user with a lease, say 24 hours.

Then, if they want more

The creation statement makes that user a member of the service's group role, which holds the grants defined in code, and adds VALID UNTIL; the revocation statement removes membership and drops the role. All objects are owned by a fixed NOLOGIN owner role and migrations run separately, because a dynamic user that owns a table cannot be dropped.

The pod renews at about two thirds of the lease and requests a new user before the maximum lifetime, switching the Hikari pool with soft eviction.

Failure modes I plan for: first, the secret manager being down at start-up, which blocks new pods, so it must be highly available, pods back off with jitter, and leases are long enough to survive an outage of hours.

Second, lease expiry while connections are open: VALID UNTIL only blocks new logins, so I keep maxLifetime far below the lease and alert when a lease is close to expiry without renewal.

Third, role-creation storms when thousands of pods start together, which hurt the role catalog, so I use jitter, rate limits and slower rollouts, plus a cleanup job for orphaned users. I monitor renewals, user counts and failed logins.

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.
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 short-lived database credentials for 100 services using a secret manager that...": https://scrivio-interview-library.netlify.app/databases/database-security/69134d0205/