go-redis/redis · error

redis: buffer too small: need %d bytes, have %d

Error message

redis: buffer too small: need %d bytes, have %d

What it means

Error "redis: buffer too small: need %d bytes, have %d" thrown in go-redis/redis.

Source

Thrown at internal/proto/reader.go:601

// after a bufio refill can be misinterpreted as the verbatim format tag.
//
// If the bulk payload does not fit in buf, an error is returned and the
// payload plus the trailing CRLF are drained from the reader so the
// connection stays aligned for the next reply. For simple-string / integer
// / float responses the payload lives in the (already-consumed) header
// line, so no drain is needed.
func (r *Reader) ReadStringInto(buf []byte) (int, error) {
	line, err := r.ReadLine()
	if err != nil {
		return 0, err
	}

	switch line[0] {
	case RespStatus:
		// Simple string — data is in the line itself.
		s := line[1:]
		if len(s) > len(buf) {
			return 0, fmt.Errorf("redis: buffer too small: need %d bytes, have %d", len(s), len(buf))
		}
		return copy(buf, s), nil

	case RespString:
		n, err := replyLen(line)
		if err != nil {
			return 0, err
		}
		if n > len(buf) {
			// Drain the payload + trailing \r\n so the next read on this
			// connection sees the start of the next reply rather than the
			// tail of this one. Otherwise the unread bytes corrupt the
			// stream and the bad connection gets handed back to the pool.
			if _, derr := r.rd.Discard(n + 2); derr != nil {
				return 0, derr
			}
			return 0, fmt.Errorf("redis: buffer too small: need %d bytes, have %d", n, len(buf))
		}

View on GitHub (pinned to 36d97525cd)

Solutions

  1. Increase the destination buffer size (ReadBufferSize) to fit the reply
  2. Use the non-buffered read variant for large values

When it happens

Trigger: Thrown at internal/proto/reader.go:601 when the library encounters an invalid state.

Common situations: Size buffers from the announced reply length before copying.


AI-assisted analysis of go-redis/redis@36d97525cd (2026-08-06). Data as JSON: /data/errors/0f6349197848d7cd.json. Report an issue: GitHub.