Fix “ERR syntax error” when calling SRANDMEMBER in Redis

If you’re getting an error that reads “ERR syntax error” when calling the SRANDMEMBER command in Redis, it could be that you’re passing too many arguments.

To fix this issue, be sure to pass the correct number of arguments. At the time of writing, SRANDMEMBER accepts up to two arguments. Any more than this results in an error.

Example of Error

Here’s an example of code that produces the above error:

SRANDMEMBER animals 2 3

Result:

(error) ERR syntax error

Solution

To fix the issue, simply pass the correct number of arguments. At the time of writing, SRANDMEMBER can be called with either one or two arguments.

Here’s an example of passing two arguments:

SRANDMEMBER animals 2

Result:

1) "Dog"
2) "Cat"

The second argument specifies how many random members to return. We can omit this argument to return just one member.

Here’s an example of passing one argument:

SRANDMEMBER animals

Result:

"Dog"

See the Redis documentation to check the latest syntax.