Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use Sorted Set instead of List Redis

Why would I use a redis sorted set of articles sorted by unix timestamp over a redis list and pushing elements onto it. They seem to offer the same end results. One thing I notice is with a redis sorted set you can do intersections with other sets and zsets

like image 405
somejkuser Avatar asked Feb 05 '18 20:02

somejkuser


1 Answers

Depends on your usage afterward.

Sorting a ZSET by the insertion order, and using a list can look similar, but there are many differences. Here are a few:

  1. LIST can have duplicates.
  2. Checking in an element exists is very efficient in ZSET, but very expansive in a LIST (especially if the element is not there).
  3. Fetching non-edges elements from a LIST can be slow (depends on the size of the LIST and on the distance of the object from one of the edges).
  4. LIST is most efficient when working with the edges (L/R PUSH/POP).
  5. ZSET has the added functionality of unions and intersects, and you can sort by any other score/weight.
  6. In ZSET, the score can be updated later on, and the order will change.

In general, look at the API that each datatype provides, and choose the one that gives you the best results for what you need (other than being sorted by insertion time).

like image 117
Ofir Luzon Avatar answered Oct 08 '22 01:10

Ofir Luzon