Updated on 2024-11-26 GMT+08:00

Ranking with DCS

Overview

Ranking is a function commonly used on web pages and apps. It is implemented by listing key-values in descending order. However, a huge number of concurrent operation and query requests can result in a performance bottleneck, significantly increasing latency.

Ranking using DCS for Redis provides the following advantages:

  • Data is stored in the memory, so read/write is fast.
  • Multiple types of data structures, such as strings, lists, sets, and hashes are supported.

Prerequisites

  • A DCS instance has been created, and is in the Running state.
  • The network between the client server and the DCS instance is connected:
    • When the client and the DCS Redis instance are in the same VPC:

      By default, networks in a VPC can communicate with each other.

    • When the client and the DCS Redis instance are in different VPCs in the same region:

      If the client and DCS Redis instance are not in the same VPC, connect them by establishing a VPC peering connection. For details, see "Does DCS Support Cross-VPC Access?" in Distributed Cache Service User Guide > FAQs.

    • To access a Redis instance of another region on a client

      If the client server and the Redis instance are not in the same region, connect the network using Direct Connect. For details, see Direct Connect User Guide.

  • You have installed JDK1.8 (or later) and a development tool (Eclipse is used as an example) on the client server, and downloaded the Jedis client.

    The development tools and clients mentioned in this document are for example only.

