Redis common API and the persistence mechanism

Redis common API and the persistence mechanism

A, Redis common API --- commands: http://redisdoc.com/geo/index.html

Redis widely supported data types, such as: string (string), List ( chain table ), SET (set), the hash (hash type and zset (sorted set - ordered set)

key 
Keys * 
EXISTS key to determine whether there is a key 
move key db, there is no current library to the library specified went 
expire key for a given key set the expiration time 
ttl key to see how much time expired -1 means never -2 is expired expired 
type key to view the key is what type 

1.string 
String redis is the most basic type, you can be understood as exactly the same type with Memcached, a key corresponding to a value. 
The string type is binary safe. Redis meaning of string can contain any data. Such as jpg image or serialized object. 
Redis is the most basic type of string data type, a redis string value can be up to 512M 
the SET key value value setting key 
GET key to view the current key value 
del key delete key 
at the end of key append key value if the key is present, the specified adding, if the key exists Similarly SET 
strlen returns the length of this key the key 
----------------------------------- -------------------------------------- 
following command only when the key value is a number to operate properly 
incr add a key value to perform the given key 
DECR key value minus a specified key
incrby key numerical value is increased to the value specified key 
decrby key value is the value of the specified key value Save 

GetRange 0 key (start position) -1 (end position) Gets a value within the range of the specified range, similar between ...... and relationship (0-1) represents all 
setrange key 1 (start position where to start setting) within a specific set value (replacement) intervals specified range of values 
setex second key value with the true value of the expiration time of the key, set dynamically. 
setnx key value key value is only set when the key is not present. 
mset key1 value key2 value while providing one or more key-value pair. 
Get all mget key1 key 2 (one or more) of values of the given key. 
msetnx key1 value key2 value while providing one or more key-value pairs, if and only if all the given key does not exist. 
getset key value is set to a given value of the key value, and returns the key of the old value (old value). 

2.list 
It is a string list, left, right can be inserted to add; if the key does not exist, create a new list; if the key already exists, the new content; if the value of the whole is removed, the corresponding button will disappear . 
List operation both head and tail are high efficiency, but if the intermediate element is operated, the efficiency is very bleak. Redis list is a simple list of strings, sort insertion order. You can add a head guide elements of the list (on the left) or tails (to the right). The bottom floor is actually a linked list lpush key value1 value2 one or more values to the list head rpush key value1 value2 one or more value is added to the bottom of the list Get a list lrange key start end of the specified range of elements (0-1) represents all lpop key and get out of the first element of the list rpop key out and acquires a list of the last element lindex key index acquired by the index element in the list llen get a list length lrem key 0 (number) value that indicates to delete all the given value. Zero values is all removed from the left to the right a specified number of elements equal to the value specified value, the number of returned value actually removed ltrim key start (where to start cut) End (end position) of the element specified index section taken format is a key starting index ltrim list of end index 3.Set Redis is a string Set the type of disorder can not duplicate collections. sadd key value1 value 2 add one or more members to the collection smembers key returns a collection of all the members sismembers key member determines whether the element is a member of a set of key members SCard acquired key set number of elements inside srem key value set to delete the specified element srandmember key set from a set of values which specify a random value taken when the maximum number of elements on all removed, the sdiff key1 key2 without any items inside a set (set difference) is set back inside the first SPOP random Key removed and an element in a set of return smove key1 key2 value (a value in key1) key1 action is performed in a predetermined value is added to remove key2 set Sinter key1 key2 are in the first set and the second set (intersection) SUNION key1 key2 two set of all elements (and current) 4.hash the Redis the hash is a key-value pairs. Redis hash field is a string type and the value of the mapping table, hash is particularly suited for storing objects. kv mode unchanged, but v is a key-value pair similar to Java inside the Map HSET Key (Key value) adds an element to the hash table to obtain an element to the hash table Key Key hget key1 value1 key2 value2 value3 to the collection hmset key key3 add one or more elements hmget key key1 key2 key3 acquire one or more elements to the collection hgetall key to obtain the specified key in the hash list all the fields and values hdel key key1 key2 delete one or more hash fields hlen key acquisition hash the number of fields in the table hexits key key to view the hash table, specify the key (field) if there is hkeys key Gets the hash table of all key (field) hvals Key Gets the hash table of all value (value) hincrdy Key number key1 (integer) the number of hash table plus a field executive set, and incr a mean hincrdyfloat key number (floating-point, decimal) key1 perform the hash table given a field number plus, and a mean incr hsetnx key key1 value1 and hset the same role, except that there is no assignment, the presence of an invalid. 5.zset Redis zset and is set as a collection of string type elements, and does not allow duplicate members. The difference is that a double score will be associated with each type of element.
It is to redis from small to large order of collection of member passing score. zset member is unique, but the score (score) it can be repeated. zadd key score value score value is added to the collection of one or more members zrange key 0 -1 indicates that all return all the specified set value zrange key 0 -1 withscores return value and the score of all the specified collection zrangebyscore key returns the specified start score score ends value between the score zrem Key score corresponding to a value (value), the value may be a plurality of removing elements zcard key acquired number of elements in the collection zcount key end score score start acquiring the number of elements within a fraction from zrank key vlaue value acquired in zset subscripts position (sorted according to score) zscore Key value in accordance with the value of the corresponding scores obtained

Two, Redis persistence mechanism (RDB, AOF)

Why 2.1 Redis need to be persistent file?

Redis NoSQL is based on memory to run the database, once the power failure or abnormal condition, if not persistent data operation, data in memory will be lost can not be recovered.

So Redis within the specified time interval, the memory of them is a snapshot of the data set is written to disk, it is to restore the snapshot file directly into memory.

2.2 Redis persistence way of what?

(1)RDB:内存写入磁盘,默认方式

(2)AOF:文件追加的形式  appendonly no 默认关闭:no  开启:yes 

2.3 Redis的持久化机制

RDB:(1)主进程Fork子进程  (2)子进程将数据写入临时文件夹 (3)持久化结束,临时文件夹替换原持久化文件,主进程不进行IO操作,保证高可用。
AOF:将Reids的操作日志以追加的方式写入文件,不记录读操作
持久化文件存放的位置:dir  ./(path)  ##path为文件存放目录,必须是一个目录,rdb、AOF都存放在改路径下

2.4 持久化触发的时机

RDB持久化文件触发机制
shutdown时,如果没有开启aof,会触发
配置文件中默认的快照配置
执行命令save或者bgsave save是只管保存,其他不管,全部阻塞;bgsave:redis会在后台异步进行快照操作,同时可以响应客户端的请求
执行flushall命令 但是里面是空的,无意义
AOF持久化文件触发机制(依据配置项决定) no:表示等操作系统进行数据缓存同步到磁盘(快,持久化没保证) always:同步持久化,每次发生数据变更时,立即记录到磁盘(慢,安全) everysec:表示每秒同步一次(默认值,很快,但可能会丢失一秒以内的数据)

SAVE命令执行一个同步保存操作,将当前 Redis 实例的所有数据快照(snapshot)以 RDB 文件的形式保存到硬盘。
一般来说,在生产环境很少执行SAVE操作,因为它会阻塞所有客户端,保存数据库的任务通常由BGSAVE命令异步地执行。
然而如果负责保存数据的后台子进程不幸出现问题时,SAVE可以作为保存数据的最后手段来使用。

BGSAVE命令执行之后立即返回OK,然后Redis fork出一个新子进程,原来的Redis进程(父进程)继续处理客户端请求,而子进程则负责将数据保存到磁盘,然后退出。
客户端可以通过LASTSAVE命令查看相关信息,判断BGSAVE命令是否执行成功。

执行一个AOF文件重写操作。重写会创建一个当前AOF文件的体积优化版本。
即使BGREWRITEAOF执行失败,也不会有任何数据丢失,因为旧的AOF文件在BGREWRITEAOF成功之前不会被修改。
重写操作只会在没有其他持久化工作在后台执行时被触发,也就是说:
如果 Redis 的子进程正在执行快照的保存工作,那么AOF重写的操作会被预定(scheduled),等到保存工作完成之后再执行AOF重写。
在这种情况下,BGREWRITEAOF的返回值仍然是OK,但还会加上一条额外的信息,说明BGREWRITEAOF要等到保存操作完成之后才能执行。
在Redis 2.6或以上的版本,可以使用INFO [section]命令查看 BGREWRITEAOF是否被预定。
如果已经有别的AOF文件重写在执行,那么BGREWRITEAOF返回一个错误,并且这个新的BGREWRITEAOF请求也不会被预定到下次执行。
从 Redis 2.4 开始, AOF重写由Redis自行触发,BGREWRITEAOF仅仅用于手动触发重写操作。

2.5.aof重写机制

当AOF文件增长到一定大小的时候Redis能够调用 bgrewriteaof对日志文件进行重写 。当AOF文件大小的增长率大于该配置项时自动开启重写(超过原大小的100%)。
auto-aof-rewrite-percentage 100

当AOF文件增长到一定大小的时候Redis能够调用 bgrewriteaof对日志文件进行重写 。当AOF文件大小大于该配置项时自动开启重写
auto-aof-rewrite-min-size 64mb

2.6.redis4.0后混合持久化机制

开启混合持久化:4.0版本的混合持久化默认关闭的,通过aof-use-rdb-preamble配置参数控制,yes则表示开启,no表示禁用,5.0之后默认开启。
混合持久化是通过bgrewriteaof完成的,不同的是当开启混合持久化时,fork出的子进程先将共享的内存副本全量的以RDB方式写入aof文件,
然后在将重写缓冲区的增量命令以AOF方式写入到文件,写入完成后通知主进程更新统计信息,并将新的含有RDB格式和AOF格式的AOF文件替换旧的的AOF文件。
简单的说:新的AOF文件前半段是RDB格式的全量数据后半段是AOF格式的增量数据。 优点:混合持久化结合了RDB持久化 和 AOF 持久化的优点, 由于绝大部分都是RDB格式,加载速度快,同时结合AOF,增量的数据以AOF方式保存了,数据更少的丢失。 缺点:兼容性差,一旦开启了混合持久化,在4.0之前版本都不识别该aof文件,同时由于前部分是RDB格式,阅读性较差

三、总结

3.1 RDB和AOF性能对比

rdb 适合大规模的数据恢复,对数据完整性和一致性不高 , 在一定间隔时间做一次备份,如果redis意外down机的话,就会丢失最后一次快照后的所有操作
aof 根据配置项而定
官方建议 两种持久化机制同时开启,如果两个同时开启 优先使用aof持久化机制

3.2 性能建议(针对单机版性能提升) 

因为RDB文件只用作后备用途,只要15分钟备份一次就够了,只保留save 900 1这条规则。
如果Enalbe AOF,好处是在最恶劣情况下也只会丢失不超过两秒数据,启动脚本较简单只load自己的AOF文件就可以了。
代价一是带来了持续的IO,二是AOF rewrite的最后将rewrite过程中产生的新数据写到新文件造成的阻塞几乎是不可避免的。
只要硬盘许可,应该尽量减少AOF rewrite的频率,AOF重写的基础大小默认值64M太小了,可以设到5G以上。
默认超过原大小100%大小时重写可以改到适当的数值。

Guess you like

Origin www.cnblogs.com/vincentYw/p/12111227.html