[Avg. reading time: 9 minutes]

List

Redis lists are linked lists of string values. Redis lists are frequently used to:

  • Implement stacks and queues.
  • Build queue management for background worker systems.

src: https://linuxhint.com/redis-lpop/

  • LPUSH adds a new element to the head of a list; RPUSH adds to the tail.

  • LPOP removes and returns an element from the head of a list; RPOP does the same but from the tails of a list.

  • LLEN Returns the length of a list.

  • LMOVE Atomically moves elements from one list to another.

  • LTRIM Reduces a list to the specified range of elements.


FIFO: First In, First Out

LIFO: Last In, First Out

Adding Tasks to the Queue

Use the LPUSH or RPUSH command to add new tasks to the list. Here, we'll use RPUSH to ensure tasks are added to the end of the list.

RPUSH task_queue "Task 1: Process image A"
RPUSH task_queue "Task 2: Send email to user B"
RPUSH task_queue "Task 3: Generate report for user C"

Sort Alphabets with ALPHA

SORT task_queue ALPHA

Processing Tasks from the Queue

Use the LPOP command to remove and get the first element from the list. This simulates processing the tasks in the order they were received.

Pops the first value: "Task 1: Process image A"

LPOP task_queue

Length of the Queue

LLEN task_queue

Use the LRANGE command to view tasks

LRANGE task_queue 0 -1

More Use Cases

Chat Application: Use lists to store messages in a chat room. Each message can be pushed to the list, and the latest messages can be displayed using LRANGE.

Activity Logs: Maintain an activity log where each action is pushed to the list. You can then retrieve the latest actions by using LRANGE or delete old ones using LTRIM.

Real-Time Leaderboard: Keep a list of top scores in a game. Push scores to the list and trim it to keep only the top N scores using LTRIM.

Try these examples

LPUSH user:123:activity "Viewed product 456"
LPUSH user:123:activity "Added product 789 to cart"
LPUSH user:123:activity "Checked out order 321"

Limit Activity Log to Last N Items Truncates the list to last 10 items. Others are deleted.

LTRIM user:123:activity 0 9

Get Recent Activities This is readonly.

LRANGE user:123:activity 0 -1

Sort

RPUSH mylist 5 3 8 1 6
SORT mylist

#RPUSH #LPUSH #List #NoSQL #RedisVer 5.5.3

Last change: 2025-10-15