认识
官网:https://redis.io/open-source/
文档:https://redis.io/docs/latest/operate/oss_and_stack/
仓库:https://github.com/redis/redis
For developers, who are building real-time data-driven applications, Redis is the preferred, fastest, and most feature-rich cache, data structure server, and document and vector query engine. 简而言之,Redis 是个开源(BSD 许可)、内存数据结构的存储,可以作为数据库、缓存、消息代理来使用。
组成
命令程序 and 配置文件
redis-server – 服务端程序
redis-cli – 客户端程序
redis-sentinel – 到 redis-server 的软链接,使 redis 运行于 Sentinel 模式,官方推荐的高可用性(HA)解决方案
redis-benchmark – 用于 Redis 的基准测试
redis-check-aof – 用于 Redis AOF 文件的检查和修复
redis-check-rdb – 用于 Redis RDB 文件的检查
性质
它支持如字符串,散列,列表,集合,带有范围查询的排序集,位图,超级日志,具有半径查询的地理空间索引,流等数据结构。
Redis 具有内置复制,Lua 脚本,LRU 驱逐,事务,不同级别的磁盘持久性,并通过 Redis Sentinel 提供高可用性,并使用 Redis Cluster 自动分区。
您可以对某些数据类型运行“原子操作”。例如,追加字符串;递增哈希值;将元素推到 List 中;计算 Set 的交集、并集、差异;或者在 Sorted set 中获得排名最高的成员。
为了实现其出色的性能,Redis 使用“内存数据集”。根据您的使用情况,您可以通过每隔一段时间将数据集转储到磁盘,或通过将每个命令附加到日志的方式来保留它。如果您只需要功能丰富的网络内存缓存,则可以选择禁用数据持久化。
Redis 还支持「主从异步复制」,具有非常快速的非阻塞首次同步,自动重新连接以及在网络分割上的部分重新同步。
其他的特性还有:
- 事务支持
- 发布 / 订阅
- 执行 Lua 脚本
- 生存时间
- LRU 驱逐
- 自动故障转移
可以在多种编程语言中使用 Redis 服务。
Redis 是用 ANSI C 编写的,适用于大多数 POSIX 系统,如 Linux,*BSD,OS X,没有外部依赖性。Linux 和 OS X 是 Redis 开发和测试的两个操作系统,建议使用 Linux 进行部署。在 Solaris 衍生系统(如 SmartOS)中也可以运行 Redis 服务,但尽量支持。Windows 版本没有官方支持,但 Microsoft 开发并维护了 Redis 的 Win-64 版本。
构建
服务 | 部署 | 该笔记将记录:如何快速部署用于学习和测试的 Redis 服务,以及相关问题的解决办法。
容器镜像
https://hub.docker.com/_/redis
[I] SRC: docker.io/library/redis:6
[I] DST: ccr.ccs.tencentyun.com/d3rm-3rd/docker.io_library_redis:6
with Manifest on Kubernetes
---
apiVersion: apps/v1 # API version
kind: Deployment
metadata:
name: redis-master # Unique name for the deployment
labels:
app: redis # Labels to be applied to this deployment
spec:
selector:
matchLabels: # This deployment applies to the Pods matching these labels
app: redis
role: master
tier: backend
replicas: 1 # Run a single pod in the deployment
template: # Template for the pods that will be created by this deployment
metadata:
labels: # Labels to be applied to the Pods in this deployment
app: redis
role: master
tier: backend
spec: # Spec for the container which will be run inside the Pod.
containers:
- name: master
image: redis
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 6379
---
apiVersion: v1
kind: Service # Type of Kubernetes resource
metadata:
name: redis-master # Name of the Kubernetes resource
labels: # Labels that will be applied to this resource
app: redis
role: master
tier: backend
spec:
ports:
- port: 6379 # Map incoming connections on port 6379 to the target port 6379 of the Pod
targetPort: 6379
selector: # Map any Pod with the specified labels to this service
app: redis
role: master
tier: backend
kubectl apply -f ./
on Docker with Docker Compose
https://redis.io/docs/latest/operate/oss_and_stack/install/install-stack/docker/
version: '3.8'
services:
redis:
image: docker.io/library/redis:4.0
restart: always
ports:
- '8170:6379'
command: redis-server --save 20 1 --loglevel warning --requirepass VvChXa5tOjcBEq
volumes:
- ./data:/data
docker-compose up -d
with YUM on CentOS
禁用 Transparent Huge Pages 参数
linux – disable transparent hugepages – Unix & Linux Stack Exchange
How to disable Transparent Huge pages (THP) on CentOS / RHEL 7? – Ucartz Online Pvt Ltd
echo never > /sys/kernel/mm/transparent_hugepage/enabled
vim /etc/default/grub ... GRUB_CMDLINE_LINUX="... transparent_hugepage=never" ... grub2-mkconfig -o /boot/grub2/grub.cfg # for On BIOS-based machines grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg # On UEFI-based machines # Reboot the system and verify option are in effect. shutdown -r now cat /proc/cmdline
on CentOS 7.0
# 安装服务 yum install -y redis.x86_64 # 启动服务,并设置为自启动 systemctl start redis.service systemctl enable redis.service
on AWS / with ElasticCache Redis OSS
Design your own cache / Cluster cache
访问测试
# redis-cli 127.0.0.1:6379> SET k4nz demo OK 127.0.0.1:6379> GET k4nz "demo" 127.0.0.1:6379> DEL k4nz (integer) 1 127.0.0.1:6379> GET k4nz (nil)
连接集群
kubectl create deployment redis-cli –image=redis:7.4.0 — sleep infinity
kubectl create deployment redis-cli –image=redis:5 — sleep infinity
[I] SRC: docker.io/library/redis:5
[I] DST: ccr.ccs.tencentyun.com/d3rm-3rd/docker.io_library_redis:5
[I] SRC: docker.io/library/redis:6
[I] DST: ccr.ccs.tencentyun.com/d3rm-3rd/docker.io_library_redis:6
应用
服务使用
#1 会话共享
和 Memcached 类似,Redis 最明显的用例之一是将其用作会话缓存。
与其他会话存储相比,使用 Redis 的优势是 Redis 提供了数据的持久化存储。
#2 数据缓存
缓存页面,提供页面的加载速度。即使发生了重启,通过持久化存储,页面的加载速度也不会下降很多。
#3 消息队列
利用 Redis 的内存存储引擎来进行 list 和 set 操作,使其成为一个用于消息队列的平台。
#4 排行榜 / 统计功能
它支持数值增量和减量。同时也可以进行排序取值操作。
#5 发布 / 订阅
最后是 Redis 的 Pub/Sub 功能。Pub/Sub 有非常多的用例。我见过人们将它用于社交网络连接,触发基于 Pub/Sub 事件的脚本,甚至是使用 Redis Pub / Sub 构建的聊天系统!
改进
[Sol] READONLY You can’t write against a read only replica.
READONLY You can’t write against a read only replica. · Issue #1643 · go-redis/redis · GitHub
问题描述:执行 SET 命令,提示 READONLY You can’t write against a read only replica. 信息;
原因分析:在我们场景,Redis 单实例,无主从。从 INFO 中,我们看到当前节点的 ROLE 为 Slave 角色。我们猜测是有人调整主从关系而引发的错误。
解决方案:redis-cli -h 127.0.0.1 -p 6379 slaveof no one
[Sol] Fatal: Can’t initialize Background Jobs
Redis: Fatal: Can’t initialize Background Jobs – Feedback & Bug Reports – CircleCI Discuss
docker redis:6.0 run error: Fatal: Can’t initialize Background Jobs. · Issue #12364 · redis/redis
# docker run --ulimit nofile=1024000:1024000 redis:7.0 1:C 13 Dec 2023 02:20:26.744 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1:C 13 Dec 2023 02:20:26.745 # Redis version=7.0.14, bits=64, commit=00000000, modified=0, pid=1, just started 1:C 13 Dec 2023 02:20:26.745 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 1:M 13 Dec 2023 02:20:26.745 * monotonic clock: POSIX clock_gettime 1:M 13 Dec 2023 02:20:26.746 * Running mode=standalone, port=6379. 1:M 13 Dec 2023 02:20:26.746 # Server initialized 1:M 13 Dec 2023 02:20:26.746 # Fatal: Can't initialize Background Jobs.
更换镜像,使用 7.0.0, 7.0.10, 6.2.6 都能成功运行。
参考
https://redis.io
Introduction to Redis
Top 5 Redis Use Cases
11 Common Web Use Cases Solved in Redis
Redis Use Cases