Skip to main content

How Cloudflare fixed its ClickHouse billing slowdown

· Do Minh Tuan · Tech Made Simple 💡

How Cloudflare fixed its ClickHouse billing slowdown

Cloudflare's engineering team published an account of a billing pipeline that had slowed down badly. Every second, queries were arriving to calculate invoices for a very large number of customer sites, and the database was spending more time deciding how to answer them than answering them. The culprit was inside ClickHouse, the database underneath.

I like this kind of post because there is no new product to evaluate. It is a debugging story, and the debugging is the useful part.

ClickHouse is a columnar database. Where a normal relational database stores a row with all its fields together, a columnar store keeps each column in its own block. If you want to sum billing amounts across a hundred million rows, you read one column instead of dragging names, addresses and timestamps along with it. For analytics that is a huge difference in speed.

Before ClickHouse runs a query, it plans it. That planning step works out which parts of the data to touch and in what order. On a healthy system it is fast and invisible. Cloudflare found that theirs had grown into the bottleneck, which is a strange place for a bottleneck to live, because nothing in the query text had changed.

Three causes, all in the same phase

Profiling pointed at the planning phase, and the team found three separate problems there.

The first was a lock. ClickHouse was taking an exclusive lock while reading a data structure, which means one query holds it and every other query waits. With enough concurrency, the queue becomes the delay. They changed it to a shared lock, so readers can look at the structure at the same time without blocking each other. If you have ever wondered why database documentation is so fussy about lock types, this is why. One lock choice can decide whether your system scales. The point is not that exclusive locks are wrong, they are the safer default when writes are involved. The point is that this path was read-only and did not need one. The change is only safe because that structure is read far more often than it is written, which is the assumption worth checking in your own system before copying the fix.

The second was copying. Every query was making a fresh copy of the parts list, the structure that records where data lives on disk. On a small table that copy is nothing. On a system with a very large number of parts, and with many queries per second, it turns into a lot of allocation and garbage collection. The fix was to let queries share one master list safely instead of each one building its own.

The third was filtering. The planner was not excluding irrelevant parts early enough, so it looked at data it never needed. Tightening that step cut work before it started, which is always the cheapest place to cut it.

What I take from it

All three fixes are small. None of them required new hardware, a rewrite, or a different database. That is the part worth remembering, because the default response to a slow system in most companies is to add capacity.

Adding capacity would have helped for a while. It would also have hidden the lock and the copying, and the bill would have grown quietly every quarter until someone asked why a billing service needed so many machines. The team found the cause by profiling, which means measuring where the time actually goes instead of guessing from the outside.

If you are chasing something similar, the tooling named in the post was flame graphs and perf. Both show you where CPU time is spent. The skill is reading them without falling in love with the first wide bar you see.

For teams running Node or React backends with a relational database, the same lesson shows up in a different shape. Connection pooling matters, because opening a connection per request is a lock of your own making. Read replicas help, because analytical queries and live user writes compete for the same resources, and the analytical ones usually lose politely until the whole thing falls over. Keeping heavy reporting off the primary database is cheaper than upgrading the primary database.

The business side of a performance bug

The original post frames this as a reason not to throw money at hosting when the real problem is code. I agree with the direction and I would be careful with the numbers. Vendor write-ups tend to attach a percentage saving to this kind of fix, and a specific figure from someone else's architecture does not transfer to yours.

What does transfer is the method. When a page gets slow, the first question should be where the time goes, not which plan to upgrade. A team that can answer that question will save more over a year than a team that buys its way out of every incident, and it will also stop the same incident from coming back.

I would add one thing the post does not emphasise. Write down what you changed and how you measured it before and after. Performance work has a way of being reverted six months later by someone who sees an unfamiliar optimisation and assumes it is dead weight. A short note with the numbers is what keeps the fix alive.

The question I would still ask is about the billing side. A planner that is slow is annoying. A planner that returns a slightly different result under load is a finance problem. The post does not say whether they ran reconciliation after the change, and I would want to know before calling it closed.

Written with AI assistance, reviewed and edited by Do Minh Tuan.