当前位置:网站首页>redis入门-1-redis概念和基础
redis入门-1-redis概念和基础
2022-08-02 14:05:00 【weixin_45773632】
文章目录
redis知识体系
什么是redis
- Redis是一款内存高速缓存数据库。Redis全称为:Remote Dictionary Server(远程数据服务),使用C语言编写,Redis是一个key-value存储系统(键值存储系统),支持丰富的数据类型,如:String、list、set、zset、hash。
- Redis是一种支持key-value等多种数据结构的存储系统。可用于缓存,事件发布或订阅,高速队列等场景。支持网络,提供字符串,哈希,列表,队列,集合结构直接存取,基于内存,可持久化。
为什么要使用Redis
一个产品的使用场景肯定是需要根据产品的特性,先列举一下Redis的特点:
- 读写性能优异 Redis能读的速度是110000次/s,写的速度是81000次/s (测试条件见下一节)。
- 数据类型丰富 Redis支持二进制案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 数据类型操作。
- 原子性 Redis的所有操作都是原子性的,同时Redis还支持对几个操作全并后的原子性执行。
- 丰富的特性 Redis支持 publish/subscribe, 通知, key 过期等特性。
- 持久化 Redis支持RDB, AOF等持久化方式
- 发布订阅 Redis支持发布/订阅模式
- 分布式 Redis Cluster
redis-benchmark 性能测试 (还受到服务器性能影响)
语法:
[[email protected] bin]# redis-benchmark --help
Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests>] [-k <boolean>]
-h <hostname> Server hostname (default 127.0.0.1)
-p <port> Server port (default 6379)
-s <socket> Server socket (overrides host and port)
-a <password> Password for Redis Auth
--user <username> Used to send ACL style 'AUTH username pass'. Needs -a.
-c <clients> 线程数(多个连接) (default 50)
-n <requests> 请求数量 (default 100000)
-d <size> Data size of SET/GET value in bytes (default 3)
--dbnum <db> SELECT the specified db number (default 0)
--threads <num> Enable multi-thread mode.
--cluster Enable cluster mode.
--enable-tracking Send CLIENT TRACKING on before starting benchmark.
-k <boolean> 1=keep alive 0=reconnect (default 1)
-r <keyspacelen> Use random keys for SET/GET/INCR, random values for SADD
Using this option the benchmark will expand the string __rand_int__
inside an argument with a 12 digits number in the specified range
from 0 to keyspacelen-1. The substitution changes every time a command
is executed. Default tests use this to hit random keys in the
specified range.
-P <numreq> Pipeline <numreq> requests. Default 1 (no pipeline).
-e If server replies with errors, show them on stdout.
(no more than 1 error per second is displayed)
-q Quiet. Just show query/sec values
--precision Number of decimal places to display in latency output (default 0)
--csv Output in CSV format
-l Loop. Run the tests forever
-t <tests> Only run the comma separated list of tests. The test
names are the same as the ones produced as output.
-I Idle mode. Just open N idle connections and wait.
测试:
[[email protected] bin]# redis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 100000
====== PING_INLINE ======
100000 requests completed in 1.42 seconds # 100000个请求
100 parallel clients # 100个并发客户端
3 bytes payload # 每次写入3个字节数据
keep alive: 1 # 单机
host configuration "save": 900 1 300 10 60 10000
host configuration "appendonly": no
multi-thread: no
0.00% <= 0.2 milliseconds
0.07% <= 0.3 milliseconds
0.44% <= 0.4 milliseconds
1.06% <= 0.5 milliseconds
2.92% <= 0.6 milliseconds
46.30% <= 0.7 milliseconds
80.05% <= 0.8 milliseconds
88.17% <= 0.9 milliseconds
91.94% <= 1.0 milliseconds
94.14% <= 1.1 milliseconds
95.20% <= 1.2 milliseconds
95.99% <= 1.3 milliseconds
96.60% <= 1.4 milliseconds
97.08% <= 1.5 milliseconds
97.50% <= 1.6 milliseconds
97.88% <= 1.7 milliseconds
98.25% <= 1.8 milliseconds
98.61% <= 1.9 milliseconds
98.95% <= 2 milliseconds
100.00% <= 2 milliseconds
70571.62 requests per second
====== PING_BULK ======
100000 requests completed in 1.47 seconds
100 parallel clients
3 bytes payload
keep alive: 1
host configuration "save": 900 1 300 10 60 10000
host configuration "appendonly": no
multi-thread: no
83.95% <= 1 milliseconds
98.14% <= 2 milliseconds
99.86% <= 3 milliseconds
99.94% <= 4 milliseconds
100.00% <= 4 milliseconds
67934.78 requests per second
====== SET ======
100000 requests completed in 1.53 seconds
100 parallel clients
3 bytes payload
keep alive: 1
host configuration "save": 900 1 300 10 60 10000
host configuration "appendonly": no
multi-thread: no
73.92% <= 1 milliseconds
98.02% <= 2 milliseconds
99.87% <= 3 milliseconds
99.99% <= 4 milliseconds
100.00% <= 4 milliseconds
65274.15 requests per second
Redis的使用场景
redis应用场景总结redis平时我们用到的地方蛮多的,下面就了解的应用场景做个总结:
热点数据的缓存 缓存是Redis最常见的应用场景,之所有这么使用,主要是因为Redis读写性能优异。而且逐渐有取代memcached,成为首选服务端缓存的组件。而且,Redis内部是支持事务的,在使用时候能有效保证数据的一致性。
作为缓存使用时,一般有两种方式保存数据:
- 读取前,先去读Redis,如果没有数据,读取数据库,将数据拉入Redis。
- 插入数据时,同时写入Redis。
方案一: 实施起来简单,但是有两个需要注意的地方:
- 避免缓存击穿。(数据库没有就需要命中的数据,导致Redis一直没有数据,而一直命中数据库。)
- 数据的实时性相对会差一点。
方案二: 数据实时性强,但是开发时不便于统一处理。
- 当然,两种方式根据实际情况来适用。如:方案一适用于对于数据实时性要求不是特别高的场景。方案二适用于字典表、数据量不大的数据存储。
1. 限时业务的运用
redis中可以使用expire命令设置一个键的生存时间,到时间后redis会删除它。利用这一特性可以运用在限时的优惠活动信息、手机验证码等业务场景。
2. 计数器相关问题
redis由于incrby命令可以实现原子性的递增,所以可以运用于高并发的秒杀活动、分布式序列号的生成、具体业务还体现在比如限制一个手机号发多少条短信、一个接口一分钟限制多少请求、一个接口一天限制调用多少次等等。
3. 分布式锁
这个主要利用redis的setnx命令进行,setnx:"set if not exists"就是如果不存在则成功设置缓存同时返回1,否则返回0 ,这个特性在很多后台中都有所运用,因为我们服务器是集群的,定时任务可能在两台机器上都会运行,所以在定时任务中首先 通过setnx设置一个lock,如果成功设置则执行,如果没有成功设置,则表明该定时任务已执行。 当然结合具体业务,我们可以给这个lock加一个过期时间,比如说30分钟执行一次的定时任务,那么这个过期时间设置为小于30分钟的一个时间就可以,这个与定时任务的周期以及定时任务执行消耗时间相关。
在分布式锁的场景中,主要用在比如秒杀系统等。
4. 延时操作
比如在订单生产后我们占用了库存,10分钟后去检验用户是否真正购买,如果没有购买将该单据设置无效,同时还原库存。 由于redis自2.8.0之后版本提供Keyspace Notifications功能,允许客户订阅Pub/Sub频道,以便以某种方式接收影响Redis数据集的事件。 所以我们对于上面的需求就可以用以下解决方案,我们在订单生产时,设置一个key,同时设置10分钟后过期, 我们在后台实现一个监听器,监听key的实效,监听到key失效时将后续逻辑加上。
当然我们也可以利用rabbitmq、activemq等消息中间件的延迟队列服务实现该需求。
5. 排行榜相关问题
关系型数据库在排行榜方面查询速度普遍偏慢,所以可以借助redis的SortedSet进行热点数据的排序。
比如点赞排行榜,做一个SortedSet, 然后以用户的openid作为上面的username, 以用户的点赞数作为上面的score, 然后针对每个用户做一个hash, 通过zrangebyscore就可以按照点赞数获取排行榜,然后再根据username获取用户的hash信息。
6. 点赞、好友等相互关系的存储
Redis 利用集合的一些命令,比如求交集、并集、差集等。 在微博应用中,每个用户关注的人存在一个集合中,就很容易实现求两个人的共同好友功能。
7. 简单队列
由于Redis有list push和list pop这样的命令,所以能够很方便的执行队列操作。
边栏推荐
猜你喜欢
Briefly write about the use and experience of PPOCRLabel
函数递归和动态内存初识
Unit 13 Mixing in View Base Classes
window10 lower semi-automatic labeling
yolov5 improvement (1) Add attention focus mechanism
C语言日记 3 常量
存储系统Cache(知识点+例题)
Unit 8 Middleware
YOLOv7 uses cloud GPU to train its own dataset
The specific operation process of cloud GPU (Hengyuan cloud) training
随机推荐
[ROS] (04) Detailed explanation of package.xml
云GPU(恒源云)训练的具体操作流程
Briefly write about the use and experience of PPOCRLabel
函数递归和动态内存初识
ABP,kendo后台接口,新增,查询
Programming Specifications - LiteOS
我理解的学习金字塔
Verilog Learning Series
无人驾驶综述:国外国内发展历程
基于ThinkPHP6.0 - 宝塔搭建漫画CMS管理系统源码实测
VS Code远程开发及免密配置
华为防火墙
The IDEA of packaged jar package
MySQL知识总结 (六) MySQL调优
mysql
无人驾驶综述:等级划分
ToF相机从Camera2 API中获取DEPTH16格式深度图
华为防火墙
drf view component
华为路由交换