Lists in Redis

7 questions found

What is a Redis list, and how do you add items to either end of it?

Beginner
A 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.
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.

Common 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

How do you retrieve a range of items from a Redis list without removing them?

Beginner
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.
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.

Common 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

How do you implement a simple first in first out queue using a Redis list?

Intermediate
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.
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.

Common 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

What does the BLPOP command do, and how is it useful for building a worker process that waits for new tasks?

Intermediate
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.
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.

Common 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

How would you implement a reliable queue pattern using Redis lists that prevents losing a task if the worker processing it crashes?

Advanced
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.
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.

Common 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

How does the internal encoding of Redis lists change as they grow, and what performance implications does this have?

Advanced
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.
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.

Common 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

How do you trim a Redis list to keep only a specific number of the most recent items, discarding older entries automatically?

Intermediate
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.
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.

Common 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