ClickHouse and OLAP Databases
ClickHouse is an open-source, column-oriented database built for analytical queries: aggregating billions of rows in well under a second. It started inside Yandex to power a web analytics product and is now one of the most widely used OLAP (online analytical processing) databases, alongside Apache Druid and Apache Pinot, which solve the same problem with different trade-offs.
What makes these systems different from Postgres or MySQL:
- Columnar storage: each column is stored separately, so a query that touches 4 of 60 columns reads only those 4 from disk.
- Heavy compression: values in one column look alike (the same country codes, increasing timestamps), so they compress very well, often by 5-10x or more. Fewer bytes on disk means fewer bytes to read.
- Vectorized execution: queries process columns in batches of thousands of values at a time, using tight loops that make full use of CPU caches and SIMD instructions.
- Built for append-heavy data: events, logs and metrics are inserted in large batches and rarely changed. Updates and deletes exist but are expensive.
- Scale-out: data is sharded across nodes and every node scans its share in parallel.
OLAP databases show up in interviews wherever a design needs to answer aggregate questions over a lot of events quickly: the query side of an ad click aggregator ("clicks per ad per minute for the last 24 hours"), a metrics and monitoring system, product analytics dashboards, a "who viewed your profile" feature, a leaderboard over historical data. Saying "raw events go through Kafka into ClickHouse (or Druid, or Pinot), and the dashboard queries it" is a common, strong answer. You should be able to say why a row-store database falls over here, how a column store gets its speed, and what it gives up in return.
OLTP vs OLAP
The databases most systems start with are OLTP (online transaction processing) databases. They're built for many small reads and writes that each touch a few rows: create an order, update a balance, fetch a user's profile. OLAP workloads look completely different.
| OLTP (Postgres, MySQL, DynamoDB) | OLAP (ClickHouse, Druid, Pinot) | |
|---|---|---|
| Typical query | Fetch or update one row by key | Aggregate millions to billions of rows |
| Rows touched per query | 1-100 | 10⁶-10¹¹ |
| Columns touched | Most of the row | A handful of many |
| Writes | Small, frequent, in-place updates | Large append-only batches |
| Concurrency | Thousands of short transactions | Tens to thousands of heavier queries |
| Storage layout | Row-oriented | Column-oriented |
| Transactions | Full ACID | Limited or none |
Consider an events table with 60 columns and 10 billion rows, about 1 KB per row. A query like
SELECT country, count() AS clicks
FROM events
WHERE event_type = 'click' AND ts >= now() - INTERVAL 7 DAY
GROUP BY country
ORDER BY clicks DESC;
needs three columns. A row store reads entire rows, so even with an index on ts it pulls in roughly 10 TB if the week covers most of the table, 59/60ths of it useless. A column store reads only country, event_type and ts. Those might be 20 bytes per row raw and a few bytes after compression, so the same query reads tens of gigabytes. Spread across 10 servers that each scan a few GB/s, that's around a second.