Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Distributed databases

Distributed databases let a single logical database run across multiple machines to improve availability, scalability and locality; this note covers fragmentation, replication, transaction commit, consistency models and distributed query processing with exam-style definitions and examples.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Two or three sentences: where an engineer uses this.

Distributed databases are used when data must be stored and processed across several machines or sites for reasons of scale, fault-tolerance or data locality (for example, a global e-commerce site). Engineers design fragmentation and replication, choose commit protocols and consistency models, and optimise distributed queries to meet performance, availability and correctness requirements.

Key ideas

Explain all concepts from first principles. Define terms before using them and bold each defined term.

  • Distributed database: a collection of logically related data that is stored at two or more physically separated sites connected by a network. The term is used to mean the system that manages such data.

  • Site (or node): a single computer (host) that stores part or whole of the database and runs a database management system (DBMS).

  • Transparency: the property that hides distribution details from users. Types are location transparency (user need not know which site holds a data item), replication transparency (user unaware of copies), and fragmentation transparency (user sees the full relation, not fragments).

  • Fragmentation: dividing a relation into smaller pieces called fragments. Types:

    • Horizontal fragmentation: each fragment contains a subset of rows (tuples) based on a predicate. Example predicate: Employee.salary > 50000.
    • Vertical fragmentation: each fragment contains a subset of columns (attributes) plus the primary key to allow reconstruction.
    • Derived fragmentation: fragments of one relation that are created to keep joins local (e.g., fragment foreign-key related relation following fragmentation of primary relation).

    Important property: reconstructability — the original relation R can be reconstructed from fragments via union (horizontal) or natural join on the primary key (vertical). State the requirement: fragments must be disjoint and union must equal original (for horizontal), and vertical fragments must include key and whose join equals original. These are often named completeness (every tuple/attribute appears in some fragment), disjointness (no overlap, for non-replicated fragmentation), and reconstruction (original can be obtained from fragments).

    University-style derivation to show reconstructability (horizontal): Given relation R and fragments R1, R2,..., Rn defined by predicates P1,...,Pn. Show that for every tuple t in R exactly one Pi(t) is true (disjoint and complete) and prove R = R1 ∪ R2 ∪ ... ∪ Rn.

  • Replication: storing copies of the same data item on multiple sites. Types:

    • Full replication: every site stores a full copy of the database.
    • Partial replication: only some data items are replicated.
    • Synchronous replication: updates are applied to all replicas before commit completes (stronger consistency, higher latency).
    • Asynchronous replication: updates applied to the primary then propagated later (lower latency, eventual consistency possible). In quorum replication with N replicas, choose write quorum W and read quorum R such that W > N/2 and R + W > N to achieve linearizable reads/writes; smaller quorums trade consistency for availability/latency.
  • Consistency model: a formal rule describing what executions are considered correct. Examples:

    • Strict serialisability: serialisability that also respects real-time order of transactions; for single-object operations this coincides with linearizability (operations appear to take effect at a point between invocation and response).
    • Serialisability: the result of concurrent transactions is the same as some serial order of them.
    • Eventual consistency: if no new updates are made, all replicas converge to the same value eventually.

    State assumptions and trade-offs: stronger consistency often increases latency or reduces availability.

  • CAP theorem (formal statement universities expect): In an asynchronous network that can suffer partitions, a distributed data system can provide at most two of the following three properties at the same time: Consistency (C), Availability (A), and Partition tolerance (P). Assumptions: network partitions can occur; during a partition, every request must be answered by some non-failing node for availability. Explain: therefore designs choose which to trade (e.g., CP, AP, CA is impossible in presence of partitions unless partitions are excluded). Note: in CAP, Consistency (C) means linearizability (single-copy semantics), not database serialisability.

  • Distributed transaction: a transaction that accesses data at more than one site. It must satisfy ACID (Atomicity, Consistency, Isolation, Durability) across sites.

  • Two-phase commit (2PC): a common protocol to guarantee atomic commit for distributed transactions. Describe coordinator and participants, give phase names (voting/prepare and commit/abort), and list possible states (INIT, WAIT, PREPARED, COMMITTED, ABORTED). Describe failure scenarios and blocking property: a participant can block (wait) if coordinator fails after voting yes but before sending the decision.

  • Three-phase commit (3PC): a non-blocking variant that adds an intermediate pre-commit phase and requires certain timing assumptions (assumes bounded network delays) — mention assumptions explicitly. Specifically, 3PC’s non-blocking property relies on a synchronous model with known bounds on message delays and processing time, and timeouts that allow processes to infer failures; if these assumptions are violated (e.g., long partitions), 3PC can block or violate safety.

  • Distributed concurrency control: apply global serialisability using:

    • Distributed locking: using locks across sites (e.g., strict two-phase locking to ensure serialisability); can deadlock and needs detection.
    • Timestamp ordering: using global or synchronized timestamps; clarify need for clock synchronisation assumptions. Physical-clock ordering requires bounded clock skew; alternatively use logical timestamps (Lamport/Hybrid/Vector) to avoid tight clock sync.
  • Distributed query processing: split a global query into sub-queries at sites, choose execution sites for operators, minimise data transfer. Key technique: semi-join — send projection of join attributes to reduce data sent.

  • Availability calculation: when multiple replicas exist, system availability increases. If components fail independently, system availability with replicated components is computed by probability rules (see formulas section).

