In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Editor to share with you how to use Redis locks to solve high concurrency. I hope you will get something after reading this article. Let's discuss it together.
The use of redis technology:
Redis is really a good technology, it can solve the instant concurrency of the website to a certain extent, such as goods snapping up second kill and other activities.
The reason why redis can solve the problem of high concurrency is that it can access memory directly. In the past, we used database (hard disk), which improved the access efficiency and solved the pressure of database server.
Why the status of redis is getting higher and higher, and why do we not choose memcache? this is because memcache can only store strings, while redis storage types are very rich (for example, strings, LIST, SET, etc.) Memcache can only store a maximum of 1m per value, and the storage resources are very limited and consume memory resources, while redis can store 1G. The most important thing is that memcache is not as secure as redis. When the server fails or shuts down unexpectedly, redsi will back up the data in memory to the hard disk, while all the things stored in memcache will be lost. This also shows that memcache is not suitable for database use, but can be used for caching.
Introduction
Here we mainly use Redis's setnx commands to deal with high concurrency.
Setnx has two parameters. The first parameter represents the key. The second parameter represents the value. If the current key does not exist, the current key is inserted with the second parameter as the value. Return 1. If the current key exists, 0 is returned.
Create an inventory table
CREATE TABLE `storage` (`id` int (11) unsigned NOT NULL AUTO_INCREMENT, `number` int (11) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1
Set the initial inventory to 10
Create an order table
CREATE TABLE `order` (`id` int (11) unsigned NOT NULL AUTO_INCREMENT, `number` int (11) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1
When the test is not locked
$pdo = new PDO ('mysql:host=127.0.0.1;dbname=test',' root', 'root'); $sql= "select `number` from storage where id=1 limit 1"; $res = $pdo- > query ($sql)-> fetch (); $number = $res [' number']; if ($number > 0) {$sql= "insert into `order`VALUES (null,$number)"; $order_id = $pdo- > query ($sql); if ($order_id) {$sql= "update storage set `number` = `number`-1 WHERE id=1"; $pdo- > query ($sql);}}
The ab test simulates concurrency and finds that the inventory is correct.
Mysql > select * from storage;+----+-+ | id | number | +-+-+ | 1 | 0 | +-+-+ 1 row in set (0.00 sec)
Let's take a look at the order form.
Mysql > select * from `order`; +-+ | id | number | +-+-+ | 1 | 10 | 2 | 10 | 3 | 9 | 4 | 7 | 5 | 6 | 5 | 7 | 5 | 8 | 5 | 9 | 4 | 10 | 1 | +-- +-- + 10 rows in set (0.00 sec)
It is found that there are several orders with the same inventory data, which may lead to oversold.
Modify the code and add the redis lock for data control
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.