Redis LLEN Command Explained

In Redis, the LLEN command returns the length of the list stored at the specified key.

Syntax

The syntax goes like this:

LLEN key

Where key is the name of the key that holds the list.

Example

Suppose we create a list like this:

LPUSH scores 1 2 3 4 5

Result:

(integer) 5

We can see that the list contains five elements by the integer reply.

Now let’s use LLEN to check the length:

LLEN scores

Result:

(integer) 5

As expected, same result.

Wrong Type

If the key exists, but it’s the wrong type, LLEN returns an error.

Example:

LLEN customer

Result:

(error) WRONGTYPE Operation against a key holding the wrong kind of value

We can use the TYPE command to check its type:

TYPE customer

Result:

hash

Non-Existent Key

If the key doesn’t exist, an integer reply of zero is returned:

LLEN nonexistentkey

Result:

(integer) 0