{"record":{"id":"f7b42bba9053962f","repo":"gofiber/fiber","slug":"cache-insufficient-space-and-no-entries-to-evict","errorCode":null,"errorMessage":"cache: insufficient space and no entries to evict","messagePattern":"cache: insufficient space and no entries to evict","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"middleware/cache/cache.go","lineNumber":724,"sourceCode":"\t\tif cfg.MaxBytes > 0 {\n\t\t\tmux.Lock()\n\t\t\t// Reserve space for the new entry first\n\t\t\tstoredBytes += bodySize\n\t\t\tspaceReserved = true\n\n\t\t\t// Now evict entries until we're under the limit\n\t\t\tvar keysToRemove []string\n\t\t\tvar sizesToRemove []uint\n\t\t\tvar candidates []evictionCandidate\n\n\t\t\tfor storedBytes > cfg.MaxBytes {\n\t\t\t\tif heap.Len() == 0 {\n\t\t\t\t\t// Can't evict more, unreserve space and fail\n\t\t\t\t\tstoredBytes -= bodySize\n\t\t\t\t\t// Set spaceReserved to false so the deferred cleanup does not unreserve again\n\t\t\t\t\tspaceReserved = false\n\t\t\t\t\tmux.Unlock()\n\t\t\t\t\treturn errors.New(\"cache: insufficient space and no entries to evict\")\n\t\t\t\t}\n\t\t\t\tnext := heap.entries[0]\n\t\t\t\tkeyToRemove, size := heap.removeFirst()\n\t\t\t\tkeysToRemove = append(keysToRemove, keyToRemove)\n\t\t\t\tsizesToRemove = append(sizesToRemove, size)\n\t\t\t\tcandidates = append(candidates, evictionCandidate{\n\t\t\t\t\tkey:  keyToRemove,\n\t\t\t\t\tsize: size,\n\t\t\t\t\texp:  next.exp,\n\t\t\t\t})\n\t\t\t\tstoredBytes -= size\n\t\t\t}\n\t\t\tmux.Unlock()\n\n\t\t\t// Perform deletions outside the lock\n\t\t\tif len(keysToRemove) > 0 {\n\t\t\t\tfor i, keyToRemove := range keysToRemove {\n\t\t\t\t\tdelErr := deleteKey(reqCtx, keyToRemove)","sourceCodeStart":706,"sourceCodeEnd":742,"githubUrl":"https://github.com/gofiber/fiber/blob/a105acad6c1e4576a77f01e02973f67e962bb58d/middleware/cache/cache.go#L706-L742","documentation":"Returned by middleware/cache when, under a MaxBytes budget, the cache has reserved space for a new response body but the eviction heap is empty and the body still does not fit. The middleware reserves bytes up front, evicts LRU entries to get under the limit, and if it runs out of evictable entries before fitting the new body it un-reserves and returns this inline error. It is a fresh errors.New value, not a package-level sentinel.","triggerScenarios":"Configuring cache with MaxBytes set small, then caching a response whose body is individually smaller than MaxBytes (so the early bail-out at the top is skipped) but larger than the free space left after the heap is drained. Concurrent inserts can drain the heap between the size pre-check and the eviction loop.","commonSituations":"Tight MaxBytes on a high-churn cache; a sudden large response after the cache filled with many small entries that were all just evicted; misjudging MaxBytes versus typical response sizes.","solutions":["Raise Config.MaxBytes so typical responses fit with headroom for churn.","Reduce the response body size being cached (compression, pagination) so it fits reliably.","Treat the error as transient: the response is still served to the client (it is not cached), so no user-facing fix is strictly required unless caching is load-bearing.","If the error is logged per-request and noisy, raise MaxBytes or filter it in your log pipeline."],"exampleFix":"// before\ncache.New(cache.Config{MaxBytes: 1 << 16}) // 64 KiB, too tight\n// after\ncache.New(cache.Config{MaxBytes: 1 << 22}) // 4 MiB headroom","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"// cache internal error; callers usually see it via logs. Treat as soft:\n// the response is still served uncached. Monitor frequency and raise MaxBytes if noisy.","preventionTips":["Size Config.MaxBytes to several times your largest cacheable response plus churn headroom.","Monitor cache error logs; a rising rate signals MaxBytes is too small for the workload.","Prefer fixing MaxBytes over suppressing the log, since uncached hot paths hurt latency."],"tags":["cache","config","eviction"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}