7 questionsA Redis list is an ordered collection of values that maintains the order items were added, and you use LPUSH to add an item to the left, meaning the beginning, of the list, or RPUSH to add an item to the right, meaning the end, both operations being very fast regardless of how many items the list already contains.
Example
LPUSH recent_activity 'user logged in'
RPUSH task_queue 'process order 123'
Real-world example
A notification feed adds the newest notification to the beginning of a list using LPUSH, ensuring the most recent activity always appears first when the list is displayed.
Likely follow-ups
- What is the time complexity of adding an item to a Redis list?
- What happens if you push to a key that does not exist yet?
Data Types Redis as a Message Queue
You use the LRANGE command, specifying the list key along with a starting and ending index, which returns the items within that range without removing them from the list, letting you view a portion of the list's contents, such as the first ten items, as many times as needed.
Example
LRANGE recent_activity 0 9
Real-world example
An activity feed displays the ten most recent items by calling LRANGE with a start index of zero and an end index of nine, leaving the underlying list completely unchanged.
Likely follow-ups
- How do you retrieve the entire list using LRANGE?
- What happens if the specified range extends beyond the actual length of the list?
Data Types Redis CLI & Basic Commands
You use RPUSH to add new items to the end of the list, representing new work being added to the queue, and LPOP to remove and retrieve the item from the beginning of the list, representing the oldest item being processed next, together implementing a straightforward first in first out queue.
Example
RPUSH task_queue 'task1'
RPUSH task_queue 'task2'
LPOP task_queue -- returns 'task1'
Real-world example
A background job processing system uses a Redis list as a simple task queue, adding new jobs with RPUSH and having worker processes continuously pull and process the oldest job with LPOP.
Likely follow-ups
- What happens when multiple workers try to LPOP from the same queue at the same time?
- What is the difference between LPOP and BLPOP?
Redis as a Message Queue Data Types
BLPOP works like LPOP but blocks and waits if the list is currently empty, up to a specified timeout, instead of immediately returning nothing, which lets a worker process efficiently wait for new work to arrive without needing to repeatedly poll an empty queue in a tight loop that wastes resources.
Example
BLPOP task_queue 30 -- waits up to 30 seconds for a new task
Real-world example
A worker process waits efficiently using BLPOP for new tasks to arrive in the queue, immediately processing a task the moment one becomes available rather than constantly checking an empty queue every few milliseconds.
Likely follow-ups
- What happens if the timeout expires without any new item arriving?
- Can BLPOP wait on multiple different list keys at the same time?
Redis as a Message Queue Redis Performance Tuning & Benchmarking
You use the RPOPLPUSH or the newer LMOVE command to atomically move a task from the main queue into a separate processing list at the same time it is retrieved, so if a worker crashes while handling that task, it remains visible in the processing list and can be detected and reprocessed by a monitoring process, rather than being lost entirely.
Example
RPOPLPUSH task_queue processing_queue
-- Worker processes the task, then removes it
-- from processing_queue only once fully complete
LREM processing_queue 1 'completed_task'
Real-world example
A payment processing system moves each task into a dedicated processing list as a worker picks it up, ensuring that if the worker crashes mid task, a monitoring process can detect the stuck task still sitting in the processing list and safely retry it.
Likely follow-ups
- How does a monitoring process detect tasks that have been stuck in the processing list too long?
- What is the difference between RPOPLPUSH and the newer LMOVE command?
Redis as a Message Queue Distributed Locks with Redis
Redis stores small lists using a compact listpack encoding, similar to small hashes and sets, automatically switching to a doubly linked list structure called a quicklist once the list grows beyond a configurable size threshold, which trades some memory efficiency for better performance characteristics on larger lists with frequent additions and removals at both ends.
Example
OBJECT ENCODING mylist
CONFIG GET list-max-listpack-size
Real-world example
A team storing many small activity log lists notices most of them stay within the compact listpack encoding threshold, keeping their overall memory usage lower than if every list had immediately switched to the larger quicklist structure.
Likely follow-ups
- What are the tradeoffs between listpack and quicklist encoding for lists?
- How do you adjust the size threshold that controls when this encoding switch happens?
Redis Memory Optimization Data Types
You use the LTRIM command after adding a new item, specifying a range that keeps only the desired portion of the list, such as the most recent one hundred items, which is a common pattern for maintaining a bounded size activity feed or log without letting the list grow indefinitely and consume unlimited memory.
Example
LPUSH recent_activity 'new event'
LTRIM recent_activity 0 99 -- keep only the most recent 100 items
Real-world example
A user activity feed pushes each new event to the front of a list and immediately trims it to keep only the hundred most recent entries, preventing the list from growing without bound over a user's entire history on the platform.
Likely follow-ups
- Does LTRIM remove items from the beginning or the end of the list?
- How do you decide an appropriate maximum size for a trimmed list?
Redis Memory Optimization Caching Patterns