How to Use Redis LLEN

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

Redis stands for Remote Dictionary Server. As an in-memory, NoSQL database (IMDB), Redis uses linked lists as opposed to arrays. This special feature separates Redis from other databases in that it gives coders the ability to use a single method call to access and add more than one list element to a list without regards to the size or length of the Redis List.

It’s an advantage that enables Redis to provide solutions that other databases readily can’t. There are more benefits to Redis. Let’s start with learning more about the basics of Redis LLEN, the command that returns the value representing the length of a list.

Prerequisites

  • Download the latest stable version of Redis.

About Redis lists of data types

Examples of fundamental structure data types are maps, sets, sorted sets, and lists. This tutorial directs attention to the Lists data types. Lists are strings with sorted, in-order elements. Each element’s placement in the sorted order is based on their given value.

To add an element to the left of a list which is called the head, or to the right of the list which is called the tail, you would use pushing commands.

Redis lists creation

  • Create an element on the left of a list, which is the head. Do this with the pushing command LPUSH.

  • Create an element on the right of a list, which is called the tail. Do this with the pushing command RPUSH.

NOTE: If the key is blank or nothing is in it when the LPUSH or RPUSH command is executed, a new list is automatically created. Furthermore, when a list is emptied, the key is removed as well.

  • Follow this sequence of list commands to create a list:
1
2
3
4
5
6
127.0.0.1:6379> LPUSH sample_list C
(integer) 1
127.0.0.1:6379> LPUSH sample_list M
(integer) 2
127.0.0.1:6379> RPUSH sample_list U
(integer) 3

Here’s the explanation of the script shown in the above example:

  1. The sample list “C” represents the first command LPUSH.
  2. The sample list becomes “M” and “C” with the second command LPUSH.
  3. The RPUSH is the third command in the above example and it adds to the tail part of the list, the “M.”

Now, you’re ready to use the LRANGE command in order to show the complete list.

1
2
3
4
127.0.0.1:6379> LRANGE sample_list 0 2
1) "M"
2) "C"
3) "U"

More about the LRANGE command

Zero counts in the LRANGE command. In other words, if your list has 12 elements, the range is from 0 to 12. It will return 13 elements. Keep this in mind if you’re used to coding in a programming language that uses arrays instead of linked lists such as the range commands and functions of Python or Ruby.

How to execute the Redis LLEN command

Let’s continue with learning how to reveal a list’s length. Realize that a key contains a list. To find out how to determine how long of a list you have, use the Redis LLEN command.

  • If the key doesn’t exist, it will return a zero.

  • If a value isn’t in the key that is stored, it will raise an exception.

*Based on the previous sample given earlier, here’s an example of what should be returned when using LLEN:

1
2
127.0.0.1:6379> LLEN sample_list
(integer) 3

NOTE: The result shows an integer of 3. This is correct based on the sample given earlier.

Conclusion

This tutorial showed you how to use the Redis list length (LLEN) command. First, you learned how to create a data list in Redis. The basic Redis commands that were explained are the LPUSH and RPUSH pushing commands used to create lists. After executing those pushing commands, the LRANGE command is used to display the list within a specified range.

The Redis LLEN command shows the length of the list. It returns the total figure by indicating the value of the integer in the results. You can tell if a key isn’t really there because a zero will show up in the results. What’s more, the value must be in the stored key, otherwise, an error will appear. Begin using Redis LLEN in your scripts today.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.