List
Last updated
Last updated
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.
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
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
FIFO: First In, First Out
LIFO: Last In, First Out
Create a Task Queue
Worker Fetching a Task (FIFO):
Creating a Stack or LIFO Queue:
adds a new element to the head of a list; adds to the tail.
removes and returns an element from the head of a list; does the same but from the tails of a list.
Returns the length of a list.
Atomically moves elements from one list to another.
Reduces a list to the specified range of elements.