{"record":{"id":"70dd4c8bfdf770d9","repo":"dgraph-io/badger","slug":"blockcachesize-should-be-set-since-compression-enc","errorCode":null,"errorMessage":"BlockCacheSize should be set since compression/encryption are enabled","messagePattern":"BlockCacheSize should be set since compression/encryption are enabled","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"db.go","lineNumber":189,"sourceCode":"\tif opt.ValueThreshold > opt.maxBatchSize {\n\t\treturn fmt.Errorf(\"Valuethreshold %d greater than max batch size of %d. Either \"+\n\t\t\t\"reduce opt.ValueThreshold or increase opt.BaseTableSize.\",\n\t\t\topt.ValueThreshold, opt.maxBatchSize)\n\t}\n\t// ValueLogFileSize should be strictly LESS than 2<<30 otherwise we will\n\t// overflow the uint32 when we mmap it in OpenMemtable.\n\tif !(opt.ValueLogFileSize < 2<<30 && opt.ValueLogFileSize >= 1<<20) {\n\t\treturn ErrValueLogSize\n\t}\n\n\tif opt.ReadOnly {\n\t\t// Do not perform compaction in read only mode.\n\t\topt.CompactL0OnClose = false\n\t}\n\n\tneedCache := (opt.Compression != options.None) || (len(opt.EncryptionKey) > 0)\n\tif needCache && opt.BlockCacheSize == 0 {\n\t\tpanic(\"BlockCacheSize should be set since compression/encryption are enabled\")\n\t}\n\treturn nil\n}\n\n// Open returns a new DB object.\nfunc Open(opt Options) (*DB, error) {\n\tif err := checkAndSetOptions(&opt); err != nil {\n\t\treturn nil, err\n\t}\n\tvar dirLockGuard, valueDirLockGuard *directoryLockGuard\n\n\t// Create directories and acquire lock on it only if badger is not running in InMemory mode.\n\t// We don't have any directories/files in InMemory mode so we don't need to acquire\n\t// any locks on them.\n\tif !opt.InMemory {\n\t\tif err := createDirs(opt); err != nil {\n\t\t\treturn nil, err\n\t\t}","sourceCodeStart":171,"sourceCodeEnd":207,"githubUrl":"https://github.com/dgraph-io/badger/blob/2a001d466f6b71a917319a1db41f99860e16e269/db.go#L171-L207","documentation":"When compression (opt.Compression != None) or encryption (opt.EncryptionKey set) is enabled, badger requires a block cache to buffer decompression/decryption work. checkAndSetOptions panics if BlockCacheSize is left at 0 with those features on.","triggerScenarios":"Calling badger.Open with opt.Compression set (e.g. options.ZSTD) or opt.EncryptionKey non-empty while leaving opt.BlockCacheSize == 0.","commonSituations":"Turning on ZSTD compression for size savings without configuring caching; enabling encryption-at-rest with default options.","solutions":["Set opt.BlockCacheSize to a nonzero value (e.g. 256MB or ~10% of RAM)","Or disable compression and encryption if caching is unwanted"],"exampleFix":"// before\nopt.Compression = options.ZSTD\n// BlockCacheSize left 0\n// after\nopt.Compression = options.ZSTD\nopt.BlockCacheSize = 256 << 20","handlingStrategy":"validation","validationCode":"if (opt.Compression != options.None || len(opt.EncryptionKey) > 0) && opt.BlockCacheSize == 0 {\n    opt.BlockCacheSize = 256 << 20\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always set BlockCacheSize when enabling compression/encryption","Centralize badger option building in one function","Recover() around Open in startup code to surface config panics clearly"],"tags":["badger","panic","configuration"],"backgroundTag":"invalid-enum-value","analyzedSha":"2a001d466f6b71a917319a1db41f99860e16e269","analyzedAt":"2026-09-05T13:00:02.264Z","contentChangedAt":"2026-09-05T13:00:02.264Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}