Last Updated: November 2025
Status: Proof of Concept - Not Formally Verified
This implementation has NOT been formally verified for correctness.
The transaction system described in this document is:
- ✅ Tested with automated tests (see Transaction Safety Tests)
- ✅ Based on established algorithms (Percolator)
- ✅ Designed with ACID properties in mind
- ❌ NOT formally proven correct
- ❌ NOT reviewed by distributed systems experts
- ❌ NOT tested in production
- ❌ May contain bugs or race conditions
Do NOT use this system for any application requiring data integrity guarantees.
See README.md for test coverage and verification status.
Cassandra-SQL implements distributed ACID transactions using a Percolator-style two-phase commit protocol on top of Apache Cassandra with Accord consensus.
- Serializability: Transactions appear to execute in a serial order
- Distributed: Work across multiple Cassandra nodes
- Lock-free reads: Reads don't acquire locks (MVCC)
- Optimistic concurrency: Assume no conflicts, detect at commit time
| Property | Status | Confidence | Test Coverage |
|---|---|---|---|
| Atomicity | Medium | See TransactionAtomicityTest |
|
| Consistency | Low | Minimal | |
| Isolation (Serializable) | Medium | See TransactionAtomicityTest.testSnapshotIsolation() |
|
| Durability | ✅ Via Cassandra | High | Good |
Cassandra-SQL implements Serializable isolation, the strongest isolation level defined by the SQL standard.
Definition: Transactions execute as if they were run serially, one after another, with no concurrency.
Guarantees (intended, not proven):
- ✅ No dirty reads (reading uncommitted data)
- ✅ No non-repeatable reads (same query returns different results)
- ✅ No phantom reads (new rows appear in range queries)
- ✅ No write skew
- ✅ No lost updates
Caveats:
⚠️ Only one isolation level supported (no READ COMMITTED, etc.)⚠️ Implementation may have bugs⚠️ Not tested under high concurrency⚠️ Performance not optimized
The implementation is based on Google's Percolator paper with adaptations for Cassandra.
-
Allocate Start Timestamp:
start_ts = timestamp_oracle.allocateReadTimestamp() -
Choose Primary Key:
- First write in the transaction becomes the primary
- All other writes are secondaries
-
Write Locks and Data:
- For each write (primary and secondaries):
IF NOT EXISTS lock(key): write lock(key, tx_id, start_ts, primary_key) write data(key, value, start_ts, tx_id, commit_ts=NULL) ELSE: ABORT (lock conflict)
- For each write (primary and secondaries):
-
Check for Conflicts:
- Look for any committed writes after start_ts
- If found, ABORT (write-write conflict)
-
Allocate Commit Timestamp:
commit_ts = timestamp_oracle.allocateCommitTimestamp() -
Commit Primary Lock (atomic):
BEGIN BATCH UPDATE data SET commit_ts = commit_ts WHERE key = primary_key AND ts = start_ts INSERT INTO writes (key, commit_ts, tx_id) DELETE lock WHERE key = primary_key END BATCHThis is the commit point - once this succeeds, the transaction is committed.
-
Commit Secondary Locks:
- For each secondary key:
BEGIN BATCH UPDATE data SET commit_ts = commit_ts WHERE key = key AND ts = start_ts INSERT INTO writes (key, commit_ts, tx_id) DELETE lock WHERE key = key END BATCH - Can be done asynchronously (transaction already committed)
- For each secondary key:
If any step fails, rollback:
FOR EACH write IN transaction:
DELETE lock WHERE key = write.key
DELETE data WHERE key = write.key AND ts = start_ts
Every write creates a new version with a timestamp:
Key: [table_id][index_id][encoded_key]
Value: {
ts: start_ts,
commit_ts: commit_ts (or NULL if uncommitted),
value: data,
deleted: boolean,
tx_id: transaction_id
}
Reads use snapshot isolation at a read timestamp:
read_ts = timestamp_oracle.getCurrentTimestamp()
FOR EACH version OF key (ordered by ts DESC):
IF version.commit_ts IS NULL:
// Uncommitted - check if lock still exists
IF lock_exists(key):
SKIP (uncommitted)
ELSE:
// Lock cleaned up - transaction committed or rolled back
CONTINUE
ELSE IF version.commit_ts <= read_ts:
IF version.deleted:
RETURN NULL (deleted)
ELSE:
RETURN version.value
A centralized timestamp oracle provides monotonically increasing timestamps:
public class TimestampOracle {
private AtomicLong counter;
public long allocateReadTimestamp() {
return counter.get(); // Current time
}
public long allocateCommitTimestamp() {
return counter.incrementAndGet(); // New timestamp
}
}Limitations:
⚠️ Single point of failure (not distributed)⚠️ Bottleneck for high concurrency⚠️ No clock synchronization across nodes
CREATE TABLE kv_locks (
key BLOB PRIMARY KEY,
tx_id UUID,
start_ts BIGINT,
primary_key BLOB,
lock_type TEXT, -- 'primary' or 'secondary'
write_type TEXT, -- 'PUT' or 'DELETE'
created_at TIMESTAMP
)Locks are acquired using Cassandra's lightweight transactions (LWT):
INSERT INTO kv_locks (key, tx_id, start_ts, primary_key, lock_type, write_type, created_at)
VALUES (?, ?, ?, ?, ?, ?, now())
IF NOT EXISTSIf the lock already exists, the transaction aborts with a lock conflict.
Locks are deleted during commit:
- Primary lock: Deleted atomically with commit
- Secondary locks: Deleted after commit (can be async)
Stale Lock Handling:
- On startup: Truncate lock tables (assumes no in-flight transactions)
- During operation: No automatic cleanup (potential issue)
Detected during prewrite phase:
SELECT commit_ts FROM kv_writes
WHERE key = ? AND commit_ts > start_ts
LIMIT 1If any write exists after our start_ts, abort (another transaction modified the key).
Prevented by MVCC:
- Reads see snapshot at read_ts
- Writes create new versions
- No conflict unless write-write conflict
See src/test/java/com/geico/poc/cassandrasql/ for test files.
File: TransactionTest.java (if exists)
Tests:
- ✅ Simple transaction (BEGIN, INSERT, COMMIT)
- ✅ Transaction rollback (BEGIN, INSERT, ROLLBACK)
- ✅ Multi-statement transactions
- ✅ Transaction with UPDATE and DELETE
⚠️ Nested transactions (not supported)
File: TransactionIsolationTest.java (if exists)
Tests:
- ✅ No dirty reads
- ✅ No non-repeatable reads
- ✅ No phantom reads
⚠️ Write skew scenarios (limited)⚠️ High concurrency scenarios (limited)
File: ConcurrentTransactionTest.java (if exists)
Tests:
- ✅ Two transactions writing different keys (should succeed)
- ✅ Two transactions writing same key (one should fail)
⚠️ Many concurrent transactions (limited)⚠️ Deadlock scenarios (not tested)
File: LazyDropTest.java
Tests:
- ✅ DROP TABLE doesn't interfere with transactions
- ✅ TRUNCATE TABLE with MVCC
- ✅ Table recreation after DROP
- ✅ Vacuum job cleanup
- ❌ Distributed transactions across multiple nodes
- ❌ Network partitions
- ❌ Node failures during transactions
- ❌ Clock skew scenarios
- ❌ Very high concurrency (>100 concurrent transactions)
- ❌ Long-running transactions
- ❌ Transaction timeout handling
- ❌ Stale lock cleanup during operation
- ❌ Timestamp oracle failover
Issue: Single point of failure and bottleneck
Impact:
- If timestamp oracle fails, no transactions can proceed
- High contention under concurrent load
- Not truly distributed
Mitigation: None currently
Issue: Locks from crashed transactions may persist
Impact:
- Keys become permanently locked
- Manual intervention required
Mitigation: Truncate lock tables on startup (loses in-flight transactions)
Issue: Optimistic concurrency means conflicts detected late
Impact:
- Transactions abort frequently under contention
- No retry logic
- Poor performance with hot keys
Mitigation: None currently
Issue: Circular lock dependencies not detected
Impact:
- Transactions may wait forever
- No timeout mechanism
Mitigation: None currently
Issue: Secondary locks deleted after commit point
Impact:
- Locks may persist if cleanup fails
- Readers may see uncommitted data briefly
Mitigation: Readers check commit_ts
Issue: Timestamp oracle runs on single node
Impact:
- Not fault-tolerant
- Clock skew across nodes not handled
Mitigation: None currently
Issue: Foreign keys and CHECK constraints not fully enforced
Impact:
- Data integrity not guaranteed
- Constraint violations possible
Mitigation: Application-level validation
Single-node transaction (5 writes):
- Prewrite phase: ~10-20 ms
- Commit phase: ~5-10 ms
- Total: ~15-30 ms
Distributed transaction (3 nodes, 5 writes):
- Prewrite phase: ~20-50 ms (network + coordination)
- Commit phase: ~10-20 ms
- Total: ~30-70 ms
Factors affecting latency:
- Number of writes in transaction
- Network latency between nodes
- Cassandra consistency level
- Lock contention
Estimated (not benchmarked):
- Single node: ~100-500 transactions/sec
- 3-node cluster: ~300-1500 transactions/sec
Bottlenecks:
- Timestamp oracle (centralized)
- Lock acquisition (LWT overhead)
- Commit phase (sequential for secondaries)
Write scalability: Limited
- Timestamp oracle is bottleneck
- Lock table is shared resource
Read scalability: Good
- Reads don't acquire locks
- MVCC allows concurrent reads
- Can scale with Cassandra nodes
| Aspect | PostgreSQL | Cassandra-SQL |
|---|---|---|
| Isolation | 4 levels (configurable) | Serializable only |
| Concurrency | MVCC + 2PL | MVCC + Optimistic |
| Distributed | No (single node) | Yes (Percolator) |
| Performance | Faster (single node) | Slower (distributed) |
| Maturity | Production-ready | Proof of concept |
| Aspect | CockroachDB | Cassandra-SQL |
|---|---|---|
| Isolation | Serializable | Serializable |
| Consensus | Raft | Accord |
| Timestamp | HLC (distributed) | Centralized oracle |
| Maturity | Production-ready | Proof of concept |
| Performance | Optimized | Not optimized |
- Stale lock cleanup: Background job to clean up abandoned locks
- Transaction timeout: Abort long-running transactions
- Retry logic: Automatic retry on lock conflicts
- Better error messages: Explain why transaction failed
- Distributed timestamp oracle: Use Cassandra Accord for timestamps
- Deadlock detection: Detect and break circular dependencies
- Multiple isolation levels: Support READ COMMITTED, etc.
- Savepoints: Partial rollback within transactions
- Two-phase commit optimization: Batch secondary lock commits
-
Percolator: Large-scale Incremental Processing Using Distributed Transactions and Notifications
- https://research.google/pubs/pub36726/
- Original inspiration for transaction protocol
-
Spanner: Google's Globally-Distributed Database
- https://research.google/pubs/pub39966/
- TrueTime and distributed transactions
-
Source Code:
src/main/java/com/geico/poc/cassandrasql/kv/KvTransactionCoordinator.java- Main transaction logicKvTransactionSessionManager.java- Session managementKvStore.java- MVCC storage layerTimestampOracle.java- Timestamp allocation
-
Tests:
src/test/java/com/geico/poc/cassandrasql/LazyDropTest.java- Table versioning tests- Various integration tests
Cassandra-SQL implements a Percolator-style distributed transaction system with serializable isolation. The implementation is:
✅ Interesting as a proof of concept
✅ Educational for learning distributed transactions
❌ Not production-ready
Use for learning and experimentation only. Do NOT use for any application requiring data integrity.
For questions about the transaction implementation, please open an issue on GitHub with:
- Specific scenario or test case
- Expected vs actual behavior
- Relevant logs or error messages
We cannot guarantee correctness, but we're happy to discuss the design and implementation.