Choosing a database in system design: SQL, NoSQL, NewSQL, columnar and time-series
The limits of one relational database in numbers, why an RDBMS is hard to scale out, five NoSQL families and NewSQL scored on the same scale, columnar for OLAP, time-series for metrics, and how to choose by the level of contention.
On this page
1. The problem
A team plans to move from PostgreSQL to Cassandra because "the orders table is big and writes are slow". Measured: 50 million orders/day ≈ 580 orders/s, peak ×5 ≈ 2,900 TPS. But the real bottleneck is a flash sale: every request updates the same product_id = 42 row. Cassandra can't save that row, and the team loses transactions on top.
2. Key idea
Know how much one node handles before mentioning NoSQL; most systems never exceed it. RDBMSs are designed for one machine with strong consistency: joins, foreign keys and transactions assume the data is in one place, so scaling out is your job. NoSQL is distributed from the start, and many systems use an LSM-tree to turn random writes into sequential ones.
| Family | Examples | Strong at | Costs |
|---|---|---|---|
| Relational | PostgreSQL, MySQL | transactions, joins, consistency | hard to scale writes out |
| Key-value | Redis, DynamoDB | very fast reads and writes by key | queries are mostly by key |
| Wide-column | Cassandra, ScyllaDB | heavy writes, availability | query-first table design, eventual consistency |
| Document | MongoDB | flexible schema | weak joins |
| Columnar | ClickHouse | scanning many rows, few columns, great compression | row-level updates are bad |
| NewSQL | TiDB, CockroachDB | cross-shard ACID, automatic sharding | 1–2 extra consensus round trips |
2.1 How does contention change the choice?
| Situation | Use | Don't use |
|---|---|---|
| High concurrency, low contention (each user edits their own data) | sharded RDBMS or NewSQL | one node once peak exceeds its limit |
| Medium concurrency, high contention (seat holds, stock) | one node, conditional updates, short transactions | sharding: the hot row stays on one shard |
| Very high concurrency, high contention (flash sale) | Redis Lua in front, split stock into N sub-rows, async DB writes | changing database |
3. In SQL
-- Medium contention: one statement, no read-then-write
UPDATE product SET stock = stock - 1 WHERE id = 42 AND stock > 0; -- 0 rows: sold out
-- Hot row: split stock into 10 sub-rows, each request picks a random bucket
UPDATE product_stock SET stock = stock - 1
WHERE product_id = 42 AND bucket = ? AND stock > 0;
-- Metrics over time: TimescaleDB splits the table into daily chunks
SELECT create_hypertable('device_metric', by_range('ts', INTERVAL '1 day'));
SELECT drop_chunks('device_metric', older_than => INTERVAL '90 days'); -- instant deletionClickHouse, Elasticsearch, and why "pick two of CAP" misleads
- Columnar: each column is its own file, so
SELECT sum(net_price) WHERE date_key = ...reads two columns instead of 20. Same-typed columns compress very well. Row-by-row inserts into ClickHouse create thousands of small parts and a too many parts error: batch through Kafka or a buffer, and keep the source of truth in OLTP. - Elasticsearch is fast thanks to an inverted index (term → list of documents); don't use it as the source of truth.
- CAP: you only choose between consistency and availability during a network partition. Normally, the real trade-off is consistency versus latency (PACELC).
4. Working with AI
Hand to AI: back-of-the-envelope estimates from user counts and behavior, feature comparisons between databases, k6 load tests with real transactions.
Check yourself in AI-written proposals:
- There are numbers: peak TPS, data size, divided by one node's limit.
- The cheap steps (index, archive, cache, replicas) were tried before proposing a new database.
- It says whether multi-record transactions are needed, and what you lose without them.
- Hot rows are handled by design, not by sharding.
5. Exercises
Exercise 1: Review a "move to MongoDB" proposal
The AI proposes moving a ticket booking system to MongoDB "to scale". Load: 300 write TPS at peak; each booking decrements seats and creates a booking in one transaction; daily revenue reports. What do you answer?
Solution 1
- 300 TPS is within one PostgreSQL/MySQL node; there's no reason to scale out.
- Booking needs multi-record transactions with per-seat contention: keep the RDBMS, use conditional updates or optimistic locking, keep transactions short.
- Revenue reports: a read replica, or ClickHouse via CDC if the scans are large.
- Propose a database change when measurements show one node isn't enough after index, cache and replicas.
Exercise 2: Pick databases for four features
An app has (a) chat messages, very write-heavy, read by conversation; (b) a product catalog with attributes varying by type; (c) revenue reports scanning 2 years of data; (d) temperatures from 100,000 sensors every 10 seconds. Pick a family for each and explain.
Solution 2
| Feature | Pick | Why |
|---|---|---|
| Chat | Cassandra/ScyllaDB, partitioned by conversation_id |
heavy writes, reads by key, no transactions |
| Catalog | PostgreSQL with jsonb, or MongoDB |
flexible attributes; PostgreSQL is enough if you already run it |
| Reports | ClickHouse, loaded through CDC | scans many rows, few columns |
| Sensors | TimescaleDB or InfluxDB | 10,000 points/s, append-only, deleted by age |
Every new database is another system to run, back up and monitor. A small team should start with PostgreSQL for (b), (c), (d) and split out when measurements demand it.
Summary
- One node: ~20K read QPS, ~1,000 write TPS, ~200 TPS on one contended row; divide peak by that.
- Big table: query, index, archive, cache, replicas before sharding.
- Need multi-record transactions: RDBMS or NewSQL; otherwise NoSQL by main query.
- Columnar for OLAP with batch inserts; time-series for metrics with chunk drops.
- Sharding doesn't save a hot row: reduce contention by design.
Further reading: Sharding: choosing a shard key and resharding.
Comments
Community rules