Skip to content

feat(db): opt-in BadgerDB memory tuning (BADGER_PROFILE=low-memory) - #137

Open
rafaelvieiras wants to merge 2 commits into
barrydeen:masterfrom
rafaelvieiras:perf/low-memory-badger
Open

rafaelvieiras wants to merge 2 commits into
barrydeen:masterfrom
rafaelvieiras:perf/low-memory-badger

Conversation

@rafaelvieiras

Copy link
Copy Markdown

Summary

Haven opens five independent BadgerDB instances (private, chat, outbox, inbox, blossom) and uses badger.DefaultOptions for each of them. Those defaults are sized for a database server (64 MB memtables x 5, 256 MB block cache, 4 compactors, 1 GB value log files), so on small hardware an idle relay reserves far more memory than its databases need. BadgerBackend already exposes BadgerOptionsModifier, but Haven never used it and there is no way to tune this.

This PR adds an opt-in way to tune it:

  • BADGER_PROFILE=low-memory: 8 MB x 2 memtables, 16 MB block cache, 8 MB index cache, 2 compactors, 64 MB value log files.
  • Per-option overrides applied on top of the profile (0 keeps the profile/BadgerDB default): BADGER_MEMTABLE_SIZE_MB, BADGER_NUM_MEMTABLES, BADGER_BLOCK_CACHE_MB, BADGER_INDEX_CACHE_MB, BADGER_NUM_COMPACTORS, BADGER_VALUE_LOG_FILE_SIZE_MB, BADGER_NUM_LEVEL_ZERO_TABLES.
  • Default behaviour is unchanged: without any BADGER_* variable no modifier is installed.
  • Guards against combinations BadgerDB refuses to open (value threshold vs. max batch size, L0 stall vs. L0 tables, a single compactor).
  • README section and .env.example entries.

Measurements

Same binary, same copy of a real database (~21 MB on disk), same environment (WoT depth 3, real import relays), two containers side by side for 12 minutes, DB_ENGINE=badger:

RSS (steady) Peak RSS
default 270-320 MB 322 MB
BADGER_PROFILE=low-memory 52-60 MB 128 MB

Both logged identical WoT results (16152 pubkeys, 3627 kept) and no errors. The default instance also started pushing pages to swap over the run; the low-memory one did not.

Tests

  • Unit tests for profile resolution, override precedence, invalid input, and a test that opens a real Badger DB with the low-memory options.
  • go test ./... passes and golangci-lint is clean for the new files.

Side note: the package tests need the OWNER_NPUB/relay variables exported in my environment, because a_testenv_test.go gets initialised after config.go (test files are appended after the regular ones). Unrelated to this change; mentioning it in case it is useful.

Haven opens five BadgerDB instances with BadgerDB's server-sized defaults
(64 MB memtables, 256 MB block cache, 4 compactors), which is far more than
a small relay needs. Add BADGER_PROFILE=low-memory plus per-option overrides
via BadgerOptionsModifier. Defaults are unchanged.
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.

1 participant