Advertisement
Beginner Redis, Lists in Redis

What is a Redis list, and how do you add items to either end of it?

Short answer

A Redis list is an ordered collection of values that maintains the order items were added, and you use LPUSH to add an item to the left, meaning the beginning, of the list, or RPUSH to add an item to the right, meaning the end, both operations being very fast regardless of how…

Save Redis mock interview
Advertisement
AModel answer
A Redis list is an ordered collection of values that maintains the order items were added, and you use LPUSH to add an item to the left, meaning the beginning, of the list, or RPUSH to add an item to the right, meaning the end, both operations being very fast regardless of how many items the list already contains.
Example

LPUSH recent_activity 'user logged in' RPUSH task_queue 'process order 123'

Real-world example
A notification feed adds the newest notification to the beginning of a list using LPUSH, ensuring the most recent activity always appears first when the list is displayed.
Likely follow-ups
  • What is the time complexity of adding an item to a Redis list?
  • What happens if you push to a key that does not exist yet?
Related topics