dgraph-io/badger · error

BlockCacheSize should be set since compression/encryption ar

Error message

BlockCacheSize should be set since compression/encryption are enabled

What it means

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.

Source

Thrown at db.go:189

	if opt.ValueThreshold > opt.maxBatchSize {
		return fmt.Errorf("Valuethreshold %d greater than max batch size of %d. Either "+
			"reduce opt.ValueThreshold or increase opt.BaseTableSize.",
			opt.ValueThreshold, opt.maxBatchSize)
	}
	// ValueLogFileSize should be strictly LESS than 2<<30 otherwise we will
	// overflow the uint32 when we mmap it in OpenMemtable.
	if !(opt.ValueLogFileSize < 2<<30 && opt.ValueLogFileSize >= 1<<20) {
		return ErrValueLogSize
	}

	if opt.ReadOnly {
		// Do not perform compaction in read only mode.
		opt.CompactL0OnClose = false
	}

	needCache := (opt.Compression != options.None) || (len(opt.EncryptionKey) > 0)
	if needCache && opt.BlockCacheSize == 0 {
		panic("BlockCacheSize should be set since compression/encryption are enabled")
	}
	return nil
}

// Open returns a new DB object.
func Open(opt Options) (*DB, error) {
	if err := checkAndSetOptions(&opt); err != nil {
		return nil, err
	}
	var dirLockGuard, valueDirLockGuard *directoryLockGuard

	// Create directories and acquire lock on it only if badger is not running in InMemory mode.
	// We don't have any directories/files in InMemory mode so we don't need to acquire
	// any locks on them.
	if !opt.InMemory {
		if err := createDirs(opt); err != nil {
			return nil, err
		}

View on GitHub (pinned to 2a001d466f)

Solutions

  1. Set opt.BlockCacheSize to a nonzero value (e.g. 256MB or ~10% of RAM)
  2. Or disable compression and encryption if caching is unwanted

Example fix

// before
opt.Compression = options.ZSTD
// BlockCacheSize left 0
// after
opt.Compression = options.ZSTD
opt.BlockCacheSize = 256 << 20
Defensive patterns

Strategy: validation

Validate before calling

if (opt.Compression != options.None || len(opt.EncryptionKey) > 0) && opt.BlockCacheSize == 0 {
    opt.BlockCacheSize = 256 << 20
}

Prevention

When it happens

Trigger: Calling badger.Open with opt.Compression set (e.g. options.ZSTD) or opt.EncryptionKey non-empty while leaving opt.BlockCacheSize == 0.

Common situations: Turning on ZSTD compression for size savings without configuring caching; enabling encryption-at-rest with default options.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of dgraph-io/badger@2a001d466f (2026-09-05). Data as JSON: /api/errors/70dd4c8bfdf770d9. Report an issue: GitHub.