Scrivio Interview library
DebuggingIntermediate8 min read · updated 13 November 2026

An order service went from 8 to 16 pods and throughput rose 60 percent. Going to 32 pods made throughput fall below the 16-pod level, and the database showed many lock waits on a single row of a sequence-like counters table.

Why did adding pods reduce throughput, and how do you fix it?

Every order updates the same row of the counters table, so all transactions serialise on that one row lock; more pods add more waiters, more lock management and more connections, and throughput falls. The Universal Scalability Law describes this: a contention term caps the gain, and a coherence term, the work to keep shared state agreed, makes throughput drop as nodes are added. We fix it by removing the hot spot with a database sequence or identity column, number blocks allocated per pod, or coordination-free ids such as time-ordered UUIDs, then check other shared resources that grow with pod count, such as connections against max_connections, and load test at several pod counts to find the next limit.

Background to this question

Adding instances is the standard answer to 'we need more throughput', and for stateless services it usually works. When it makes things worse, the cause is almost always something all instances share. This debugging question gives us a clear clue, lock waits on one row, and asks us to explain why more pods hurt and how to remove the bottleneck. In this answer, we will reproduce the contention, explain it with the Universal Scalability Law, look at three ways to generate order numbers without the hot row, check for other shared limits, and set up a test that finds the next one before production does.

1Reproducing the Problem

The order service probably generates order numbers with a counters table, something like the code below, inside the same transaction that creates the order.

SQL
BEGIN;
UPDATE counters SET value = value + 1 WHERE name = 'order_no' RETURNING value; -- 100523
-- the row lock on 'order_no' is now held until COMMIT
INSERT INTO orders (order_no, customer_id, total) VALUES (100523, 42, 59.90);
INSERT INTO order_lines (...) VALUES (...); -- several more statements
COMMIT; -- only now can the next transaction update the counter
-- meanwhile, in 31 other pods:
UPDATE counters SET value = value + 1 WHERE name = 'order_no' ...; -- waits for the lock

The UPDATE takes a row lock that PostgreSQL holds until the transaction commits. Every other transaction that wants a number must wait for it. So the throughput of the whole system is limited to one order per transaction duration: if the transaction holds the lock for 5 ms, including the network round trips for the inserts that follow, the ceiling is about 200 orders per second, no matter how many pods run. Notice that the expensive part is not the counter update itself but everything that happens between it and the commit.

2Why More Pods Made It Worse, Not Just Flat

With 8 pods, some requests were waiting for the lock but the lock was not yet the main limit, so 16 pods still raised throughput by 60 percent, less than double, which was already a sign of contention. At 32 pods, almost every transaction is waiting. Waiting is not free: each waiting transaction holds a database connection and a backend process, PostgreSQL manages a longer queue of lock waiters, every commit wakes waiters up, and the pods' own threads and connection pools sit blocked. The database spends more CPU on coordination and less on useful work, and the lock is held a little longer each time because the database is busier. So throughput falls.

3The Universal Scalability Law

This pattern has a name. The Universal Scalability Law, from Neil Gunther, models the throughput of N nodes with two penalties. The contention term, often written alpha, is the fraction of work that must happen one at a time, like waiting for our lock; it alone would make throughput level off at a ceiling. The coherence term, beta, is the cost of keeping shared state agreed between nodes, which grows with the number of pairs of nodes; it makes throughput reach a peak and then decline.

Java
X(N) = N / (1 + alpha * (N - 1) + beta * N * (N - 1))
// with alpha = 0.03, beta = 0.002 (illustrative values):
// X(8) / X(1) = 8 / (1 + 0.21 + 0.112) = 6.05
// X(16) / X(1) = 16 / (1 + 0.45 + 0.48) = 8.29
// X(32) / X(1) = 32 / (1 + 0.93 + 1.98) = 8.18 -> lower than at 16
Relative throughput under contention
PodsMeasuredUSL shape
8baselineabout 6x one pod
16+60 percentabout 8.3x one pod
32below the 16-pod levelabout 8.2x one pod, past the peak

With these illustrative values, 16 nodes are better than 8 but far less than twice as good, and 32 are slightly worse than 16: the same shape the order service showed, although the real drop at 32 pods was steeper, because the waiting itself made each transaction hold the lock longer. In practice we can fit alpha and beta to measured throughput at a few pod counts to predict where the peak is. The lesson is that adding nodes only helps when the shared, serialised part of the work is small.

4Removing the Hot Spot

The fix is to stop every order from touching the same row. There are three good options. The first is a database sequence or identity column. PostgreSQL sequences are designed for exactly this: nextval() hands out numbers without holding a lock until commit, so transactions never wait for each other. The numbers are unique and increasing, but there can be gaps, because a number taken by a transaction that rolls back is not reused.

The second is block allocation. Each pod reserves a block of, say, 1,000 numbers with one short transaction on the counter row, then hands them out from memory. The hot row is touched once per thousand orders instead of every order. Numbers stay unique and roughly ordered, with gaps when a pod restarts. Hibernate's pooled sequence optimizers use this idea with a sequence and an allocation size.

The third is to generate ids without any coordination, such as time-ordered UUIDs (UUIDv7), which are unique, sort roughly by creation time and index well. If the business wants a short human-friendly order number, it can be derived separately, for example from a sequence.

