100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Redis

Lists in Redis

Understand Redis Lists as ordered, linked-list-backed collections, covering push/pop operations, blocking reads for queues, and range-based trimming.

Core Data StructuresBeginner8 min readJul 10, 2026
Analogies

What Redis Lists Are

A Redis list is an ordered collection of strings, internally stored as a doubly linked structure of compact listpacks (quicklist), which makes pushing or popping from either end an O(1) operation while accessing an element by index near the middle is O(n). This makes lists a natural fit for queues, stacks, activity feeds, and any scenario where insertion order matters and you mostly touch the head or tail.

🏏

Cricket analogy: A Redis list is like a Test match batting order — you can easily slot a new opener in at the top (LPUSH) or add a number eleven at the bottom (RPUSH), but swapping the number six batter mid-innings means shuffling through the whole card.

Push and Pop: LPUSH, RPUSH, LPOP, RPOP

LPUSH and RPUSH add one or more elements to the left (head) or right (tail) of a list, while LPOP and RPOP remove and return elements from those same ends, both running in O(1) per element. Combining RPUSH on the producer side with LPOP on the consumer side gives you a simple FIFO queue, while LPUSH with LPOP gives a LIFO stack — the same primitive supports either pattern depending on which ends you use.

🏏

Cricket analogy: RPUSH followed by LPOP mirrors a bowling rotation queue where the next bowler is added at the back of the list and the captain always calls up whoever's been waiting longest from the front, like rotating through a five-man attack in an ODI.

bash
# Build a queue: producers push right, consumers pop left
RPUSH orders:queue "order:5001"
RPUSH orders:queue "order:5002"
LPOP orders:queue          # => "order:5001"

# Build a stack: push and pop from the same end
LPUSH undo:doc42 "edit:bold"
LPUSH undo:doc42 "edit:italic"
LPOP undo:doc42            # => "edit:italic"

# Blocking pop: wait up to 5 seconds for work to arrive
BLPOP orders:queue 5

# Inspect and trim
LRANGE orders:queue 0 -1
LTRIM recent:activity 0 999   # keep only the newest 1000 entries

Blocking Operations for Work Queues

BLPOP and BRPOP behave like LPOP/RPOP but block the client for up to a specified timeout when the list is empty, waking up immediately once another client pushes an element — this turns Redis lists into a lightweight, low-latency job queue without the consumer polling in a tight loop. BLMOVE (the successor to the deprecated BRPOPLPUSH) atomically pops from one list and pushes onto another, which is the standard pattern for a reliable queue where a worker moves a job into a 'processing' list before working on it.

🏏

Cricket analogy: BLPOP is like a twelfth man waiting in the dugout who doesn't keep checking the scoreboard every second — he's instantly called onto the field the moment an injury happens, rather than polling the captain every ball.

BLMOVE (introduced in Redis 6.2 to replace BRPOPLPUSH) atomically moves an element from a source list to a destination list, so if a worker crashes mid-processing, the job is still sitting safely in the destination 'processing' list and can be recovered instead of being lost.

Range Operations and Capped Lists

LRANGE returns a slice of the list by zero-based index, supporting negative indices to count from the tail (-1 is the last element), which makes it easy to fetch 'the newest 20 items' without scanning the whole structure. LTRIM keeps only the elements within a given index range and discards the rest, which is the standard way to implement a capped list — such as keeping only the most recent 1,000 activity-feed entries — so memory usage stays bounded even under continuous LPUSH traffic.

🏏

Cricket analogy: LRANGE 0 4 is like pulling up just the top five run-scorers on an IPL season's leaderboard without loading every player's full stats, while LTRIM mirrors a broadcaster keeping only the last 10 overs of highlights and discarding the rest.

LRANGE and LINDEX on elements near the middle of a very long list are O(n), not O(1) — unlike arrays, Redis lists are linked structures. If you need frequent random access by position on a huge collection, a Hash keyed by index or a Sorted Set is usually a better fit than a giant List.

  • Redis lists are ordered collections backed by a linked quicklist structure: O(1) push/pop at either end, O(n) for middle access.
  • LPUSH/RPUSH and LPOP/RPOP together let the same primitive act as a FIFO queue or a LIFO stack.
  • BLPOP/BRPOP block until an element is available, avoiding wasteful polling for consumers.
  • BLMOVE atomically transfers an element between two lists, the standard pattern for a crash-safe processing queue.
  • LRANGE supports negative indices to slice from the tail, useful for 'most recent N items' queries.
  • LTRIM caps a list's size, keeping memory bounded for continuously growing feeds or logs.

Practice what you learned

Was this page helpful?

Topics covered

#Redis#RedisStudyNotes#Database#ListsInRedis#Lists#Push#Pop#LPUSH#DataStructures#StudyNotes#SkillVeris