Where relevant, refer to related topics rather than re-teaching them: this note assumes you know Transaction basics and Query optimisation and evaluation plans.

Diagrams to draw for exams (describe exactly):

  • For 2PC: draw three boxes labeled Coordinator, Participant A, Participant B; draw arrows: Coordinator -> Participants: PREPARE; Participants -> Coordinator: VOTE-YES/VOTE-NO; Coordinator -> Participants: COMMIT/ABORT. Optionally show ACKs from participants after COMMIT/ABORT if your variant includes them. Label time axis downward.
  • For fragmentation: draw one relation box labelled R with arrows splitting into R1 (rows 1..k) and R2 (rows k+1..n) for horizontal; for vertical draw R split into attributes sets with primary key column included in each fragment.

Formulas

Each formula on its own line and explanation.

A_system = 1 - Π_i (1 - A_i)

  • A_system is overall availability (probability system responds), dimensionless (probability). A_i is availability of replica i (probability it is up). Applies when system is up if at least one replica is up and component failures are independent.

T_commit_2PC ≈ 2·RTT + T_local_commit (approximate)

  • T_commit_2PC is commit latency for 2PC (seconds), RTT is round-trip time to the slowest participant (seconds), T_local_commit is time to apply local commit (seconds). Applies when there are two message rounds (prepare and commit) and ignores durable log fsync times at coordinator/participants, which add extra fixed latency in practice.

Cost_total = Σ_sites (Cost_local_execution + Cost_transfer)

  • Cost_total is total query cost in time or bytes; Cost_local_execution (seconds or CPU cycles) is time to compute local operators; Cost_transfer (seconds or bytes converted to time) is time to transfer data across network. Use network bandwidth and data size to convert bytes to seconds.

P(eventual convergence) = 1 under asynchronous replication if updates stop and all messages eventually delivered (no permanent failures)

  • This is a conditional property: dimensionless. It applies under the assumption of reliable eventual delivery and no new updates.

If using probabilities for n identical replicas each with availability p:

A_system = 1 - (1 - p)^n

  • p is single-node availability (probability), n is number of replicas. Applies under independent-failure assumption.

Worked examples

Two or three examples of exam standard.

