Redis缓存

简介

Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理。它支持字符串、哈希表、列表、集合、有序集合,位图,hyperloglogs等数据类型。内置复制、Lua脚本、LRU收回、事务以及不同级别磁盘持久化功能,同时通过Redis Sentinel提供高可用,通过Redis Cluster提供自动分区。

作用

缓存

在高并发场景下,数据库查询将会触发磁盘IO操作,磁盘IO将会成为其中一个瓶颈,有些数据并不是频繁修改,所以并不需要到到数据库中查询。

分布式锁

在分布式系统中,可以为系统提供分布式锁服务

  • 使用setnx指令
  • 添加守护线程,延长过期时间

    Redisson方案

    Redisson是架设在Redis基础上的一个Java驻内存数据网格(In-Memory Data Grid)。充分的利用了Redis键值数据库提供的一系列优势,基于Java实用工具包中常用接口,为使用者提供了一系列具有分布式特性的常用工具类。使得原本作为协调单机多线程并发程序的工具包获得了协调分布式多机多线程并发系统的能力,大大降低了设计和研发大规模分布式系统的难度。同时结合各富特色的分布式服务,更进一步简化了分布式环境中程序相互之间的协作。

    zookeeper方案(推荐)

    ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务

    消息队列

常规命令

GET key
summary: Get the value of a key
since: 1.0.0

GETDEL key
summary: Get the value of a key and delete the key
since: 6.2.0

GETEX key [EX seconds|PX milliseconds|EXAT timestamp|PXAT milliseconds-timestamp|PERSIST]
summary: Get the value of a key and optionally set its expiration
since: 6.2.0

MGET key [key …]
summary: Get the values of all the given keys
since: 1.0.0

MSET key value [key value …]
summary: Set multiple keys to multiple values
since: 1.0.1

MSETNX key value [key value …]
summary: Set multiple keys to multiple values, only if none of the keys exist
since: 1.0.1

PSETEX key milliseconds value
summary: Set the value and expiration in milliseconds of a key
since: 2.6.0

SET key value [EX seconds|PX milliseconds|EXAT timestamp|PXAT milliseconds-timestamp|KEEPTTL] [NX|XX] [GET]
summary: Set the string value of a key
since: 1.0.0

SETEX key seconds value
summary: Set the value and expiration of a key
since: 2.0.0

SETNX key value
summary: Set the value of a key, only if the key does not exist
since: 1.0.0

数据类型

string

字符类型

APPEND key value
summary: Append a value to a key
since: 2.0.0

GETRANGE key start end
summary: Get a substring of the string stored at a key
since: 2.4.0

GETSET key value
summary: Set the string value of a key and return its old value
since: 1.0.0

SETRANGE key offset value
summary: Overwrite part of a string at key starting at the specified offset
since: 2.2.0

STRALGO LCS algo-specific-argument [algo-specific-argument …]
summary: Run algorithms (currently LCS) against strings
since: 6.0.0

STRLEN key
summary: Get the length of the value stored in a key
since: 2.2.0

数值类型

DECR key
summary: Decrement the integer value of a key by one
since: 1.0.0

DECRBY key decrement
summary: Decrement the integer value of a key by the given number
since: 1.0.0

INCR key
summary: Increment the integer value of a key by one
since: 1.0.0

INCRBY key increment
summary: Increment the integer value of a key by the given amount
since: 1.0.0

INCRBYFLOAT key increment
summary: Increment the float value of a key by the given amount
since: 2.6.0

bitmaps

GETBIT key offset
summary: Returns the bit value at offset in the string value stored at key
since: 2.2.0

BITCOUNT key [start end]
summary: Count set bits in a string
since: 2.6.0

BITFIELD key [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP|SAT|FAIL]
summary: Perform arbitrary bitfield integer operations on strings
since: 3.2.0

BITOP operation destkey key [key …]
summary: Perform bitwise operations between strings
since: 2.6.0

BITPOS key bit [start] [end]
summary: Find first bit set or clear in a string
since: 2.8.7

SETBIT key offset value
summary: Sets or clears the bit at offset in the string value stored at key
since: 2.2.0

hashes

HDEL key field [field …]
summary: Delete one or more hash fields
since: 2.0.0

