redis/go-redis · error

MemoryUsage expects single sample count

Error message

MemoryUsage expects single sample count

What it means

MemoryUsage's variadic samples parameter accepts at most one SAMPLES count, matching the MEMORY USAGE key SAMPLES n syntax. Passing more than one value is rejected locally and returned as the command's error.

Source

Thrown at commands.go:856

func (c cmdable) Time(ctx context.Context) *TimeCmd {
	cmd := NewTimeCmd(ctx, "time")
	_ = c(ctx, cmd)
	return cmd
}

func (c cmdable) DebugObject(ctx context.Context, key string) *StringCmd {
	cmd := NewStringCmd(ctx, "debug", "object", key)
	_ = c(ctx, cmd)
	return cmd
}

func (c cmdable) MemoryUsage(ctx context.Context, key string, samples ...int) *IntCmd {
	args := []interface{}{"memory", "usage", key}
	if len(samples) > 0 {
		if len(samples) != 1 {
			cmd := NewIntCmd(ctx)
			cmd.SetErr(errors.New("MemoryUsage expects single sample count"))
			return cmd
		}
		args = append(args, "SAMPLES", samples[0])
	}
	cmd := NewIntCmd(ctx, args...)
	cmd.SetFirstKeyPos(2)
	_ = c(ctx, cmd)
	return cmd
}

//------------------------------------------------------------------------------

// ModuleLoadexConfig struct is used to specify the arguments for the MODULE LOADEX command of redis.
// `MODULE LOADEX path [CONFIG name value [CONFIG name value ...]] [ARGS args [args ...]]`
type ModuleLoadexConfig struct {
	Path string
	Conf map[string]interface{}
	Args []interface{}

View on GitHub (pinned to c5cad058c7)

Solutions

  1. Pass exactly one sample count: rdb.MemoryUsage(ctx, key, 5).
  2. If no SAMPLES argument is needed, call without any variadic values.
  3. If you have a slice, check len==1 and pass only samples[0].

Example fix

// before
samples := []int{1, 5}
rdb.MemoryUsage(ctx, key, samples...)
// after
if len(samples) == 1 {
    rdb.MemoryUsage(ctx, key, samples[0])
}
Defensive patterns

Strategy: validation

Validate before calling

func canCallMemoryUsage(samples []int) bool {
    return len(samples) <= 1
}

Try / catch

cmd := rdb.MemoryUsage(ctx, key, samples...)
if err := cmd.Err(); err != nil {
    if strings.Contains(err.Error(), "expects single sample count") {
        cmd = rdb.MemoryUsage(ctx, key, samples[0])
    }
}

Prevention

When it happens

Trigger: Calling rdb.MemoryUsage(ctx, key, 1, 2) or otherwise passing two or more ints in the samples variadic slot.

Common situations: Spreading a slice of sample counts (samples...) collected elsewhere; misreading the variadic as a list of per-sample counts.

Related errors


AI-assisted analysis of redis/go-redis@c5cad058c7 (2026-09-01). Data as JSON: /api/errors/593f1739ca448937. Report an issue: GitHub.