Skip to main content

What is List in Redis?

A List in Redis is an ordered collection of string values, implemented as a double-ended queue (a doubly linked list). Every element is a Redis String, and insertion order is preserved.

Main properties

  • Elements are stored in the order they were added.
  • Elements can be added and removed from either end, from the front or the back.
  • Duplicate values are allowed (it's not a Set).
  • It fits queues, stacks, logs, action histories, and temporary buffers.

Example

bash
LPUSH tasks "send the report" RPUSH tasks "call with the client" LRANGE tasks 0 -1

Result:

javascript
1) "call with the client" 2) "send the report"

LPUSH adds to the front, RPUSH adds to the back.

Main commands

CommandDescription
LPUSH key value [value ...]add element(s) to the front of the list
RPUSH key value [value ...]add element(s) to the back of the list
LPOP keyremove and return an element from the front
RPOP keyremove and return an element from the back
LRANGE key start stopget a range of elements
LLEN keyget the list's length
LINDEX key indexget an element by index
LREM key count valueremove an element by value
LTRIM key start stoptrim the list to a range

A queue example

bash
RPUSH queue "task1" RPUSH queue "task2" LPOP queue

Result:

javascript
"task1"

"task2" is left. This is how a FIFO queue (first in, first out) is implemented.

A stack example

bash
LPUSH stack "item1" LPUSH stack "item2" LPOP stack

Result:

javascript
"item2"

This is how a LIFO stack (last in, first out) works.

Additional capabilities

  • The BLPOP and BRPOP commands are blocking versions: they wait for an element to appear (handy for asynchronous queues).
  • LPUSHX and RPUSHX add an element only if the list already exists.
  • LSET lets you change an element by index.

Internal structure

Redis stores lists in two formats:

  1. Quicklist, the main structure: a combination of a doubly linked list and a ziplist (a compact array).
  2. Ziplist (deprecated), used for very short lists.

Quicklist picks the optimal storage method: if there are few elements, they're stored compactly in memory, as the list grows, Redis automatically "unpacks" it into a more efficient form.

Uses

  • Task queues between services
  • Storing a feed of messages or actions
  • Implementing undo/redo
  • Temporary log buffers

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.