Cruxly

Cruxly › System design › Principles of Distributed Design

Make the Expensive Path Rare

if you cannot make an operation fast, make it uncommon

Open the interactive lesson

The principle: some operations are irreducibly expensive — a cross-shard join, a consensus round, a cold read from disk, a call to a slow third party. You usually cannot make them cheap. You can almost always make them rare, and rare-and-expensive is a system that performs well.

This is the principle underneath caching, denormalisation, batching and precomputation, all of which are usually taught as separate techniques. They are one idea wearing four costumes: move work off the common path.

The violation: optimising the wrong end

The same error, in other shapes:

Recompute a user's recommendation set on every page load
  → it was expensive; now it is expensive 40,000 times/second

Count rows with SELECT COUNT(*) on every dashboard render
  → the count is expensive and the answer barely changes between renders

Call the pricing service for every line item in a cart
  → N calls on the hot path where one batched call would do

The four ways to make a path rare

:::key How to choose between them Ask what the expensive operation is expensive in. Expensive in computation → precompute or cache. Expensive in round trips → batch. Expensive in the amount of data that must be examined → approximate. Choosing the wrong one produces a solution that adds complexity without removing cost. :::

The discipline: the rare path must still work

Three obligations follow:

How to say it in the interview

"I can't make this operation cheap — a cross-shard query is going to cost what it costs. So the design question becomes how to make it rare rather than how to make it fast, which usually means precomputing the common case and accepting the expensive path only for the long tail."

Checklist

Practice this interactively
Share this lessonWhatsAppLinkedInX

Related lessons