Example 1 — Horizontal fragmentation and reconstructability (theory/derivation) Problem: Relation Employee(EmpID primary key, Name, Dept, Salary). Define horizontal fragments: F1 = Employee where Dept = 'HR', F2 = Employee where Dept <> 'HR'. Show reconstructability. Solution steps:

  1. Define predicates P1(t): t.Dept = 'HR', P2(t): t.Dept <> 'HR'. These are mutually exclusive and collectively exhaustive for all possible Dept values. (Explain exclusivity: cannot be both HR and not HR.)
  2. By definition F1 = {t in Employee | P1(t)} and F2 = {t in Employee | P2(t)}.
  3. For every tuple t in Employee either P1(t) or P2(t) is true, so t ∈ F1 ∪ F2. Conversely, any tuple in F1 or F2 satisfies Employee conditions, so union equals Employee. Hence reconstructability holds. Bold final: Employee = F1 ∪ F2.

Example 2 — Two-phase commit trace (algorithm trace) Problem: Coordinator C, Participants P1 and P2. Both participants vote YES. Show message sequence and states; then P1 crashes after sending YES and recovers after decision. Solution steps:

  1. C -> P1,P2: PREPARE (phase 1). P1 and P2 evaluate local constraints; if ready they force-write a prepare record and then enter the PREPARED state awaiting the decision.
  2. P1 -> C: VOTE-YES (and record prepared to local stable storage). P2 -> C: VOTE-YES.
  3. C receives both YES and writes global decision COMMIT to stable storage, then sends COMMIT to P1 and P2 (phase 2).
  4. P1 receives COMMIT, applies local commit, writes COMMITTED to log, and sends ACK to C. If P1 had crashed after sending YES but before receiving COMMIT, on recovery it consults log: prepared but no decision, so it contacts coordinator to obtain decision (blocking if coordinator unavailable). Finally after receiving COMMIT it completes. Bold final: 2PC guarantees atomic commit but is blocking if coordinator fails between phases.

Example 3 — Availability numeric Problem: Three replicas each with availability 0.95. Compute system availability if any replica can serve reads. Solution: A_system = 1 - (1 - 0.95)^3 = 1 - 0.05^3 = 1 - 0.000125 = 0.999875. Final answer bold: A_system = 0.999875 (99.9875%).

Common mistakes

  • Forgetting to include the primary key in vertical fragments for reconstructability.
  • Saying 2PC is non-blocking; it is blocking in the coordinator-failure case — only 3PC can avoid blocking but with extra timing assumptions.
  • When computing availability, forgetting independence assumption or using addition instead of 1 - product.
  • In distributed query cost, forgetting network transfer cost (students sometimes add only local CPU costs).

For your exam

Kinds of questions and what full-marks answers include:

  • Definitions and short notes: define terms like distributed database, fragmentation, replication, consistency model, 2PC, CAP. Include assumptions and small examples.
  • Derivations: prove reconstructability for a fragmentation scheme (show predicates cover and are disjoint for horizontal; show join reconstructs vertical). Show step-by-step logic.
  • Protocol traces: draw 2PC message sequence and explain states and blocking scenarios; for 3PC, state extra phase and timing assumptions.
  • Numericals: compute availability with given replica availabilities (show use of formula explicitly), compute approximate 2PC latency given RTT and local commit time.
  • Algorithm traces / query processing: demonstrate semi-join to reduce transfer — show steps: project join attribute, send projection, filter local, send reduced relation, join locally.

A full-marks answer names and bolds defined terms, states assumptions, includes neat diagrams described above, and shows calculations with units where appropriate.

Quick check

Three short questions. Answer line at the end.

  1. What is horizontal fragmentation? Give one requirement for reconstructability.
  2. What are the two phases of 2PC called?
  3. With two replicas each availability 0.9, what is system availability if any replica suffices?

Answers: 1) Horizontal fragmentation: partitioning rows by predicate; requirement: fragments must be disjoint and their union equals the original relation (completeness). 2) Phase 1: PREPARE (voting), Phase 2: COMMIT/ABORT (decision). 3) A = 1 - (1 - 0.9)^2 = 1 - 0.01 = 0.99 (99%).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?