[Redis] entry

Redis Overview

 Redis is an open source, memory-based data structure of the memory can be used as a database, cache and messaging middleware

Redis most commonly used functions

  • Cache
  • Distributed Lock
 This article describes the use of the environment
  • Windows 10

  • Redis 3.0.504

How to install Redis not be introduced, if do not want to install Redis environment, you can use try redis try Redis function
CMD start Redis, open Reids directory, execute the following command, appears below the picture represents the successful start
redis-server redis.windows.conf

 

 

Redis common data structure

String (String)

use

  • Storage string / Digital
  • Conventional key-value cache application
  • Conventional count: the number of orders, product number
Common Commands use
set key value Setting the value of the specified key
get key Gets the value of the specified key
decr key The value of the specified key decremented by one, if the digital value is not set, error
incr key The specified key value is incremented by one, if the digital value is not set, error
mget key1 [key2] Get the specified key, return multiple values

 

Hash (Hash)

use

  • hash is particularly suitable for storing objects (user information, product information)
  • You can modify / read a target value
Common Commands use
hset key field value Single object set value
hget key field value The target value obtained
hmset key field1 value [field2 value2] Object provided a plurality of field values
hmget Get Object multiple values, returns a list
hgetall Gets the key value and object fields

 

 

List (List)

use

  • Doubly linked list traversal and support reverse lookup
  • You can add duplicate data
  • Suitable for storing the message list, product list, order list, etc.
Common Commands use
lpush key value To push the value of the list on the left
rpush key value Push the right value to the list
lpop key The left side of the pop-up list of values
rpop key The value of the right side of the pop-up list
lrange key start end The left side of the list elements in the specified range, used for paging
Llen key Gets the length of the list

 

 

Collection (Set)

use

  • Similarly with the list, the set will go to repeat
Common Commands use
sadd key value Adding an element
spop key value It returns a random element
smembers key Returns a collection of all the members
sunion key1 key2 Return all the given set of union

 

Ordered set (Sorted Set)

 use

  • Elements of the collection can be ordered by score
  • Used for ranking data
  • Under the same circumstances score, sort key Dictionary
Common Commands use
zadd key score1 member1 [score2 member2] Adding to the ordered set of one or more members, or update an existing member of the score
zrange key start end Returns the ordered set of data specified range
zrem key member Removes the specified member
zcard key Gets a collection of membership

 

 

 

Redis deletion of expired data policy

Redis deletion of expired data, there are two mechanisms

1. regularly delete: redis default is every 100ms on a random sample of expired data deletion

2. inert Delete: whether to access data when the data has expired inspection

 

Redis memory elimination mechanism

When confronted write data memory is not enough, Redis has several mechanisms to decide what to do next

the way Explanation
volatile-lru Pick out the least recently used data set from data sets expiration time (server.db [i] .expires) in
volatile-ttl Data selection to expire expiration time has been set out from the data set (server.db [i] .expires) in
volatile-random Out data from a selected set of expiration data set (server.db [i] .expires) any
allkeys-lru When insufficient memory to accommodate the new data is written in the key space, the key is removed the least recently used
allkeys-random Out data from the selected data set (server.db [i] .dict) any
noeviction Prohibition deletion, if out of memory error
volatile-lfu Pick the lowest frequency of use data from the phase-out has set the expiration time of the data set (Redis5.0 later only)
allkeys-lfu 从数据集中挑选使用频率最低的数据淘汰(Redis5.0 以后才有)

在配置文件里面打开maxmemory-policy配置项,默认是:noeviction

通常情况配合maxmemory配置设置最大内存使用

 

Redis持久化机制

Redis支持两种持久化方式

快照(snapshotting)持久化(RDB)

  • Redis可以通过创建快照来获得存储在内存里面的数据在某个时间点上的副本
  • Redis默认采用的持久化方式
  • 配置多个save配置项指定触发创建副本条件,例:save 900 1 , 900秒内至少一个Key发生变化触发创建快照
  • 缺点:若没达到触发条件,数据容易丢失

AOF(append-only file)持久化

  • 开启AOF持久化后,根据选择的方式写入AOF文件中
  • 配置appendfsync 配置项,配置同步频率
  • 缺点:可能经常发生同步,影响Redis的速度 

AOF有3种方式

方式 说明
appendfsync always 每次有数据修改发生时都会写入AOF文件 ,严重降低Redis的速度
appendfsync everysec 每秒钟同步一次,显示地将多个写命令同步到硬盘
appendfsync no 让操作系统决定何时进行同步

为了兼顾数据和写入性能,用户可以考虑 appendfsync everysec 选项 ,让 Redis 每秒同步一次 AOF 文件,Redis 性能几乎没受到任何影响。

Redies默认AOF是关闭的,通过修改配置打开

 

 

Redis 4.0 对于持久化机制的优化

Redis 4.0 开始支持 RDB 和 AOF 的混合持久化(默认关闭,可以通过配置项 aof-use-rdb-preamble 开启)。

如果把混合持久化打开,AOF 重写的时候就直接把 RDB 的内容写到 AOF 文件开头。

这样做的好处是可以结合 RDB 和 AOF 的优点, 快速加载同时避免丢失过多的数据。

当然缺点也是有的,AOF 里面的 RDB 部分是压缩格式不再是 AOF 格式,可读性较差。

由于Windows的版本最新只有3.0,这个就没法验证

缓存问题及解决方案

缓存雪崩

同一时间大面积缓存失效,大量请求落到数据库上

解决

  • 缓存设置过期时间时加上一个随机值时间,使得每个key的过期时间分布开来,不会集中在同一时刻失效

缓存穿透

请求缓存不存在的数据,然请求穿透缓存到数据库,通常是黑客攻击

解决

  • 采用布隆过滤器,使用一个足够大的bitmap,用于存储可能访问的key,不存在的key直接被过滤
  • 访问key未在DB查询到值,也将空值写进缓存,但可以设置较短过期时间
  • 采用签名验证提交数据

缓存击穿

缓存过期的一刻,同时有大量的请求,由于缓存失效,导致请求都落到DB

解决

  • 单体使用互斥锁(mutex key)
  • 分布式使用setnx
  • 双重校验访问缓存数据(Double Check),就是线程安全的单例模式

Guess you like

Origin www.cnblogs.com/WilsonPan/p/12635605.html
Recommended