Scrivio Interview library
LeadershipAdvanced6 min read · updated 12 November 2026

Deadlock and lock-timeout errors are rising across many services that share a database. How would you organise the investigation and long-term fix?

I would treat it as one shared problem with a data-driven investigation: collect every deadlock and lock-timeout report centrally, attribute each to a service and statement, and rank the patterns by impact. Most will fall into a few known causes, such as inconsistent lock order, missing indexes, long transactions and hot rows, which we fix with shared guidelines and a common retry helper. In the long term we reduce how much the services share the database and track the error rate as a reliability metric.

Why this question is asked

When one service has deadlocks, its team fixes them. When many services sharing one database see deadlocks and lock timeouts rising together, no single team can see the whole picture, and each may blame the others. Interviewers ask this at senior level because the technical facts are only half the answer; the other half is how we organise many teams so the problem gets smaller and stays small. In this answer, we will walk through how to get visibility first, how to find the common causes, how to fix them across teams without a big rewrite, and how to change the architecture and the metrics so the problem does not come back.

1Start by Making the Problem Visible

Rising errors across many services usually come with many opinions and little data. So the first step is to collect the database's own evidence in one place. In PostgreSQL, deadlocks are written to the server log, and log_lock_waits = on adds every lock wait longer than deadlock_timeout. In MySQL, innodb_print_all_deadlocks = ON sends every deadlock report to the error log instead of only keeping the latest one. We ship those logs to the central logging system that teams already use.

Next we need to know which service sent each statement, because a shared database sees only connections. The cheapest way is to set the application name on every connection pool, for example ApplicationName=orders-service in the PostgreSQL JDBC URL, which then appears in pg_stat_activity and can be added to log lines. For MySQL, or for more detail, a short comment with the service and endpoint can be added to the SQL itself. After a week we can say: 60 percent of deadlocks involve these three statements from these two services.

2Look for the Common Causes

Deadlocks and lock timeouts look endlessly varied, but in practice most come from a handful of patterns. Grouping the reports by pattern is what turns a hundred incidents into four fixes. We go through the top statements one by one and ask which pattern each fits.

Common causes of lock errors
PatternHow it shows in the reportsTypical fix
Inconsistent lock ordersame statements with ids swappedlock rows in ascending key order
Missing indexlocking read scans many rows (InnoDB)add the index; check with EXPLAIN
Long transactionlock timeouts; old xact_start in pg_stat_activitymove remote calls out; shorter units
Hot rowmany waits on one counter or status rowsplit counters, queue, or batch updates
Foreign key checksshared locks on parent rowsorder parent locks first; index child columns
Grouping reports by pattern turns many incidents into a few fixes.

Notice that these causes interact. A missing index makes a statement lock more rows, a long transaction holds them longer, and a hot row is where everyone collides. That is why rising errors often follow traffic growth rather than any single bad release: the same code with more load crosses a threshold.

3Fix the Patterns, Not Just the Incidents

Once we know the patterns, we fix them in a way that spreads to every team instead of only the service that hurts most today. A short set of shared guidelines covers it: lock rows in a fixed order, keep transactions to database work only, never call another service while holding locks, set a statement and lock timeout, and index every column used in a locking read. We attach a real example from our own reports to each rule, because teams follow rules they recognise.

The second tool is a shared retry helper in the common library, so every team handles deadlocks the same correct way: retry the whole transaction from outside the transactional boundary, a few attempts with jittered backoff, and only for operations that are safe to repeat. It also records a metric for every retry, so a retry never hides a growing problem. Without a shared helper, some teams retry inside a dead transaction and others do not retry at all.

Then we work through the ranked list with the owning teams, top pattern first, and re-measure after each fix. This keeps the effort proportional: the first three or four fixes often remove most of the errors.

4Reduce Sharing Over Time

