Skip to content
~/docs/guides/concurrency
DOCUMENTATION

Concurrency

Multi-threaded access and parallel reads

KiteDB supports concurrent access from multiple threads, enabling parallel reads for improved throughput in multi-threaded applications.

Concurrency Model

KiteDB uses a readers-writer lock pattern:

  • Multiple concurrent readers – Any number of threads can read simultaneously
  • Exclusive writer – Write operations acquire exclusive access
  • MVCC isolation – Transactions see consistent snapshots
typescript
// Concurrent reads from multiple async operations
const alice = await db.get(user, 'alice');
const results = await Promise.all([
  db.get(user, 'bob'),
  db.get(user, 'charlie'),
  db.from(alice).out('follows').toArray(),
]);

// With worker threads, share the db path (each worker opens independently)
// Workers can read concurrently from the same database file

Performance Notes

Read throughput typically improves with parallel readers, while write throughput is constrained by serialized commit ordering. Measure with your workload and tune batch sizes and sync mode accordingly.

Best Practices

  • Batch writes – Group multiple writes into single operations to minimize exclusive lock time
  • Use transactions for consistency – MVCC ensures readers see consistent snapshots even during concurrent writes
  • Profile your workload – The optimal thread count depends on your read/write ratio and data access patterns
  • Avoid long-held locks – Keep critical sections short; do processing outside the lock

MVCC and Transaction Semantics

KiteDB uses Multi-Version Concurrency Control (MVCC) with serialized writes:

  • Multiple readers can run concurrently
  • A write waits for in-flight reads, then blocks new reads while it commits
  • Each committed transaction is atomic
  • Write conflicts are detected at commit time
typescript
// Atomic transaction (auto-commit on success, rollback on error)
await db.transaction(async (ctx) => {
  const alice = ctx.get(user, 'alice');
  if (alice) {
    ctx.update(user, 'alice')
      .set('name', 'Alice Updated')
      .execute();
  }
});

Limitations

  • Single-process only – Concurrent access is within a single process; multi-process access requires external coordination
  • Write serialization – All writes are serialized; high-write workloads may see contention
  • Memory overhead – MVCC maintains version history, using additional memory

Next Steps