perf: S3 snapshot uploads block proactor thread, causing replication lag spikes
Environment
- Dragonfly v1.35.0, Kubernetes with dragonfly-operator v1.3.1
- DFS snapshots to S3 every 5 minutes,
--proactor_threads=4 - 3-pod StatefulSet (1 primary, 2 replicas) per namespace
- Worst-affected instance: ~1.9 GiB dataset, ~1,700 ops/s, ~180 mutations/s avg
Problem
During S3 snapshots, replication lag spikes heavily. Our worst namespace shows 7d avg lag of 69 records, p99 of 659, max of 897. Lag spikes correlate almost exactly with the 5-minute snapshot cron. Snapshots take ~12 seconds, during which heartbeat fiber stalls are logged (~132/day):
W engine_shard.cc:618] Stalled heartbeat() fiber for 1 secondsbackground_snapshotting=true doesn't help because the bottleneck is S3 IO latency, not CPU scheduling.
Root cause (from code analysis on main)
In DFS mode (SaveMode::SINGLE_SHARD), no channel is created (rdb_save.cc:1201 — channel is only created for SaveMode::RDB). So ConsumeData writes directly to the S3 sink on the shard's proactor thread:
SliceSnapshot::HandleFlushData (snapshot.cc:439)
→ RdbSaver::Impl::ConsumeData — no channel, hits else branch
→ WriteRecord → sink_->Write (rdb_save.cc:1274)
→ S3WriteFile::WriteSome (s3_write_file.cc:37)
→ S3WriteFile::Flush (s3_write_file.cc:168)
→ client_->UploadPart(request) ← synchronous AWS SDK callUploadPart is a blocking HTTP PUT (~50-200ms per 8MB part). For a 1.9 GiB dataset, that's ~240 sequential blocking calls on the proactor thread. Replication fibers on the same thread are starved for the entire duration.
Possible solution direction
Wrap the S3 sink in an async io::Sink adapter that queues data and drains it from a fiber on a different proactor thread. This would decouple S3 IO from the shard's proactor with no changes to the snapshot or serialization pipeline. A bounded queue provides back-pressure.
Workarounds we've applied
- Increased
proactor_threadsfrom 2 to 4 (helps, but doesn't eliminate the issue) - Reducing snapshot frequency (trades off durability)
Related
- #5625 — Snapshot causes dragonfly freezing
- #3941 — S3 snapshot loading might block
- #6389 — Dragonfly goes offline if there is an issue with S3
Source: dragonflydb/dragonfly