Skip to content

Tune RocksDB compaction for desktop app — WAL bloat causes 10x oversized database - #994

Merged
malibio merged 4 commits into
mainfrom
feature/issue-992-rocksdb-compaction-tuning
Mar 22, 2026
Merged

malibio merged 4 commits into
mainfrom
feature/issue-992-rocksdb-compaction-tuning

Conversation

@malibio

@malibio malibio commented Mar 22, 2026

Copy link
Copy Markdown
Collaborator

Closes #992

malibio and others added 2 commits March 22, 2026 03:20
…rs (#992)

SurrealDB's default RocksDB settings are tuned for server workloads with
large write buffers and lazy compaction. On a desktop app, a 210-doc
import produces ~147 MiB of WAL logs with zero SST files — 10x the
expected post-compaction size.

Set desktop-appropriate SURREAL_ROCKSDB_* env vars before SurrealDB
opens the store:

- WAL size limit: 64 MiB (forces earlier flushing)
- Write buffer: 8 MiB (down from 32-128 MiB default)
- Max write buffers: 3 (bounded memory)
- Min buffers to merge: 1 (flush immediately)
- Target file size: 16 MiB (smaller SSTs for desktop datasets)
- L0 compaction trigger: 2 (compact sooner)
- Background flush interval: 100ms (more frequent WAL→SST)
- Log file retention: 5 (bounded info logs)

Env vars are only set when not already present, so users can override
any value. Uses std::sync::Once to apply exactly once per process.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Add a comment to graceful_shutdown() explaining that SurrealDB 3.0 has
no close() or flush() API for the embedded RocksDB backend, with a
pointer to upstream issue surrealdb/surrealdb#2399. Committed WAL data
is safe on normal shutdown via OS page cache flush on drop.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@malibio

malibio commented Mar 22, 2026

Copy link
Copy Markdown
Collaborator Author

Pragmatic Code Review -- PR #994

Reviewer: Claude Opus 4.6 (Principal Engineer)
PR: Tune RocksDB compaction for desktop app -- WAL bloat causes 10x oversized database
Issue: #992


Overall Assessment

This is a well-motivated, targeted fix for a real problem (147 MiB WAL for 210 docs). The approach of using SurrealDB's documented SURREAL_ROCKSDB_* environment variables is the correct lever -- it avoids forking or patching SurrealDB and keeps configuration overridable. The code is clean, well-documented, and atomic in purpose.

Verdict: Approve with one blocker to fix.


Findings

Critical Issues

[Critical/Blocker] SURREAL_ROCKSDB_BACKGROUND_FLUSH_INTERVAL does not exist in SurrealDB 3.0.x

packages/core/src/db/surreal_store.rs line 445:

("SURREAL_ROCKSDB_BACKGROUND_FLUSH_INTERVAL", "100"),

I verified the complete list of SURREAL_ROCKSDB_* environment variables in surrealdb-core-3.0.4/src/kvs/rocksdb/cnf.rs. There is no SURREAL_ROCKSDB_BACKGROUND_FLUSH_INTERVAL variable defined anywhere in SurrealDB 3.0.3 or 3.0.4. This env var is silently ignored at runtime.

The background WAL flush interval in SurrealDB is controlled via the sync query parameter on the connection URL (e.g., rocksdb:///path?sync=200ms) or the SURREAL_DATASTORE_SYNC_DATA env var -- not a RocksDB-specific env var. See surrealdb-core-3.0.4/src/kvs/config.rs and src/kvs/rocksdb/background_flusher.rs.

Action required: Remove SURREAL_ROCKSDB_BACKGROUND_FLUSH_INTERVAL from the defaults array and its corresponding doc-comment bullet point. Optionally, if you want periodic background flushing, either append ?sync=200ms to the RocksDB connection URL or set SURREAL_DATASTORE_SYNC_DATA=200ms. However, note that the default sync mode for RocksDB in SurrealDB is SyncMode::Every (sync on every commit), which is already the most durable option. Switching to interval-based flushing would trade durability for write speed -- a valid tradeoff for a desktop app, but one that should be made deliberately, not accidentally via a phantom env var.

Suggested Improvements

[Improvement] env::set_var is unsafe in Rust 2024 edition

packages/core/src/db/surreal_store.rs line 452:

env::set_var(key, value);

This compiles fine under edition 2021 (current), but std::env::set_var was made unsafe in Rust 2024 edition because it mutates global process state and is not thread-safe when other threads are reading env vars concurrently. The std::sync::Once guard prevents repeated calls but does not prevent other threads from reading env vars mid-set.

Since this code runs early in SurrealStore::new() before connect() spawns any SurrealDB threads, the practical risk is very low today. However, when the project upgrades to edition 2024, this will become a compile error.

No immediate action needed -- just noting for awareness. When upgrading editions, you will need an unsafe block with a safety comment, or consider using std::sync::LazyLock to set all vars atomically before any thread creation.

[Improvement] MIN_WRITE_BUFFER_NUMBER_TO_MERGE = 1 is very aggressive

Setting this to 1 (down from default 2) means every single memtable is flushed individually rather than being merged with another memtable first. This maximizes WAL-to-SST conversion speed but produces more L0 files and more compaction work. Combined with the L0 compaction trigger of 2, this could cause write stalls during sustained bulk imports because the compaction pipeline may not keep up with the flush rate.

For 210-doc imports this is likely fine, but for the stated goal of supporting 2K+ docs, consider keeping this at the default of 2 or testing with a larger import to verify no write stalls occur.

Nitpicks

Nit: packages/core/src/db/surreal_store.rs line 414 -- The doc-comment says "WAL size limit 64 MiB" which is correct (the SurrealDB source confirms this env var is in MB, passed to set_wal_size_limit_mb). Good accuracy.

Nit: packages/desktop-app/src-tauri/src/lib.rs line 434 -- The upstream issue reference surrealdb/surrealdb#2399 is a good breadcrumb for future maintainers. Consider verifying the issue number is correct (it is a real tracking issue) so the link remains useful.

Nit: packages/core/src/db/surreal_store.rs -- The configure_rocksdb_defaults method is placed after new_http() which does not call it. This is fine for embedded-only tuning, but a brief comment in new_http() noting "RocksDB tuning is not applied for HTTP connections (server manages its own RocksDB config)" would prevent future confusion about why the two constructors differ.


Verified Environment Variables

Cross-referenced against surrealdb-core-3.0.4/src/kvs/rocksdb/cnf.rs:

Env Var Exists in SurrealDB Value Units Correct
SURREAL_ROCKSDB_WAL_SIZE_LIMIT Yes (line 31) "64" MB Yes
SURREAL_ROCKSDB_WRITE_BUFFER_SIZE Yes (line 166) "8388608" bytes Yes
SURREAL_ROCKSDB_MAX_WRITE_BUFFER_NUMBER Yes (line 189) "3" count Yes
SURREAL_ROCKSDB_MIN_WRITE_BUFFER_NUMBER_TO_MERGE Yes (line 214) "1" count Yes
SURREAL_ROCKSDB_TARGET_FILE_SIZE_BASE Yes (line 35) "16777216" bytes Yes
SURREAL_ROCKSDB_FILE_COMPACTION_TRIGGER Yes (line 43) "2" count Yes
SURREAL_ROCKSDB_BACKGROUND_FLUSH_INTERVAL NO "100" N/A INVALID
SURREAL_ROCKSDB_KEEP_LOG_FILE_NUM Yes (line 78) "5" count Yes

Summary

7 of 8 env vars are correctly named and valued. One (BACKGROUND_FLUSH_INTERVAL) is a phantom that does nothing. Remove it, and this is a clean, well-reasoned performance fix that directly addresses the stated issue. The documentation in both the code comments and the graceful shutdown note is excellent.

@malibio malibio left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One blocker: SURREAL_ROCKSDB_BACKGROUND_FLUSH_INTERVAL does not exist in SurrealDB 3.0.x (verified against surrealdb-core-3.0.4/src/kvs/rocksdb/cnf.rs). It is silently ignored at runtime. Remove it from the defaults array and its doc-comment bullet. If periodic WAL flushing is desired, use the sync query parameter on the connection URL or the SURREAL_DATASTORE_SYNC_DATA env var instead. See full review in PR comment.

…992)

- Remove SURREAL_ROCKSDB_BACKGROUND_FLUSH_INTERVAL: does not exist in
  SurrealDB 3.0.x (flush interval is controlled via the connection URL
  sync parameter, not an env var). Was silently ignored at runtime.
- Change MIN_WRITE_BUFFER_NUMBER_TO_MERGE from 1 back to 2: merging
  pairs before flushing reduces L0 file count and avoids potential
  write stalls during sustained bulk imports.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@malibio

malibio commented Mar 22, 2026

Copy link
Copy Markdown
Collaborator Author

Review Feedback Addressed

✅ Addressed (3)

  • 🔴 Critical: Removed phantom SURREAL_ROCKSDB_BACKGROUND_FLUSH_INTERVAL — confirmed it doesn't exist in SurrealDB 3.0.x kvs/rocksdb/cnf.rs
  • 🟡 Important: Changed MIN_WRITE_BUFFER_NUMBER_TO_MERGE from 1 → 2 to avoid excessive L0 files during bulk imports
  • 🟢 Suggestion: Verified surrealdb/surrealdb#2399 link — confirmed open and correctly titled "Graceful shutdown support when using embedded mode"

⏭️ Skipped (2)

  • 🟢 Comment in new_http(): HTTP mode doesn't read env vars by nature; adding a comment would be noise
  • 🟢 Rust 2024 unsafe env::set_var: Not actionable until edition upgrade; noted for future

Commit: 4a502de

…992)

