architecture1 min read

Fixing a Hidden ClickHouse Query Plan Bottleneck at Cloudflare

  • ClickHouse
  • Cloudflare Ready-Analytics
  • ZooKeeper

AI Summary

Cloudflare encountered severe billing pipeline slowdowns after migrating to a finer-grained ClickHouse partitioning strategy. Investigation using CPU and Real flame graphs revealed a major lock contention bottleneck in the MergeTreeData structure during query planning, alongside heavy vector copying. The team implemented three patches: switching to shared locks, deferring vector duplication via shared replicas, and applying binary search on sorted partition IDs. These optimizations successfully broke the correlation between partition count and query latency.

Key Takeaways

  • Identified a severe lock contention issue in ClickHouse MergeTreeData during query planning.
  • Resolved performance degradation through shared locks, shared partition replica caches, and binary search pruning.
  • Contributed improvements back to the upstream ClickHouse project.

Why it matters

Understanding how database internal locks, query planning phases, and partition scaling interact is vital for engineers operating high-throughput analytical data stores. This case study demonstrates rigorous profiling methodologies using flame graphs and upstream contribution practices.

Conversation0

What would you add from your experience?

Checking your session…

Every good conversation starts with one perspective.