Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Redis · Command

LPUSH

LPUSH prepends one or more elements to a Redis list, creating the list when the key is absent. It returns the list's length after insertion.

Since
1.0.0

How it works

LPUSH prepends one or more values to the list at the specified key. Redis creates the key as a list when it is absent; multiple arguments are inserted sequentially, so the rightmost supplied value becomes the first list item.

Syntax

LPUSH key element [element ...]

Parameters

NameTypeDescription
keykeyName of the key that holds the list.
element [element ...]one or more valuesOne or more values to prepend to the list.

Returns

Integer reply: the length of the list after the push operation.

Key points

  • LPUSH prepends every supplied element to the target list.
  • An absent key is initialized as an empty list before insertion.
  • The result is the list length after all insertions complete.
  • Each individual element insertion has constant-time complexity.
  • Redis classifies LPUSH as a write command for list data structures and marks it fast.

Common mistakes

  • The command fails if the key already contains a non-list value.
  • With multiple elements, insertion order is reversed in the resulting list: the last argument ends up at the head.

Example: Prepend values to a list

LPUSH mylist "world"
LPUSH mylist "hello"
LRANGE mylist 0 -1

Adds two values at the list head, then reads the complete list to show the newest value first.

When to use it

Recent-item feeds

Add newest items at the front of a list while retaining insertion history.

Queue production

Place work identifiers onto a list for later consumption by a worker.

LIFO stacks

Build stack-like behavior by pairing head insertion with head removal.

Interview questions

QWhat does LPUSH do and what does it return?

LPUSH adds one or more elements to the head of a list and returns the list's length afterward. If the key is absent, Redis creates the list automatically.

Follow-up: How does LPUSH mylist a b c order the values? The resulting order is c, b, a because arguments are inserted one at a time from left to right at the head.

QWhat is the syntax and argument structure of LPUSH?

Its syntax is LPUSH key element [element ...]. The command accepts one or more elements, and the key identifies the target list.

Follow-up: What happens when the key contains a string? Redis returns a type error because the existing value is not a list.

QWhat is LPUSH's complexity?

LPUSH has constant-time complexity for each element added, so a call adding multiple elements takes time proportional to the number of supplied elements. It is categorized as a write operation on a list.

Follow-up: Which command provides the create-only-if-present behavior? LPUSHX prepends values only when the key already exists and contains a list.

  • LPUSHX
  • RPUSH
  • LPOP
  • LRANGE
  • LLEN

Source: LPUSH | Docs - Redis. Checked 2026-10-16.