当前位置:网站首页>简短截说阐述redis中事务的使用
简短截说阐述redis中事务的使用
2022-08-08 11:24:00 【用户9127725】
我们知道,在关系型数据库中,比如mysql,如果要使用事务,首先向数据库服务器发送 BEGIN ,然后执行各个相互一致的写操作和读操作,最后,用户可以选择发送 COMMIT 来确认之前所做的修改,或者发送 ROLLBACK 来放弃那些修改。
同样, Redis 中也有简单的方法处理一连串相互一致的读操作和写操作。首先是以 MULTI 命令开始事务,后续跟着一连串命令,最后以 EXEC 结束事务或者以 DISCARD 命令撤销所有命令并结束事务。
但是redis事务和mysql事务最重要的一点区别是,redis事务不管指令正确或者错误,都会执行,中途遇见错误指令也会继续执行后面的指令,Redis并没有像mysql那样的事务回滚机制。mysql事务中如果执行过程中发生了错误不仅后面的sql语句不会执行,还会进行数据回滚,这是二者事务的最大区别。Redis的事务出错需要开发人员自己进行数据回滚等操作。
在翻阅了redis官方手册以后,官方对此的解释是:If you have a relational databases background, the fact that Redis commands can fail during a transaction, but still Redis will execute the rest of the transaction instead of rolling back, may look odd to you. However there are good opinions for this behavior: Redis commands can fail only if called with a wrong syntax (and the problem is not detectable during the command queueing), or against keys holding the wrong data type: this means that in practical terms a failing command is the result of a programming errors, and a kind of error that is very likely to be detected during development, and not in production. Redis is internally simplified and faster because it does not need the ability to roll back. An argument against Redis point of view is that bugs happen, however it should be noted that in general the roll back does not save you from programming errors. For instance if a query increments a key by 2 instead of 1, or increments the wrong key, there is no way for a rollback mechanism to help. Given that no one can save the programmer from his or her errors, and that the kind of errors required for a Redis command to fail are unlikely to enter in production, we selected the simpler and faster approach of not supporting roll backs on errors.
大白话的意思就是:redis的作者认为,当事务的执行时,一般发生错误都是因为业务编程错误造成的,这种错误通常只会出现在开发环境中,而基本很少会在实际的生产环境中出现(因为这种业务错误都会在测试的时候消灭),所以他认为没有必要为 Redis 开发事务自动回滚功能,这和Redis追求的简单高效的设计主旨不符合。
而mysql恰恰相反,mysql认为数据库有必要也有责任处理事务中报错的情况,所以mysql有自动回滚的功能。
在redis中使用事务:
liuyue:~ liuyue$ redis-cli
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set test 123
QUEUED
127.0.0.1:6379> exec
1) OK
127.0.0.1:6379> get test
"123"
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set test 456
QUEUED
127.0.0.1:6379> discard
OK
127.0.0.1:6379> get test
"123"
127.0.0.1:6379>
liuyue:~ liuyue$ clear在python中操作redis事务
#导包
import redis
#定义ip
host = 'localhost'
#建立服务连接
r = redis.Redis(host=host)
pipe = r.pipeline()
#开启事务
pipe.multi()
#存储子命令
pipe.set('key2', 4)
#执行事务
pipe.execute()
print(r.get('key2'))边栏推荐
- 键值数据库是将什么作为标识符的呢?
- 【力扣】两数相加
- Postman使用简单演示
- JVM的GC讲解及调优
- #yyds干货盘点#【愚公系列】2022年08月 Go教学课程 005-变量
- CentOS MySQL体系管理
- oracle存储过程中表名变量的异常
- 便利贴--48{再次,适配屏幕宽高class}
- 如何使用shell来进行版本管理-以iptables为例
- Jingdong, zhang, director of the cloud wireless products division treasure jingdong cloud wireless treasure close relationship with the open source | the great god, open source BUFF gain strategy revi
猜你喜欢
随机推荐
读取SolidWorks文档中的属性,生成PDF(工具开发)
轻量级接口自动化框架(jmeter+ant+jenkins)
JVM的GC讲解及调优
#yyds干货盘点#【愚公系列】2022年08月 Go教学课程 005-变量
MySQL安装及使用
[Horizon Rising Sun X3 Trial Experience] WIFI connection, SSH login, TogetherROS installation (section 2)
LeetCode_14_最长公共前缀
vim /etc/profile 写入时 出现 E121:无法打开并写入文件解决方案
使用ApacheBench来对美多商城的秒杀功能进行高并发压力测试
图数据库是使用什么作为数据模型的呢?
搞清楚系统到底怎样支撑高并发以及架构图的绘制(面试向)
Oracle ASM磁盘组使用新存储替换旧存储方案
Alibaba微服务组件Nacos注册中心
分布式系统设计策略
一文读懂配置管理(CM)
带你深入理解3.4.2的版本更新,对用户带来了什么?
#yyds Dry Goods Inventory#【Yugong Series】August 2022 Go Teaching Course 005-Variable
STM32的内存管理相关(内存架构,内存管理,map文件分析)
模式识别 学习笔记:第六章 其他分类方法 (持续更新中。。。)
《STM32MP1 M4裸机CubeIDE开发指南》第二十四章 DAC实验









