Skip to content

Draft: Forkless Snapshot (Threadsave)#3648

Draft
nitaicaro wants to merge 4 commits into
valkey-io:forkless-pre-threadsavefrom
nitaicaro:forkless-snapshot
Draft

Draft: Forkless Snapshot (Threadsave)#3648
nitaicaro wants to merge 4 commits into
valkey-io:forkless-pre-threadsavefrom
nitaicaro:forkless-snapshot

Conversation

@nitaicaro
Copy link
Copy Markdown
Contributor

@nitaicaro nitaicaro commented May 8, 2026

Draft: Thread-based RDB snapshot (Threadsave)

Motivation

Fork-based BGSAVE relies on fork(), which on large datasets causes significant memory overhead from copy-on-write page duplication. On systems where memory is constrained, fork can fail entirely. Threadsave provides an alternative that avoids fork by performing the RDB snapshot on a background thread while the main thread continues serving clients.

How it works

Threadsave builds on the bgIteration framework (introduced in #3553). bgIteration walks the keyspace on the main thread, delivering keys to a FIFO queue. A background thread consumes entries from the queue and serializes them to an RDB file using the standard rdbSaveKeyValuePair encoding. The result is a normal RDB file - loadable by any Valkey instance.

Usage

A thread-based snapshot can be triggered in three ways:

  • BGSAVE THREAD - explicit command (requires forkless-options-supported yes)
  • config set threadsave-enabled-for-backup yes - makes plain BGSAVE use threadsave
  • Automatic periodic saves will use threadsave when threadsave-enabled-for-backup is enabled

BGSAVE FORK explicitly forces a fork-based snapshot regardless of configuration. BGSAVE SCHEDULE THREAD / BGSAVE SCHEDULE FORK schedule a snapshot with explicit type.

Draft status

This PR is marked as draft because it includes three cherry-picked commits (the pending_command lifecycle fix, the findDbForEntry NULL check, and the hashtable bucket-chain iteration fix) that belong in the underlying bgIteration and blocked-inuse branches. Once those patches are merged into their respective base branches, this PR will be rebased and those commits will disappear from the diff. The threadsave-specific changes should mostly remain the same.

Nitai Caro and others added 2 commits May 8, 2026 17:31
@codecov
Copy link
Copy Markdown

codecov Bot commented May 8, 2026

Codecov Report

❌ Patch coverage is 83.68421% with 62 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.98%. Comparing base (281d627) to head (fa3301a).

Files with missing lines Patch % Lines
src/threadsave.c 82.81% 33 Missing ⚠️
src/rdb.c 88.59% 13 Missing ⚠️
src/server.c 78.18% 12 Missing ⚠️
src/module.c 0.00% 4 Missing ⚠️
Additional details and impacted files
@@                     Coverage Diff                     @@
##           forkless-pre-threadsave    #3648      +/-   ##
===========================================================
+ Coverage                    76.76%   76.98%   +0.22%     
===========================================================
  Files                          161      162       +1     
  Lines                        81423    81712     +289     
===========================================================
+ Hits                         62502    62906     +404     
+ Misses                       18921    18806     -115     
Files with missing lines Coverage Δ
src/bgiteration.c 82.24% <100.00%> (+12.77%) ⬆️
src/blocked.c 92.27% <ø> (+0.21%) ⬆️
src/commands.def 100.00% <ø> (ø)
src/config.c 78.33% <ø> (ø)
src/db.c 94.61% <100.00%> (+0.29%) ⬆️
src/hashtable.c 97.86% <100.00%> (ø)
src/networking.c 92.23% <100.00%> (+0.09%) ⬆️
src/rdb.h 100.00% <ø> (ø)
src/replication.c 86.17% <100.00%> (-0.03%) ⬇️
src/server.h 100.00% <ø> (ø)
... and 4 more

... and 18 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread src/threadsave.c
Comment thread src/threadsave.c
Comment thread src/threadsave.c
Comment thread src/threadsave.c Outdated
Comment thread src/threadsave.c
Comment thread src/threadsave.c Outdated
Comment thread src/threadsave.c Outdated
Comment thread src/threadsave.c Outdated
Comment thread src/threadsave.c
Comment thread src/threadsave.c
Comment thread src/module.c
Comment thread src/server.c Outdated
Comment thread src/rdb.c Outdated
Comment thread src/rdb.c
Comment thread src/rdb.c
@nitaicaro nitaicaro force-pushed the forkless-snapshot branch from a00a573 to 5482b96 Compare May 14, 2026 18:48
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 14, 2026

Important

Review skipped

Ignore keyword(s) in the title.

⛔ Ignored keywords (1)
  • draft

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 8831f7e4-6ed2-4266-a282-ff8a22daf00f

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Comment @coderabbitai help to get the list of available commands and usage tips.

@nitaicaro nitaicaro force-pushed the forkless-snapshot branch 2 times, most recently from 1588575 to 79b3cc3 Compare May 14, 2026 21:17
@nitaicaro nitaicaro requested a review from murphyjacob4 May 14, 2026 22:32
@nitaicaro nitaicaro force-pushed the forkless-snapshot branch 6 times, most recently from c4090e9 to cca0dfe Compare May 15, 2026 20:51
@nitaicaro nitaicaro force-pushed the forkless-snapshot branch from cca0dfe to fa3301a Compare May 19, 2026 18:49
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.

2 participants