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.
LLEN
Returns the length of a list.
LMOVE
Atomically moves elements from one list to another.
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
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: