Advertisement
Beginner Redis, Lists in Redis

How do you retrieve a range of items from a Redis list without removing them?

Short answer

You use the LRANGE command, specifying the list key along with a starting and ending index, which returns the items within that range without removing them from the list, letting you view a portion of the list's contents, such as the first ten items, as many times as needed.

Save Redis mock interview
Advertisement
AModel answer
You use the LRANGE command, specifying the list key along with a starting and ending index, which returns the items within that range without removing them from the list, letting you view a portion of the list's contents, such as the first ten items, as many times as needed.
Example

LRANGE recent_activity 0 9

Real-world example
An activity feed displays the ten most recent items by calling LRANGE with a start index of zero and an end index of nine, leaving the underlying list completely unchanged.
Likely follow-ups
  • How do you retrieve the entire list using LRANGE?
  • What happens if the specified range extends beyond the actual length of the list?
Related topics