Momentic’s Innovative Transition to ClickHouse: A Caching Revolution
In the rapid landscape of software testing, particularly with AI-driven solutions, performance and efficiency are paramount. Momentic, a trailblazer in this domain, recently undertook a significant overhaul of its caching system to accommodate an astronomical growth in data and usage. The transition from PostgreSQL to ClickHouse has allowed Momentic to manage over 2 million queries per day across a staggering 20 billion total entries while maintaining a swift average response latency of around 250 milliseconds.
The Challenge: PostgreSQL Performance Limits
Initially, Momentic relied on PostgreSQL for its caching needs. However, the demanding nature of their workload posed challenges. The cache table ballooned from approximately 80,000 entries to an eye-watering 1 billion entries. This unprecedented expansion highlighted PostgreSQL’s inherent performance limitations, primarily due to high-write, high-read workloads that led to increased resource consumption and lock contention.
As stated by Momentic’s team, “Because of the high-write, high-read workload, we were running into elevated resource usage as well as lock contention from queries trying to read and write to the cache concurrently.” As the number of cache entries grew, these issues became exacerbated, making it imperative to seek a more robust solution.
The Move to ClickHouse: Architectural Advantages
When Momentic made the strategic decision to transition to ClickHouse, they were initially facing around 600,000 cache lookups per day and a critical requirement for sub-second latency. The architectural advantages of ClickHouse played a pivotal role in this migration. Unlike PostgreSQL, which utilizes B-tree indexes, ClickHouse employs sparse primary indexes. This distinction allows ClickHouse to efficiently narrow down searches to a minimal number of “granules” based on known key values, streamlining query processing dramatically.
Momentic’s team meticulously crafted a primary key consisting of critical fields such as test ID, step ID, Momentic version, git branch, and commit timestamp. This careful design facilitated efficient lookup behavior in 90% of cases, particularly with feature branches. However, the architecture posed challenges when dealing with main branches, which necessitated accessing a vast number of entries.
“This meant that most queries were reading 1-2 data parts,” they noted, “but some outliers were reading almost all of the parts on every query, leading to spiky memory usage and disk operations.”
Leveraging Materialized Views
To combat the challenges of immense data sets, Momentic adopted materialized views to precompute available commit timestamps linked to each test ID. This strategic move allowed them to jump directly to the pertinent data part with enhanced precision and speed.
Streamlining Query Operations
Another critical overhaul involved reducing the number of queries per test run. Momentic found that the three separate queries designed for PostgreSQL were ill-suited for ClickHouse’s architecture.
In a remarkable shift, they transitioned to a framework utilizing only INSERT operations combined with ClickHouse’s ReplacingMergeTree. This allowed them to retrieve the caches, reinsert those used to extend TTL (Time-to-Live), and seamlessly add new caches after each test run. ClickHouse effectively handles the deduplication of entries asynchronously. The elegance of this new process led Momentic to eliminate the Redis layer, which had become less valuable due to rising cache key cardinality.
The Migration Process
Momentic executed the migration with a structured strategy to ensure reliability. They ran ClickHouse in parallel with PostgreSQL, duplicating all writes to both systems. During this phase, production traffic continued to be served through PostgreSQL while ClickHouse was subject to “shadow querying.” The results from both systems were compared to ensure data integrity, which provided reassurance about the accuracy of the implementation.
Once confidence in the new setup solidified, the company gradually shifted traffic from PostgreSQL to ClickHouse. To further guarantee safety, dual writes were maintained for an extended period, enabling a straightforward rollback if required.
Performance Outcomes
The successful rearchitecture has empowered Momentic to scale its cache solution to 20 billion entries, efficiently managing over 2 million daily queries with an impressive average response latency of 250 ms. This leap not only enhances user experience but also solidifies Momentic’s position as a leader in AI-driven software testing solutions.
The exploration of such a transformative migration underscores the critical nature of adapting technology to meet evolving demands in today’s fast-paced software environment.
For further insight into the intricacies of this migration and the accompanying design considerations, readers are encouraged to refer to the original article.
Inspired by: Source

