Citus swirl

With Citus, Algolia Speeds Up Search Analytics & Eradicates Database Management Headaches

5-10B
rows ingested
per day
800ms
response times for 95% of queries

Businesses use Algolia’s hosted search as a service to power search-based tools on their own websites and apps. Algolia also provides its customers with a variety of analytics about their end users’ search behaviors. For example, customers can see the most popular search terms in the past week, or which queries did not return any results in the past month.

Previous analytics solution suffered from frequent slowdowns & crashes, taking developer time away from Algolia’s core application

Algolia initially used a self-hosted Elasticsearch cluster to power its analytics features. However, Algolia’s API regularly handles more than one billion search operations per day, as well as frequent traffic spikes—and managing the Elasticsearch cluster during these spikes was becoming a full-time job, preventing the Algolia team from focusing on their core application.

“Being a SaaS company, we often talk about the value of focusing on your core business rather than on maintaining tools. As such, we were ready to migrate to something easier to manage, so we could focus on building features,” says Friquet. “It was critical that we find something new.”

In their search for the right database solution, the team at Algolia decided that they’d like to leverage a relational model for their future products and features.

Algolia evaluated options to drive up performance & enable scale. The goal: a scalable solution that wasn’t a pain to maintain

The Algolia team began searching for a new database that would meet its scaling demands and be easy to manage, so the team could focus instead on building features. “We didn’t have the will or the resources to dedicate someone full-time to making sure our Elasticsearch cluster scaled with us,” says Friquet.

Algolia first considered Google BigQuery. “We decided not to use BigQuery for two reasons,” says Friquet. “First, it didn’t give us the speed we needed for real-time queries. Second, BigQuery pricing is based on the amount of data scanned by the queries. With a public API and a growing base of customers and API calls, it would have been difficult to estimate our query volume and associated costs.”

Next, the team evaluated Amazon Redshift. However, without the combination of rollups and approximation algorithms like HyperLogLog (HLL) and TopN, the cost of delivering sub-second aggregations with Redshift would have been prohibitively expensive. Friquet also considered Clickhouse but ruled it out because no cloud offering was available.

Finally, Friquet began looking at Citus. He was intrigued by the ability to distribute tables and queries across multiple nodes. Citus also provided extensions useful for real-time analytics, such as TopN and HLL. And a fully-managed database as a service was available too. He quickly engaged the Citus team for help with proof of concept testing.

At Algolia, our job is to provide the best search as a service, not to maintain clusters of PostgreSQL servers. We really wanted a database that scales, a database we wouldn’t have to worry about. Citus has given us that.
Sylvain Friquet, Former Software Engineer, Algolia

Citus helped Algolia complete a successful proof of concept

The Algolia and Citus teams worked side by side to create a new approach to the scale and reliability challenges Algolia was facing. “Citus helped us design our solution,” says Friquet. “That help has been very, very valuable. At first we had a weekly meeting. Later we set up a Slack channel—we still use it today. The communication has been extremely smooth. That really helped to clear all the last blocking points.”

Benchmark tests showed that the Citus database would be capable of ingesting up to seven million rows per second.

“We knew we needed to ingest a lot of data,” says Friquet. “But we also knew that because we roll up data into precalculated metrics tables every five minutes and every 24 hours, we were going to have to delete a lot of data at different intervals. Citus handles our complex ingest and delete operations seamlessly—and that’s with a workflow that creates five to ten billion new rows of data per day. The way in which Citus shards and distributes data across multiple nodes has given us the parallelization we need.”

Ninety-five percent of the queries made to our Citus database receive responses in less than 800 milliseconds. That’s a huge improvement over our previous database.
Sylvain Friquet, Former Software Engineer, Algolia

Citus Cloud enables Algolia to ingest 5-10B rows per day—and growing

Because a single search on the user side can generate many events, Algolia currently ingests between five and ten billion rows per day—and the company expects this volume to double annually.

“A search is not just a simple search query,” explains Friquet. “It can also include filters. For example, let's say you are on an e-commerce website that sells clothes. Each brand may be a category, so there could be a filter for each brand. So you may search for something and then refine on a category. Price can be a category—for example, if you want to see t-shirts between $20 and $50. We also track which objects are fetched and displayed in the results. So one search might actually generate multiple events, not just one.”