HEXISTS key field
summary: Determine if a hash field exists
since: 2.0.0

HGET key field
summary: Get the value of a hash field
since: 2.0.0

HGETALL key
summary: Get all the fields and values in a hash
since: 2.0.0

HINCRBY key field increment
summary: Increment the integer value of a hash field by the given number
since: 2.0.0

HINCRBYFLOAT key field increment
summary: Increment the float value of a hash field by the given amount
since: 2.6.0

HKEYS key
summary: Get all the fields in a hash
since: 2.0.0

HLEN key
summary: Get the number of fields in a hash
since: 2.0.0

HMGET key field [field …]
summary: Get the values of all the given hash fields
since: 2.0.0

HMSET key field value [field value …]
summary: Set multiple hash fields to multiple values
since: 2.0.0

HRANDFIELD key [count [WITHVALUES]]
summary: Get one or multiple random fields from a hash
since: 6.2.0

HSCAN key cursor [MATCH pattern] [COUNT count]
summary: Incrementally iterate hash fields and associated values
since: 2.8.0

HSET key field value [field value …]
summary: Set the string value of a hash field
since: 2.0.0

HSETNX key field value
summary: Set the value of a hash field, only if the field does not exist
since: 2.0.0

HSTRLEN key field
summary: Get the length of the value of a hash field
since: 3.2.0

HVALS key
summary: Get all the values in a hash
since: 2.0.0

lists

BLMOVE source destination LEFT|RIGHT LEFT|RIGHT timeout
summary: Pop an element from a list, push it to another list and return it; or block until one is available
since: 6.2.0

BLPOP key [key …] timeout
summary: Remove and get the first element in a list, or block until one is available
since: 2.0.0

BRPOP key [key …] timeout
summary: Remove and get the last element in a list, or block until one is available
since: 2.0.0

BRPOPLPUSH source destination timeout
summary: Pop an element from a list, push it to another list and return it; or block until one is available
since: 2.2.0

LINDEX key index
summary: Get an element from a list by its index
since: 1.0.0

LINSERT key BEFORE|AFTER pivot element
summary: Insert an element before or after another element in a list
since: 2.2.0

LLEN key
summary: Get the length of a list
since: 1.0.0

LMOVE source destination LEFT|RIGHT LEFT|RIGHT
summary: Pop an element from a list, push it to another list and return it
since: 6.2.0

LPOP key [count]
summary: Remove and get the first elements in a list
since: 1.0.0

LPOS key element [RANK rank] [COUNT num-matches] [MAXLEN len]
summary: Return the index of matching elements on a list
since: 6.0.6

LPUSH key element [element …]
summary: Prepend one or multiple elements to a list
since: 1.0.0

LPUSHX key element [element …]
summary: Prepend an element to a list, only if the list exists
since: 2.2.0

LRANGE key start stop
summary: Get a range of elements from a list
since: 1.0.0

LREM key count element
summary: Remove elements from a list
since: 1.0.0

LSET key index element
summary: Set the value of an element in a list by its index
since: 1.0.0

LTRIM key start stop
summary: Trim a list to the specified range
since: 1.0.0

RPOP key [count]
summary: Remove and get the last elements in a list
since: 1.0.0

RPOPLPUSH source destination
summary: Remove the last element in a list, prepend it to another list and return it
since: 1.2.0

RPUSH key element [element …]
summary: Append one or multiple elements to a list
since: 1.0.0

RPUSHX key element [element …]
summary: Append an element to a list, only if the list exists
since: 2.2.0

sets

SADD key member [member …]
summary: Add one or more members to a set
since: 1.0.0

SCARD key
summary: Get the number of members in a set
since: 1.0.0

SDIFF key [key …]
summary: Subtract multiple sets
since: 1.0.0

SDIFFSTORE destination key [key …]
summary: Subtract multiple sets and store the resulting set in a key
since: 1.0.0

SINTER key [key …]
summary: Intersect multiple sets
since: 1.0.0

SINTERSTORE destination key [key …]
summary: Intersect multiple sets and store the resulting set in a key
since: 1.0.0

SISMEMBER key member
summary: Determine if a given value is a member of a set
since: 1.0.0

