gofiber/fiber · error

failed to save response: %w

Error message

failed to save response: %w

What it means

Returned by the middleware (idempotency.go:179-181) when cfg.Storage.SetWithContext fails to persist the freshly produced (marshaled) response for the idempotency key. This is a backend write failure and breaks the idempotency guarantee for subsequent retries.

Source

Thrown at middleware/idempotency/idempotency.go:180

				// Filter
				res.Headers = make(map[string][]string)
				for h, vals := range headers {
					if shouldKeepHeader(h) {
						res.Headers[h] = vals
					}
				}
			}
		}

		// Marshal response
		bs, err := res.MarshalMsg(nil)
		if err != nil {
			return fmt.Errorf("failed to marshal response: %w", err)
		}

		// Store response
		if err := cfg.Storage.SetWithContext(c, key, bs, cfg.Lifetime); err != nil {
			return fmt.Errorf("failed to save response: %w", err)
		}

		_ = c.Locals(localsKeyWasPutToCache, true)

		return nil
	}
}

View on GitHub (pinned to 9a4c7e57fe)

Solutions

  1. Verify Storage health and capacity (memory, disk, connections).
  2. Increase the storage write timeout relative to handler+marshal time.
  3. Make downstream handlers idempotent on their own so a missing cache entry on retry is still safe.
  4. Monitor this error — every occurrence is a broken idempotency contract.

Example fix

// before
app.Use(idempotency.New(idempotency.Config{Storage: redis.New()}))

// after — pre-flight check + sized connection pool
store := redis.New(redis.Config{PoolSize: 64})
if err := store.SetWithContext(context.Background(), "hc", []byte("ok"), time.Minute); err != nil {
    log.Fatal(err)
}
app.Use(idempotency.New(idempotency.Config{Storage: store}))
Defensive patterns

Strategy: retry

Validate before calling

// startup write check
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
if err := store.SetWithContext(ctx, "__hc__", []byte("ok"), time.Minute); err != nil {
    log.Fatalf("storage write failed: %v", err)
}

Try / catch

// make downstream handlers idempotent themselves so a missed cache write on retry is still safe

Prevention

When it happens

Trigger: Storage backend write error: Redis OOM, MySQL dead connection, MongoDB write concern failure, context cancellation before SetWithContext returns, storage quota exhausted.

Common situations: Storage eviction policy dropping writes; storage out of disk/memory; TLS to storage resetting; request cancelled by client mid-write so the key is never recorded and a retry will re-execute the handler.

Related errors


AI-assisted analysis of gofiber/fiber@9a4c7e57fe (2026-08-04). Data as JSON: /data/errors/cfcc13b6c9f52b3d.json. Report an issue: GitHub.