Quick revision
The short answer
Timeouts must shrink as we go inward: the gateway waits longest, each service waits less than its caller, the transaction and statement limits sit below the service limit, and the lock timeout is the shortest of all. Across 100 services we enforce this with safe defaults in shared configuration, distinct error types and metrics for each timeout, deadlines passed along with each call, and reviews that record why every default has its value.
What to remember
In this answer, we saw that timeouts across a stack must shrink from the outside in, so that the innermost layer fails first with a specific error and no layer keeps working for a user who has gone. We set concrete defaults in a shared starter and per database role, allowed teams to lower them freely and to raise them only with review, gave each timeout its own error type and metric, propagated deadlines so downstream work stops in time, and made timeouts part of design reviews, post-mortems and an automated ordering check.
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.
Timeouts must shrink as we go inward: the gateway waits longest, each service waits less than its caller, the transaction and statement limits sit below the service limit, and the lock timeout is the shortest of all. Across 100 services we enforce this with safe defaults in shared configuration, distinct error types and metrics for each timeout, deadlines passed along with each call, and reviews that record why every default has its value.
Why this question is asked
After a few lock-wait outages, most organisations discover that every team picked its timeouts independently, and many picked none. The result is that a single slow lock makes requests pile up in the database, in the connection pools and in the gateway at the same time. Interviewers ask this at a senior level because the answer is half technical (which number goes where, and why) and half governance (how to make 100 teams follow it without a committee approving every change). In this answer, we will see why the order of timeouts matters, choose concrete defaults, make timeouts visible, add deadline propagation, and set up the review process that keeps it working.
1A Quick Recap of the Timeouts in One Request
A single user request passes through several layers, and each has its own timer. The client, for example a mobile app, gives up after some seconds. The API gateway has a timeout for each route. The service has a request timeout and, for every call it makes, a client timeout. When it needs the database, it first waits for a connection from the pool (HikariCP's connectionTimeout), then runs a transaction (Spring's @Transactional timeout), then individual statements (PostgreSQL's statement_timeout), and inside a statement it may wait for a lock (lock_timeout).
lock_timeout limits waiting for another transaction to release a row or table; statement_timeout limits one statement, including that wait; the transaction timeout bounds the unit of work; the pool timeout limits waiting for a connection. If we do not choose them, defaults decide, and PostgreSQL's lock_timeout and statement_timeout default to 0, meaning no limit.
2Why Timeouts Must Shrink From the Outside In
The rule is that every inner timeout is shorter than the one around it. The reason is simple: when an outer layer gives up, the user has already received an error, so any work still running inside is wasted. If the gateway gives up after 10 seconds but the database statement may run for 60, the statement keeps holding locks and a connection for 50 seconds after nobody is waiting for its result. Under load, that wasted work is exactly what turns a slow lock into an outage.
The opposite order, inner timeouts shorter than outer ones, means the innermost layer fails first and returns a specific error that every outer layer can still handle: the service can return a clean 503 with a retry hint before the gateway's timer expires. If a service retries once, the inner budget must fit twice inside the outer one.
- Client15 s, then shows an error
- API gateway10 s per route
- Service request8 s, deadline passed downstream
- Pool connectionTimeout2 s to get a connection
- Transaction timeout5 s for the unit of work
- statement_timeout3 s per statement
- lock_timeout1 s waiting for a lock
7 layers, from the top down. Step through them one at a time.
Each layer gives up before the one around it, so failures surface as specific errors from the inside.
3Safe Defaults in Shared Configuration
With 100 services, the rule only holds if it is the default. We put the defaults where every service picks them up without effort: in the shared Spring Boot starter or configuration library the company already uses, and in the database itself, attached to each application role so that a service that forgets its configuration still gets a limit.
Teams may lower a timeout freely, since that never breaks the ordering. Raising one goes through a short review, because a longer timeout holds more connections for longer during a slowdown.
The database settings are a safety net: they catch the service that forgot and the ad hoc script, and the idle limit catches code that calls a slow remote service while holding locks.
4Make Every Timeout Visible and Distinct
A timeout only helps if people can tell which one fired. A lock timeout means another transaction holds what we need, and the fix is to find that transaction. A statement timeout without lock waits means the query itself is slow, and the fix is an index or a better plan. A pool timeout means we ran out of connections, often as a consequence of one of the first two. If all three surface as a generic 500 error, every incident starts with an hour of guessing.
So the shared library maps them to distinct exception types and metric tags: SQLSTATE 55P03 for a lock timeout, 57014 for a statement timeout, and Hikari's SQLTransientConnectionException for an empty pool.
An incident review can then see at once that orders hit 37 lock timeouts at 09:02, which points at a blocking transaction, not the planner.
5Deadlines That Travel With the Request
Fixed timeouts still waste work when the caller has already used most of its budget: with 8 of 10 seconds gone, a 5-second transaction downstream cannot finish in time. A deadline solves this. The first service computes an absolute time at which the request expires and passes it to every downstream call, for example in a header or as gRPC's built-in deadline. Each service uses the smaller of its own timeout and the time left.
For the database, the service can set the statement timeout for the current transaction from the remaining time, so work stops as soon as the user is gone. SET LOCAL limits the change to the current transaction.
Retries must respect the same deadline, and a retry budget, such as one retry only when time remains, prevents retry storms during a lock-wait incident.
6Governing Timeouts Across 100 Services
Keeping the defaults right for years is governance work. One short standard explains the reasoning for each number, for example that lock_timeout is one second because healthy row locks last under 100 milliseconds, so a change request has reasoning to argue with.
Timeouts then join the places where decisions already happen: design reviews ask whether a new service fits the gateway budget, post-mortems ask which timeout fired and in which layer, and a build check flags any inner timeout larger than its outer one before deployment.
The platform team owns the defaults and reviews them twice a year against the metrics; a service that hits lock timeouts daily needs a design change, not a bigger number.
7Follow-up questions
Should lock_timeout ever be longer than statement_timeout?
There is no point. statement_timeout already includes time spent waiting for locks, so a longer lock_timeout would never fire. We keep lock_timeout clearly shorter so that a lock problem is reported as a lock problem, with its own error code, rather than as a generic slow statement.
What about batch jobs and migrations that legitimately run for minutes?
They get their own database roles with their own statement limits, reviewed as exceptions, and they keep a short lock_timeout. A migration that cannot get its lock within a few seconds should give up and retry rather than queue in front of production traffic. Long jobs also commit in batches so they never hold locks for minutes.
8Conclusion
In this answer, we saw that timeouts across a stack must shrink from the outside in, so that the innermost layer fails first with a specific error and no layer keeps working for a user who has gone. We set concrete defaults in a shared starter and per database role, allowed teams to lower them freely and to raise them only with review, gave each timeout its own error type and metric, propagated deadlines so downstream work stops in time, and made timeouts part of design reviews, post-mortems and an automated ordering check.
Open with
I would start from one rule: timeouts shrink from the outside in. The client waits longest, then the gateway, then the service, then the pool, the transaction, the statement, and lock_timeout is the shortest.
Then, if they want more
That way the inner layer fails first with a precise error, and nobody keeps holding locks and connections after the user has already got an error. Then I would make that rule the default rather than a guideline.
The shared Spring Boot starter sets Hikari's connection timeout, the default transaction timeout and a JPA query timeout, and the database sets lock_timeout, statement_timeout and an idle-in-transaction limit per application role as a safety net. Teams may lower these freely, but raising one needs a short review because it affects capacity.
Next, visibility: the shared library maps lock timeouts, statement timeouts and pool timeouts to different exceptions and metric tags, so an incident tells us immediately whether to look for a blocking transaction, a slow query or pool exhaustion.
I would also propagate a deadline with every call and set the statement timeout from the time remaining, with a small retry budget.
Finally, governance: a one-page standard explaining why each number was chosen, a build check that catches inner timeouts larger than outer ones, timeouts as a question in design reviews and post-mortems, and a twice-yearly review of the defaults against the metrics.
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
- PostgreSQL documentation, MySQL reference manual
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 "After several outages caused by lock waits, you are asked to define timeouts across the...": https://scrivio-interview-library.netlify.app/databases/database-locking/635f0f8356/