Skip to Content

Resolving ClickHouse Bottlenecks in High-Volume Billing Pipelines

21 May 2026 by
TechStora

Understanding the Challenge of ClickHouse Bottlenecks

ClickHouse serves as a critical component in the billing pipeline, processing millions of analytical queries daily to calculate user charges. When the pipeline slowed significantly after a migration, the impact was severe, threatening revenue systems and complicating invoice reconciliation. Despite all known performance metrics-such as I/O, memory utilization, rows scanned, and parts read-showing no anomalies, a deeper issue was clearly at play. Identifying the root cause required a detailed examination of ClickHouse's internal mechanisms.

One of the inherent complexities was the database's role as a petabyte-scale analytics platform. With over a hundred petabytes of data distributed across dozens of clusters, the system was built to handle massive ingestion rates of millions of rows per second. However, this scale also introduced hidden risks that standard monitoring tools failed to detect, underscoring the importance of understanding the underlying architecture.

The Role of Data Organization in Query Performance

ClickHouse's design emphasizes the importance of data sorting for query efficiency. In this scenario, data was organized by a primary key structure comprising namespace, indexID, and timestamp. While this architecture allowed for targeted query optimization per namespace, the strategy revealed limitations under certain conditions. A single namespace could inadvertently disrupt the performance of others, especially during high ingestion rates.

The aggregation jobs in question were responsible for summarizing daily data. These jobs play a pivotal role in generating accurate billing reports. The slowdown occurred because the primary key design, while effective in many cases, introduced inefficiencies when dealing with massive, heterogeneous datasets. The default retention policy exacerbated these inefficiencies.

Retention Policy Constraints and Their Impact

The system was constrained by a universal retention policy that applied across all namespaces. While this approach offered simplicity in implementation, it failed to account for the diverse requirements of different internal teams. Some namespaces required data to be retained for extended periods, while others could afford shorter retention windows. This one-size-fits-all strategy led to unnecessary processing overhead and storage bloat.

As data accumulated to over two petabytes, the strain on resources became increasingly apparent. The retention policy caused ClickHouse to handle obsolete data during queries, further compounding the bottleneck. Addressing this required a more granular approach to retention policies tailored to the specific needs of each namespace.

Patch Development to Resolve Underlying Issues

Solving the bottleneck involved the creation of three targeted patches. The first patch focused on optimizing the handling of primary keys to reduce the computational burden during aggregation. By refining the logic associated with primary key traversal, the team achieved notable improvements in query execution times.

The second patch introduced a mechanism for dynamic retention policies. This allowed each namespace to define its own data retention parameters, aligning storage and processing requirements with specific operational needs. The third patch addressed inefficiencies in data compaction processes, ensuring that outdated records were removed without disrupting ongoing queries.

Lessons for High-Scale Database Management

This case highlights the unpredictable nature of performance issues in complex systems like ClickHouse. Relying solely on common monitoring metrics often falls short in diagnosing deep-seated problems. Organizations must develop a thorough understanding of their data architecture and invest in customized solutions to address unique bottlenecks.

Additionally, the importance of flexibility in system design cannot be overstated. Overly rigid frameworks, such as a universal retention policy, can create inefficiencies that ripple through interconnected systems. By adopting adaptable configurations, organizations can prevent such issues while maintaining high levels of performance and reliability.