当前位置:网站首页>How to use redis to realize things and locks?
How to use redis to realize things and locks?
2022-07-28 20:45:00 【I am a cabbage】
Hello everyone , I'm cabbage . This article mainly explains Redis And the simple implementation of the lock , If you get something after reading the article , You can support bloggers three times in a row ~, Hee hee .
List of articles
One 、 What is? Redis thing
- Redis Business is a Separate isolation operation : All commands in the transaction are serialized 、 To execute in order . Transaction is in the process of execution , Will not be interrupted by command requests from other clients .
effect :Concatenate multiple commands to prevent other commands from jumping in line .
Two 、Redis Business order
The following table lists them redis Orders related to the transaction :

Be careful : The command to join the transaction temporarily enters the task queue , Not immediately , Only execute exec It's the order that begins
3、 ... and 、 Command line Demo
Demonstrate the beginning of things 、 perform
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379(TX)> set name cabbage
QUEUED
127.0.0.1:6379(TX)> set age 20
QUEUED
127.0.0.1:6379(TX)> EXEC
1) OK
2) OK
127.0.0.1:6379> keys *
1) "name"
2) "age"
Demonstrate the beginning and cancellation of things :
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379(TX)> set name cabbage
QUEUED
127.0.0.1:6379(TX)> set age 20
QUEUED
127.0.0.1:6379(TX)> DISCARD
OK
127.0.0.1:6379> keys *
(empty array)
Demonstrating things watch:
Four 、 Things to note
Refers to when the command writing format is incorrect, All commands in the overall transaction will not be executed , Including commands with correct syntax
Indicates that the command format is correct , But when it cannot be executed correctly, Commands that run correctly execute , The wrong command will not be executed and the corresponding data will not be rolled back .

5、 ... and 、 Three characteristics of transaction
- Separate isolation operation
All commands in the transaction are serialized 、 To execute in order . Transaction is in the process of execution , Will not be interrupted by command requests from other clients . - There is no concept of isolation level
The commands in the queue are not actually executed until they are submitted , Because no instruction is actually executed before the transaction is committed - There is no guarantee of atomicity
If a command fails in a transaction , Subsequent orders will still be executed , No rollback
6、 ... and 、 Distributed lock
Business analysis :although redis It's single threaded , But when multiple clients operate on the same data at the same time , How to avoid not being modified at the same time ?Solution :Use expire For lock key Add time limit , Don't release , Then give up the lockCommand line Demo :
127.0.0.1:6379> set name cabbage
OK
127.0.0.1:6379> setnx lock_time 1
(integer) 1
127.0.0.1:6379> EXPIRE lock_time 20
(integer) 1
127.0.0.1:6379> get name
"cabbage"
In this way , Even if a client does not manually release the lock for some reason , It won't cause deadlock . Because the lock is set lock_time The effective time of is 20 second , When 20 Seconds later , Other clients can get the lock 、 Implement business operations 、 Lock released .
Thank you for reading , Progress together , Hee hee ~
边栏推荐
- Unity package project to vs deploy hololens process summary
- 什么是数据中台?数据中台带来了哪些价值?_光点科技
- Teach you how to draw a map with ArcGIS [thermal map]
- 关于链接到其他页面的标题
- Yum package management
- Redis的三种删除策略以及逐出算法
- 3D激光SLAM:LeGO-LOAM论文解读---简介部分
- Linxu 【基本指令】
- Who cares about the safety of the battery when it ignites in a collision? SAIC GM has something to say
- 想画一张版权属于你的图吗?AI作画,你也可以
猜你喜欢

Data mining (data preprocessing) -- Notes

Redis入门二:redhat 6.5安装使用

H5 wechat shooting game source code

漂亮的蓝色背景表单输入框样式

CNN convolutional neural network structure

Unity package project to vs deploy hololens process summary

Want to draw a picture that belongs to you? AI painting, you can also

Introduction to redis II: RedHat 6.5 installation and use

卡通js射击小游戏源码

Read JSON configuration file to realize data-driven testing
随机推荐
CNN convolutional neural network structure
Unity gets which button (toggle) is selected under togglegroup
JS picture hanging style photo wall JS special effect
js飞入js特效弹窗登录框
C reads the data in the CSV file and displays it after importing the DataTable
Database tuning - connection pool optimization
Explain the life cycle function in unity in detail
“当你不再是程序员,很多事会脱离掌控”—— 对话全球最大独立开源公司SUSE CTO...
[complete collection of common ADB commands and their usage (from a comprehensive summary of [wake up on Sunday)]
超大模型工程化实践打磨,百度智能云发布云原生AI 2.0方案
如何平衡SQL中的安全与性能?
H5 wechat shooting game source code
LVS+KeepAlived高可用部署实战应用
Unity performance optimization scheme arrangement
Use of DDR3 (axi4) in Xilinx vivado (1) create an IP core
Dynamic planning: code summary of knapsack problem template
Solve the problem that jupyter cannot import new packages
prometheus配置alertmanager完整过程
MySQL error: specified key was too long; max key length is 767 bytes
Redis review summary