@@ -5,14 +5,85 @@ use std::path::PathBuf;
55
66use crate :: rocksdb:: column:: { Column , ColumnMemoryBudget } ;
77use anyhow:: { Context , Result } ;
8- use rocksdb:: { DBCompressionType , Env , Options , SliceTransform } ;
8+ use rocksdb:: { DBCompressionType , Env , Options , SliceTransform , WriteOptions } ;
9+ use serde:: { Deserialize , Serialize } ;
910
1011const KiB : usize = 1024 ;
1112const MiB : usize = 1024 * KiB ;
1213const GiB : usize = 1024 * MiB ;
1314
1415pub use rocksdb:: statistics:: StatsLevel ;
1516
17+ /// RocksDB write durability configuration. Controls WAL (Write-Ahead Log) and fsync behavior.
18+ ///
19+ /// # Performance & Safety Trade-offs
20+ ///
21+ /// | WAL | Fsync | Performance | Safety | Use Case |
22+ /// |---------|---------|-------------|---------|---------------------------------------|
23+ /// | Enabled | Enabled | Slowest | Highest | Production critical data |
24+ /// | Enabled | Disable | Medium | High | Production (safe on crash) - **RECOMMENDED** |
25+ /// | Disable | Enabled | Fast | Medium | Testing, can tolerate data loss |
26+ /// | Disable | Disable | Fastest | Lowest | Devnet, testing |
27+ ///
28+ /// # Safety Considerations
29+ ///
30+ /// - **WAL enabled**: Write-Ahead Log records changes before applying them. Enables crash recovery.
31+ /// Disabling WAL is faster but may lose recent uncommitted data on crash.
32+ ///
33+ /// - **Fsync enabled**: Forces data to be flushed to disk before acknowledging writes.
34+ /// Survives power failures but slower. Disabling fsync relies on OS buffering (faster, survives crashes but not power loss).
35+ ///
36+ /// **Recommended settings:**
37+ /// - Production: `wal=true, fsync=false` (safe on crash, good performance)
38+ /// - Maximum durability: `wal=true, fsync=true` (survives power failures)
39+ /// - Testing/Development: `wal=false, fsync=false` (fastest)
40+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Serialize , Deserialize ) ]
41+ pub struct DbWriteMode {
42+ /// Enable Write-Ahead Log (WAL). Provides crash recovery but adds overhead.
43+ /// Default: true (recommended for production)
44+ pub wal : bool ,
45+ /// Enable fsync after writes. Ensures data reaches disk before acknowledging.
46+ /// Default: false (recommended for production - survives crashes, faster than fsync)
47+ pub fsync : bool ,
48+ }
49+
50+ impl Default for DbWriteMode {
51+ fn default ( ) -> Self {
52+ Self {
53+ wal : true ,
54+ fsync : false ,
55+ }
56+ }
57+ }
58+
59+ impl DbWriteMode {
60+ /// Convert the write mode to RocksDB WriteOptions
61+ pub fn to_write_options ( & self ) -> WriteOptions {
62+ let mut opts = WriteOptions :: default ( ) ;
63+
64+ if !self . wal {
65+ opts. disable_wal ( true ) ;
66+ }
67+
68+ if !self . fsync {
69+ opts. set_sync ( false ) ;
70+ }
71+
72+ opts
73+ }
74+ }
75+
76+ impl std:: fmt:: Display for DbWriteMode {
77+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
78+ match ( self . wal , self . fsync ) {
79+ ( true , true ) => write ! ( f, "WAL enabled, fsync enabled (safest)" ) ,
80+ ( true , false ) => write ! ( f, "WAL enabled, fsync disabled (recommended)" ) ,
81+ ( false , true ) => write ! ( f, "WAL disabled, fsync enabled (fast)" ) ,
82+ ( false , false ) => write ! ( f, "WAL disabled, fsync disabled (fastest, least safe)" ) ,
83+ }
84+ }
85+ }
86+
1687#[ derive( Debug , Clone ) ]
1788pub struct RocksDBConfig {
1889 /// Enable statistics. Statistics will be put in the `LOG` file in the db folder. This can have an effect on performance.
@@ -41,6 +112,9 @@ pub struct RocksDBConfig {
41112 pub backup_dir : Option < PathBuf > ,
42113 /// When true, the latest backup will be restored on startup.
43114 pub restore_from_latest_backup : bool ,
115+
116+ /// Write durability mode (WAL and fsync settings)
117+ pub write_mode : DbWriteMode ,
44118}
45119
46120impl Default for RocksDBConfig {
@@ -59,6 +133,7 @@ impl Default for RocksDBConfig {
59133 snapshot_interval : 5 ,
60134 backup_dir : None ,
61135 restore_from_latest_backup : false ,
136+ write_mode : DbWriteMode :: default ( ) ,
62137 }
63138 }
64139}
0 commit comments