Best Seller Ranking with Distributed Cache Service for REDIS
Introduction
Hello everyone in this article firstly we will talk about Redis service on Huawei cloud. After that, we will start to do an exercise about Ranking on DCS for Redis.
With this exercise you can melt Redis, which is actually a third-party tool, using Distributed Cache Service. Huawei Cloud gives us this opportunity. Prepare your coffee and enjoy readings. ☕
What Is Redis?
Redis is an open-source, in-memory data structure store used as a database, cache, message broker, and streaming engine. Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.
What Is DCS for Redis?
Huawei Cloud Distributed Cache Service (DCS) is an online, distributed, fast in-memory cache service compatible with Redis. Redis is an open-source, in-memory data structure store used as a database, cache, message broker, and streaming engine. Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams. It is reliable, scalable, usable out of the box, and easy to manage, meeting your requirements for high read/write performance and fast data access.
Let's start to do our practice.
Step-by-step Creating DCS Instance and running to Redis Queries
In the Ranking with Redis exercise, our main goal is to process the data in the cache and make a quick determination of the analysis measures. A Java project will be created on a virtual machine on this official cloud and the client will be used as ready. Then, it will communicate with a DCS Redis on the same network in these virtual contents and analyze the data.
Creating a DCS Instance for Redis
Step 1 Click to Buy DCS Instance.
Step 2 Set the parameters like the screenshot below.
Step 3 Check the options that set yours. We will use the IP address of the instance.
Creating an ECS Windows server
Step 1 We have to create a Windows server. Click on the Buy ECS button.
Step 2 Then we have to set the ECS parameters like the screenshots below.
Step3 Then click on Next Configure Network
Step 4 After then click on Next Configure Advanced Settings.
Step 5 Finally click on the Submit button.
Step 6 Check our ECS’s features.
Login to the ECS Windows server
Step1 Then we will install Java and Eclipse IDE on this virtual machine and download the Jedis client file that we will use in the project. We have to be careful the AZ it has to be the same as DCS AZ’s.
For the Install Java: https://www.oracle.com/java/technologies/downloads/#java8
For the Jedis Client:
https://jar-download.com/artifacts/redis.clients/jedis/2.9.0/source-code
Step 2 Run Eclipse on the ECS and create a Maven project. Then, create a productSalesRankDemo.java file for the example code, and reference the Jedis client as a library to the project.
I used this sample code:
package mert;
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 Mert {
static final int PRODUCT_KINDS = 30;public static void main(String[] args) {
//Instance connection address, which is obtained from the DCS console.
String host = "192.168.0.164";
//Redis port number
int port = 6379; Jedis jedisClient = new Jedis(host, port); try {
//Instance password
String authMsg = jedisClient.auth("3141mrt..");
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();
}
}
}
Step 3 After that we have to add a dependency group for the client.
Step 4_ We have to compile and run the code. And we can show the result in the screenshot below.
Conclusion
In this demo, we saw how to use the DCS service and how to show the Ranking scenario. While doing the demo, I logged into the remote computer from my personal computer and kept the bandwidth high. At the same time, the point we need to pay attention to is that the ECS and DCS instances are in the same AZ. Afterward, we easily saw the results of the best-seller ranking. See you in the next articles. 🙌