What Redis is what features and benefits
Redis is an open source C language-based memory can be persistent, high-performance key-value database, and provides multi-lingual API.
It is also called a data structure of the server, since the value (value) may be a string, hash, list, sets, zsets (ordered set) Type
Redis has the following characteristics:
- Redis supports data persistence, data in memory can be kept on disk, restart when you can load be used again.
- Redis only supports simple key-value data types, while also providing a storage list, set, zset, hash and other data structures.
- Redis supports backup data, i.e., data backup master-slave mode.
Redis advantage
- High performance - Redis can read as fast as 110,000 times / s, write speed is 81000 times / s.
- Rich data types - Redis supports binary case Strings, Lists, Hashes, Sets and Ordered Sets the data type of the operation.
- Atomic - Redis All operations are atomic, and atomic Redis also supports the implementation of the whole operation and several.
- Feature-rich - Redis also supports publish / subscribe, notification, key characteristics and so expired
Installation Redis
Download the latest version from the redis.io redis-XYZtar.gz decompression, and then enter the redis-XYZ folder to make direct,
This is a linux
redis server runs under,
Download the latest version from the redis.io redis-XYZtar.gz decompression, and then enter the redis-XYZ folder to make direct,
./redis-benchmark //用于进行redis性能测试的工具
./redis-check-dump //用于修复出问题的dump.rdb文件
./redis-cli //redis的客户端
./redis-server //redis的服务端
./redis-check-aof //用于修复出问题的AOF文件
./redis-sentinel //用于集群管理
Start Redis server:./redis-server ./redis.conf
After running this command, redis server will be a non-daemon start method, the default port number 6379
, the port number on why 6379 may reference
Use Redic-cli
./redis-cli
127.0.0.1:6379> auth ranger
OK
127.0.0.1:6379> keys *
1) "key1"
2) "key2"
127.0.0.1:6379> get key1
"123456"
Introduction to Redis data structures
redis is a key-value
data structure that value
supports five data structures:
- String (string)
- String list (list)
- String set (Stes)
- An ordered set of character strings (zsets)
- Hash (hash)
How to choose the rightkey
key
Do not be too long will consume a lot of memory, and reduces the search efficiencykey
Also not too short, too short reduces readability- In one project,
key
the best use a unified naming rules
Data structures string Redis
If redis just use string type and function without the use of persistent, redis and it is very similar to the memcached.
string is a very basic data types
127.0.0.1:6379> set mystr "hello redis"
OK
127.0.0.1:6379> get mystr
"hello redis"
127.0.0.1:6379> set num "2"
OK
127.0.0.1:6379> get num
"2"
127.0.0.1:6379> incr num
(integer) 3
127.0.0.1:6379> get num
"3"
In operation of the face value, redis string type to convert into a numerical value. For example incr
instructions
Since INCR instruction itself has properties like atomic operations, so that we can use to achieve the redis count atoms INCR, INCRBY, DECR, DECRBY instructions such effects
The data structure list Redis
redis Another important data structure, Chinese translation as a list. Underlying data structures stored in redis list is a linked list. Therefore, the same list element is inserted in a head and tail, and the speed is quite fast, but slow positioning element
list
There are common operation lpush
, rpush
,lrange
lpush
Insert element for a list on the left,
rpush
Insert a list element for the right
lrange
You can specify a range extraction element from a list
127.0.0.1:6379> lpush mylist tom
(integer) 1
127.0.0.1:6379> lpush mylist jery
(integer) 2
127.0.0.1:6379> lrange mylist 0 2
1) "jery"
2) "tom"
127.0.0.1:6379> rpush mylist jack
(integer) 3
127.0.0.1:6379> lrange mylist 0 3
1) "jery"
2) "tom"
3) "jack"
127.0.0.1:6379> lrange mylist 1 2
1) "tom"
2) "jack"
list
Applications
- Using the list to achieve a simple message queue, ensure order, not necessarily the sort
- Lrange can be achieved using the paging function
- It can be used as blog comments
sets Redis data structures
the redis sets a set of unordered collection of elements in no particular order.
127.0.0.1:6379> sadd mysets one
(integer) 1
127.0.0.1:6379> sadd mysets tow
(integer) 1
127.0.0.1:6379> smembers mysets
1) "one"
2) "tow"
127.0.0.1:6379> scard mysets
(integer) 2
127.0.0.1:6379> srem mysets tow
(integer) 1
127.0.0.1:6379> scard mysets
(integer) 1
127.0.0.1:6379> smembers mysets
1) "one"
127.0.0.1:6379> sismember mysets one
(integer) 1
sets
Scenarios
Social tag number of friends, each user's friends label exists a set collection
zsets Redis data structures
redis not only provides an unordered set, it also provides an ordered collection (sorted ses). Ordered set of each element is associated with a sequence number.
In many cases, we are in an ordered set redis called zsets, because in redis, the ordered collection of related operating instructions are beginning to z, such zrange, zadd, zrevrange, zrangebyscore etc.
127.0.0.1:6379> zadd myzsets 1 baidu.com
(integer) 1
127.0.0.1:6379> zadd myzsets 2 qq.com
(integer) 1
127.0.0.1:6379> zrange myzsets 0 3
1) "baidu.com"
2) "qq.com"
127.0.0.1:6379> zrange myzsets 0 3 withscores
1) "baidu.com"
2) "1"
3) "qq.com"
4) "2"
127.0.0.1:6379>
Data structures hash Redis
Hash is only from after redis-2.0.0 version of the data structure.
hashes are stored mapping between string and string values, such as a user to store their full name, last name, age, etc., it is suitable for use hash.
127.0.0.1:6379> hgetall ranger
1) "name"
2) "cyp"
3) "age"
4) "20"
127.0.0.1:6379> hset ranger age 21
(integer) 0
127.0.0.1:6379> hgetall ranger
1) "name"
2) "cyp"
3) "age"
4) "21"
Redis persistence
redis persistence provides two methods: RDB(Redis DataBase)
andAOF(Append Only File)
RDB
: That is, at different points in time, generate a snapshot of the data, the data stored on disk
AOF:
Redis instructions executed recorded, then re-execute the instructions once reached the reproduced data object
Both persistent manner can be used simultaneously, redis restart will give priority to AOF, higher integrity of such data
If there is no demand for data persistence, you can turn this feature off
Redis persistence of RDB
RDB embodiment, the data is a time redis persisted to disk, a snapshot of formula persistence method.
redis during the process of data persistence, data will first be written to a temporary file, be persistent process is over, it will replace the previous document with good persistence of this temporary file. It is this characteristic, we can always come back up, because the snapshot files are always complete available.
For RDB way, redis creates a separate (fork) a child process to persist, while the main course is not going to make any IO operations, thus ensuring redis high performance.
If you need to restore large-scale data, and is not very sensitive to the integrity of the data recovery, a more efficient way than AOF that RDB way (directly save the data).
Although RDB have many advantages, its disadvantages should not be ignored. If you are very sensitive to the integrity of the data, then RDB way it is not for you, because even if you are persistent every 5 minutes once, when redis fault, there will still be nearly five minutes of data loss. So, redis also provides another persistent way, that is AOF.
If you want to open the way redis persistence of RDB
You can configure the save point, so if the Redis M times changed in every N seconds after the data is saved snapshot file. For example, the following configuration represents a saving point every 60 seconds, if the data has more than 1000 times of change, Redis will automatically save a snapshot of the file:
save 60 1000
Can set up multiple save points, Redis configuration file to the default settings saved three points:
# 格式为:save <seconds> <changes> # 可以设置多个。 save 900 1 #900秒后至少1个key有变动 save 300 10 #300秒后至少10个key有变动 save 60 10000 #60秒后至少10000个key有变动
If you want to disable saved snapshot feature can be configured to achieve by commenting out all "save", or add the following configuration after the last "save" configuration:
save ""
Redis persistence of AOF
AOF
English is Append Only File
, that only allows additional files being overwritten.
Through configuration redis.conf中的appendonly yes
you can open AOF function. If there is a write operation (such as SET, etc.), redis AOF will be appended to the end of the file.
AOF default persistence strategy is once per second fsync (fsync refers to the write instruction cache record to disk), because in this case, redis can still maintain good processing performance, even redis fault, too only recently lost one second of data.
Because the use append mode, if no treatment, then, AOF files become larger and larger, therefore, redis provided AOF文件重写
when (rewrite) mechanism, that is, when the threshold AOF file size exceeds the set, redis on content starts AOF file compression, leaving only the minimum set of instructions can recover data. For example perhaps more vivid, if we call 100 INCR instruction in AOF file will store 100 instructions, but this is obviously very inefficient, can merge these into a SET instruction 100 instructions, which is the principle of rewriting mechanism.
Although a lot of advantages, but also shortcomings AOF manner, such as in the case where the same data size, the file AOF bulky than the RDB file. Moreover, the recovery rate is also slower than the AOF way RDB way
Redis persistence of AOF rewrite
In rewriting the upcoming occasion, redis created (fork) a "rewrite child process", the child process will first read the existing AOF files and instructions it contains to analyze compressed and written to a temporary file.
At the same time, the main work of the new process will be received write instruction while accumulated into a memory buffer, while continuing to write original AOF file, doing so is to ensure the availability of AOF original file, to avoid heavy accident occurred during writing.
When the "Rewrite the child" complete rewrite work, it will send a signal to the parent, the parent will receive a signal after the memory cache write instruction is appended to the new AOF file.
After the end of the additional, redis will use the new file to replace the old AOF AOF file, then no new write command, it will be added to the new file in the AOF.
The master-slave synchronization Redis
Like MySQL, like, redis support master-slave synchronization, but also supports a master multi-slave and multi-level from the structure.
Master-slave structure, one for purely redundant backup, the second is to improve the reading performance
redis the master-slave synchronization is asynchronous, which means that the master-slave synchronization will not affect the main logic, processing performance does not decrease the redis.
In the master-slave architecture, from the server is typically set to read-only mode, to avoid misuse of the modified data from the server. But still acceptable from the server command CONFIG, etc., it still should not be directly exposed to the insecure server network environment from. If you have to, then you can consider giving important instructions to rename, to avoid outsiders mistakenly command is executed.
From the main principle
SYNC will issue instructions to the master server from the server when the main server receives this command, it will call BGSAVE instructions to create a child process dedicated to data persistence work, that is, the data written to the primary server RDB file. During data persistence, write command from the host server to be executed are cached in memory.
After BGSAVE instruction is complete, the primary server will send a good persistence to the RDB file, the file received from the server from the server stores it to the disk, and then read it into memory. When this action is completed, the master cache server sends this time write command from the server and then sent to redis protocol format.
Further, the point is to say, even if there are a plurality of simultaneously SYNC instruction sent from the server, the master server bgsave performed only one time, and then a good persistence RDB files to multiple downstream. Before redis2.8 version from the server if the primary server is disconnected for some reason, then, the total amount will be between a master data synchronization from
And after version 2.8, redis support greater efficiency incremental synchronization strategy, which greatly reduces the cost of recovery disconnected.
The master server maintains a buffer in memory buffer stores to be sent from the content server. After the short break occurred and the network master server, from the server from the server again attempts to connect with the primary server, once the connection is successful, the server will put "want to synchronize the master server ID" shift position and the "desired data request ( replication offset) "sent. After the primary server receives such a synchronization request, it first verifies whether the master ID and the own ID matches, secondly checks the "request" displaced position exists in the own buffer, if both are met, the main incremental content server will be sent to the server.
Delta Sync, you need server-side support new PSYNC instructions. This directive, only after having redis-2.8.
Redis transaction
redis matters related to four instructions: multi, exec, discard, watch
- multi: to assemble a transaction
- exec: to perform a transaction
- discard: to cancel a transaction
- watch: used to monitor some of the key, once the key is changed prior to transaction execution, executing a transaction will be canceled.
and multi exec
127.0.0.1:6379> multi
OK
127.0.0.1:6379> sadd mysets ranger
QUEUED
127.0.0.1:6379> lpush mylist hello
QUEUED
127.0.0.1:6379> ping
QUEUED
127.0.0.1:6379> exec
1) (integer) 1
2) (integer) 4
3) PONG
Redis data elimination strategy
In redis, allowing the user to set the maximum memory size can be used ( maxmemory 100mb
), which is useful in tight memory situations.
redis data memory size up to a certain size, it will use part of the data out of memory out of policy. 6 redis provide the data out of the policy
- volatile-lru: selection of the least recently used data out of the set of data set expiration time (server.db [i] .expires) in
- volatile-ttl: selection of data to be expired expiration time has been set out from the data set (server.db [i] .expires) in
- volatile-random: the expiration time has been set from the data set (server.db [i] .expires) arbitrarily selected out of the data
- allkeys-lru: selection of the least recently used data out from the data set (server.db [i] .dict) in
- allkeys-random: selecting out data from the data set (server.db [i] .dict) any
- no-enviction (expulsion): prohibits the expulsion data
After redis determine the expulsion of a key-value pairs, and will delete the data and publish the data change message to the local (AOF persistence) and slave (master-slave connection).