Recent-item feeds
Add newest items at the front of a list while retaining insertion history.
Redis · Command
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.
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.
LPUSH key element [element ...]| Name | Type | Description |
|---|---|---|
key | key | Name of the key that holds the list. |
element [element ...] | one or more values | One or more values to prepend to the list. |
Integer reply: the length of the list after the push operation.
LPUSH mylist "world"
LPUSH mylist "hello"
LRANGE mylist 0 -1Adds two values at the list head, then reads the complete list to show the newest value first.
Add newest items at the front of a list while retaining insertion history.
Place work identifiers onto a list for later consumption by a worker.
Build stack-like behavior by pairing head insertion with head removal.
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.
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.
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.
LPUSHXRPUSHLPOPLRANGELLEN