ClickHouse vs. PostgreSQL: Scaling Mobile Telemetry to Millions of Events
When starting a mobile app project, PostgreSQL is the default choice for everything: user accounts, subscription state, and feature telemetry tracking. It is reliable, ACID-compliant, and well-understood. But as active users grow, event logs quickly scale to tens of millions of rows. Suddenly, query performance plunges, and PostgreSQL struggles under the load.
Row-Oriented vs. Column-Oriented Storage
The difference between PostgreSQL and ClickHouse lies in how they store data on disk:
- PostgreSQL (Row-Oriented): Stores complete rows next to each other. To calculate the average read time of articles in a blog, Postgres must load every column of every row from disk into memory, filtering out the unnecessary columns afterwards.
- ClickHouse (Column-Oriented): Stores data column by column. If you run a query to count event types, ClickHouse only reads the
event_typecolumn from disk, ignoring user IDs, timestamps, and devices. This reduces disk I/O by orders of magnitude.
Row-Oriented (Postgres):
+-------------------------------------------------------+
| Row 1: [ID1, Timestamp1, Event1, Device1, Country1] |
| Row 2: [ID2, Timestamp2, Event2, Device2, Country2] |
+-------------------------------------------------------+
Column-Oriented (ClickHouse):
+-----------------------------------------+
| Column ID: [ID1, ID2] |
| Column Event: [Event1, Event2] |
| Column Country: [Country1, Country2] |
+-----------------------------------------+
ClickHouse MergeTree Engine Schema
Here is how a telemetry event tracking table is optimized in ClickHouse. The key is organizing the data by partition (e.g. monthly) and sorting by event and timestamp to optimize range queries:
CREATE TABLE telemetry_events (
event_id UUID,
user_id UUID,
event_name LowCardinality(String),
device_os LowCardinality(String),
country LowCardinality(String),
timestamp DateTime64(3),
properties String -- JSON payload
) ENGINE = MergeTree()
PARTITION BY toYYYYMM(timestamp)
ORDER BY (device_os, event_name, timestamp)
SETTINGS index_granularity = 8192;
Why the Query Times Diverge at 10M Rows
The figures below are indicative of the architectural difference between a row-store and a column-store on analytical queries — they are not a benchmark we ran on published hardware, and you should reproduce them on your own data before planning around them. The ratio is the point, not the absolute numbers: a columnar engine reads only the columns a funnel query touches, so it scales with column count rather than table width.
| Query Type | PostgreSQL (Indexed) | ClickHouse | Performance Gain |
|---|---|---|---|
| Count All Events | 1.8s | 0.02s | 90x faster |
| 3-Step Funnel | 4.5s | 0.12s | 37x faster |
| Monthly Active Users (Unique) | 3.1s | 0.05s | 62x faster |
When to Migrate
You should consider introducing ClickHouse alongside your primary PostgreSQL database if your event table grows by more than 100,000 rows per day, or if dashboard queries take longer than 2 seconds. The best architecture utilizes both: keep PostgreSQL for high-value relational transactions and stream raw events asynchronously into ClickHouse.
Get the BillDog newsletter
Practical guides on subscription pricing, paywalls and app growth, about twice a month. No spam.