SQL
-- option 1: identity column, no row lock held until commit
CREATE TABLE orders (
order_no bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer_id bigint NOT NULL,
total numeric(12,2) NOT NULL
);
INSERT INTO orders (customer_id, total) VALUES (42, 59.90) RETURNING order_no; -- 100524
// option 2 in JPA: one sequence call per 1,000 ids per pod
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "order_seq")
@SequenceGenerator(name = "order_seq", sequenceName = "order_seq", allocationSize = 1000)
private Long orderNo;

Any of these removes the serial bottleneck, and throughput should again rise with pod count until the next shared limit.

5Other Shared Resources That Grow With Pod Count

The counter row was the clearest bottleneck, but scaling out multiplies other things too. Database connections are the most common: 32 pods with a pool of 20 connections each want 640 connections. If PostgreSQL's max_connections is 500, some pods cannot connect at all, and even below the limit each connection is a process using memory and CPU. A pooler such as PgBouncer in transaction mode, or smaller pools per pod, keeps the total sensible.

Per-pod caches are another. Each pod with its own local cache misses separately, so 32 pods can send 32 times the cache-fill load to the database after a deploy. A shared cache such as Redis, or accepting fewer, larger pods, avoids that. Rate limits on downstream APIs, shared queue partitions and file locks follow the same pattern.

6Finding the Next Limit Before Production Does

Finally, we turn this into a habit. We load test at several pod counts, for example 4, 8, 16, 32 and 64, with production-like traffic, and plot throughput against pods. A straight line means we are scaling well; a curve that flattens shows contention; a curve that turns down shows coherence costs. Fitting the Universal Scalability Law to the points predicts where the peak will be, and the database's lock wait and connection metrics show what causes it. Running this test after significant changes means the next hot row is found in a test environment instead of on a busy day.

Step throughDebugging a scale-out that hurts
  1. Throughput falls as pods are addedClear sign of shared contention
  2. Look at database lock waitspg_locks, pg_stat_activity wait events
  3. One hot row or resource?Same row or counter for every request
    YesRemove the hot spotNoCheck connections and caches
  4. Load test at 4 to 64 podsPlot throughput, fit the USL
  5. Scale to below the predicted peakFind and fix the next limit

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

7Follow-up questions

If the business requires gap-free order numbers, what are your options?

Gap-free numbering is inherently serial, because number N+1 cannot be given out until we know N will commit. The honest options are: keep the counter row but make the locked section tiny, taking the number as the very last step just before commit, so the lock is held for the shortest time; assign gap-free numbers asynchronously after the order is committed, for example a single consumer numbering confirmed orders in sequence, while the order itself uses a UUID or sequence id internally; or keep separate gap-free sequences per legal entity or country, so the serial part is split. Often it is worth asking whether the law really needs gap-free numbers for orders or only for invoices, which can be numbered later by a single process.

Would SELECT ... FOR UPDATE SKIP LOCKED help here?

Not for a single counter row, since every transaction needs that same row. It helps for work queues, where many rows are available and each worker can take a different one.

Why not use a Redis INCR for the counter?

Redis INCR is atomic and fast, so it removes the long lock, but the number is no longer part of the database transaction. A transaction that rolls back leaves a gap, and a Redis failover might reuse or skip numbers unless persistence is configured carefully.

8Conclusion

In this answer, we saw that all orders serialised on one counter row held locked until commit, so more pods only added waiters and coordination, and throughput fell. The Universal Scalability Law describes this with a contention term that caps gains and a coherence term that makes throughput decline. We removed the hot spot with an identity column or sequence, block allocation per pod, or coordination-free ids such as UUIDv7, checked connections and per-pod caches as other shared limits, and set up load tests across pod counts to find the next limit early.

What to say in the interviewAbout 100 seconds aloud

Open with

Every order updates the same row in the counters table, and that row lock is held until the transaction commits, so all orders across all pods go through it one at a time.

Then, if they want more

The ceiling is roughly one order per lock-hold time, no matter how many pods we run.

Going from eight to sixteen pods only gave sixty percent, which was already a sign of contention, and at thirty-two almost everyone is waiting: more connections, more backend processes, more lock management, more wake-ups, so the database does more coordination than work and throughput drops.

The Universal Scalability Law captures this: a contention term caps the speedup, and a coherence term, the cost of keeping shared state agreed, makes throughput fall as nodes are added. The fix is to remove the hot spot.

I would use a PostgreSQL sequence or identity column, which hands out numbers without holding a lock until commit, or allocate blocks of a thousand numbers per pod, like Hibernate's pooled optimizer, or use coordination-free ids such as UUIDv7.

Then I would check other shared resources that grow with pods, especially connections: thirty-two pods with pools of twenty is 640 connections against max_connections, so PgBouncer or smaller pools. And I would load test at several pod counts and plot throughput, to find the next limit before production.

If gap-free numbers are truly required, I would assign them asynchronously after commit or keep the locked section tiny.

About this answer

Last updated
13 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 official documentation for the version you use. 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 "Why did adding pods reduce throughput, and how do you fix it?": https://scrivio-interview-library.netlify.app/architecture/sd-fundamentals/ee8b2140ef/