SMEMBERS key
summary: Get all the members in a set
since: 1.0.0

SMISMEMBER key member [member …]
summary: Returns the membership associated with the given elements for a set
since: 6.2.0

SMOVE source destination member
summary: Move a member from one set to another
since: 1.0.0

SPOP key [count]
summary: Remove and return one or multiple random members from a set
since: 1.0.0

SRANDMEMBER key [count]
summary: Get one or multiple random members from a set
since: 1.0.0

SREM key member [member …]
summary: Remove one or more members from a set
since: 1.0.0

SSCAN key cursor [MATCH pattern] [COUNT count]
summary: Incrementally iterate Set elements
since: 2.8.0

SUNION key [key …]
summary: Add multiple sets
since: 1.0.0

SUNIONSTORE destination key [key …]
summary: Add multiple sets and store the resulting set in a key
since: 1.0.0

sorted sets

BZPOPMAX key [key …] timeout
summary: Remove and return the member with the highest score from one or more sorted sets, or block until one is available
since: 5.0.0

BZPOPMIN key [key …] timeout
summary: Remove and return the member with the lowest score from one or more sorted sets, or block until one is available
since: 5.0.0

ZADD key [NX|XX] [GT|LT] [CH] [INCR] score member [score member …]
summary: Add one or more members to a sorted set, or update its score if it already exists
since: 1.2.0

ZCARD key
summary: Get the number of members in a sorted set
since: 1.2.0

ZCOUNT key min max
summary: Count the members in a sorted set with scores within the given values
since: 2.0.0

ZDIFF numkeys key [key …] [WITHSCORES]
summary: Subtract multiple sorted sets
since: 6.2.0

ZDIFFSTORE destination numkeys key [key …]
summary: Subtract multiple sorted sets and store the resulting sorted set in a new key
since: 6.2.0

ZINCRBY key increment member
summary: Increment the score of a member in a sorted set
since: 1.2.0

ZINTER numkeys key [key …] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]
summary: Intersect multiple sorted sets
since: 6.2.0

ZINTERSTORE destination numkeys key [key …] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
summary: Intersect multiple sorted sets and store the resulting sorted set in a new key
since: 2.0.0

ZLEXCOUNT key min max
summary: Count the number of members in a sorted set between a given lexicographical range
since: 2.8.9

ZMSCORE key member [member …]
summary: Get the score associated with the given members in a sorted set
since: 6.2.0

ZPOPMAX key [count]
summary: Remove and return members with the highest scores in a sorted set
since: 5.0.0

ZPOPMIN key [count]
summary: Remove and return members with the lowest scores in a sorted set
since: 5.0.0

ZRANDMEMBER key [count [WITHSCORES]]
summary: Get one or multiple random elements from a sorted set
since: 6.2.0

ZRANGE key min max [BYSCORE|BYLEX] [REV] [LIMIT offset count] [WITHSCORES]
summary: Return a range of members in a sorted set
since: 1.2.0

ZRANGEBYLEX key min max [LIMIT offset count]
summary: Return a range of members in a sorted set, by lexicographical range
since: 2.8.9

ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
summary: Return a range of members in a sorted set, by score
since: 1.0.5

ZRANGESTORE dst src min max [BYSCORE|BYLEX] [REV] [LIMIT offset count]
summary: Store a range of members from sorted set into another key
since: 6.2.0

ZRANK key member
summary: Determine the index of a member in a sorted set
since: 2.0.0

ZREM key member [member …]
summary: Remove one or more members from a sorted set
since: 1.2.0

ZREMRANGEBYLEX key min max
summary: Remove all members in a sorted set between the given lexicographical range
since: 2.8.9

ZREMRANGEBYRANK key start stop
summary: Remove all members in a sorted set within the given indexes
since: 2.0.0

ZREMRANGEBYSCORE key min max
summary: Remove all members in a sorted set within the given scores
since: 1.2.0

ZREVRANGE key start stop [WITHSCORES]
summary: Return a range of members in a sorted set, by index, with scores ordered from high to low
since: 1.2.0

