interviewpath
  • 0day streak
  • 10total XP
  • Level 1
0/6

Unit 6 · Databases · Lesson 8 of 10

OLTP vs OLAP, distributed joins and distributed transactions

Transactional and analytical workloads stress databases in opposite ways, and once data is partitioned across machines, joins must move data and multi-partition transactions need an atomic commit protocol. This topic covers the design choices behind warehouses, sharded OLTP systems and two-phase commit that recur in system-design interviews.
About 4 hFocus mode

Step 1: Learn the ideas

  • OLTP versus OLAP workloads; HTAP at awareness level
  • Star and snowflake schemas; fact and dimension tables
  • Columnar warehouses, MPP execution and separated storage and compute
  • Partitioning: hash, range, list; co-location and global secondary indexes
  • Distributed joins: co-located, broadcast, shuffle, semi-join and Bloom-filter reduction
  • Distributed aggregation: partial and final aggregates
  • Two-phase commit, the prepared state and the blocking problem
  • Presumed abort and coordinator logging
  • Avoiding cross-partition transactions: partitioning, sagas, the outbox pattern

Why it matters

"Design an analytics pipeline for X" and "the database has outgrown one machine" are standard system-design prompts. Good answers rest on recognizing the workload's shape, choosing a partition key that keeps work local, knowing what a distributed join costs, and being honest about what a cross-partition transaction requires.

Core ideas

Two workload shapes. OLTP: many concurrent short transactions, point reads and writes by key, strict latency targets, row storage with B+ tree indexes. OLAP: a few large queries that scan, filter, join and aggregate millions or billions of rows; columnar storage, compression, vectorized and parallel execution. Running heavy analytics on the OLTP primary competes for memory, I/O and CPU, and long-running queries hold old snapshots that delay version cleanup. Analytical data is therefore usually copied to a warehouse or lake through ETL or change data capture, trading freshness for isolation. HTAP systems try to serve both, often with separate row and column copies.

Warehouse modeling. A star schema has a large fact table (one row per event, with numeric measures) surrounded by smaller dimension tables (customer, product, date). Typical queries filter dimensions, join them to facts and aggregate, which is exactly what columnar scans and hash joins do well.

Partitioning. Hash partitioning spreads load evenly but turns range queries into scatter-gather; range partitioning keeps ranges local but creates hot spots on monotonically increasing keys. Partitioning by entity (tenant, user, account) keeps most transactions on one node. A global secondary index on another attribute is itself partitioned, so maintaining it can require cross-node writes.

Distributed joins. If both tables are partitioned on the join key, the join is co-located and moves no data. Otherwise:

  • Broadcast join: send the small table to every node holding part of the large one; cost is roughly the small table's size times the number of nodes.
  • Shuffle (repartition) join: rehash both inputs on the join key so matching rows meet; cost is roughly the size of both inputs. Broadcast wins when one side is small after filtering; shuffle wins when both are large. A heavily skewed key overloads the node that receives it, so engines split heavy hitters or broadcast only their matching rows. Semi-join reduction ships just the join keys of one side, and a Bloom filter of those keys is smaller still, so the other side can be filtered before it moves. Aggregation runs in two phases: partial aggregates per node, then a final merge.

Atomic commit across partitions. In two-phase commit (2PC) the coordinator asks every participant to prepare; each makes its changes durable, keeps its locks and votes. If all vote yes, the coordinator durably records commit and informs everyone; any no vote or timeout means abort. A participant that voted yes cannot decide alone afterwards: if the coordinator crashes before announcing the outcome, the participant waits with its locks held. That is the blocking problem. Production systems shrink it by replicating the coordinator's decision and each participant with consensus (each participant becomes a Paxos or Raft group), so a failed leader is replaced rather than waited for. PostgreSQL exposes the participant side as PREPARE TRANSACTION, and an orphaned prepared transaction keeps its locks until someone commits or rolls it back.

Atomicity is not isolation. 2PC makes all participants commit or abort together; it does not stop concurrent transactions from seeing a partial state. Distributed systems still need concurrency control spanning partitions, and reading a consistent snapshot across partitions needs coordinated timestamps.

Common misconceptions

  • "2PC is a consensus protocol." 2PC needs every participant's vote and blocks on coordinator failure; consensus needs a majority and survives minority failures. Practical systems combine the two.
  • "Sharding makes everything faster." Single-partition operations scale; cross-partition joins and transactions get slower and more failure-prone.
  • "Broadcast joins are always cheaper." Only when the broadcast side is small; otherwise network traffic and memory multiply.
  • "Atomic commit gives isolation." It gives all-or-nothing; isolation needs concurrency control across partitions.
  • "Analytics can just run on a read replica." A replica removes write contention but keeps the row layout and indexes built for OLTP; scan-heavy work is usually far cheaper on columnar storage.

