Learnitweb

Working with Lists in Redis

In this lecture, we will explore Redis Lists, which are ordered collections of strings. Lists in Redis can be used in many ways, including as queues, stacks, or simply as an ordered collection of elements.


What is a Redis List?

A Redis list is:

  • An ordered collection of items.
  • Each item is a string.
  • Initially implemented as a linked list, and now as a quick list, which optimizes memory and performance.
  • Can be used like other data structures:
    • Queue (FIFO) – first in, first out.
    • Stack (LIFO) – last in, first out.
  • Useful in real-world scenarios like message queues for microservices.

Creating a List

Redis automatically creates a list if it doesn’t exist. You don’t need to initialize it manually like in Java (new ArrayList<>()). If the list already exists, Redis will append to it; otherwise, it will create a new list.

Commands to add items:

  • Push to the right (end) of the list):
RPUSH users "Sam" "Mike" "Jake"
  • Push to the left (start) of the list):
LPUSH users "Sam" "Mike" "Jake"

Output:

(integer) 3

This shows that three items were successfully added.


Checking List Type

If you try to GET a list key, it will fail because GET works only for strings. To check the type:

TYPE users

Output:

list

Checking the Length of a List

To find the number of elements in a list:

LLEN users

Output:

(integer) 3

This shows there are three items in the list.


Accessing Elements in a List

Retrieve a range of elements:

LRANGE users 0 -1
  • 0 is the start index.
  • -1 represents the last element (end of the list).

Output:

1) "Sam"
2) "Mike"
3) "Jake"

Retrieve specific elements:

LRANGE users 0 1

Output:

1) "Sam"
2) "Mike"

This retrieves only the first two elements.


Removing Items from a List

To remove elements, use pop commands:

  • Pop from the left (start):
LPOP users
  • Pop from the right (end):
RPOP users
  • You can also remove multiple elements by specifying the count:
LPOP users 2

The removed elements are returned, and the list is updated.

Example:

LRANGE users 0 -1

After removing two elements from the left:

1) "Jake"

Adding Multiple Items Again

You can push multiple items at once:

RPUSH users "1" "2" "3" "4" "5" "6"

Check the length:

LLEN users

Output:

(integer) 6

Key Features of Redis Lists

  1. Automatic creation – Lists are created automatically when you push an element to a non-existent key.
  2. Ordered collection – The order of elements is preserved.
  3. Flexible insertion – You can push items to either the left or right.
  4. Efficient retrieval – Use LRANGE to access a subset or the entire list.
  5. Queue/Stack usage – Can act as FIFO queues (RPUSH + LPOP) or LIFO stacks (LPUSH + LPOP).
  6. Safe removal – Popping items from an empty list returns nil without throwing exceptions.
  7. Batch operations – You can push or pop multiple items at once using the count parameter.

Practical Example: Queue Processing

  • Producer process: keeps pushing items to the right:
RPUSH taskQueue "task1" "task2" "task3"
  • Consumer process: keeps popping items from the left:
LPOP taskQueue

This simulates a message queue where tasks are processed in order.


Summary of Common List Commands

CommandDescription
LPUSH key value [value...]Add one or more items to the start (left)
RPUSH key value [value...]Add one or more items to the end (right)
LPOP key [count]Remove one or more items from the start
RPOP key [count]Remove one or more items from the end
LLEN keyGet the number of items in the list
LRANGE key start stopGet a range of items
TYPE keyGet the type of the key