ZREVRANGEBYLEX key max min [LIMIT offset count]
summary: Return a range of members in a sorted set, by lexicographical range, ordered from higher to lower strings.
since: 2.8.9

ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
summary: Return a range of members in a sorted set, by score, with scores ordered from high to low
since: 2.2.0

ZREVRANK key member
summary: Determine the index of a member in a sorted set, with scores ordered from high to low
since: 2.0.0

ZSCAN key cursor [MATCH pattern] [COUNT count]
summary: Incrementally iterate sorted sets elements and associated scores
since: 2.8.0

ZSCORE key member
summary: Get the score associated with the given member in a sorted set
since: 1.2.0

ZUNION numkeys key [key …] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]
summary: Add multiple sorted sets
since: 6.2.0

ZUNIONSTORE destination numkeys key [key …] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
summary: Add multiple sorted sets and store the resulting sorted set in a new key
since: 2.0.0

应用

实时数据

  • 发布订阅

    历史数据

  • 缓存

    抽奖

  • spop

常见问题

击穿

定义

key过期的时刻发生高并发,同时向DB获取数据的情况

应对

  • 使用分布式锁(setnx)
  • 为防止客户端挂了而产生死锁,给分布式锁添加过期时间
  • 为防止出现DB耗时而锁提前过期,给分布式锁添加一个看门狗,在DB查询正常情况下给分布式锁续期

穿透

定义

客户端并发请求不存在的数据

应对

  • 布隆过滤器

雪崩

定义

大量的key同时过期,触发大量的访问数据库获取key值

应对

  1. 随机过期时间(可能导致脏读)
  2. 依赖击穿方案
  3. 在零点(特定时间业务等待)

持久化

RDB

  • save
  • bgsave
  • fork
  • copy on write

    AOF

混合模式

HA(高可用)

主从

从节点只能查不能写

  • replicaof host port :配置主节点
  • repl-diskless-sync : yes 直接通过网络发给从节点;no 生成RDA文件写入磁盘后再通过网络IO给到从节点
  • REPLIOF no one :从节点断开连接主节点
  • REPLIOF xxxxx :变为从节点,连接到目标主节点

sentinel

  • 监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服务器是否运作正常。
  • 提醒(Notification): 当被监控的某个 Redis 服务器出现问题时, Sentinel 可以通过 API 向管理员或者其他应用程序发送通知。
  • 自动故障迁移(Automatic failover): 当一个主服务器不能正常工作时, Sentinel 会开始一次自动故障迁移操作, 它会将失效主服务器的其中一个从服务器升级为新的主服务器, 并让失效主服务器的其他从服务器改为复制新的主服务器; 当客户端试图连接失效的主服务器时, 集群也会向客户端返回新主服务器的地址, 使得集群可以使用新主服务器代替失效服务器。
1
sentinel monitor mymaster 127.0.0.1 6379 2

集群

- 客户端分片 codis twemproxy redis-cluster
集群模式 无中心化 中心化 中心化 无中心化
使用方式 客户端编写路由规则代码,直连Redis 通过Proxy访问 通过Proxy访问 使用Smart Client直连Redis,Smart Client内置路由规则
性能 高 有性能损耗 有性能损耗 高
支持的数据库数量 多个 多个 多个 一个
Pipeline 支持 支持 支持 仅支持单个节点Pipeline,不支持跨节点
需升级客户端SDK? 否 否 否 是
支持在线水平扩容? 不支持 支持 不支持 支持
Redis版本 支持最新版 仅支持3.2.8,升级困难 支持最新版 支持最新版
可维护性 运维简单,开发人员使用成本高 组件较多,部署复杂 只有Proxy组件,部署简单 运维简单,官方持续维护
故障自动恢复 需部署哨兵 需部署哨兵 需部署哨兵 内置哨兵逻辑,无需额外部署

布隆过滤器

实现

  1. 客户端独立实现
  • 缺点,消耗客户端内存
  1. client实现数据存储Redis -> bitmap
  • 缺点,client逻辑繁琐
  1. Redis集成布隆
  • 优点,轻量实现

    缺点

  1. 只能增加,不能删除(使用布谷鸟过滤器)

参考

Redis中文网

Author: suce
Link: https://haoubox.cn/2021/05/22/Redis缓存/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.