After initial tuning, 210-doc import still produced 105 MiB (vs 147 MiB
before). Two culprits:

- BlobDB (.blob files): ~31 MiB of overhead from separating large values
  into separate files. Disabled — inline SST storage compacts better at
  desktop scale (~200-10K docs).

- Target file size multiplier: default of 2 meant L1=32 MiB, L2=64 MiB.
  Set to 1 for uniform 16 MiB files across all levels, producing tighter
  compaction for small datasets.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@malibio
malibio merged commit 10dd180 into main Mar 22, 2026
@malibio
malibio deleted the feature/issue-992-rocksdb-compaction-tuning branch March 22, 2026 02:58
malibio added a commit that referenced this pull request Jun 22, 2026
…zed database (#994)

* fix: tune RocksDB compaction for desktop — cap WAL size, shrink buffers (#992)

SurrealDB's default RocksDB settings are tuned for server workloads with
large write buffers and lazy compaction. On a desktop app, a 210-doc
import produces ~147 MiB of WAL logs with zero SST files — 10x the
expected post-compaction size.

Set desktop-appropriate SURREAL_ROCKSDB_* env vars before SurrealDB
opens the store:

- WAL size limit: 64 MiB (forces earlier flushing)
- Write buffer: 8 MiB (down from 32-128 MiB default)
- Max write buffers: 3 (bounded memory)
- Min buffers to merge: 1 (flush immediately)
- Target file size: 16 MiB (smaller SSTs for desktop datasets)
- L0 compaction trigger: 2 (compact sooner)
- Background flush interval: 100ms (more frequent WAL→SST)
- Log file retention: 5 (bounded info logs)

Env vars are only set when not already present, so users can override
any value. Uses std::sync::Once to apply exactly once per process.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* docs: note SurrealDB lacks explicit close/flush for RocksDB (#992)

Add a comment to graceful_shutdown() explaining that SurrealDB 3.0 has
no close() or flush() API for the embedded RocksDB backend, with a
pointer to upstream issue surrealdb/surrealdb#2399. Committed WAL data
is safe on normal shutdown via OS page cache flush on drop.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* fix: address review — remove phantom env var, safer merge threshold (#992)

- Remove SURREAL_ROCKSDB_BACKGROUND_FLUSH_INTERVAL: does not exist in
  SurrealDB 3.0.x (flush interval is controlled via the connection URL
  sync parameter, not an env var). Was silently ignored at runtime.
- Change MIN_WRITE_BUFFER_NUMBER_TO_MERGE from 1 back to 2: merging
  pairs before flushing reduces L0 file count and avoids potential
  write stalls during sustained bulk imports.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* fix: disable blob files and flatten compaction levels for smaller DB (#992)

After initial tuning, 210-doc import still produced 105 MiB (vs 147 MiB
before). Two culprits:

- BlobDB (.blob files): ~31 MiB of overhead from separating large values
  into separate files. Disabled — inline SST storage compacts better at
  desktop scale (~200-10K docs).

- Target file size multiplier: default of 2 meant L1=32 MiB, L2=64 MiB.
  Set to 1 for uniform 16 MiB files across all levels, producing tighter
  compaction for small datasets.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tune RocksDB compaction for desktop app — WAL bloat causes 10x oversized database

1 participant