As Algolia grows, Friquet plans to scale its Citus database geographically. “Right now we have a single Citus database cluster, and we host it in the US,” says Friquet. “Customers are asking for other options. So we plan to start spinning up new Citus database clusters outside the US—soon, we’ll have a cluster in Europe.”

Algolia architectural diagram
Architecture of Algolia’s Search Analytics solution, featuring the Citus database

Sharding and rollup tables reduce query response times to less than 800 milliseconds

After Algolia ingests a customer’s data, the data is distributed across shards. Raw events are aggregated every five minutes, and then again daily, into dedicated rollup tables. These rollup tables hold pre-computed metrics for a given time range. Algolia’s APIs target the rollup tables, never the raw tables.

“Thanks to Citus and our rollup approach, we don't have to keep the raw data—just the aggregate,” says Friquet. “That’s useful because it reduces the amount of data we have to keep in our database. That saves us money, makes our queries faster, and makes some queries possible that would not be possible otherwise.”

The speed at which Algolia responds to search requests has been dramatically reduced by adding Citus to the stack. “Ninety-five percent of the queries made to our Citus database receive responses in less than 800 milliseconds,” says Friquet. “That’s a huge improvement over our previous database.”

Citus handles our complex ingest and delete operations seamlessly—and that’s with a workflow that creates five to ten billion new rows of data per day. The way in which Citus shards and distributes data across multiple nodes has given us the parallelization we need.
Sylvain Friquet, Former Software Engineer, Algolia

TopN extension makes it easy to calculate the most frequently occurring values

Another way Algolia makes queries faster is by pre-computing key metrics like search term rankings.

TopN is an open source extension to Postgres, created by Citus Data. Algolia uses TopN to calculate the most frequently occurring values in a column so they can keep top items sorted by frequency. With TopN, Algolia's APIs can quickly find the most popular queries in a given time frame across the entire distributed database. This feature is useful for Algolia’s customers such as Hacker News, whose search is powered by Algolia.

“Citus is more than just a distributed database,” says Friquet. “The fact that Citus provides Postgres extensions like TopN and HyperLogLog, which are not available on Redshift, is very valuable to us.”

Freed from database management tasks, Algolia can now devote more time to developing new products

After having to perform maintenance on its Elasticsearch database on a weekly basis, the Algolia team is thrilled with the decision to migrate to Citus. “At Algolia, our job is to provide the best search as a service, not to maintain clusters of PostgreSQL servers,” says Friquet. “We really wanted a database that scales, a database we wouldn’t have to worry about. Citus has given us that.”

Since Algolia put Citus into production, Friquet says the company hasn’t had any database downtime or major outages. “With Citus, our database just works,” he says. “The on-call load for our devops team is pretty close to zero.”

“We see analytics as the pillar for whole new categories of Algolia products,” says Friquet. “For example, we shipped Click Analytics earlier this year. Now, in addition to tracking which searches are being made, we can also track clicks and conversion. When we merge this with search data, we can tell our customers not only what their users are searching for, but we can also quantify the value of those searches.”

Algolia screenshot
Algolia’s Search Analytics, powered by the Citus Cloud database

Citus is more than just a clever way to scale out PostgreSQL

As the Algolia team builds out new analytics capabilities, they will continue to work with Citus to help optimize performance. “Citus is more than just a technical product,” says Friquet. “There’s a team behind it. It has been a real pleasure to work with the Citus engineers. They are always responsive, and they pay attention to what we say and what kind of help we need. Both the Citus database and our relationship with the Citus team have been very, very worthwhile.”

Algolia + Citus Data Story Highlights

algolia.com

  • With Citus, response times for 95% of queries are less than 800 milliseconds
  • Citus Cloud enables Algolia to provide analytics at scale, for over 1B searches per day
  • Went from frequent downtime with previous analytics solution—to no downtime with the Citus Cloud database

At Algolia, our job is to provide the best search as a service, not to maintain clusters of PostgreSQL servers. We really wanted a database that scales, a database we wouldn’t have to worry about. Citus has given us that.

Sylvain Friquet, Former Software Engineer, Algolia

About Algolia

Algolia is the single best way to build search into your mobile app or website. Trusted by 5,500+ customers, Algolia’s hosted search API gives developers a lightning-fast engine with out-of-the-box support for a typo-tolerant, as-you-type search experience. Powering 25,000+ apps, Algolia analytics give customers specialized search metrics to help them understand how their search performs.

algolia.com