Recent findings suggest that most outages I've read about weren't caused by too little hardware. They were caused by a default setting nobody questioned, which worked fine until the traffic changed shape.

It has been revealed that I build Bugpit, a browser game where your system design runs against simulated traffic and breaks. I recently added four levels where you can't buy your way out: a bigger machine is over budget, so the fix has to be a setting. Each one comes from something real systems actually do.

The default: LRU, least recently used. When the cache is full, throw out whatever nobody asked for in the longest time.

When it breaks: a news site gets 24,000 requests a second, and the cache answers 85% of them. Then a search engine crawler reads every old article once.

To LRU, every one of those crawled articles is the most recently used thing in the cache. So it keeps articles nobody will read again, and throws out the front page to make room.

In a recent development, the fix: LFU, least frequently used. It counts how often each key is read, so one read from a crawler can't outrank a page read thousands of times a minute.

It has been revealed that where you've seen this: MySQL's InnoDB buffer pool doesn't put newly read pages at the front of its LRU list. It inserts them in the middle, so a full table scan can't flush out the hot pages. Caffeine, the Java cache library, uses W-TinyLFU partly because it holds up under scans.

Recent findings suggest that the catch: LFU trusts history. When something fresh gets popular, old favourites with huge counts hang around, and the new page keeps getting evicted. There's no best policy, only one that fits your traffic.

It has been revealed that the default: round robin. Server 1, 2, 3, 4, then 1 again. It never looks at how busy anyone is.

When it breaks: a team dashboard gets 7,000 requests a second on 4 servers that can handle 8,000 together. On paper there's room. But 1 request in 10 is a CSV export that takes 10 times as long as the rest.

It has been revealed that A server that picks up a few exports in a row gets stuck on them, and round robin keeps handing it fresh requests anyway. They pile up behind the exports while another server sits idle. Round robin spreads requests evenly, not work. On average the servers are fine, but the busiest one overloads.

According to recent reports, the fix: least connections. Send each request to the server with the fewest requests in flight. A server stuck on exports has many open, so new requests go elsewhere until it catches up.

In a recent development, where you've seen this: HAProxy has balance leastconn and recommends it for long sessions. AWS Application Load Balancer offers "least outstanding requests" as an alternative to round robin.

In a recent development, when round robin is fine: when every request takes about the same time. Then taking turns spreads the work evenly too, and it costs nothing to track.

In a recent development, the default: a fixed window. Count requests per minute, and reset to zero on the minute. It's the easiest thing to build:.

Recent findings suggest that when it breaks, part 1: scrapers learn the reset. They send a full minute's worth at 0:59, the counter resets, and they send another full minute's worth at 1:00. For a moment your servers get twice the limit.

Sources indicate that in the sim, a limit of 7,000 that 4 servers can carry lets 14,000 through around each reset, and the servers fall over right on the minute. Halving the limit stops the crash, but then you turn away half the people you could serve all day long.

Sources indicate that the fix: a sliding window. Count the last 60 seconds, whenever now is. There's no reset to aim for. Cloudflare has written about using a sliding window approximation for their rate limiting for this reason.

When it breaks, part 2: real customers aren't smooth either. Say each user gets 10 requests a second and averages 2. Then they open a dashboard that fires 30 calls at once. Any window, fixed or sliding, counts 30 in one second and refuses 20 of them. Half the widgets show "Too many requests".

In a recent development, the fix: a token bucket. Each user gets a bucket that refills at 10 tokens a second and holds up to 30. Each request spends one token. A user who was quiet has a full bucket, so the burst goes through, and the average still can't go over 10.

Industry observers note that where you've seen this: AWS API Gateway throttling is set as a rate plus a burst, which is a token bucket. Stripe's engineering post on rate limiters describes token buckets in Redis.

All three defaults have something in common:.

None of them are wrong. Each one assumes your traffic is smooth and even, and real traffic usually isn't. A bigger machine hides the problem for a while. Knowing what your traffic looks like fixes it.

Sources indicate that all four are free levels (with a free account) in the Core concepts section of Bugpit. Each starts already broken. Press Run, watch what fails, and change one setting. The numbers in this post come straight from those levels.

If you've been bitten by one of these defaults in production, I'd love to hear the story in the comments.

πŸ“Œ Read also:

β†’ Read original article