Esta página ainda não está disponível no idioma selecionado. Estamos trabalhando para adicionar mais opções de idiomas. Agradecemos sua compreensão.

Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
On this page

Flashing E-commerce Sales with DCS

Updated on 2024-10-28 GMT+08:00

Overview

Application Scenario

An e-commerce flash sale is like an online auction. To attract customers, merchants release a small number of scarce offerings on the platform. Platforms receive dozens or even hundreds of more order placements than usual. However, only a few customers can place orders successfully. The traffic distribution process of an e-commerce flash sales system is as follows:

  1. User requests: When users place orders, the requests enter the load balancing server.
  2. Load balancing: The load balancing server distributes requests to multiple backend servers based on certain algorithms. The algorithms include round robin, random, and least connections.
  3. Service processing logic: Backend servers receive requests and verify the requested quantity and user identity.
  4. Inventory deduction: If the inventory is robust, the backend server deducts stocks, generates an order, and returns a success message to the user. If the inventory is insufficient, the backend server returns a failure message.
  5. Order processing: Backend servers save the order information to the database and perform asynchronous processing such as notifying users of the order status.
  6. Cache update: Backend servers update the inventory information in the cache for the next flash sale request.

The database is accessed multiple times during the flash sale process. Row-level locking is usually used to restrict access. The database can be accessed and an order can be placed only after a lock is obtained. However, the database is blocked by the sheer number of order requests.

Solution

As the cache of the database, DCS for Redis has the following advantages for clients to access Redis for inventory query and order placement:

  • Redis offers high read/write speed and concurrency performance to meet the high concurrency requirements of e-commerce flash sales systems.
  • Redis supports high-availability architecture such as master/standby and cluster. Data persistence is supported, so data can be restored even if the server breaks down.
  • Redis supports transactions and atomic operations to guarantee the consistency and accuracy of operations.
  • Redis caches offering and user information to reduce the database load.

In this example, the hash structure of Redis shows the offering information. total refers to the total amount, booked refers to the number of placed orders, and remain refers to the inventory.

"product": {
"total": 200
"booked":0
"remain":200
}

During inventory deduction, the server sends a request to Redis for placing an order. Redis is single-threaded, and Lua can guarantee the atomicity of multiple commands. Run the following Lua script to deduct inventory:

local n = tonumber(ARGV[1])
if not n  or n == 0 then
   return 0
end
local vals = redis.call(\"HMGET\", KEYS[1], \"total\", \"booked\", \"remain\");
local booked = tonumber(vals[2])
local remain = tonumber(vals[3])
if booked <= remain then
   redis.call(\"HINCRBY\", KEYS[1], \"booked\", n)
   redis.call(\"HINCRBY\", KEYS[1], \"remain\", -n)
   return n;
end
return 0

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?

    • 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 What Is Direct Connect.

    • For public access

      For details about how to access a DCS Redis 4.0/5.0/6.0 instance on a client over a public network, see Using Nginx for Public Access to DCS or Using ELB for Public Access to DCS.

  • JDK1.8 (or later) and IntelliJ IDEA have been installed on the client server. Download the Jedis client.

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

Procedure

  1. Run IntelliJ IDEA on the server. Create a Maven project, create a SecondsKill.java file, and paste the sample code into it. In pom.xml, import Jedis:

    <dependency>
          <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
          <version>4.2.0</version>
    </dependency>

  2. Compile and run the following demo (this example uses Java).

    Change the Redis connection address and port to the actual values.
    package com.huawei.demo;
    import java.util.ArrayList;
    import java.util.*;
    
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    
    public class SecondsKill {
    	private static void InitProduct(Jedis jedis) {
    		jedis.hset("product", "total", "200");
    		jedis.hset("product", "booked", "0");
    		jedis.hset("product","remain", "200");
    	}
    
    	private static String LoadLuaScript(Jedis jedis) {
    		String lua = "local n = tonumber(ARGV[1])\n"
    			+ "if not n  or n == 0 then\n"
    			+ "return 0\n"
    			+ "end\n"
    			+ "local vals = redis.call(\"HMGET\", KEYS[1], \"total\", \"booked\", \"remain\");\n"
    			+ "local booked = tonumber(vals[2])\n"
    			+ "local remain = tonumber(vals[3])\n"
    			+ "if booked <= remain then\n"
    			+ "redis.call(\"HINCRBY\", KEYS[1], \"booked\", n)\n"
    			+ "redis.call(\"HINCRBY\", KEYS[1], \"remain\", -n)\n"
    			+ "return n;\n"
    			+ "end\n"
    			+ "return 0";
    		String scriptLoad = jedis.scriptLoad(lua);
    
    		return scriptLoad;
    	}
    
    	public static void main(String[] args) {
    		JedisPoolConfig config = new JedisPoolConfig();
    		// Maximum connections
    		config.setMaxTotal(30);
    		// Maximum idle connections
    		config.setMaxIdle(2);
    		// Connect to Redis. Replace the Redis instance connection address and port with the actual values.
    		JedisPool pool = new JedisPool(config, "127.0.0.1", 6379);
    		Jedis jedis = null;
    		try {
    			jedis = pool.getResource();
                            jedis.auth("password");   //Configure the password of the instance. You do not need to set this parameter for password-free access.
    			System.out.println(jedis);
    
    			// Initialize product information.
    			InitProduct(jedis);
    
    			// Load the Lua script.
    			String scriptLoad = LoadLuaScript(jedis);
    
    			List<String> keys = new ArrayList<>();
    			List<String> vals = new ArrayList<>();
    			keys.add("product");
    
    			// Request 15 items.
    			int num = 15;
    			vals.add(String.valueOf(num));
    
    			// Run the Lua script.
    			jedis.evalsha(scriptLoad, keys, vals);
    			System.out.println("total:"+jedis.hget("product", "total")+"\n"+"booked:"+jedis.hget("product",
    				"booked")+"\n"+"remain:"+jedis.hget("product","remain"));
    
    		} catch (Exception ex) {
    			ex.printStackTrace();
    		} finally {
    			if (jedis != null) {
    				jedis.close();
    			}
    		}
    	}
    }

    Result:

    total:200
    booked:15
    remain:185

Usamos cookies para aprimorar nosso site e sua experiência. Ao continuar a navegar em nosso site, você aceita nossa política de cookies. Saiba mais

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback