LPUSH
Prepends one or more elements to a list. Creates the key if it doesn’t exist.Syntax
The list key. Created if it doesn’t exist.
One or more elements to prepend to the list.
The length of the list after the push operations.
- 2.4.0: Accepts multiple
elementarguments.
Examples
RPUSH
Appends one or more elements to a list. Creates the key if it doesn’t exist.Syntax
The list key. Created if it doesn’t exist.
One or more elements to append to the list.
The length of the list after the push operations.
- 2.4.0: Accepts multiple
elementarguments.
Examples
LPOP
Removes and returns the first element of a list.Syntax
The list key.
Number of elements to pop. Returns an array if specified.
The value of the first element, or
nil when key does not exist.
If count is specified, returns an array of popped elements.- 6.2.0: Added the
countargument.
Examples
RPOP
Removes and returns the last element of a list.Syntax
The list key.
Number of elements to pop. Returns an array if specified.
The value of the last element, or
nil when key does not exist.
If count is specified, returns an array of popped elements.- 6.2.0: Added the
countargument.
Examples
LRANGE
Returns a range of elements from a list.Syntax
The list key.
The starting index (0-based). Negative indices count from the end (-1 is last element).
The ending index (inclusive). Negative indices count from the end.
List of elements in the specified range.
Examples
LLEN
Returns the length of a list.Syntax
The list key.
The length of the list, or 0 when key does not exist.
Examples
Related Commands
Additional List Commands
- LINDEX: Get element by index
- LSET: Set element value by index
- LINSERT: Insert element before or after another
- LREM: Remove elements by value
- LTRIM: Trim list to specified range
- BLPOP: Blocking left pop
- BRPOP: Blocking right pop
- RPOPLPUSH: Pop from one list, push to another
- BRPOPLPUSH: Blocking RPOPLPUSH
- LMOVE: Move element between lists
- BLMOVE: Blocking LMOVE
- LPOS: Find index of element
- LMPOP: Pop from multiple lists
Use Cases
Message Queue
Implement a simple message queue:Activity Feed
Store user activity with LPUSH and retrieve with LRANGE:Fixed-Size List
Maintain a capped list with LPUSH and LTRIM:Stack (LIFO)
Use LPUSH and LPOP for stack operations:Queue (FIFO)
Use RPUSH and LPOP for queue operations:Best Practices
Performance Considerations
- Head/Tail Operations: LPUSH, RPUSH, LPOP, RPOP are O(1) - very fast
- Index Access: LINDEX is O(N) for middle elements - avoid on large lists
- Range Operations: Use LRANGE carefully with large lists
- List Size: Monitor list size to prevent unbounded growth
Memory Management
- Use LTRIM to maintain fixed-size lists
- Set expiration on temporary lists
- Consider streams for large message queues
- Use LLEN to check size before operations
Blocking Operations
- Blocking Pop
- Non-Blocking Pop
Use BLPOP/BRPOP for efficient queue consumption:
Patterns
Reliable Queue
Implement a reliable queue with processing list:Circular List
Use RPOPLPUSH with same list for circular iteration:Timeline
Store time-ordered events:List vs Other Data Structures
| Feature | List | Stream | Sorted Set |
|---|---|---|---|
| Order | Insertion | Time-based ID | Score-based |
| Duplicates | Yes | Yes | No (unique members) |
| Access | Index, Range | ID, Range | Score, Rank |
| Use Case | Queues, Stacks | Event streams | Leaderboards |
| Blocking | Yes (BLPOP) | Yes (XREAD) | No |
Common Errors
-
WRONGTYPE: Operating on wrong type
-
Index out of range: LINDEX returns nil