Administration

Resource use and tuning

The settings that change how much CPU, memory and I/O Raft DB uses and how fast it answers: shards, engine, one process or two, spinning, the allocator cache, synchronous replicas, and resource control by the watcher.

Tune only after measuring your own workload. The numbers quoted here come from the project's measurements on development machines and show direction, not what you will get.

Threads and memory of the server#

ResourceWhat uses itControl
ThreadsOne shard thread and one flusher thread per shard per table; two threads per client connection (reader and writer).shards (default: cores, at most 4). Fewer shards mean fewer threads and less parallelism for writers.
Memory (memory engine)Every document, its key entry (about 15.7 bytes), indexes (about 12 bytes per entry), and the connections' buffers.Size the machine for the data.
Memory (lsm engine)About shards x (memtable + block cache share).lsm-memtable-mb, lsm-block-cache-mb.
Disk I/OOne fsync per group of writes, per shard; checkpoints rewrite logs; lsm compaction rewrites files.checkpoint-min-mb; lsm-l0-*, lsm-level1-mb; the watcher's resource control.
Idle connectionsTwo threads each. A server cannot hold very many idle connections until a poller exists.Pool connections in the application.

Choosing the engine#

  • memory (default): fastest; all data in RAM; restart replays whole logs.
  • lsm: data may exceed RAM; restart replays at most one memtable; reads were measured at 1.27 times the memory engine's cost after tuning (D49); a replicating server writes every row twice (D47). Set it before creating tables: existing tables keep their engine.

One process or two for the SQL front-end#

embed (one process) removes a network round trip from every row operation. Measured through the stock JDBC driver, one process was level with or ahead of MySQL 9.7 on every workload of the project's benchmark; with two processes every row operation pays a loopback round trip (about 60 microseconds on the test PC) and reads stay behind MySQL (D66, D69). Use two processes only to put the front-end on another machine than the data.

Two processes: shared connections#

SettingEffect
raftdb_connections (default 1)Long-lived connections that all sessions share for reads by key. 0 shares nothing (every session has its own connections, up to six per session).
raftdb_share (default reads)all puts every row request on the shared connections: use it with 2 or more connections for a front-end with hundreds of sessions or on another machine. Measured on 12 processors with 16 busy sessions: reads by key faster, range reads level, joins and writes a tenth to a fifth slower than own connections.

Spinning and the allocator#

SettingEffectWhen to change
spin_us (default 150, maximum 1000, 0 = never)A busy connection looks for its next message this long before its thread sleeps, saving the wake-up (tens of microseconds on a desktop). Off for idle or slow connections and when more than a quarter of the processors' worth of connections are busy.Set 0 on a machine that is short of CPU or shared with other work: spinning uses CPU.
alloc_cache (default on)The program's allocator keeps freed blocks per thread. A statement makes hundreds of small blocks; on Windows the system allocator was most of the executor's time.Switch off only to compare, or if memory use matters more than speed.

Both apply to raftdb-server and raftdb-mysql.

Writes#

  • Group commit is automatic: more concurrent writers share each fsync, so throughput grows with concurrency until the disk's flush rate is the limit.
  • Transactions in the SQL front-end wait for the disk once, at COMMIT; many small autocommit statements wait for the disk each. Group them in a transaction when you can.
  • INSERT ... VALUES (...), (...), ... into a table with no unique indexes and no foreign keys is sent to the server together (one wait for the disk per shard). Unique indexes and foreign keys make rows go one by one.
  • sync-replicas adds a network round trip and a remote fsync to every write: in the project's measurement (loopback, one replica) the median went from 143 to 304 microseconds and p99 from 962 to 1,401 microseconds (D39). Writes of several clients share one wait.
  • sync-timeout-ms (default 1000): a dead replica slows each write by up to this long, and is counted in sync.degradedCommits.

Reads#

  • A primary-key lookup, and a comparison or range on an indexed column, use an index; anything else scans the table. Create indexes for the columns of your WHERE clauses (Indexes and constraints).
  • An index is used by counts, pages and sorted queries when it points to at most about 3% of the rows (otherwise a scan is faster).
  • A page deep into a sorted result is expensive: OFFSET 500000 took about 0.9 s in a measurement on 1 million rows, against 40 ms for the top 100. Page by key instead.
  • Select only the columns you need: the front-end reads only the columns a statement names.

Resource control by the watcher (Linux)#

In a managed cluster, resource_control = true makes the watcher read the node's foreground latency every 500 ms and limit compaction and the replication sender so that the foreground p99 stays under fg_latency_ms (default 50). It can also write cpu.weight, io.weight, memory.high and memory.max of the server's cgroup. See Failover with a watcher quorum.

Running benchmarks#

Heavy benchmarks run at below-normal priority and one at a time, so the desktop stays usable: nice -n 10 on Linux, start /belownormal on Windows. See the performance chapter.

See also#