Video in progress

When I added threads, the system got slower instead of faster. How do Amdahl's law and the Universal Scalability Law explain this?

Amdahl's law: the sequential part of the code sets the limit on speedup. USL adds a coherency cost to that: the time threads spend coordinating shared state grows quadratically with the thread count, so past a certain point throughput drops.

📖 Answer

Amdahl: if a fraction s of the work is sequential, the maximum speedup with N threads is 1 / (s + (1 - s)/N). With a 5% sequential part, even infinite cores won't get you past 20x. The sequential part is often invisible: a global lock, a log appender, a single DB row, a synchronized singleton.

But Amdahl says "adding threads doesn't increase speed", not "it decreases". The decrease is explained by USL (Neil Gunther):

C(N) = N / (1 + α(N − 1) + βN(N − 1))
  • α is contention: waiting in a queue (similar to the sequential part in Amdahl);
  • β is coherency: every thread has to agree on state with the others, through cache line ping-pong, CAS retries, distributed locks, cache invalidation.

If β > 0, the curve rises to a peak and then goes down. On prod it looks like this: the pool was raised from 32 to 64, CPU went up to 90%, and throughput fell by 15%.

How I use it in practice:

  1. In a load test I vary N as 1, 2, 4, 8, 16, 32... and measure throughput.
  2. I fit USL to the results (simple regression, in Excel or Python) and get α and β.
  3. If α is large, I look for the sequential part (a lock, a single resource). If β is large, I look for shared mutable state (a shared counter, a global cache, false sharing).
  4. The peak of the model becomes the upper bound for the pool size and the number of instances.

This way of thinking applies to distributed systems too: adding pods also stops helping after a point if they fight over a shared DB row or a Redis lock. That's why in capacity planning, before the question "how many instances do we need", I ask "what is shared".