The deeper cause is that many services lock the same tables, and no team can see what the others do with them. Guidelines reduce the damage, but every new service is another chance to break them. The long-term fix is to give services their own schemas or databases, so each team owns its tables and its locking behaviour, and other services reach that data through an API or events rather than by writing to it directly.

This is a multi-quarter change, so we do it gradually. We start with the tables that appear most often in cross-service deadlocks, move write ownership of each to one service, and only then consider physically separating them. Each move removes a whole class of conflicts that no guideline could.

Step throughOrganising the investigation
  1. Collect reports centrallydeadlock and lock-wait logs from every database
  2. Attribute to servicesapplication name or SQL comments on every connection
  3. Group by patternlock order, index, long transaction, hot row
  4. Fix top patterns with ownersguidelines, shared retry helper, re-measure
  5. Do cross-service conflicts remain?
    YesMove table ownership to one serviceNoKeep tracking the rate
  6. Track as a reliability metricrate per service, alert on growth

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

5Track the Rate as a Reliability Metric

Finally, we make lock errors something the organisation watches, not something it rediscovers. PostgreSQL counts deadlocks per database in pg_stat_database.deadlocks and MySQL exposes Innodb_deadlocks; together with lock timeouts and retry counts from the shared helper, they go on a dashboard per service. A rate, such as deadlocks per million transactions, is fairer than a raw count because traffic grows.

We review the trend in the regular reliability review and alert when a service's rate rises sharply after a release. That turns lock problems into ordinary engineering feedback: a team that ships a change with a new lock pattern finds out within a day, while the change is still fresh in their minds.

6Follow-up questions

How do you get teams to prioritise this work?

With their own data. A ranked list showing that a team's two statements cause a third of all deadlocks, with the number of failed requests attached, is more persuasive than a general request. We also make the fix cheap by providing the guidelines and the retry helper, so most teams need a small change rather than a project.

Should we just increase lock timeouts to reduce errors?

Usually not. A longer timeout turns a quick error into a slow request, holds connections and threads longer and can make the pool run out. Deadlocks are not affected at all by lock timeouts. It is better to keep timeouts short so problems surface quickly and fix the cause.

What if one hot row is the main source of contention?

Then the design of that row is the problem, not the code around it. Common options are splitting a counter into several rows that are summed when read, appending events and aggregating them later, or sending updates through a queue processed by a single consumer. Each removes the need for many transactions to lock the same row at once.

7Conclusion

In this answer, we organised the investigation into a sequence: collect deadlock and lock-wait reports centrally, attribute them to services and statements, group them into common causes such as lock order, missing indexes, long transactions and hot rows, and fix those patterns with shared guidelines and a retry helper. For the long term, we reduce how many services share the same tables and track the error rate as a reliability metric so the problem stays visible.

What to say in the interviewAbout 90 seconds aloud

Open with

I would start by getting data instead of opinions. I would turn on full deadlock logging, log_lock_waits in PostgreSQL or innodb_print_all_deadlocks in MySQL, and ship those logs centrally.

Then, if they want more

Then I would make sure every connection pool sets an application name, so each report can be attributed to a service and a statement. After a week or two, I would rank the patterns by impact.

In my experience most fall into a few causes: inconsistent lock order, missing indexes that make locking reads touch too many rows, long transactions that hold locks while calling other services, and hot rows that everyone updates.

For each pattern I would write a short guideline with a real example from our reports, and provide a shared retry helper that retries the whole transaction from outside it, with backoff and a metric. Then I would work with the owning teams on the top items first and re-measure after each fix.

In parallel, I would address the root cause, which is that many services write the same tables: move ownership of the most contended tables to one service and have others use its API, and over time give services their own schemas.

Finally, I would track deadlocks and lock timeouts per million transactions per service on a dashboard and review the trend regularly.

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 "Deadlock and lock-timeout errors are rising across many services that share a database....": https://scrivio-interview-library.netlify.app/databases/locking-and-concurrency-control/85dcd30025/