Skip to content

Commit 360cbf2

Browse files
committed
document how this is supposed to work
1 parent 0166952 commit 360cbf2

File tree

2 files changed

+93
-243
lines changed

2 files changed

+93
-243
lines changed

docs/reference/protocol.md

Lines changed: 0 additions & 243 deletions
This file was deleted.

docs/reference/queries.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Query Execution and Serializability in AtlasDb
2+
3+
## Introduction
4+
5+
AtlasDb is a distributed database designed to ensure serializable query execution while balancing performance and
6+
availability across multiple regions. It provides strong consistency guarantees, optimizes query routing, and supports
7+
efficient transaction management. This document outlines how AtlasDb executes queries and enforces serializability.
8+
9+
## Query Execution Model
10+
11+
### Query Planning and Execution
12+
13+
When a client submits a query, AtlasDb parses and analyzes it to determine the optimal execution strategy. The query
14+
planner optimizes execution based on factors such as data locality, consistency requirements, and replication
15+
constraints. Write queries are only allowed within a `BEGIN IMMEDIATE` transaction to guarantee atomicity and isolation.
16+
Read queries, however, can be executed within or outside a transaction, depending on the client’s requirements.
17+
18+
### Query Routing and Execution
19+
20+
Write transactions typically execute on the local node unless specific conditions require otherwise. The system first
21+
determines the leader of the table being modified. If the leader resides in the local region, the writer temporarily
22+
borrows the table for the duration of the transaction. If the leader is remote, a table leadership transfer (stealing)
23+
may be initiated for long-term ownership.
24+
25+
A leader may deny a steal attempt if it determines that the local region has a higher write load, in which case the
26+
writer continues borrowing the table. To maintain consistency, tables, views, and triggers that are frequently updated
27+
should be grouped to ensure that updates propagate correctly across replicas.
28+
29+
## Borrowing Process
30+
31+
Borrowing allows a node to execute a write transaction without assuming long-term ownership of the table. The process
32+
follows these steps:
33+
34+
1. The leader initiates an immediate transaction and synchronizes the writer with the latest table state.
35+
36+
2. The writer executes the transaction while maintaining consistency with the leader.
37+
38+
3. The writer sends the migration changeset to the leader to ensure state consistency before commit.
39+
40+
4. The leader applies the migration and verifies the changes.
41+
42+
5. Upon commit, the leader finalizes the migration using a two-phase commit protocol to ensure atomicity and durability.
43+
44+
6. If a rollback occurs, the leader propagates the rollback using the same two-phase mechanism.
45+
46+
7. In the event of a leader failure, the writer assumes leadership of the table. If another node is elected leader, it
47+
captures the pending migration on commit.
48+
49+
Borrowing allows transaction execution with minimal leadership disruption while maintaining strong consistency
50+
guarantees.
51+
52+
### Borrowing and SQLite WAL Mode
53+
54+
AtlasDb leverages SQLite’s Write-Ahead Logging (WAL) mode, which enhances Borrowing by enabling the following
55+
optimizations:
56+
57+
1. **Full Database Locking for Writes**: SQLite locks the database during `BEGIN IMMEDIATE` transactions, ensuring that no
58+
conflicting writes occur. This eliminates the need to coordinate multiple write transactions.
59+
60+
2. **Concurrent Read Access**: While writes are blocked, WAL mode enables concurrent reads, ensuring that query execution
61+
performance remains high.
62+
63+
3. **Eliminating Leader Election Overhead**: Borrowing avoids the costly leader election process (Phase 1 of Paxos), allowing
64+
transactions to execute immediately.
65+
66+
4. **Failure Recovery**: If the leader fails mid-transaction, the writer can seamlessly assume leadership and ensure
67+
transaction durability.
68+
69+
5. **Optimized Multi-Table Transactions**: If a leader already governs multiple tables involved in a transaction, only a single
70+
leader lock is required, reducing synchronization overhead.
71+
72+
By integrating Borrowing with SQLite’s WAL mode, AtlasDb minimizes transaction latency while maintaining strong
73+
consistency and availability guarantees.
74+
75+
## Definitions
76+
77+
**Borrowing**: A temporary execution mechanism where a node processes a write transaction without permanently assuming table
78+
leadership, ensuring synchronized execution with the leader.
79+
80+
**Stealing**: The process of transferring table leadership to a new node, allowing it to manage writes long-term while
81+
ensuring strong consistency.
82+
83+
**Leader**: The node responsible for executing and committing writes to a particular table, ensuring serializability across
84+
transactions.
85+
86+
**Writer**: A node executing write operations within a region but not necessarily the leader. Writers help distribute the
87+
load while maintaining synchronization with the leader.
88+
89+
**Replication Constraints**: Rules defining how data is propagated across nodes to guarantee consistency for read and write
90+
operations.
91+
92+
By combining Borrowing, leader-based execution, and SQLite WAL mode, AtlasDb ensures high-performance, serializable
93+
query execution in a distributed environment while minimizing disruption to ongoing transactions.

0 commit comments

Comments
 (0)