A, Redis Introduction
Redis is an open source use written in ANSI C, BSD comply with the agreement and support network, based on the persistence of memory can log type, Key-Value database, and provides multi-lingual API.
It is often called a data structure of the server, since the value (value) can be a string (String), a hash (the Map), list (List), set (sets), and an ordered collection (sorted sets) and other types. Meet many application scenarios. Also provides key expired, publish additional subscription features, services, lines and so on.
Github Source: https: //github.com/antirez/redis
Redis official website: https: //redis.io/
Two, Redis usage scenarios
Memory cache, vote, session session, charts, counter, publish-subscribe, message queues etc. The most important scene is the use of storage caching and session Session.
When used as a cache memory, as shown below:
Redis database for the first time when there is no data access, so start with the MySQL query, and then save to memory Redis database; when a second visit can get data directly from the Redis database.
When used as a cache Redis session Session. As shown below:
All applications are to visit Redis, such distributed applications share a session Session.
Third, rapid installation
Redis3.0 before "unsupported cluster" based on client implementation
After Redis3.0 "Cluster (the Cluster Redis)" Based on the server
Redis installation is very simple, as follows:
$ wget http://download.redis.io/releases/redis-5.0.5.tar.gz $ tar xzf redis-5.0.5.tar.gz $ cd redis-5.0.5 $ make
Details about the installation steps can refer (Mac version, the same installation procedure on Linux): Mac under installation and Redis Redis Desktop Manager
Redis installation directory under the common file as follows:
Fourth, communication principles outlined
Redis speed is very fast, based on the following reasons:
- Redis is single-threaded processes running, so there is no waste of resources caused by the thread switching (but also a disadvantage, if the command execution will cause slow obstruction);
- Redis data is installed on the hard disk, but the operating data is in memory, hard disk access speed is much larger than, avoid a lot of IO.
- Redis server is based on an efficient IO multiplexing, high efficiency.
Redis server is an event-driven program, event server processing time is divided into two types of events and file events.
- File event: Redis main process, the main connection process client requests and corresponding.
- Time Event: child process fork out, the processing tasks such as AOF persistence.
Since Redis file event is a single-process, single-threaded model, but to ensure the holding of the excellent throughput, IO multiplexing played a major role. Simple explanation is that a multiplex thread monitor multiple sockets, who should have read and write on the handling of the event who, thread switching overhead and reducing blocking IO to improve CPU utilization.
Redis access process is as follows:
Five, Redis daily operations
There are five Redis data types are: String (String), hash (Map), list (list), the set (sets) and ordered collection (sorted sets). About five data types and the role please refer to: five kinds of data structures, operation and use of learning scenarios Redis
Five kinds of data common operations as follows:
1, string
String type: in fact can be a string (including XML JSON), as well as digital (plastic float), binary (picture audio video), the maximum can not exceed 512MB.
Set value of the command: SET Key value SET name Monkey EX 20 is // expired px 20000 milliseconds after 20 seconds expire SETNX name Monkey // key name does not exist, returns a success provided; failure exists 0 SET Age 18 is XX // presence bond age, it returns a success batch setter: mset name monkey sex boy get bulk: Sex mget name (\ R & lt \ n-) the append additional instructions: sET Hello name; the append name World // after helloworld appended to the string length: sET Monkey ' Zhenshuai ' strlen Monkey // result 6, each Chinese 3 bytes (utf-8) , taken string: SET name Monkey; GetRange name 2 . 4 // returns "nke" counter: CAS (Atomic atomic operations) incr age // must be an integer from 1 plus non-integer returns an error, no self-energizing key returns an age from 0 DECR age // integer of age minus 1 incrby age 1 // integer of 1 + age decrby age 1 // integer -1 Age incrbyfloat Score 3.3 // float + 3.3 Score
// distributed high concurrency, you can solve thread safety issues, for example, generate a unique number (order number)
2, Hash type
Hash is a hash of a string field type and value of the mapping table, hash especially suitable for storing objects, such as:
Command hset key field value set value: HSET the User: 1 name monkey // successful return 1, else return 0 value: hget the User: 1 name // returns monkey value-: HDEL the User: 1 Age // returns the number of deleted counting the number of: HSET User: . 1 name Monkey; HSET User: . 1 Age 18 is ; hlen User: . 1 // returns 2, user: 1 has two attribute values batch setter: hmset User: 2 name Age 18sex Monkey Boy // OK to return the bulk value: hmget the User: 2 name Age Sex // return three lines: monkey 18 boy determine whether there is field: hexists the User: 2name // If there is a return, there is no return 0 accessories field: hkeys User: 2 // Returns the name age sex three field acquired user: 2 all value: hvals User: 2 // Returns monkey18 boy acquired user: 2 All field with the value: User hgetall: 2 // monkey18 value name age sex Boy 18 is increased. 1: User hincrby: 2 Age . 1 // Age +. 1 floating-point increase: hincrbyfloat User: 2 Age 2 // float plus 2
3, List collection
For storing a plurality of ordered strings, a list can be stored up to 2 ^ 32 minus one element.
Add command: RPUSH Monkey cba // right to insert cba left, returns the value 3 lrange Monkey 0 - 1 // from left to right to get a list of all the elements returned cba LPUSH Key cba // from left to right insert cba linsert the before bz Monkey // inserted before b z, then after, for use lrange monkey 0 -1 See: czba
Find command: Lrange Key Start End // index subscript features: from left to right to 0. 1-N the lindex Monkey - . 1 / / returns the rightmost end of a, -2 returns B LLEN Monkey // returns the current length of the list lpop Monkey // the leftmost first element c remove RPOP Monkey // the rightmost element a deleted
4, Set collection
Commonly used in user tags, social, inquiries have common interests of people, intelligent recommendation. Save multi-element, is not the same as the list does not permit duplicates, and the set is unordered, a power set 32 can be stored up to a minus element 2, in addition to supporting CRUD, also it supports a set of intersection, union, , difference sets;
user EXISTS // check whether the user key exists Sadd user ABC // insert user three elements to return. 3 Sadd user ab & // if more elements added to the same, is repeated invalid, returns 0 smembers user // Get all user elements, unordered return results Srem a User // returns 1, a delete element SCard User // returns 2, the number of elements calculated
5, ordered set ZSET
Commonly used in the list, such as video sites require users to upload videos do list, or the number of thumbs up with a set of linked, can not have duplicate members
Instruction: Zadd Member Key Score [Score Member ......] Examples: Zadd User: Zan 200 is monkey // Number of points like a monkey, returns the number of successful operating a Zadd User: Zan 200 is monkey 120 Fox 100 Lion // returns. 3 Zadd User: Zan NX 100 Monkey @ key test: 1 must not exist, for adding the primary Zadd User: Zan XX incr 200 is Monkey @ key test: 1 must be present to modify the master, in this case 300 Zadd User: Zan XX CH incr - 299 Monkey // returns the result of the operation. 1 = 1,300-299 Z Range The User: Zan 0 - . 1 withscores //Like view point (score) and the member name zcard User: Zan // count the number of members, returns a ranked scene: Zadd User: . 3 200 is monkey120 Fox 100 Lee // Insert data first Z Range The User: . 3 0 - 1 withscores // Check scores with members zrank the User: 3 Monkey // return ranking: No. 3 returns 2, starting from 0 to 2, a total of three zrevrank the User: 3 Monkey // returns 0, anti-sorting, the higher the number of points praise, ranking Echizen
6, Redis global command
View all keys: Keys * The total number of keys: dbsize // 2 keys, if there are a large number of keys, prohibit the use of this command line to check if there is key: EXISTS Key // there is a return, there is no return to 0 delete key: del Key / / del key, returns the number of delete key to delete the key to return to 0 absent key expires: the expire seconds the key // SET name Test the expire name 10, indicates 10 seconds expire TTL key // check the remaining expiration time of the data type of the key structure: key of the type // of the type the Hello // return string, the key does not exist or none
7, redis database management
the SELECT 0 // database 16 database (0-15) default is 0 library flushdb // clear the current database flushall // clear all database records dbsize // command returns the current number of key database
Six, Redis persistence
Persistence role: redis is a persistent memory database support, that redis often need to synchronize the data in memory to disk to ensure persistence, persistence can be avoided by the process of withdrawal and loss of data. Redis persistence in two ways: RDB snapshots and AOF way.
1、RDB
RDB persistent data generation process to save the current snapshot (.rdb) files to the hard disk of the process, a manual trigger and trigger automatic trigger manually save and bgsave have two commands:
- save command: blocking the current Redis, RDB persisted until the process is complete, if memory instances relatively long blockage caused by the General Assembly, online environment is not recommended to use it
- bgsave command: redis process of implementation fork operation to create a child thread completed by the child thread persistence, blocking time is very short (microseconds), is to optimize the save, and in the implementation of redis-cli shutdown closed redis service, if not open AOF persistence automatic execution bgsave;
1.1, Save command
As the save command is a synchronous command, it will occupy the main process of Redis. If Redis data very long time, save the command execution speed will be very slow, blocked requests for all clients.
SAVE command is rarely used directly in a production environment, you can use BGSAVE command instead. If an error occurs in the child process to save data BGSAVE command, save the latest data with the SAVE command is the last resort. as the picture shows:
1.2, Bgsave command
bgsave command executes an asynchronous operation, by way of RDB files stored snapshot of all the data. Such as:
127.0.0.1:6379> bgsave Background saving started
as the picture shows:
1.3, save and compare bgsave
operating:
Command: config the SET dir / usr / local // set rdb file path backup: bgsave // will be saved to dump.rdb usr / local recovery: the dump.rdb redis installation directory and redis.conf into the same directory, redis to restart
1.4, RDB advantages and disadvantages
RDB advantages:
- The compressed binary files for backup, the full amount of replication for disaster recovery loaded RDB to recover data much faster than the AOF way
- Compared with the AOF, when restoring large data sets, RDB way faster.
- Use bgsave save, handed over the child to save a snapshot, the parent process does not need to do other IO operations, so RDB persistent way to maximize the performance of redis.
RDB Disadvantages:
- Persistence can not do real-time, every time create a child process, frequently operating costs too high
- Binary file is saved, there is the old version is not compatible with the new version rdb file problem
2、AOF
Snapshot function (RDB) is not very durable (durable): If for some reason Redis caused downtime, the server will lose the most recently written, and those data are not yet saved to the snapshot. From version 1.1, Redis adds a completely durable persistent way: AOF persistence.
After opening AOF, whenever a change Redis Run dataset (such as SET), this command will be appended to the end of the file AOF. In this case, when Redis restarted, the program can achieve the purpose of reconstruction data sets by re-executing commands in the file AOF.
Operation: open: redis.conf Setting: appendonly yes (the default is not open for no) default file name: appendFileName " appendonly.aof "
Three strategies always, everysec, no comparison
- always: do not lose large data IO overhead, usually only a few hundred TPS SATA disk each time a new command is added to the AOF document on the implementation of a fsync: very slow, very safe.
- Be everysec per second and fsync: the loss of up to one second data may be lost one second of data every second fsync: fast enough (and almost persistent use RDB), and only one second of data loss in case of failure. Recommended (and also the default) measures to fsync once per second, this strategy can take into account the fsync speed and security.
- no do not control the uncontrollable Never fsync: the data to the operating system to handle, to decide when to synchronize data by the operating system. Faster and less safe choice.
AOF advantages:
- AOF will make your use more durable Redis: You can use different strategies fsync: No fsync, fsync per second, each time to write fsync. Use the default fsync strategy per second, Redis performance is still very good (fsync is processed by a background thread, the main thread will try to handle client requests), event of a failure, you lose a maximum of 1 second of data.
- AOF is a file only append log file, even if for some reason (disk space is full, the process of writing down, etc.) is not a complete write command, you can also use redis-check-aof tool fix these problems.
- When Redis AOF may become too large file size, automatically in the background rewriting of AOF: AOF new file after overwriting command contains a minimal set of data required to restore the current collection. Rewrite the entire operation is absolutely safe, because in the process of creating a new Redis AOF file, the command will continue to append to an existing file AOF inside, even during downtime rewrite existing AOF files will not be lost . Once the new AOF file is created, Redis will switch from the old to the new file AOF AOF file and start the new AOF file append operation.
- AOF save the file in an orderly manner all write operations performed on the database, the write operation to save the format Redis protocol, so the contents of AOF file is very easy to read people, to file for analysis (parse) is also very easy. Export (export) AOF file is very simple: For example, if you are not careful FLUSHALL execute the command, but as long as AOF file has not been overwritten, so long as the server stops, remove FLUSHALL command AOF end of the file, and restart Redis, you can restore the data set to the state before FLUSHALL execution.
AOF disadvantages:
- For the same data sets, files generally AOF volume greater than the volume RDB file.
- According to fsync strategy being used, AOF speed may be slower than RDB. In general, fsync per second performance is still very high, and close fsync allows AOF speed and RDB as fast, even under high loads. But when dealing with a huge write load, RDB can provide more assured of the maximum delay time (latency)
3, RDB and Comparative AOF