How to approach it

Use one running example (orders, customers, products) and answer four questions: which partition key, which queries stay local, what a cross-partition join moves, and what a transfer between two customers on different partitions requires. Practice the 2PC failure cases as timelines, then run the PREPARE TRANSACTION exercise to see a prepared transaction hold locks. In interviews, quantify data movement and name the failure behavior of whatever commit protocol you propose.

Suggested order

  1. 1Read CMU 15-445 Fall 2025 Lecture #23 notes (Distributed Database Systems I): architectures, partitioning and distributed concurrency control.
  2. 2Read Lecture #24 notes (Distributed Database Systems II): atomic commit protocols and distributed join algorithms.
  3. 3Read DDIA Ch. 6 (Partitioning), then the atomic-commit sections of Ch. 9 (Consistency and Consensus).
  4. 4Read Berkeley CS186 Note 15 (Parallel Query Processing) for parallel joins and aggregation and Note 16 (Distributed Transactions) for 2PC and presumed abort.
  5. 5Read the PostgreSQL PREPARE TRANSACTION page and run the two-phase commit blocking exercise.
  6. 6Skim the CMU 15-721 Spring 2024 notes on Modern Analytical Database Systems to see how warehouses separate storage and compute.

Paper guides: Spanner: Google’s Globally-Distributed Database

Step 2: Check off what you can do

Tap each one you can genuinely do or explain. Each earns 10 XP.

Step 3: Go deeper

  • CMU 15-445/645 Intro to Database SystemsStart here

    Carnegie Mellon University Database Group

    Section: Fall 2025 Lecture #24: Distributed Database Systems II (notes)

    Atomic commit protocols and distributed join algorithms; Lecture #23 covers architectures and partitioning.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • Designing Data-Intensive ApplicationsStart here

    O'Reilly Media (author's book site)

    Section: Ch. 6: Partitioning

    Partitioning schemes, skew and hot spots, secondary indexes and rebalancing; Ch. 9 covers atomic commit and two-phase commit.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • Berkeley CS186 Database Systems course notes

    UC Berkeley CS186

    Section: Note 16: Distributed Transactions

    Two-phase commit, its failure cases and the presumed-abort optimization, with practice questions.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • PostgreSQL documentation (current release)

    PostgreSQL Global Development Group

    Section: PREPARE TRANSACTION

    The participant side of two-phase commit in a real database, including why prepared transactions must not be left open.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • CMU 15-721 Advanced Database Systems

    Carnegie Mellon University Database Group

    Section: Spring 2024 Lecture #01: Modern Analytical Database Systems (notes)

    How modern analytical systems are organized: columnar formats, separated storage and compute, distributed execution.

    Open(opens in a new tab)Link responded, 5 Oct 2026

Step 4: Practice

Set max_prepared_transactions above zero, then play coordinator by hand across two PostgreSQL databases: update a row in each, PREPARE TRANSACTION both, and stop. From a third session, try to update the same rows and inspect pg_prepared_xacts and pg_locks. Then commit one and roll back the other to see why the coordinator’s durable decision matters. Separately, compute the bytes moved by broadcast and shuffle joins for three size combinations on a four-node cluster and pick a strategy for each.

Record attempts on each problem’s page (+15 XP, +10 more when solved on your own).

Step 5: Test yourself

Answer each one out loud or on paper before you finish the lesson.

  1. Question 1What exactly is a participant waiting for after it votes yes, and why can it not decide alone?
  2. Question 2How does replicating each participant with consensus change 2PC’s failure behavior?
  3. Question 3When does a broadcast join beat a shuffle join, and what can make it fail?
  4. Question 4Why does range partitioning on a timestamp create a hot spot?
  5. Question 5Why is atomic commit not enough to give isolation across partitions?
  6. Question 6What is a fact table, and what queries is a star schema designed for?

You’re done when you can

  • Draw 2PC timelines for coordinator failure before and after the decision and state what each participant can do.
  • Choose a partition key for a given schema and list which of five queries stay single-partition.
  • Compute network cost for broadcast versus shuffle joins on a given example and justify the choice.
  • Explain in under three minutes why analytics moves to a columnar system and what freshness costs.

Finish the lesson

How confident do you feel?

Lesson complete!

+50 XP

Reviews will come up on the Practice tab so it sticks.

Unlocks next: Distributed transactions and recovery research

Your notes and study time

Log study time