← All lessons

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.

22 min readUpdated November 2026Free

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:

The point

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:

Where candidates lose it

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:

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.

Where candidates lose it

“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:

Scaling it

In order, roughly from cheapest to most expensive:

  1. Index and fix queries. Most “Postgres is slow” is a missing index or an N+1.
  2. Vertical scaling. Machines with hundreds of GB of RAM are ordinary. A working set in memory changes everything.
  3. Read replicas for read-heavy paths that tolerate a little lag — with the read-your-writes care from the replication lesson.
  4. Partitioning big tables by time or tenant: faster maintenance, and dropping a month is DROP TABLE rather than a giant DELETE.
  5. Caching in front of hot reads.
  6. 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.

Say it like this

“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

The follow-ups, and the one-line answers

  1. “What happens on UPDATE?” A new tuple version is written; the old one is dead until vacuum.
  2. “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.
  3. “How many connections can it take?” Hundreds directly; thousands through PgBouncer.
  4. “Can Postgres be a queue?” Yes, with FOR UPDATE SKIP LOCKED, up to thousands of jobs per second.
  5. “Composite index on (a, b) — does WHERE b = ? use it?” Not efficiently; leading column first.
  6. “When would you leave Postgres?” When sustained writes or storage outgrow one primary and sharding it costs more than a purpose-built store.