redis/redis · error

--memkeys-samples value should be positive.

Error message

--memkeys-samples value should be positive.

What it means

After a successful integer conversion of `--memkeys-samples`, the CLI checks `config.memkeys_samples < 0` and exits. Note 0 is permitted by this check (the upstream SAMPLES command handles it). The flag implicitly enables `--memkeys` and `--keystats` modes (lines 2885-2886).

Source

Thrown at src/redis-cli.c:2893

            config.pipe_mode = 1;
        } else if (!strcmp(argv[i],"--pipe-timeout") && !lastarg) {
            config.pipe_timeout = atoi(argv[++i]);
        } else if (!strcmp(argv[i],"--bigkeys")) {
            config.bigkeys = 1;
        } else if (!strcmp(argv[i],"--memkeys")) {
            config.memkeys = 1;
            config.memkeys_samples = -1; /* use redis default */
        } else if (!strcmp(argv[i],"--memkeys-samples") && !lastarg) {
            char *endptr;
            config.memkeys = 1;
            config.keystats = 1;
            config.memkeys_samples = strtoll(argv[++i], &endptr, 10);
            if (*endptr) {
                fprintf(stderr, "--memkeys-samples conversion error.\n");
                exit(1);
            }
            if (config.memkeys_samples < 0) {
               fprintf(stderr, "--memkeys-samples value should be positive.\n");
               exit(1);
            }
        } else if (!strcmp(argv[i],"--hotkeys")) {
            config.hotkeys = 1;
        } else if (!strcmp(argv[i], "--keystats")) {
            config.keystats = 1;
            config.memkeys_samples = -1; /* use redis default */
        } else if (!strcmp(argv[i],"--keystats-samples") && !lastarg) {
            char *endptr;
            config.keystats = 1;
            config.memkeys_samples = strtoll(argv[++i], &endptr, 10);
            if (*endptr) {
                fprintf(stderr, "--keystats-samples conversion error.\n");
                exit(1);
            }
            if (config.memkeys_samples < 0) {
               fprintf(stderr, "--keystats-samples value should be positive.\n");
               exit(1);

View on GitHub (pinned to 4f20cb4893)

Solutions

  1. Use a non-negative integer (`0` or higher).
  2. Omit the flag entirely to let Redis use its default samples value.
  3. Fix the sign in the variable/expression feeding the flag.

Example fix

// before
redis-cli --memkeys --memkeys-samples -5
// after
redis-cli --memkeys --memkeys-samples 5
Defensive patterns

Strategy: validation

Validate before calling

val="$MEMKEYS_SAMPLES"
if ! [[ "$val" =~ ^-?[0-9]+$ ]]; then echo "not an integer" >&2; exit 1; fi
if [ "$val" -lt 0 ] 2>/dev/null; then echo "--memkeys-samples must be >= 0" >&2; exit 1; fi
redis-cli --memkeys --memkeys-samples "$val"

Try / catch

if ! redis-cli --memkeys --memkeys-samples "$val"; then
  echo "memkeys-samples range check failed" >&2; exit 1
fi

Prevention

When it happens

Trigger: `--memkeys-samples -5`, or a negative value produced by a sign error in a computed variable.

Common situations: A negative default in a script; subtracting instead of adding when computing a sample count; copy-paste of a negative value.

Related errors


AI-assisted analysis of redis/redis@4f20cb4893 (2026-08-10). Data as JSON: /api/errors/d1797b36447ab19e. Report an issue: GitHub.