Performance problems
A checklist for slow statements, slow writes, high CPU use, memory growth and slow start-up in Raft DB, with the settings and the tools Raft DB provides.
Measure before you change anything. Tools Raft DB gives you: RAFTDB_MYSQL_TRACE=1 (every statement and its outcome), RAFTDB_MYSQL_PROF=1 (where each statement's time goes), GET /v1/server and GET /v1/stats, the watcher's latency check, and the benchmark programs of the project (not part of the download).
Slow reads#
| Cause | Check | Fix |
|---|---|---|
No index for the WHERE | A comparison or range on an unindexed column scans the table | CREATE INDEX on the column (tutorial). A primary key (integer especially) is indexed already. |
| The index is not selective | An index is used when it points to at most about 3% of the rows; otherwise a scan is faster | Nothing to fix. |
| A condition the engine cannot compare exactly like MySQL | Only comparisons Raft DB makes exactly as MySQL does are pushed to the engine; the rest is evaluated in the front-end | Rewrite with plain comparisons on columns. |
| Reading wide rows | The front-end reads only the columns a statement names | Name columns, avoid SELECT *. |
A deep OFFSET | OFFSET 500000 took about 0.9 s on 1 million rows (every shard returns m + page rows) | Page by key (WHERE id > last ORDER BY id LIMIT n). |
| Two processes | Every row operation pays a round trip (about 60 microseconds on the test PC) | Use embed (one process) (architecture). |
| A join over a big table | Joins run in the front-end; key lists are used to narrow the second table | Index the join column; filter the first table in WHERE. |
Slow writes#
| Cause | Fix |
|---|---|
| One statement per row in autocommit | Group rows in INSERT ... VALUES (...), (...) or one transaction (the front-end waits for the disk once, at COMMIT). |
| Unique indexes and foreign keys | Rows with unique indexes or foreign keys are written one by one, each with its claims and checks. Keep only the indexes you need. |
| A slow disk | Every write waits for an fsync. Check the disk's flush latency. A drive that does not flush honestly is fast and unsafe. |
sync-replicas without a healthy replica | Each write waits sync-timeout-ms (Replication problems). |
| A checkpoint or compaction running | A checkpoint on demand stops the table. On lsm, level-0 files over lsm-l0-slowdown delay writes and over lsm-l0-stop stop them. Raise compaction capacity, or in a managed cluster use the watcher's resource control. |
| Too few shards | Writers of different keys use different shards; shards defaults to the cores, at most 4. |
High CPU use with few clients#
spin_us: a busy connection looks for its next message for up to 150 microseconds before it sleeps. On a shared or CPU-starved machine setspin_us = 0(bothraftdb-serverandraftdb-mysql). The spin is off for idle connections and when more than a quarter of the processors' worth of connections are busy.- The watcher's checks are small; check that
interval_secondsis not set very low. - The first minutes after a start: replaying logs and rebuilding indexes.
Memory#
- The
memoryengine keeps every document in RAM.GET /v1/statsshows rows per database; compare with the size of the process. A table that is overwritten a lot grows its log (disk), not its memory. alloc_cache(on by default) keeps freed blocks per thread: memory may look higher than the live data.alloc_cache = offreturns blocks to the system.- The
lsmengine:lsm-memtable-mbandlsm-block-cache-mbper shard bound its memory. - Each connection has two threads and buffers.
Slow start-up#
Replay of the logs (about 3.2 million records a second) takes time on a big history. Let the start-time rewrite shrink a mostly dead log (checkpoint-min-mb), or use engine = lsm for tables you expect to be large.
Disk growth#
The memory engine's logs grow until a checkpoint (at start, or on demand with POST .../checkpoint). Free space is watched by the watcher's disk check.
Compare with the numbers of the project#
The performance chapter lists the project's measurements and the machines they came from; compare like with like, and expect your hardware, disk and workload to differ. If your numbers are far below them on comparable hardware, report it with the statement and RAFTDB_MYSQL_PROF=1 output.