Procedure

  1. Run Eclipse on the server. Choose File > New Project to create a Java project named dcsDemo02.
  2. Choose New > Class to create a productSalesRankDemo.java file.
  3. Copy the following demo code to the productSalesRankDemo.java file.

    package dcsDemo02;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Set;
    import java.util.UUID;
    
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.Tuple;
    
    public class productSalesRankDemo {
        static final int PRODUCT_KINDS = 30;
    
        public static void main(String[] args) {
            // Address and port for connecting to the Redis instance. Replace them with the actual values.
            String host = "192.168.0.246";
            int port = 6379;
    
            Jedis jedisClient = new Jedis(host, port);
    
            try {
                // Password for connecting to the Redis instance. Replace it with the actual value.
                String authMsg = jedisClient.auth("******");
                if (!authMsg.equals("OK")) {
                    System.out.println("AUTH FAILED: " + authMsg);
                }
    
                //Key
                String key = "Best-seller Rankings";
    
                jedisClient.del(key);
    
                //Generate product data at random
                List<String> productList = new ArrayList<>();
                for(int i = 0; i < PRODUCT_KINDS; i ++) {
                    productList.add("product-" + UUID.randomUUID().toString());
                }
    
               //Generate sales volume at random
                for(int i = 0; i < productList.size(); i ++) {
                    int sales = (int)(Math.random() * 20000);
                    String product = productList.get(i);
                   //Insert sales volume into Redis SortedSet
                    jedisClient.zadd(key, sales, product);
                }
    
                System.out.println();
                System.out.println("                   "+key);
    
               //Obtain all lists and display the lists by sales volume
                Set<Tuple> sortedProductList = jedisClient.zrevrangeWithScores(key, 0, -1);
                for(Tuple product : sortedProductList) {
                   System.out.println("Product ID: " + product.getElement() + ", Sales volume: "
                            + Double.valueOf(product.getScore()).intValue());
                }
    
                System.out.println();
                System.out.println("                   "+key);
                System.out.println("                   Top 5 Best-sellers");
    
                //Obtain the top 5 best-selling products and display the products by sales volume
                Set<Tuple> sortedTopList = jedisClient.zrevrangeWithScores(key, 0, 4);
                for(Tuple product : sortedTopList) {
                   System.out.println("Product ID: " + product.getElement() + ", Sales volume: "
                            + Double.valueOf(product.getScore()).intValue());
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                jedisClient.quit();
                jedisClient.close();
            }
        }
    
    }

  4. Configure the connection address, port, and password for the DCS instance in the example code file.
  5. Compile and run the code.

Operation Result

Compile and run the preceding Demo code. The operation result is as follows:

Best-seller Rankings
Product ID: product-b290c0d4-e919-4266-8eb5-7ab84b19862d, Sales volume: 18433
Product ID: product-e61a0642-d34f-46f4-a720-ee35940a5e7f, Sales volume: 18334
Product ID: product-ceeab7c3-69a7-4994-afc6-41b7bc463d44, Sales volume: 18196
Product ID: product-f2bdc549-8b3e-4db1-8cd4-a2ddef4f5d97, Sales volume: 17870
Product ID: product-f50ca2de-7fa4-45a3-bf32-23d34ac15a41, Sales volume: 17842
Product ID: product-d0c364e0-66ec-48a8-9ac9-4fb58adfd033, Sales volume: 17782
Product ID: product-5e406bbf-47c7-44a9-965e-e1e9b62ed1cc, Sales volume: 17093
Product ID: product-0c4d31ee-bb15-4c88-b319-a69f74e3c493, Sales volume: 16432
Product ID: product-a986e3a4-4023-4e00-8104-db97e459f958, Sales volume: 16380
Product ID: product-a3ac9738-bed2-4a9c-b96a-d8511ae7f03a, Sales volume: 15305
Product ID: product-6b8ad4b7-e134-480f-b3ae-3d35d242cb53, Sales volume: 14534
Product ID: product-26a9b41b-96b1-4de0-932b-f78d95d55b2d, Sales volume: 11417
Product ID: product-1f043255-a1f9-40a0-b48b-f40a81d07e0e, Sales volume: 10875
Product ID: product-c8fee24c-d601-4e0e-9d18-046a65e59835, Sales volume: 10521
Product ID: product-5869622b-1894-4702-b750-d76ff4b29163, Sales volume: 10271
Product ID: product-ff0317d2-d7be-4021-9d25-1f997d622768, Sales volume: 9909
Product ID: product-da254e81-6dec-4c76-928d-9a879a11ed8d, Sales volume: 9504
Product ID: product-fa976c02-b175-4e82-b53a-8c0df96fe877, Sales volume: 8630
Product ID: product-0624a180-4914-46b9-84d0-9dfbbdaa0da2, Sales volume: 8405
Product ID: product-d0079955-eaea-47b2-845f-5ff05a110a70, Sales volume: 7930
Product ID: product-a53145ef-1db9-4c4d-a029-9324e7f728fe, Sales volume: 7429
Product ID: product-9b1a1fd1-7c3b-4ae8-9fd3-ab6a0bf71cae, Sales volume: 5944
Product ID: product-cf894aee-c1cb-425e-a644-87ff06485eb7, Sales volume: 5252
Product ID: product-8bd78ba8-f2c4-4e5e-b393-60aa738eceae, Sales volume: 4903
Product ID: product-89b64402-c624-4cf1-8532-ae1b4ec4cabc, Sales volume: 4527
Product ID: product-98b85168-9226-43d9-b3cf-ef84e1c3d75f, Sales volume: 3095
Product ID: product-0dda314f-22a7-464b-ab8c-2f8f00823a39, Sales volume: 2425
Product ID: product-de7eb085-9435-4924-b6fa-9e9fe552d5a7, Sales volume: 1694
Product ID: product-9beadc07-aab0-438c-ac5e-bcc72b9d9c36, Sales volume: 1135
Product ID: product-43834316-4aca-4fb2-8d2d-c768513015c5, Sales volume: 256

            Best-seller Rankings
            Top 5 Best-sellers 
Product ID: product-b290c0d4-e919-4266-8eb5-7ab84b19862d, Sales volume: 18433
Product ID: product-e61a0642-d34f-46f4-a720-ee35940a5e7f, Sales volume: 18334
Product ID: product-ceeab7c3-69a7-4994-afc6-41b7bc463d44, Sales volume: 18196
Product ID: product-f2bdc549-8b3e-4db1-8cd4-a2ddef4f5d97, Sales volume: 17870
Product ID: product-f50ca2de-7fa4-45a3-bf32-23d34ac15a41, Sales volume: 17842