> For the complete documentation index, see [llms.txt](https://gchandra.gitbook.io/big-data-and-tools-with-nosql/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gchandra.gitbook.io/big-data-and-tools-with-nosql/nosql/redis/data-structures/list.md).

# List

Redis lists are linked lists of string values. Redis lists are frequently used to:

* Implement stacks and queues.
* Build queue management for background worker systems.

<figure><img src="https://1471795080-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrtXPLjVTxuTGIysjCx89%2Fuploads%2FAQVjVM28xFfiLjaYiPE5%2Fimage.png?alt=media&amp;token=f4dde3cb-39b5-42e8-97b6-ddc454f80073" alt=""><figcaption><p>src: <a href="https://linuxhint.com/redis-lpop/">https://linuxhint.com/redis-lpop/</a></p></figcaption></figure>

* [`LPUSH`](https://redis.io/commands/lpush) adds a new element to the head of a list; [`RPUSH`](https://redis.io/commands/rpush) adds to the tail.
* [`LPOP`](https://redis.io/commands/lpop) removes and returns an element from the head of a list; [`RPOP`](https://redis.io/commands/rpop) does the same but from the tails of a list.
* [`LLEN`](https://redis.io/commands/llen) Returns the length of a list.
* [`LMOVE`](https://redis.io/commands/lmove) Atomically moves elements from one list to another.
* [`LTRIM`](https://redis.io/commands/ltrim) Reduces a list to the specified range of elements.

Image website tracking user experience

**LPUSH** (add elements to the beginning of a list):

* **Example**: `LPUSH "recentPages" "/home"`
* `LPUSH "recentPages" "/about" "/contact"`
* **Use case**: Track a user's page visit history. New pages the user visits are added to the front, showing the most recent visit at the top.

The list will look like&#x20;

```
contact about home
```

**RPUSH** (add elements to the end of a list):

* **Example**: `RPUSH "pageQueue" "/checkout" "/confirmation" "/thankyou"`
* **Use case**: Queue pages for batch processing, like sequentially processing user actions.

**LRANGE** (get a range of elements from a list):

* **Example**: `LRANGE "recentPages" 0 -1`
* **Use case**: Display the pages a user has recently visited on their profile or dashboard.

**LLEN** (get the length of a list):

* **Example**: `LLEN "recentPages"`
* **Use case**: Show the number of pages users have visited in their current session or a set period.

**LPOP** (remove and get the first element in a list):

* **Example**: `LPOP "recentPages"`
* **Use case**: Remove the oldest page in the user's visit history when the list exceeds a certain size to maintain only a fixed number of recent pages.

**RPOP** (remove and get the last element in a list):

* **Example**: `RPOP "pageQueue"`
* **Use case**: Process the last action in a user's action queue, like the previous step in a multi-step form.

How do we track which User accessed which page?

Remember Namespaces?

* `LPUSH "user:123:recentPages" "/home"`
* `LPUSH "user:123:recentPages" "/contact"`
* `LRANGE "user:123:recentPages" 0 -1`

### Use Case: Managing Queues of Work

**FIFO**: First In, First Out

**LIFO**: Last In, First Out<br>

**Create a Task Queue**

```
RPUSH taskQueue "task1" "task2" "task3"
LRANGE taskQueue 0 -1
```

**Worker Fetching a Task (FIFO):**

```
LPOP taskQueue
```

```
LRANGE taskQueue 0 -1
```

**Creating a Stack or LIFO Queue:**

```
LPUSH jobStack "task1" "task2" "task3"
```

```
LPOP jobStack
```
