{"id":"f70e1e0427619fe5","repo":"gofiber/fiber","slug":"cache-failed-to-store-raw-key-q-w","errorCode":null,"errorMessage":"cache: failed to store raw key %q: %w","messagePattern":"cache: failed to store raw key %q: %w","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"middleware/cache/manager.go","lineNumber":188,"sourceCode":"\t\t\treturn fmt.Errorf(\"cache: failed to marshal key %q: %w\", m.logKey(key), err)\n\t\t}\n\t\tif err := m.storage.SetWithContext(ctx, key, raw, exp); err != nil {\n\t\t\tm.release(it)\n\t\t\treturn fmt.Errorf(\"cache: failed to store key %q: %w\", m.logKey(key), err)\n\t\t}\n\t\tm.release(it)\n\t\treturn nil\n\t}\n\n\tm.memory.Set(key, it, exp)\n\treturn nil\n}\n\n// set data to storage or memory\nfunc (m *manager) setRaw(ctx context.Context, key string, raw []byte, exp time.Duration) error {\n\tif m.storage != nil {\n\t\tif err := m.storage.SetWithContext(ctx, key, raw, exp); err != nil {\n\t\t\treturn fmt.Errorf(\"cache: failed to store raw key %q: %w\", m.logKey(key), err)\n\t\t}\n\t\treturn nil\n\t}\n\n\tm.memory.Set(key, raw, exp)\n\treturn nil\n}\n\n// delete data from storage or memory\nfunc (m *manager) del(ctx context.Context, key string) error {\n\tif m.storage != nil {\n\t\tif err := m.storage.DeleteWithContext(ctx, key); err != nil {\n\t\t\treturn fmt.Errorf(\"cache: failed to delete key %q: %w\", m.logKey(key), err)\n\t\t}\n\t\treturn nil\n\t}\n\n\tm.memory.Delete(key)","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/middleware/cache/manager.go#L170-L206","documentation":"Thrown at middleware/cache/manager.go:188 by manager.setRaw() when storage.SetWithContext fails on the raw-bytes write path (used for cached bodies / ETag-stamped payloads). Same cause class as error 146 but for raw entries.","triggerScenarios":"A raw cache write (body bytes) hits a failing Storage backend: out of memory, connection lost, ctx cancelled, or quota exceeded.","commonSituations":"Redis maxmemory-policy under load; disk full on persistent backends; ctx cancellation from client disconnect; transient network error to the storage host.","solutions":["Resolve the underlying backend issue identified by the wrapped error.","Cap the size of cached bodies via the cache config / upstream response size to avoid hitting backend value-size limits.","Tune storage write timeout and pool to be resilient to brief outages.","Ensure the per-request context outlives the storage SET to avoid spurious ctx-cancellation errors.","Log and degrade gracefully via a custom ErrorHandler instead of returning 500 to the client."],"exampleFix":"// before\nstore := redis.New()\n\n// after: bound body size + explicit eviction policy\nstore := redis.New(redis.Config{\n    URL: os.Getenv(\"REDIS_URL\"),\n})\napp.Use(cache.New(cache.Config{\n    Storage: store,\n    MaxBytes: 1 << 20, // skip caching bodies > 1MB to avoid storage pressure\n}))","handlingStrategy":"try-catch","validationCode":"// Validate the backend accepts the largest raw value you intend to cache.\nbig := make([]byte, maxBodyBytes)\nif err := store.SetWithContext(ctx, \"__probe_big__\", big, time.Second); err != nil {\n    log.Fatalf(\"storage rejects raw payload size: %v\", err)\n}","typeGuard":null,"tryCatchPattern":"if err := m.storage.SetWithContext(ctx, key, raw, exp); err != nil {\n    log.Printf(\"raw cache write failed for %q: %v\", key, err)\n    return err\n}","preventionTips":["Cap cached body size via the cache config / upstream to avoid storage value-size limits.","Probe write capability with a realistically-sized payload at startup.","Tune storage write timeout to fit the request lifetime.","Degrade to uncached serving on raw-write failure rather than 500-ing."],"tags":["cache","storage","network","redis","fiber"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}