- Redis 支持丰富的数据类型,每种类型的特点及应用场景;
- Redis 支持持久化机制,有 RDB、AOF 等持久化方式;
- Redis 多机数据库的实现,有复制(replication)、哨兵(Sentinel)和集群(cluster)等功能;
- Redis 支持事务,Redis 事务的 ACID 特性;
- Redis 支持的其他独立功能,如发布/订阅、慢日志查询、Lua 脚本、排序及二进制位数组等;
- Redis 不仅仅是缓存中间件,也可以当做 nosql 数据库、消息队列等使用。
当然了,Redis 的优势也有必要说明一下,比如:
- 读写性能很好,Redis 能读的速度是 110000 次/s,写的速度是 81000 次/s,原因有多路复用机制、Resp 协议、单线程、基于内存持久化操作等;
- 数据结构丰富,Redis 不仅支持 strings、lists、hashes、sets、sorted sets 等 5 种基本数据结构,也支持 bitmaps、hyperLogLogs、geospatial 等复杂数据结构;
- 所有操作原子性,所有 Redis 的操作都是原子的,能确保当两个客户同时访问 Redis 服务器时,得到的是更新后的最新值;
- 拥有多样的特性,比如:支持多种键过期删除策略、支持多机数据库的实现(主从复制、哨兵和集群等)、可当做消息队列使用等。
同时,Redis 作为缓存使用时,缺点也很明显,会出现以下几种问题:
- 缓存和数据库双写一致性问题;
- 缓存雪崩问题;
- 缓存击穿问题;
- 缓存的并发竞争问题。
而这些问题的解决方法也是有的,后面重点再梳理~
2、Redis 版本选择及新版本特性
- Redis 使用标准版本标记进行版本控制:major.minor.patchlevel。偶数的版本号表示稳定的版本, 例如 1.2,2.0,2.2,2.4,2.6,2.8,奇数的版本号用来表示非标准版本,例如 2.9.x 是非稳定版本,它的稳定版本是 3.0。【中文官网描述】
- Redis 4.0 在 2017 年 7 月以 GA 的形式发布,新用户应该使用 Redis 5,但 Redis 4 是目前最成熟的版本,并将在明年更新,直到 Redis 6 发布。【中文官网描述】
- Redis 5.0 是 Redis 的第一个版本,引入了新的流数据类型与消费者组,排序集阻塞 pop 操作,RDB 中的 LFU/LRU 信息,Redis -cli 中的集群管理器,活跃的碎片整理 V2, HyperLogLogs 改进和许多其他改进,2018 年 10 月,Redis 5 作为 GA 发布。【英文官网描述】
- Redis 6.0 引入了 SSL,新的 RESP3 协议,acl,客户端缓存,无磁盘副本,I/O 线程,更快的 RDB 加载,新的模块 api 和更多的改进。【英文官网描述】
经过一番比较后,最终选择使用 Redis5.0.6 版本!,下载地址:https://download.redis.io/releases/。
3、Windows 安装和使用 Redis
- redis 是基于单线程的高性能操作,而 redis 需要单线程轮询,Windows 和 Linux 的轮询机制有所不同。
- Windows 使用的是 selector,而 Linux 使用的是 epoll,从性能上来说 epoll 是高于 selector 的,所以 redis 官方推荐使用 linux 版本。不过,window 版本的 redis 是民间大神或者微软修改过的,可以在网上搜到相关的安装包使用,不过最新版本是不可能和官方保持一致的。
4、Linux 安装和使用 Redis
- CentOS 7.9
- redis-5.0.6 【暂用单机版】
<2>. 安装步骤:
先检查安装 wget、gcc 等依赖:
- yum -y install wget
- yum install gcc
在线下载 redis-5.0.6 安装包,或本地上传安装包:
- wget https://download.redis.io/releases/redis-5.0.6.tar.gz
下载目录解压安装包:
- tar -zxvf redis-5.0.6.tar.gz
将解压出来 redis-5.0.6 移动到/usr/local/,并进入 redis-5.0.6 目录:
- mv redis-5.0.6 /usr/local/ && cd /usr/local/redis-5.0.6
编译安装:
- make MALLOC=libc
编译后产生一个新的 src 目录,将里面文件 cp 到/usr/local/bin/:
- cp -rf src/redis-server /usr/local/bin/
- cp -rf src/redis-cli /usr/local/bin/
进入 src 后并安装:
- cd src && make install
<3>. 启动 Redis 的三种方式:
第一种方式:直接启动,但需要一直打开当前窗口,很不方便!
[root@localhost src]# ./redis-server
启动效果:
第二种方式:后台进程方式启动,需要指定配置文件启动
# 修改 redis.conf 参数配置,yes 表示后台启动服务
[root@localhost redis-5.0.6]# sed -i ‘s/daemonize no/daemonize yes/’ /usr/local/redis-5.0.6/redis.conf
# 后台启动时,指定配置文件 redis.conf
[root@localhost redis-5.0.6]# ./src/redis-server redis.conf
6326:C 14 Nov 2021 15:02:44.601 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6326:C 14 Nov 2021 15:02:44.601 # Redis version=5.0.6, bits=64, commit=00000000, modified=0, pid=6326, just started
6326:C 14 Nov 2021 15:02:44.601 # Configuration loaded
查看进程:
第三种方式:以 redis 服务形式启动,开启自启动
# 在/etc 目录下新建 redis 目录
[root@localhost etc]# mkdir redis
# 将/usr/local/redis-5.0.6/redis.conf 文件复制一份到/etc/redis 目录下,命名为 6379.conf
[root@localhost redis]# cp /usr/local/redis-5.0.6/redis.conf /etc/redis/6379.conf
# 将 redis 的启动脚本复制一份放到/etc/init.d 目录下
[root@localhost init.d]# cp /usr/local/redis-5.0.6/utils/redis_init_script /etc/init.d/redisd
# 设置 redis 开机自启动
[root@localhost init.d]# chkconfig redisd on
# 如果不支持 chkconfig ,使用 vim 编辑 redisd 文件,在第一行加入如下两行注释,保存退出
[root@localhost init.d]# vi redisd
# redis 服务必须在运行级 2,3,4,5 下被启动或关闭,启动的优先级是 90,关闭的优先级是 10。
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
# 重新开机自启动
[root@localhost init.d]# chkconfig redisd on
# 现在可以以服务的形式启动和关闭 redis 了
[root@localhost ~]# service redisd start
启动效果:
<4>. 关闭 Redis:
- 如果是后台启动的话,关闭 redis 使用:kill -9 进程号 ;
- 如果是服务形式启动的话,关闭 redis 使用:service redisd stop ;
<5>. 修改配置文件(可选):
- 上面已经通过 sed 命令修改了daemonize yes,表示后台自启动;
- 如果需要远程调用 redis 服务,要注释掉 bind 127.0.0.1 ,把 protected-mode 改为 no;
<6>. Redis 的读写性能测试:
模拟 N 个客户端同时发出 M 个请求,有两种方式:
- 直接通过 redis-benchmark 命令,测试结果如下:(每秒请求数)
[root@localhost /]# redis-benchmark -n 10000 -q PING_INLINE: 133333.33 requests per second PING_BULK: 126582.27 requests per second SET: 131578.95 requests per second GET: 136986.30 requests per second INCR: 131578.95 requests per second LPUSH: 140845.06 requests per second RPUSH: 133333.33 requests per second LPOP: 129870.13 requests per second RPOP: 133333.33 requests per second SADD: 135135.14 requests per second HSET: 136986.30 requests per second SPOP: 136986.30 requests per second LPUSH (needed to benchmark LRANGE): 140845.06 requests per second LRANGE_100 (first 100 elements): 74626.87 requests per second LRANGE_300 (first 300 elements): 31746.03 requests per second LRANGE_500 (first 450 elements): 24038.46 requests per second LRANGE_600 (first 600 elements): 18587.36 requests per second MSET (10 keys): 121951.22 requests per second
- 也可以通过 redis-5.0.6/src/redis-benchmark 脚本,测试结果如下:(每秒请求数)
[root@localhost src]# ./redis-benchmark -n 10000 -q PING_INLINE: 129870.13 requests per second PING_BULK: 125000.00 requests per second SET: 126582.27 requests per second GET: 133333.33 requests per second INCR: 135135.14 requests per second LPUSH: 133333.33 requests per second RPUSH: 125000.00 requests per second LPOP: 135135.14 requests per second RPOP: 135135.14 requests per second SADD: 125000.00 requests per second HSET: 147058.81 requests per second SPOP: 131578.95 requests per second LPUSH (needed to benchmark LRANGE): 135135.14 requests per second LRANGE_100 (first 100 elements): 70422.53 requests per second LRANGE_300 (first 300 elements): 31055.90 requests per second LRANGE_500 (first 450 elements): 23640.66 requests per second LRANGE_600 (first 600 elements): 19531.25 requests per second MSET (10 keys): 116279.06 requests per second
<7>.使用 Redis 客户端进行简单的一些操作:
使用 redi-cli 客户端通过命令操作 redis,这里先热身一下:
root@localhost src]# ./redis-cli
127.0.0.1:6379>
127.0.0.1:6379> set msg helloworld
OK
127.0.0.1:6379> get msg
"helloworld"
127.0.0.1:6379> set cnt 100
OK
127.0.0.1:6379> incr cnt
(integer) 101
127.0.0.1:6379> incrby cnt 10
(integer) 111
127.0.0.1:6379> mset apple 6.82 orange 3.5 bananar 9.9
OK
127.0.0.1:6379>
127.0.0.1:6379> mget apple orange bananar
1) "6.82"
2) "3.5"
3) "9.9"
127.0.0.1:6379> rpush list0 java
(integer) 1
127.0.0.1:6379> rpush list0 python
(integer) 2
127.0.0.1:6379> rpush list0 go
(integer) 3
127.0.0.1:6379> lrange list0 0 -1
1) "java"
2) "python"
3) "go"
127.0.0.1:6379>
<8>.Redis 可视化连接工具:
最后,总结下安装,配置及使用过程中遇到的一些问题~
错误总结:
-
在 winscp 远程连接 Linux 时,连接不上指定的主机,经过排查是本机没有开启相应的服务,如下:
- 执行”service redisd start”命令时,出现提示:/var/run/redis_6379.pid exists, process is already running or crashed,删除这个 pid 即可
rm -rf /var/run/redis_6379.pid
来源:https://mp.weixin.qq.com/s/uyTFSrWf9MHYKEinWmZ2dg