Key technologies
Postgres
The default answer for a system of record, and usually the right one. The interview is about knowing exactly where it stops being right: MVCC bloat, connection limits, and the single writer.
Postgres is what most system designs should start with and what most candidates abandon too early. “Postgres won’t scale, so we’ll use Cassandra” is a sentence that loses points with any interviewer who has run Postgres at a few terabytes. “Postgres, until this specific limit, and here is how I’d see it coming” wins them.
This lesson is that specific knowledge: how it stores and versions rows, why it needs vacuuming, what an index can and can’t do, and the real ceilings.
What Postgres actually is
A single-node, process-per-connection relational database with MVCC (multi-version concurrency control), a write-ahead log for durability and replication, and a cost-based query planner. One primary takes writes; any number of replicas replay its WAL to serve reads.
The two halves of that sentence that interviews hinge on: one primary for writes, and process per connection.
MVCC: why readers never block writers
When a row is updated, Postgres does not overwrite it. It writes a new version of the row
(a new tuple) and marks the old one as dead as of that transaction. Every tuple carries the ids of
the transaction that created it (xmin) and the one that deleted it (xmax).
A query sees the versions that were committed before its snapshot was taken. So:
- Readers never block writers, and writers never block readers. A long report reads the old versions while updates write new ones.
- Writers still block writers on the same row — the second
UPDATEwaits for the first to commit. That is the row lock the locking lesson builds on.
An UPDATE in Postgres is an insert of a new version plus a delete-mark on the old one. That single
fact explains table bloat, why heavily updated tables need tuning, why HOT updates exist, and why
updating one column of a wide row costs about as much as rewriting the row.
VACUUM, bloat, and the wraparound trap
Dead tuples stay on disk until VACUUM reclaims them, and it can only reclaim a version once no running transaction could still need it. Autovacuum does this in the background.
It goes wrong in two ways:
- Bloat. A table that is updated faster than autovacuum keeps up — a job-status table, a
counters table — grows with dead tuples. Scans slow down and indexes swell. The fix is tuning
autovacuum per table (lower
autovacuum_vacuum_scale_factorfor big hot tables), not runningVACUUM FULL, which locks the table. - A long transaction blocks all of it. One forgotten
BEGINin an idle session, or a replica withhot_standby_feedbackrunning a six-hour report, pins the oldest snapshot. Nothing newer can be vacuumed anywhere in the database.
Transaction ids are 32-bit. If vacuum cannot freeze old rows — usually because a long-running transaction is holding it back — the database approaches transaction id wraparound and Postgres will eventually refuse writes to protect data. It is rare, it has taken down large companies, and “monitor the age of the oldest transaction and alert on idle-in-transaction sessions” is the answer that shows you have operated it.
Indexes: what each type is for
| Index | Good for | Example |
|---|---|---|
| B-tree (default) | Equality, ranges, sorting, prefix LIKE 'abc%' |
WHERE created_at > now() - interval '1 day' |
| Hash | Equality only | Rarely better than B-tree |
| GIN | “Contains”: arrays, jsonb, full-text |
WHERE tags @> '{kafka}', to_tsvector(...) @@ query |
| GiST | Geometric, ranges, nearest-neighbour | PostGIS, exclusion constraints on time ranges |
| BRIN | Huge, naturally ordered tables | Append-only events by timestamp — tiny index |
Three ideas that come up in follow-ups:
- Composite index column order matters. An index on
(customer_id, created_at)servesWHERE customer_id = ?andWHERE customer_id = ? AND created_at > ?, but notWHERE created_at > ?alone. Equality columns first, range column last. - Covering indexes (
INCLUDE (status)) let the query be answered from the index alone — an index-only scan — skipping the table. - Partial indexes (
WHERE status = 'pending') index only the rows you query. A queue table with millions of done rows and a few hundred pending ones wants exactly this.
Every index makes every write slower and costs disk. Add them for queries you actually run, and
check with EXPLAIN (ANALYZE, BUFFERS), not intuition.
You can see the planner change its mind in a minute with Docker:
-- docker run --rm -e POSTGRES_PASSWORD=pg -p 5432:5432 postgres:18
CREATE TABLE orders AS
SELECT g AS id, (random() * 100000)::int AS customer_id, now() - g * interval '1 second' AS created_at
FROM generate_series(1, 2000000) g;
ANALYZE orders;
EXPLAIN ANALYZE SELECT * FROM orders WHERE customer_id = 42; -- Parallel Seq Scan, tens of ms
CREATE INDEX ON orders (customer_id, created_at);
EXPLAIN ANALYZE SELECT * FROM orders WHERE customer_id = 42; -- Index Scan, well under 1 ms
Transactions and isolation
The default isolation level is Read Committed: each statement sees data committed before it
started. That allows the lost update and other anomalies. Repeatable Read gives the whole
transaction one snapshot and raises a serialization error (40001) if you try to update a row
someone else changed. Serializable (SSI) detects every anomaly and aborts one of the
transactions; it costs some throughput and requires retry logic.
The practical answer: Read Committed plus explicit SELECT ... FOR UPDATE or atomic updates where it
matters, and Serializable only for the few transactions whose invariants span rows you can’t lock
directly.
Connections: the ceiling people hit first
Every connection is an OS process with its own memory, typically several MB. A few hundred is fine; a few thousand thrashes the machine. Now add 50 app pods with a pool of 20 each, and you are at 1,000 before a single autoscale event.
PgBouncer in transaction mode is the standard fix: thousands of client connections share a few
dozen server connections, each borrowed only for the duration of a transaction. The catch is that
session state — SET variables, advisory locks held across transactions, some prepared-statement
setups — does not survive the hand-off.
“We’ll autoscale the API to 200 pods” without a word about connection pooling is how a traffic spike
becomes a database outage. Size pools from the database’s limit backwards: max_connections divided
by the number of pods is the most each pod may hold.
Postgres as everything else
It is often the right answer for jobs people reach for other systems to do:
- Queue:
SELECT ... FOR UPDATE SKIP LOCKED LIMIT 10lets many workers each claim different rows. Good to thousands of jobs per second, and it is transactional with your data. - Document store:
jsonbwith a GIN index handles flexible attributes without a second database. - Search:
tsvectorfull-text search with ranking is fine for a product catalogue of a few million rows. Beyond that, or for fuzzy relevance, see Elasticsearch. - Vectors:
pgvectorwith an HNSW index for embeddings at moderate scale. - Pub/sub:
LISTEN/NOTIFYfor small fan-out signals — not a durable event bus.
Scaling it
In order, roughly from cheapest to most expensive:
- Index and fix queries. Most “Postgres is slow” is a missing index or an N+1.
- Vertical scaling. Machines with hundreds of GB of RAM are ordinary. A working set in memory changes everything.
- Read replicas for read-heavy paths that tolerate a little lag — with the read-your-writes care from the replication lesson.
- Partitioning big tables by time or tenant: faster maintenance, and dropping a month is
DROP TABLErather than a giantDELETE. - Caching in front of hot reads.
- Sharding — application-level, or Citus — when writes or storage outgrow one primary. This is the one-way door; the partitioning lesson is about choosing the key.
Postgres versus the alternatives
| Reach for it when | Avoid when | |
|---|---|---|
| Postgres | System of record, relations, transactions, mixed queries | Write volume beyond one primary and you won’t shard |
| MySQL | Team knows it; Vitess for sharding at huge scale | You want rich types, jsonb, extensions |
| Cassandra | Huge write volume, known query patterns, multi-region writes | You need joins, ad-hoc queries, or transactions |
| DynamoDB | Serverless, predictable key-value access on AWS | Access patterns are still changing |
| CockroachDB / Spanner | Postgres-like SQL with horizontal writes and strong consistency | You don’t need it — latency and cost are higher |
When to propose Postgres in an interview
Almost always, for the system of record: users, orders, payments, inventory — anything with relationships, constraints and transactions. Say what the ceiling is and what you’ll do at it.
“Orders go in Postgres: they’re relational, they need transactions, and at 2,000 writes a second we’re nowhere near one primary’s limit. I’d put PgBouncer in front so the API can scale out without exhausting connections, add a read replica for the order-history page with read-your-writes on the user’s own orders, and watch autovacuum on the order-status table because it’s updated constantly. If writes grow 20×, I’d shard by customer id — but that’s a decision for when the graphs say so.”
Sizing it on the whiteboard
- Writes: a well-tuned primary on fast NVMe handles on the order of 10,000–50,000 simple writes/s; batching and fewer indexes push it higher.
- Reads: indexed point lookups from memory at tens of thousands per second per node; add replicas linearly.
- Storage: single nodes run comfortably at several TB. The practical limit is how long a restore from backup takes — plan for it.
- Connections: a few hundred real connections; PgBouncer for anything beyond.
The follow-ups, and the one-line answers
- “What happens on UPDATE?” A new tuple version is written; the old one is dead until vacuum.
- “Why is this table huge when it only has 10,000 rows?” Bloat: dead tuples autovacuum hasn’t reclaimed, often because a long transaction is pinning them.
- “How many connections can it take?” Hundreds directly; thousands through PgBouncer.
- “Can Postgres be a queue?” Yes, with
FOR UPDATE SKIP LOCKED, up to thousands of jobs per second. - “Composite index on (a, b) — does
WHERE b = ?use it?” Not efficiently; leading column first. - “When would you leave Postgres?” When sustained writes or storage outgrow one primary and sharding it costs more than a purpose-built store.