Troubleshoot sorting problems in Redis cluster mode

Source: Internet
Author: User
Tags redis cluster

The usual Redis sort we can do this:

Sort by commodity price: sort Goods_id_set by P_*_price

This is not a problem in non-clustered mode, but if you are in cluster mode, you will get an error:

It is not possible to use the sort by clause in cluster mode, because in cluster mode, the above p_*_price matching key may be distributed in the slots of different nodes, so it cannot be sorted.

What about that? Don't worry, there's still some way, we'll use SortedSet .

How to use it? Very simple, look at the code:

Let's add a few items and assign the price

Zadd goods_prices 1 //Product ID 1, price is 10 yuan

Zadd goods_prices 2 //Product ID 2, price is 20 yuan

Zadd goods_prices 3 //Product ID 3, price is 15 yuan

Zadd goods_prices 5 4 //commodity ID 4 Price is $5

Then sort:

1. Zrange goods_prices 0,9999999 //Sort by price from low to high

2. Zrevrange goods_prices 0,9999999 //Sort by price from highest to lowest

Printing results:

1.4,1,3,2

2.2,3,1,4

And then go through these IDs to get the product information.

Troubleshoot sorting problems in Redis cluster mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.