Databases for Data Engineers
OLTP versus OLAP, B-tree and LSM storage engines, WAL and replication slots, MVCC and isolation in reports, replica lag, partitioning, and MPP distribution.
10 questions
JuniorTheoryVery commonHow do OLTP and OLAP workloads differ, and why keep dashboards off the OLTP primary?
How do OLTP and OLAP workloads differ, and why keep dashboards off the OLTP primary?
OLTP is many short transactions on a few rows by key: row store, B-tree indexes. OLAP is a few long scans of many rows and few columns: columnar, compressed. A huge GROUP BY on the primary starves transactions of I/O and cache; dashboards go to a warehouse.
Common mistakes
- ✗Believing an index makes a full-table aggregation over billions of rows cheap on an OLTP database
- ✗Thinking a physical read replica stores data differently from the primary it copies
- ✗Treating columnar storage as a disk-saving trick rather than a way to read only the needed columns
Follow-up questions
- →When is a read replica enough for reporting, and when do you need a warehouse fed by change data capture (CDC)?
- →Which storage layout serves a customer's last 20 orders, and which serves revenue by region?
JuniorTheoryCommonWhich anomalies does each PostgreSQL isolation level allow, and how does it differ from the standard?
Which anomalies does each PostgreSQL isolation level allow, and how does it differ from the standard?
Read Committed (default) takes a snapshot per statement, so re-reads and phantoms can change. Read Uncommitted acts the same: no dirty reads. Repeatable Read keeps one snapshot per transaction, no phantoms. Serializable (SSI) aborts write skew; retry.
Common mistakes
- ✗Reciting the SQL-standard table and expecting dirty reads or phantoms where PostgreSQL never shows them
- ✗Thinking Read Committed gives a stable view across the statements of one transaction
- ✗Running Serializable without retry logic for the serialization failure it raises
Follow-up questions
- →Which isolation level would you choose for a loader that reads a table and then writes based on what it saw?
- →Why can a PostgreSQL Repeatable Read transaction fail on UPDATE with a serialization error?
JuniorTheoryCommonHow does a massively parallel (MPP) warehouse like Greenplum spread one table across its nodes?
How does a massively parallel (MPP) warehouse like Greenplum spread one table across its nodes?
MPP is shared-nothing: a coordinator plans, each segment holds a share of every table. A distribution key hashes rows to segments; a small table can be replicated to all, or rows spread randomly. A join on that key runs locally; otherwise rows move.
Open full question →Common mistakes
- ✗Confusing the distribution key, which places rows on segments, with the partition key, which splits a table by value
- ✗Thinking an MPP segment owns whole tables rather than a share of every table
- ✗Assuming every join is local, without checking whether both sides share the distribution key
Follow-up questions
- →When would you choose
DISTRIBUTED REPLICATEDin Greenplum orDISTSTYLE ALLin the Redshift warehouse? - →What makes a column a bad distribution key even when it is the most common join key?
JuniorTheoryCommonWhat is a write-ahead log, and how does the same log feed replicas and change data capture?
What is a write-ahead log, and how does the same log feed replicas and change data capture?
A change is appended to the log and flushed at commit, before data pages are written: durability via cheap sequential I/O; after a crash WAL is replayed from the last checkpoint. Replicas replay the same WAL; logical decoding turns it into row changes for CDC.
Common mistakes
- ✗Thinking the data files must be updated on disk before a commit can be acknowledged
- ✗Treating the write-ahead log as an LSM-only idea rather than a part of PostgreSQL and InnoDB too
- ✗Assuming log-based CDC has to query the source tables to find changed rows
Follow-up questions
- →Why does PostgreSQL need
wal_level = logicalbefore the change-capture connector Debezium can read changes? - →What does a checkpoint change about how much WAL a crash recovery must replay?
MiddleTheoryCommonHow do B-tree and LSM-tree storage engines differ on writes, point reads and range scans?
How do B-tree and LSM-tree storage engines differ on writes, point reads and range scans?
A B-tree updates pages in place: random writes, one root-to-leaf path per lookup, ordered leaves for ranges. An LSM appends to WAL and memtable, flushes sorted files, compacts them: cheap writes, but reads probe several files; Bloom filters aid point reads.
Open full question →Common mistakes
- ✗Repeating that B-trees always win reads and LSM trees always win writes without naming the read and compaction costs
- ✗Believing a Bloom filter speeds up range scans, when it only answers whether a file may hold a given key
- ✗Forgetting that an LSM delete is a tombstone that lingers until compaction removes it
Follow-up questions
- →How do leveled and tiered compaction trade write, read and space amplification?
- →Why is ClickHouse's table engine MergeTree, with immutable parts and background merges, called LSM-like?
MiddleDesignCommonAn events table of 5 billion rows grows by 20 million rows a day and is moving from a PostgreSQL replica to a massively parallel (MPP) warehouse (Greenplum or Redshift). Almost every query filters by a range of a few days and often by user_id; many join events to a 50-million-row users table on user_id; data older than 400 days must be removed. One teammate says a B-tree index on (event_date, user_id) is all the table needs. Another wants to distribute it by event_date so a one-day query touches a single segment. For an index, date partitioning, a sort key and a distribution key, say what work each lets the engine skip, then choose the layout for events and explain what is wrong with each proposal.
An events table of 5 billion rows grows by 20 million rows a day and is moving from a PostgreSQL replica to a massively parallel (MPP) warehouse (Greenplum or Redshift). Almost every query filters by a range of a few days and often by user_id; many join events to a 50-million-row users table on user_id; data older than 400 days must be removed. One teammate says a B-tree index on (event_date, user_id) is all the table needs. Another wants to distribute it by event_date so a one-day query touches a single segment. For an index, date partitioning, a sort key and a distribution key, say what work each lets the engine skip, then choose the layout for events and explain what is wrong with each proposal.
An index finds a few rows by key (OLTP); date partitions prune whole ranges and drop old days cheaply; a sort key on the filter columns lets block min/max skip data inside a partition; a distribution key only places rows. Distribute by user_id, never by date.
Common mistakes
- ✗Distributing by the date column, which puts each day's load and each day's queries on one segment
- ✗Expecting a distribution key to filter data the way partition pruning does
- ✗Carrying OLTP B-tree index habits into a columnar MPP warehouse built for scans
Follow-up questions
- →Why does a compound sort key in the Redshift warehouse prune well on its leading column but barely on the second?
- →How would you check that
user_idspreads rows evenly before choosing it as the distribution key?
MiddleDebuggingCommonThe change-data-capture connector Debezium stalled, and write-ahead log files fill the PostgreSQL primary's disk — diagnose
The change-data-capture connector Debezium stalled, and write-ahead log files fill the PostgreSQL primary's disk — diagnose
The inactive slot debezium_orders pins 412 GB of WAL: a slot keeps every segment from its restart_lsn until the consumer confirms it, connected or not. Restart the connector, or drop an abandoned slot and re-snapshot; alert on retained WAL, cap it.
Common mistakes
- ✗Assuming a slot stops retaining WAL once its consumer disconnects
- ✗Deleting files from pg_wal by hand, which can corrupt the database
- ✗Dropping a logical slot without planning a new snapshot for the changes the consumer never read
Follow-up questions
- →What does
max_slot_wal_keep_sizetrade away when it stops a slot from filling the disk? - →Why does a stalled logical slot also hold back VACUUM of system catalog rows?
MiddleDebuggingCommonA five-query PostgreSQL report's totals don't reconcile on a busy database — fix it
A five-query PostgreSQL report's totals don't reconcile on a busy database — fix it
Under Read Committed each statement takes its own snapshot, so orders committed between the queries land in some totals only. Run the report as one REPEATABLE READ READ ONLY transaction: one MVCC snapshot, writers never blocked. It holds back VACUUM.
Common mistakes
- ✗Assuming a BEGIN … COMMIT block alone gives every statement the same view of the data
- ✗Reaching for table locks to get consistent totals and blocking the application's writes
- ✗Running a long snapshot on the OLTP primary without noticing it holds back VACUUM
Follow-up questions
- →What happens to dead row versions on the primary while a three-minute snapshot is open?
- →How would you make the report consistent if it ran as five separate jobs instead of one session?
SeniorPerformanceOccasionalIn the massively parallel warehouse Greenplum, a 5B-row fact join to a 10M-row dimension redistributes both sides — fix it
In the massively parallel warehouse Greenplum, a 5B-row fact join to a 10M-row dimension redistributes both sides — fix it
Neither table is distributed on the join key, so both are reshuffled across the interconnect. Distribute the fact on the join key if it spreads evenly and the dimension on the same key, or replicate the dimension. The busiest segment sets the query time.
Open full question →Common mistakes
- ✗Believing table partitioning decides which segment a row lives on
- ✗Distributing a fact on a low-cardinality or NULL-heavy key and piling rows onto one segment
- ✗Reading the average segment time instead of the slowest one when judging a skewed query
Follow-up questions
- →How do you measure rows per segment in Greenplum, and what does
skew_rowsin the Redshift warehouse tell you? - →When is replicating the dimension better than redistributing the fact on the join key?
SeniorDebuggingOccasionalExtracts from a PostgreSQL read-only replica (hot standby) miss rows and get canceled — diagnose both
Extracts from a PostgreSQL read-only replica (hot standby) miss rows and get canceled — diagnose both
Missing rows: the async standby replays WAL minutes behind; extract once replay passes midnight, or read the primary. Cancels: replaying VACUUM cleanup would remove rows the extract's snapshot sees. A longer max_standby_streaming_delay adds lag; hot_standby_feedback bloats the primary.
Common mistakes
- ✗Assuming an asynchronous replica is current at the moment an extract starts
- ✗Turning on hot_standby_feedback without noticing it moves the cost to bloat on the primary
- ✗Raising max_standby_streaming_delay and making the replica fall even further behind for the next extract
Follow-up questions
- →How would you make the 00:05 extract wait until the standby has replayed everything committed before midnight?
- →When does a dedicated reporting replica, or a warehouse fed by change data capture (CDC), beat tuning a shared hot standby?