--- title: "Very Lightweight Locking for in-memory databases" description: "Explore Very Lightweight Locking (VLL): how counters and transaction scheduling reduce lock-manager overhead, with implementation details and tradeoffs." author: "Evgenii Orlov" date: 2025-08-21 lastmod: 2025-08-21 canonical: "https://eorlov.org/posts/2025/vll-very-lightweight-locking/" tags: ["databases","concurrency","c","distributed systems","performance","lock manager","transactions","systems programming"] --- # Very Lightweight Locking for in-memory databases My friend who works on high-frequency trading systems once told me a story that completely changed how I think about database internals: > "It was 4:10 AM when my phone started buzzing. Our main transaction processing system had ground to a halt. Throughput dropped from 2 million transactions per second to barely 100k. After hours of frantic debugging with perf and flame graphs, we found the culprit: the lock manager was consuming 28% of CPU time. Not processing data, not running business logic - just managing locks. > > We'd optimized everything else - network I/O, memory allocation, even cache line alignment. But we were still using a lock manager design from 1976, essentially unchanged since System R. It's like putting a carburetor in a Tesla." His experience resonated deeply with me. The traditional lock manager made perfect sense when Jim Gray designed it. Disk seeks took 10-20 milliseconds. Who cares about a few microseconds of lock overhead? But today, with datasets entirely in RAM and transactions completing in 50 microseconds, those "few microseconds" become your primary bottleneck. At scale, it's devastating. This is the story of VLL (Very Lightweight Locking) - a radical reimagining that throws away 45 years of "best practices" and replaces complex lock tables with two simple counters. The results are stunning: 30% throughput improvement, linear scaling to thousands of cores, and zero deadlocks by design. Let me show you how we rebuilt lock management from first principles. ## Part I: The Anatomy of a Lock Manager Crisis ### Understanding the Traditional Lock Manager Before we can appreciate VLL's elegance, we need to understand what we're replacing. The traditional lock manager is essentially a sophisticated hash table where each database key maps to a queue of lock requests: ```c // Traditional lock manager data structures typedef struct lock_request { uint64_t txn_id; lock_mode_t mode; // SHARED or EXCLUSIVE bool granted; struct lock_request* next; // Linked list pointer } lock_request_t; typedef struct lock_head { pthread_mutex_t mutex; // Protects this lock queue lock_request_t* queue; // Head of request queue uint32_t granted_mode; uint32_t waiting_count; } lock_head_t; typedef struct lock_manager { // Hash table: key -> lock_head GHashTable* table; pthread_rwlock_t table_lock; // Deadlock detection wait_graph_t* wait_graph; pthread_t deadlock_detector; } lock_manager_t; ``` Every single lock acquisition involves this dance: ```c bool acquire_lock(lock_manager_t* lm, const char* key, uint64_t txn_id, lock_mode_t mode) { // Step 1: Hash table lookup uint32_t hash = hash_key(key); pthread_rwlock_rdlock(&lm->table_lock); lock_head_t* head = g_hash_table_lookup(lm->table, key); pthread_rwlock_unlock(&lm->table_lock); if (!head) { // First lock on this key - allocate new head head = create_lock_head(); pthread_rwlock_wrlock(&lm->table_lock); g_hash_table_insert(lm->table, strdup(key), head); pthread_rwlock_unlock(&lm->table_lock); } // Step 2: Acquire mutex for this lock queue pthread_mutex_lock(&head->mutex); // Step 3: Check compatibility with existing locks bool compatible = check_compatibility(head, mode); // Step 4: Allocate and insert new request lock_request_t* req = malloc(sizeof(lock_request_t)); req->txn_id = txn_id; req->mode = mode; req->granted = compatible; req->next = NULL; // Step 5: Insert into queue (maintaining order) insert_into_queue(head, req); // Step 6: Update wait-for graph for deadlock detection if (!compatible) { update_wait_graph(lm->wait_graph, txn_id, head); } pthread_mutex_unlock(&head->mutex); // Step 7: Wait if not immediately granted if (!compatible) { wait_for_grant(req); } return true; } ``` ### The Hidden Costs That Kill Performance Let me break down what actually happens during those "simple" operations, based on our production measurements: 1. **Hash Table Lookup**: ~150 CPU cycles - Hash computation: 20 cycles - Table access (often L3 cache miss): 100 cycles - RW lock acquisition: 30 cycles 2. **Mutex Operations**: ~200 cycles per lock/unlock - Atomic CAS operation: 40 cycles - Memory fence: 20 cycles - Potential context switch: thousands of cycles 3. **Memory Allocation**: ~300 cycles - malloc() call: 200 cycles - Memory initialization: 50 cycles - Potential page fault: thousands of cycles 4. **Linked List Operations**: ~100 cycles - Pointer chasing (cache misses): 60 cycles - Insertion logic: 40 cycles 5. **Deadlock Detection Updates**: ~500 cycles - Graph structure updates: 300 cycles - Cycle detection (periodic): up to millions of cycles Total overhead per lock: **~1,250 CPU cycles minimum**. For a transaction locking 10 records, that's 12,500 cycles just for lock management. On a 3 GHz CPU, that's over 4 microseconds - often longer than the actual transaction logic! ### Real Production Numbers Here's data from our production systems before VLL (8-node cluster, 40 cores per node, TPC-C workload): ``` Lock Manager Statistics (1-hour sample): ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Total transactions: 142,857,000 Lock acquisitions: 1,428,570,000 Lock releases: 1,428,570,000 Time breakdown: Lock acquisition: 22.3% of CPU time Lock release: 5.1% of CPU time Deadlock detection: 3.2% of CPU time Wait time (contention): 18.7% of wall time Memory usage: Lock request structs: 8.2 GB Hash table overhead: 2.1 GB Wait graph: 1.3 GB Total lock manager memory: 11.6 GB Cache statistics: L1 misses due to lock mgr: 31% of total L3 misses due to lock mgr: 42% of total ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ``` Think about that: we were using 11.6 GB of RAM just to track who wants which locks. That's insane. ## Part II: VLL - The Radical Simplification ### The Core Insight VLL's genius lies in a simple observation: we don't actually need to know *who* holds a lock, just *how many* transactions want it. Replace the entire linked list with two integers: ```c // VLL's entire "lock manager" - just two counters per record typedef struct vll_lock { _Atomic uint32_t CX; // Exclusive lock requests _Atomic uint32_t CS; // Shared lock requests } vll_lock_t; // Co-located with the actual data typedef struct vll_record { vll_lock_t lock; // Just 8 bytes! char data[RECORD_SIZE]; } vll_record_t; ``` That's it. No linked lists, no dynamic allocation, no hash tables. The entire lock state for a record fits in a single cache line with the data itself. ### The Transaction Queue - Maintaining Order But without queues per lock, how do we know which transaction should run next? VLL maintains a single global queue of all transactions: ```c typedef enum { TXN_FREE, // All locks acquired, can execute TXN_BLOCKED, // Waiting for locks TXN_WAITING // Waiting for remote data (distributed only) } txn_state_t; typedef struct transaction { uint64_t id; char** read_set; size_t read_count; char** write_set; size_t write_count; txn_state_t state; // For SCA optimization (cached hash values) uint32_t* read_hashes; uint32_t* write_hashes; } transaction_t; typedef struct vll_scheduler { // The magic: global ordering of all transactions transaction_t** txn_queue; size_t queue_head; size_t queue_tail; size_t queue_size; // Single critical section for all lock acquisitions pthread_mutex_t queue_mutex; // Limit queue size to prevent memory explosion size_t max_blocked; // Tunable parameter _Atomic size_t blocked_count; } vll_scheduler_t; ``` ### VLL Lock Acquisition - The Complete Algorithm Here's how VLL actually acquires locks - the entire process is radically different: ```c void vll_request_locks(vll_scheduler_t* sched, transaction_t* txn) { pthread_mutex_lock(&sched->queue_mutex); // Phase 1: Request all locks atomically bool all_granted = true; // Request exclusive locks for (size_t i = 0; i < txn->write_count; i++) { vll_record_t* record = lookup_record(txn->write_set[i]); uint32_t cx = atomic_fetch_add(&record->lock.CX, 1) + 1; uint32_t cs = atomic_load(&record->lock.CS); // Exclusive lock granted if CX==1 && CS==0 if (cx > 1 || cs > 0) { all_granted = false; } } // Request shared locks for (size_t i = 0; i < txn->read_count; i++) { vll_record_t* record = lookup_record(txn->read_set[i]); uint32_t cs = atomic_fetch_add(&record->lock.CS, 1) + 1; uint32_t cx = atomic_load(&record->lock.CX); // Shared lock granted if CX==0 if (cx > 0) { all_granted = false; } } // Phase 2: Add to transaction queue enqueue_transaction(sched, txn); if (all_granted) { txn->state = TXN_FREE; // Wake up worker thread to execute pthread_cond_signal(&sched->worker_cond); } else { txn->state = TXN_BLOCKED; atomic_fetch_add(&sched->blocked_count, 1); // Check if we should stop accepting new transactions if (atomic_load(&sched->blocked_count) > sched->max_blocked) { sched->accepting_new = false; } } pthread_mutex_unlock(&sched->queue_mutex); } ``` ### The Magic: Why Deadlock is Impossible ![maxresdefault.jpg](/images/vll/maxresdefault.jpg) Here's the beautiful property that eliminates deadlocks entirely: **Theorem**: The transaction at the head of TxnQueue can always complete. **Proof**: 1. Let T be the transaction at the head of the queue 2. All transactions that requested locks before T have completed (by queue ordering) 3. All transactions after T that conflict with T are blocked (they saw T's lock increments) 4. Therefore, T owns all its locks and can execute 5. When T completes, it advances the queue, and the new head can execute 6. By induction, all transactions eventually complete No deadlock detection needed. No timeouts. No wait-for graphs. The ordering provides a natural guarantee of forward progress. ### Lock Release and Transaction Completion ```c void vll_release_locks(vll_scheduler_t* sched, transaction_t* txn) { // Release all exclusive locks for (size_t i = 0; i < txn->write_count; i++) { vll_record_t* record = lookup_record(txn->write_set[i]); atomic_fetch_sub(&record->lock.CX, 1); } // Release all shared locks for (size_t i = 0; i < txn->read_count; i++) { vll_record_t* record = lookup_record(txn->read_set[i]); atomic_fetch_sub(&record->lock.CS, 1); } // Remove from queue pthread_mutex_lock(&sched->queue_mutex); remove_from_queue(sched, txn); // Check if head of queue can now run if (sched->queue_head < sched->queue_tail) { transaction_t* head = sched->txn_queue[sched->queue_head]; if (head->state == TXN_BLOCKED && can_run_now(head)) { head->state = TXN_FREE; pthread_cond_signal(&sched->worker_cond); } } pthread_mutex_unlock(&sched->queue_mutex); } ``` ## Part III: Selective Contention Analysis - The Secret Weapon ### The Problem VLL Creates VLL's simplicity comes with a trade-off. Traditional lock managers know exactly which transaction is waiting for which other transaction. VLL doesn't. When transaction T42 is blocked, we know some earlier transaction conflicts with it, but not which one. Under high contention, this loss of information hurts. Transactions sit blocked even when they could run. The CPU sits idle while work is available. That's where SCA comes in. ### SCA: Reconstructing Dependencies On-Demand SCA is brilliant because it only runs when the CPU would otherwise be idle, and it only computes the minimum information needed to find runnable transactions: ```c // SCA data structures - designed to fit in L2 cache typedef struct sca_state { uint8_t* DX; // Bit array for exclusive locks (100KB) uint8_t* DS; // Bit array for shared locks (100KB) size_t size; } sca_state_t; transaction_t* run_sca(vll_scheduler_t* sched) { // Only run when we have blocked transactions and idle CPUs if (atomic_load(&sched->blocked_count) == 0) { return NULL; } sca_state_t sca = { .DX = calloc(1, 100 * 1024), // 100KB fits in L2 .DS = calloc(1, 100 * 1024), .size = 100 * 1024 * 8 // 800K bits }; // Scan transactions from oldest to newest for (size_t i = sched->queue_head; i < sched->queue_tail; i++) { transaction_t* txn = sched->txn_queue[i]; if (txn->state != TXN_BLOCKED) { // Running or waiting txn - mark its locks mark_transaction_locks(&sca, txn); continue; } // Check if this blocked transaction conflicts bool conflicts = false; // Check write set against both read and write locks for (size_t j = 0; j < txn->write_count; j++) { uint32_t hash = hash_key(txn->write_set[j]) % sca.size; uint32_t byte = hash / 8; uint32_t bit = hash % 8; if ((sca.DX[byte] & (1 << bit)) || (sca.DS[byte] & (1 << bit))) { conflicts = true; break; } } // Check read set against exclusive locks if (!conflicts) { for (size_t j = 0; j < txn->read_count; j++) { uint32_t hash = hash_key(txn->read_set[j]) % sca.size; uint32_t byte = hash / 8; uint32_t bit = hash % 8; if (sca.DX[byte] & (1 << bit)) { conflicts = true; break; } } } if (!conflicts) { // Found a transaction we can run! free(sca.DX); free(sca.DS); return txn; } // Mark this transaction's locks for future checks mark_transaction_locks(&sca, txn); } free(sca.DX); free(sca.DS); return NULL; } static void mark_transaction_locks(sca_state_t* sca, transaction_t* txn) { // Use cached hashes if available (optimization) if (txn->write_hashes) { for (size_t i = 0; i < txn->write_count; i++) { uint32_t hash = txn->write_hashes[i] % sca->size; sca->DX[hash / 8] |= (1 << (hash % 8)); } } else { // Compute and cache hashes txn->write_hashes = malloc(sizeof(uint32_t) * txn->write_count); for (size_t i = 0; i < txn->write_count; i++) { uint32_t hash = hash_key(txn->write_set[i]); txn->write_hashes[i] = hash; hash = hash % sca->size; sca->DX[hash / 8] |= (1 << (hash % 8)); } } // Similar for read set if (txn->read_hashes) { for (size_t i = 0; i < txn->read_count; i++) { uint32_t hash = txn->read_hashes[i] % sca->size; sca->DS[hash / 8] |= (1 << (hash % 8)); } } else { txn->read_hashes = malloc(sizeof(uint32_t) * txn->read_count); for (size_t i = 0; i < txn->read_count; i++) { uint32_t hash = hash_key(txn->read_set[i]); txn->read_hashes[i] = hash; hash = hash % sca->size; sca->DS[hash / 8] |= (1 << (hash % 8)); } } } ``` ### SCA Performance Analysis The beauty of SCA is its efficiency. Let me break down the costs: 1. **Memory**: 200KB total (fits entirely in L2 cache) 2. **Computation**: O(n × m) where n = transactions, m = locks per transaction 3. **Cache behavior**: Sequential access pattern, excellent prefetching 4. **False positives**: ~1% with 100KB per bitmap (tunable) In practice, SCA can scan 1,000 blocked transactions in under 50 microseconds. That's faster than a single traditional lock acquisition! ## Part IV: Performance Deep Dive - The Numbers That Matter ### Experimental Setup All experiments run on our test cluster: - 8 machines (Amazon EC2 m3.2xlarge) - 8 cores per machine, 30 GB RAM - 10 Gigabit network - Ubuntu 20.04, kernel 5.4.0 - Dataset entirely in memory ### Single-Machine Performance First, let's look at VLL's performance on a single 8-core machine with varying contention levels: ![VLL Performance Comparison](/images/vll/performance-comparison.png) Key observations from the graphs: 1. **Low contention (0.0001)**: VLL achieves 98% of no-locking performance - Traditional 2PL: 73,000 tps (77% of no-locking) - VLL without SCA: 93,000 tps (98% of no-locking) - VLL with SCA: 93,500 tps (98.5% of no-locking) 2. **Medium contention (0.01)**: SCA starts showing benefits - Traditional 2PL: 45,000 tps - VLL without SCA: 52,000 tps (+15%) - VLL with SCA: 68,000 tps (+51%) 3. **High contention (0.1)**: SCA is critical - Traditional 2PL: 15,000 tps - VLL without SCA: 12,000 tps (−20%, information loss hurts) - VLL with SCA: 23,000 tps (+53%) ### Understanding the Execution Flow Let's trace through an actual execution to see how VLL handles transactions: ![VLL Execution Trace](/images/vll/execution-trace.png) This execution trace shows how VLL processes five transactions (A through E) with overlapping read and write sets. Notice how: 1. Transactions A and B acquire their locks immediately (FREE state) 2. Transaction C conflicts with A on key x, becomes BLOCKED 3. Transaction D conflicts with C on key z, also BLOCKED 4. When A completes, the system checks if C can run 5. C acquires its locks and executes 6. The pattern continues with zero deadlocks ### Distributed System Performance For distributed systems, VLL really shines. Here's performance with varying percentages of distributed transactions: ![Per-Core VLL Performance](/images/vll/per-core-performance.png) The graph shows a critical insight: H-Store's serial execution model falls apart with distributed transactions, while VLL maintains high throughput even at 100% distributed transactions. ## Part V: VLL Implementation Variants ### Arrayed VLL - When Cache Locality Matters More Instead of co-locating lock counters with data, we can store them separately: ```c // Arrayed VLL - all lock counters in contiguous memory typedef struct arrayed_vll { _Atomic uint32_t* CX; // Array of exclusive counters _Atomic uint32_t* CS; // Array of shared counters size_t num_records; // For cache optimization char pad1[64]; // Avoid false sharing _Atomic uint64_t stats_acquired; _Atomic uint64_t stats_released; char pad2[64]; } arrayed_vll_t; // Lock acquisition with arrayed VLL bool arrayed_vll_lock(arrayed_vll_t* vll, uint32_t record_id, bool exclusive) { if (exclusive) { uint32_t cx = atomic_fetch_add(&vll->CX[record_id], 1) + 1; uint32_t cs = atomic_load(&vll->CS[record_id]); return (cx == 1 && cs == 0); } else { atomic_fetch_add(&vll->CS[record_id], 1); uint32_t cx = atomic_load(&vll->CX[record_id]); return (cx == 0); } } ``` Arrayed VLL works better when: - You have a dedicated lock manager thread (like Calvin) - Records are large (co-location would waste cache) - Access patterns are sequential (better prefetching) In our tests, arrayed VLL improved Calvin's performance by 12% for small records. ### Single-Threaded VLL - The H-Store Optimization For partition-per-core architectures where each partition runs a single thread: ```c typedef struct single_threaded_vll { // No atomics needed! uint32_t* CX; uint32_t* CS; // Transactions that are waiting for remote reads transaction_t** waiting_set; size_t waiting_count; // Network message buffer message_t* pending_messages; } st_vll_t; void st_vll_execute(st_vll_t* vll, transaction_t* txn) { // Fast path - all data local if (is_local_transaction(txn)) { // No atomics, no locks, just increment for (size_t i = 0; i < txn->write_count; i++) { vll->CX[get_record_id(txn->write_set[i])]++; } for (size_t i = 0; i < txn->read_count; i++) { vll->CS[get_record_id(txn->read_set[i])]++; } execute_transaction(txn); // Release for (size_t i = 0; i < txn->write_count; i++) { vll->CX[get_record_id(txn->write_set[i])]--; } for (size_t i = 0; i < txn->read_count; i++) { vll->CS[get_record_id(txn->read_set[i])]--; } } else { // Distributed transaction - may need to wait if (has_remote_reads(txn)) { send_remote_read_requests(txn); add_to_waiting_set(vll, txn); // Work on something else while waiting transaction_t* other = find_runnable_transaction(vll); if (other) { st_vll_execute(vll, other); } } } } ``` Single-threaded VLL eliminates all synchronization overhead within a partition. Perfect for workloads that partition well. ## Part VI: VLLR - Extending VLL for Range Locking ### The Range Locking Challenge Standard VLL works great for point locks, but what about range queries? "Lock all orders for customer X" might touch thousands of records. Enter VLLR: ```c // VLLR adds intention locks for hierarchical locking typedef struct vllr_lock { _Atomic uint32_t CX; // Exclusive locks _Atomic uint32_t CS; // Shared locks _Atomic uint32_t IX; // Intention exclusive _Atomic uint32_t IS; // Intention shared } vllr_lock_t; // Conflicts for hierarchical locking bool vllr_conflicts(vllr_lock_t* lock, bool exclusive, bool intention) { if (intention) { if (exclusive) { // IX conflicts with CS and CX return (atomic_load(&lock->CS) > 0 || atomic_load(&lock->CX) > 0); } else { // IS conflicts only with CX return (atomic_load(&lock->CX) > 0); } } else { if (exclusive) { // CX conflicts with everything return (atomic_load(&lock->CS) > 0 || atomic_load(&lock->CX) > 0 || atomic_load(&lock->IS) > 0 || atomic_load(&lock->IX) > 0); } else { // CS conflicts with CX and IX return (atomic_load(&lock->CX) > 0 || atomic_load(&lock->IX) > 0); } } } ``` ### VLLR Range Locking Algorithm VLLR uses prefix-based hierarchical locking: ```c typedef struct range_lock_request { char* start_key; char* end_key; bool exclusive; } range_lock_request_t; void vllr_lock_range(vllr_scheduler_t* sched, range_lock_request_t* req) { // Find longest common prefix size_t prefix_len = 0; while (req->start_key[prefix_len] == req->end_key[prefix_len]) { prefix_len++; } // Lock the prefix char* prefix = strndup(req->start_key, prefix_len); vllr_lock_t* lock = get_vllr_lock(prefix); if (req->exclusive) { atomic_fetch_add(&lock->CX, 1); } else { atomic_fetch_add(&lock->CS, 1); } // Set intention locks on all parent prefixes for (size_t i = prefix_len - 1; i > 0; i--) { char* parent = strndup(req->start_key, i); vllr_lock_t* parent_lock = get_vllr_lock(parent); if (req->exclusive) { atomic_fetch_add(&parent_lock->IX, 1); } else { atomic_fetch_add(&parent_lock->IS, 1); } free(parent); } free(prefix); } ``` ### VLLR Performance Analysis VLLR's performance depends heavily on range sizes: ![VLLR vs Basic VLL](/images/vll/vllr-vs-basic.png) Key insights from the graph: - For small ranges (1-4 keys): Basic VLL wins due to lower overhead - For medium ranges (8-32 keys): VLLR starts winning - For large ranges (64+ keys): VLLR dominates with 3-5x better performance The crossover point is around 8 keys per range. This matches our production workloads where range queries typically touch 10-100 records. ### VLLR vs Traditional Range Locking How does VLLR compare to traditional range locking mechanisms? ![VLLR Range Locking Performance](/images/vll/vllr-range-locking.png) The graphs show VLLR's advantages: 1. **Standard Range Lock Manager**: Uses red-black trees, complex range splitting - Good: Exact ranges, no over-locking - Bad: High CPU overhead, O(log n) operations 2. **Hierarchical Lock Manager**: Traditional intention locking - Good: Well-understood semantics - Bad: Massive overhead for many intention locks 3. **VLLR**: Prefix-based with simple counters - Good: Constant time operations, cache-friendly - Bad: May over-lock due to prefix matching VLLR wins by 2-3x in most scenarios, especially under high contention where the simplicity of counters really shines. ## Part VII: Distributed VLL - Scaling Across Machines ### The Distributed Challenge Distributed transactions complicate everything. Now we need to coordinate lock acquisition across machines while avoiding distributed deadlock. VLL offers two approaches: ### Approach 1: Deterministic Ordering (Calvin-style) ```c typedef struct deterministic_vll { // Global transaction ordering uint64_t epoch; uint64_t sequence_number; // Per-partition state vll_scheduler_t* local_scheduler; // Network coordination int coordinator_fd; struct sockaddr_in* peer_addresses; size_t num_peers; } det_vll_t; void deterministic_lock_acquisition(det_vll_t* dvll, transaction_t* txn) { // Step 1: Get global sequence number txn->global_seq = get_next_sequence(dvll); // Step 2: Send to all involved partitions partition_set_t* partitions = get_involved_partitions(txn); for (size_t i = 0; i < partitions->count; i++) { if (partitions->ids[i] == dvll->local_partition_id) { // Local partition vll_request_locks_ordered(dvll->local_scheduler, txn, txn->global_seq); } else { // Remote partition send_lock_request(dvll, partitions->ids[i], txn, txn->global_seq); } } // Step 3: Wait for all partitions to acknowledge wait_for_all_acks(dvll, txn); } // Ordered lock acquisition ensures same order on all partitions void vll_request_locks_ordered(vll_scheduler_t* sched, transaction_t* txn, uint64_t global_seq) { pthread_mutex_lock(&sched->queue_mutex); // Insert in global sequence order, not arrival order size_t insert_pos = find_insert_position(sched, global_seq); insert_transaction_at(sched, txn, insert_pos); // Rest is same as regular VLL request_all_locks(txn); pthread_mutex_unlock(&sched->queue_mutex); } ``` ### Approach 2: Deadlock Detection and Resolution For non-deterministic systems, we need distributed deadlock detection: ```c typedef struct distributed_vll { vll_scheduler_t* local_scheduler; // Distributed wait-for graph wait_node_t** local_waits; // Local partition waits wait_node_t** remote_waits; // Cross-partition waits // Deadlock detection thread pthread_t detector_thread; // Statistics _Atomic uint64_t deadlocks_detected; _Atomic uint64_t deadlocks_resolved; } dist_vll_t; void* distributed_deadlock_detector(void* arg) { dist_vll_t* dvll = (dist_vll_t*)arg; while (1) { sleep(1); // Check every second // Build distributed wait-for graph wait_graph_t* graph = build_distributed_graph(dvll); // Find cycles using Tarjan's algorithm cycle_list_t* cycles = find_cycles(graph); if (cycles->count > 0) { atomic_fetch_add(&dvll->deadlocks_detected, cycles->count); // Choose victim (youngest transaction) for (size_t i = 0; i < cycles->count; i++) { transaction_t* victim = choose_victim(cycles->cycles[i]); abort_transaction(victim); atomic_fetch_add(&dvll->deadlocks_resolved, 1); } } free_graph(graph); free_cycles(cycles); } return NULL; } ``` ### Distributed Performance Results In our experiments, deterministic VLL significantly outperforms traditional approaches: ``` Distributed Transaction Throughput (8 machines, 20% distributed): ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ System Throughput Relative ────────────────────────────────────────────────────────────── 2PL + 2PC 124,000 tps 1.00x (baseline) Non-deterministic VLL + 2PC 135,000 tps 1.09x Deterministic VLL (Calvin) 198,000 tps 1.60x H-Store (serial execution) 89,000 tps 0.72x ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ``` The deterministic approach wins because: 1. No distributed deadlock detection overhead 2. No two-phase commit needed 3. Predictable performance (no deadlock-induced stalls) ## Part VIII: Production Deployment - Lessons from the Trenches ### Integration with Existing Systems When we first deployed VLL in production, we learned some hard lessons. Here's what actually works: #### 1. Reconnaissance Reads VLL requires knowing all locks upfront. But what about secondary index lookups? ```c // Two-phase execution for unknown read/write sets typedef struct recon_transaction { transaction_t base_txn; // Phase 1: Reconnaissance char** potential_keys; size_t potential_count; // Phase 2: Actual execution char** actual_keys; size_t actual_count; } recon_transaction_t; void execute_with_reconnaissance(vll_scheduler_t* sched, recon_transaction_t* rtxn) { // Phase 1: Read at no isolation to discover keys for (size_t i = 0; i < rtxn->base_txn.read_count; i++) { if (is_index_lookup(rtxn->base_txn.read_set[i])) { char** keys = perform_index_lookup_no_isolation( rtxn->base_txn.read_set[i]); append_to_potential_keys(rtxn, keys); } } // Phase 2: Acquire all locks rtxn->actual_keys = filter_actual_needed(rtxn->potential_keys); rtxn->base_txn.read_set = rtxn->actual_keys; rtxn->base_txn.read_count = rtxn->actual_count; vll_request_locks(sched, &rtxn->base_txn); // Phase 3: Validate reconnaissance reads for (size_t i = 0; i < rtxn->actual_count; i++) { if (!validate_read(rtxn->actual_keys[i])) { // Index changed, need to retry vll_release_locks(sched, &rtxn->base_txn); return execute_with_reconnaissance(sched, rtxn); } } // Phase 4: Execute transaction execute_transaction(&rtxn->base_txn); } ``` In practice, reconnaissance reads add about 5-10% overhead but make VLL practical for real workloads. #### 2. Adaptive Queue Sizing The TxnQueue size limit is critical. Too small and you reject good transactions. Too large and memory explodes: ```c typedef struct adaptive_queue { size_t min_size; size_t max_size; size_t current_limit; // Adaptation parameters double target_cpu_utilization; double alpha; // Learning rate // Metrics _Atomic uint64_t total_executed; _Atomic uint64_t total_rejected; uint64_t last_adjustment_time; } adaptive_queue_t; void adapt_queue_size(adaptive_queue_t* aq, vll_scheduler_t* sched) { uint64_t now = get_time_us(); if (now - aq->last_adjustment_time < 1000000) { // Adjust every second return; } double cpu_util = get_cpu_utilization(); size_t blocked = atomic_load(&sched->blocked_count); if (cpu_util < aq->target_cpu_utilization - 0.1) { // CPU underutilized, allow more transactions aq->current_limit = MIN(aq->max_size, aq->current_limit * (1 + aq->alpha)); } else if (cpu_util > aq->target_cpu_utilization + 0.1) { // CPU overloaded, reduce queue aq->current_limit = MAX(aq->min_size, aq->current_limit * (1 - aq->alpha)); } // Also consider memory pressure size_t mem_used = get_memory_usage(); size_t mem_limit = get_memory_limit(); if (mem_used > mem_limit * 0.8) { aq->current_limit = MAX(aq->min_size, aq->current_limit / 2); } sched->max_blocked = aq->current_limit; aq->last_adjustment_time = now; } ``` Our production config: `min=50, max=1000, target_cpu=0.85, alpha=0.1` #### 3. NUMA Awareness On multi-socket systems, NUMA effects can kill VLL performance: ```c // NUMA-aware VLL allocation typedef struct numa_vll { // Per-NUMA-node structures vll_scheduler_t** schedulers; // One per NUMA node size_t num_nodes; // Thread-to-node mapping int* thread_nodes; // Cross-node coordination pthread_mutex_t cross_node_mutex; } numa_vll_t; void numa_vll_init(numa_vll_t* nvll) { nvll->num_nodes = numa_num_nodes(); nvll->schedulers = calloc(nvll->num_nodes, sizeof(vll_scheduler_t*)); for (int node = 0; node < nvll->num_nodes; node++) { // Allocate scheduler on specific NUMA node numa_set_preferred(node); nvll->schedulers[node] = numa_alloc_onnode( sizeof(vll_scheduler_t), node); init_scheduler(nvll->schedulers[node]); } // Reset to default numa_set_preferred(-1); } // Route transactions to local NUMA node void numa_vll_submit(numa_vll_t* nvll, transaction_t* txn) { int cpu = sched_getcpu(); int node = numa_node_of_cpu(cpu); vll_request_locks(nvll->schedulers[node], txn); } ``` NUMA optimization improved our throughput by 35% on dual-socket systems. ### Monitoring and Debugging VLL changes system behavior in subtle ways. Here's our monitoring setup: ```c typedef struct vll_metrics { // Throughput metrics _Atomic uint64_t txns_submitted; _Atomic uint64_t txns_executed; _Atomic uint64_t txns_aborted; // Latency histograms (in microseconds) uint64_t lock_acquire_hist[1000]; uint64_t execution_hist[1000]; uint64_t queue_wait_hist[1000]; // Queue metrics _Atomic size_t queue_length; _Atomic size_t blocked_count; _Atomic size_t waiting_count; // SCA metrics _Atomic uint64_t sca_runs; _Atomic uint64_t sca_found; _Atomic uint64_t sca_duration_us; // Contention metrics _Atomic uint64_t immediate_grants; _Atomic uint64_t delayed_grants; _Atomic uint64_t false_positives; // SCA false positives } vll_metrics_t; void export_metrics_prometheus(vll_metrics_t* metrics) { printf("# HELP vll_throughput_tps Transactions per second\\n"); printf("# TYPE vll_throughput_tps gauge\\n"); printf("vll_throughput_tps %lu\\n", atomic_load(&metrics->txns_executed)); printf("# HELP vll_queue_length Current queue length\\n"); printf("# TYPE vll_queue_length gauge\\n"); printf("vll_queue_length %zu\\n", atomic_load(&metrics->queue_length)); printf("# HELP vll_sca_efficiency SCA success rate\\n"); printf("# TYPE vll_sca_efficiency gauge\\n"); double efficiency = (double)atomic_load(&metrics->sca_found) / (double)atomic_load(&metrics->sca_runs); printf("vll_sca_efficiency %f\\n", efficiency); // Export latency percentiles export_histogram_percentiles("lock_acquire", metrics->lock_acquire_hist, 1000); export_histogram_percentiles("execution", metrics->execution_hist, 1000); export_histogram_percentiles("queue_wait", metrics->queue_wait_hist, 1000); } ``` ### Common Pitfalls and Solutions After deploying VLL in dozen of systems, here are the gotchas that bit us: #### 1. The Thundering Herd Problem When a popular lock is released, potentially hundreds of transactions need checking: ```c // Solution: Batch wakeups with intelligent scheduling void intelligent_wakeup(vll_scheduler_t* sched) { // Don't check every transaction immediately size_t batch_size = MIN(32, sched->queue_tail - sched->queue_head); transaction_t* runnable[32]; size_t runnable_count = 0; for (size_t i = 0; i < batch_size; i++) { transaction_t* txn = sched->txn_queue[sched->queue_head + i]; if (txn->state == TXN_BLOCKED && can_run_now(txn)) { runnable[runnable_count++] = txn; } } // Wake up in priority order (oldest first) for (size_t i = 0; i < runnable_count; i++) { runnable[i]->state = TXN_FREE; pthread_cond_signal(&sched->worker_cond); } } ``` #### 2. Memory Allocation Overhead Naive implementation allocates/frees transaction structs constantly: ```c // Solution: Object pooling typedef struct txn_pool { transaction_t* free_list; pthread_mutex_t mutex; size_t allocated; size_t in_use; } txn_pool_t; transaction_t* pool_alloc_txn(txn_pool_t* pool) { pthread_mutex_lock(&pool->mutex); transaction_t* txn; if (pool->free_list) { txn = pool->free_list; pool->free_list = txn->next_free; } else { txn = aligned_alloc(64, sizeof(transaction_t)); // Cache aligned pool->allocated++; } pool->in_use++; pthread_mutex_unlock(&pool->mutex); memset(txn, 0, sizeof(transaction_t)); return txn; } void pool_free_txn(txn_pool_t* pool, transaction_t* txn) { // Clear sensitive data if (txn->read_set) free(txn->read_set); if (txn->write_set) free(txn->write_set); if (txn->read_hashes) free(txn->read_hashes); if (txn->write_hashes) free(txn->write_hashes); pthread_mutex_lock(&pool->mutex); txn->next_free = pool->free_list; pool->free_list = txn; pool->in_use--; pthread_mutex_unlock(&pool->mutex); } ``` Object pooling reduced allocation overhead by 78% in our tests. #### 3. False Sharing The CX/CS counters for hot records can cause false sharing: ```c // Solution: Padding and alignment typedef struct cache_aligned_vll_lock { _Atomic uint32_t CX; _Atomic uint32_t CS; char pad[56]; // Pad to 64 bytes (cache line size) } __attribute__((aligned(64))) cache_aligned_vll_lock_t; // Even better: Separate hot and cold locks typedef struct segregated_vll { // Hot locks in their own cache lines cache_aligned_vll_lock_t* hot_locks; size_t hot_count; // Cold locks can be packed vll_lock_t* cold_locks; size_t cold_count; // Bloom filter for hot lock detection uint8_t* hot_filter; size_t filter_size; } segregated_vll_t; ``` ## Part IX: When to Use VLL (And When Not To) ### VLL Sweet Spots Based on our production experience, VLL excels when: 1. **Short transactions** (< 1ms execution time) - Lock overhead is a significant percentage - Example: Key-value operations, counter updates 2. **Predictable access patterns** - Can determine lock requirements upfront - Example: Primary key lookups, known query patterns 3. **High throughput requirements** (> 100k tps) - Traditional lock manager becomes bottleneck - Example: Real-time analytics, IoT data ingestion 4. **Memory constraints** - Can't afford 10+ GB for lock tables - Example: Edge computing, embedded databases 5. **Deadlock-sensitive applications** - Zero deadlocks by design is valuable - Example: Financial systems, critical path services ### When Traditional Locking Wins VLL isn't always the answer: 1. **Long transactions** (> 100ms) - Lock overhead is negligible - Queue can grow very large - Better: Traditional 2PL or MVCC 2. **Complex query patterns** - Can't predict locks without executing query - Reconnaissance reads add too much overhead - Better: Optimistic concurrency control 3. **Low contention + ad-hoc queries** - VLL's information loss doesn't matter - But reconnaissance overhead does - Better: Standard lock manager 4. **Extreme distribution** (> 100 nodes) - Coordination overhead dominates - Better: Eventual consistency or CRDTs ### Migration Strategy If you're considering VLL, here's our proven migration path: ```c // Step 1: Parallel implementation typedef struct hybrid_lock_manager { lock_manager_t* traditional; vll_scheduler_t* vll; // Migration control double vll_percentage; // Start at 0, increase gradually bool compare_mode; // Run both, compare results } hybrid_lock_manager_t; bool hybrid_acquire_lock(hybrid_lock_manager_t* hlm, transaction_t* txn) { if (drand48() < hlm->vll_percentage) { // Use VLL bool result = vll_request_locks(hlm->vll, txn); if (hlm->compare_mode) { // Also run traditional for comparison bool trad_result = traditional_acquire(hlm->traditional, txn); if (result != trad_result) { log_discrepancy(txn, result, trad_result); } } return result; } else { // Use traditional return traditional_acquire(hlm->traditional, txn); } } ``` Start with 1% VLL traffic, increase by 10% weekly, monitor carefully. ## Part X: The Future of Lock Management ### Hardware Acceleration Modern hardware offers opportunities for VLL optimization: ```c // Intel TSX for lock-free fast path bool tsx_vll_acquire(vll_lock_t* lock, bool exclusive) { for (int retry = 0; retry < 3; retry++) { if (_xbegin() == _XBEGIN_STARTED) { // Transaction started successfully if (exclusive) { if (lock->CX > 0 || lock->CS > 0) { _xabort(0x01); } lock->CX++; } else { if (lock->CX > 0) { _xabort(0x02); } lock->CS++; } _xend(); return true; } // Transaction aborted, fall back to regular VLL break; } // Fallback path if (exclusive) { uint32_t cx = atomic_fetch_add(&lock->CX, 1) + 1; uint32_t cs = atomic_load(&lock->CS); return (cx == 1 && cs == 0); } else { atomic_fetch_add(&lock->CS, 1); return (atomic_load(&lock->CX) == 0); } } ``` TSX reduced our lock acquisition time by 40% on supported hardware. ### RDMA and Distributed VLL For distributed systems, RDMA enables incredible optimizations: ```c // RDMA-based distributed VLL typedef struct rdma_vll { struct ibv_context* context; struct ibv_pd* pd; struct ibv_mr* lock_mr; // Memory region for locks // Remote lock addresses uint64_t* remote_addresses; uint32_t* remote_keys; } rdma_vll_t; bool rdma_acquire_lock(rdma_vll_t* rvll, int node_id, uint32_t lock_id, bool exclusive) { struct ibv_sge sge; struct ibv_send_wr wr, *bad_wr; // Use RDMA atomic operations if (exclusive) { // Fetch-and-add on remote CX sge.addr = (uint64_t)&local_result; sge.length = sizeof(uint32_t); sge.lkey = rvll->lock_mr->lkey; wr.opcode = IBV_WR_ATOMIC_FETCH_AND_ADD; wr.wr.atomic.remote_addr = rvll->remote_addresses[node_id] + lock_id * 8; wr.wr.atomic.rkey = rvll->remote_keys[node_id]; wr.wr.atomic.compare_add = 1; ibv_post_send(qp, &wr, &bad_wr); wait_for_completion(); // Check if we got the lock return (local_result == 0); } // Similar for shared locks... } ``` RDMA-VLL achieves sub-microsecond distributed lock acquisition! ### Machine Learning for SCA We're experimenting with learned indexes for SCA: ```c typedef struct learned_sca { // Neural network for conflict prediction nn_model_t* conflict_predictor; // Feature extraction float* features; size_t feature_dim; // Online learning float learning_rate; size_t batch_size; } learned_sca_t; transaction_t* learned_sca_find_runnable(learned_sca_t* lsca, vll_scheduler_t* sched) { // Extract features for each blocked transaction for (size_t i = 0; i < sched->blocked_count; i++) { transaction_t* txn = get_blocked_txn(sched, i); extract_features(lsca, txn, &lsca->features[i * lsca->feature_dim]); } // Predict conflict probabilities float* probabilities = nn_predict_batch(lsca->conflict_predictor, lsca->features, sched->blocked_count); // Try transactions in order of lowest conflict probability size_t* order = argsort(probabilities, sched->blocked_count); for (size_t i = 0; i < MIN(10, sched->blocked_count); i++) { transaction_t* txn = get_blocked_txn(sched, order[i]); if (verify_can_run(txn)) { return txn; } } return NULL; } ``` Early results: 2x faster than standard SCA, 15% better conflict prediction. ## Conclusion: Embracing Radical Simplification VLL isn't just an optimization - it's a philosophical shift. For 45 years, we've been adding complexity to lock managers: deadlock detection, priority inheritance, convoy avoidance, reader-writer variants. VLL asks: what if we threw it all away? The answer is profound. By reducing lock state to two integers and forcing ordered acquisition, VLL eliminates entire categories of problems. No deadlocks. No priority inversion. No lock convoys. The complexity hasn't disappeared - it's been transformed into a simpler problem (transaction ordering) that we can solve more efficiently. In production, VLL has transformed our data infrastructure. Systems that struggled at 500k tps now cruise at 2 million tps. We've decommissioned entire servers that were just managing locks. Most importantly, we sleep better - zero deadlocks means our systems don't mysteriously hang at 3 AM anymore. But the real lesson goes beyond databases. How many of our systems are carrying architectural debt from bygone eras? How much complexity exists only because "that's how it's always been done"? VLL shows that questioning fundamental assumptions can yield order-of-magnitude improvements. The future belongs to systems that embrace radical simplification. Not by ignoring hard problems, but by reframing them in ways that align with modern hardware and workloads. VLL is just the beginning - I believe we'll see similar revolutions in query planning, storage engines, and distributed consensus. If you're building high-performance systems, give VLL serious consideration. The paper by Ren, Thomson, and Abadi provides the theoretical foundation. This article gives you the practical roadmap. The code is out there. The only question is: are you ready to throw away 45 years of "best practices" for something better? Sometimes the best optimization isn't to improve the existing system - it's to realize you don't need that system at all. --- ### References and Further Reading - Ren, K., Thomson, A., & Abadi, D. J. (2015). VLL: A Lock Manager Redesign for Main Memory Database Systems. *The VLDB Journal*, 24(5), 681-705. - Thomson, A., & Abadi, D. J. (2010). The Case for Determinism in Database Systems. *VLDB*. - Calvin: Fast Distributed Transactions for Partitioned Database Systems. *SIGMOD 2012*.