[TOC]

I. Introduction to Redis

Redis is a database based on key-value pairs, in which value can be a string, hash, list, set, zset and other data structures, which can meet many application scenarios. It also provides key expiration, publish and subscribe, transaction, pipeline, and other additional functions

Second, Redis features

  • 1 speed
  • 2 key value pair data structure server
  • 3 Rich functions
  • 4 Simple and Stable
  • 5 the persistence
  • 6 Primary/secondary Replication
  • 8 High availability and distributed migration
  • 9 Multiple client languages

Three, use scenarios

  • 1 Cache database
  • 2 list
  • 3 Counter Application
  • 4 Social Networks
  • 5 Message Queue

4. Data types

1.String

The string type is the most basic data structure of Redis. First, the key is the string type, and other structures are built on the basis of the string type, so the string type can be the basis for learning the other four data structures.

The string type can be a string (simple string, complex string (XML, JSON), number (integer, floating point), binary (image, audio, video), but cannot exceed 512 MB.

  • Example:
Redis 127.0.0.1:6379 > SET name"runoob"
OK
redis 127.0.0.1:6379> GET name
"runoob"
Copy the code
  • Usage Scenarios:

Cache function: The most classic use scenario of strings, redis is the cache layer, Mysql as the storage layer, most of the requested data is obtained from Redis, because Redis has the characteristics of supporting high concurrency, so cache can usually play a role in accelerating read and write and reducing back-end pressure.

Counter: Many applications will use Redis as a basic tool for counting, it can achieve fast counting, query cache functions, while the data can be dropped to other data sources. For example, the video playback system uses Redis as the basic component of the video playback counting.

Sharing session: In consideration of load balancing, distributed services will balance access to user information to different servers, and users may need to re-log in once refreshing access. To avoid this problem, user sessions can be centrally managed by REDis. In this mode, as long as the high availability and scalability of Redis are guaranteed, Every time you retrieve user updates or query login information, it is centrally fetched directly from Redis.

Rate limiting: For security purposes, users are required to enter mobile phone verification codes each time they log in. To prevent frequent access to the SMS interface, users are limited in obtaining verification codes every minute.

2.Hash

In Redis, hash type means that the key itself is a key-value pair structure, such as value={{field1,value1},…… {fieldN, valueN}} instead!

A Redis hash is a set of key=>value pairs.

Redis hash is a mapping table of fields and values of string type. Hash is especially suitable for storing objects.

Each hash can store 232-1 key-value pairs (over 4 billion)

  • Using the instance
redis> HMSET myhash field1 "Hello" field2 "World"
"OK"
redis> HGET myhash field1
"Hello"
redis> HGET myhash field2
"World"
Copy the code
  • Usage scenarios

The hash structure is more intuitive than string serialization cache information and is easier to update. So often used for user information management, but hash type and relational database is different, hash type is sparse, and relational database is completely structured, relational database can do complex relational queries, and REDis to simulate relational complex queries, difficult development, high maintenance costs.

() 3.

The list type is used to store multiple ordered strings, each string in the list becomes an element, a list can store up to 2 ^ 32 -1 elements, in redis, can queue both ends of the table insert (pubsh) and pop (pop), You can also get the list of elements in a specified range, and the elements of the table under a specified index. The list is a more flexible data structure, which can act as a stack and queue. There are many application scenarios in the actual development.

Lists can store up to 232-1 elements (4294967295, more than 4 billion per list).

  • Example:
Redis 127.0.0.1:6379> lpush runoob redis (integer1 redis 127.0.0.1:6379> lpush runoob mongodb (integer) 2
redis 127.0.0.1:6379> lpush runoob rabitmq
(integer) 3
redis 127.0.0.1:6379> lrange runoob 0 10
1) "rabitmq"
2) "mongodb"
3) "redis"Redis 127.0.0.1:6379 >Copy the code
  • Usage scenarios

Message pairs: The combination of Redis lpush+ BRPOP commands can block queues, producer clients insert elements from the left side of the list with lupsh, and multiple consumer clients “grab” elements at the end of the list with BRPOP blocking. Multiple clients ensure load balancing and high availability for consumption

Article list: Each user has their own article list, now need to paging display article list, you can consider using the list, the list is not only ordered, at the same time to support the index range of elements.

Tips:

  • Lpush + lpop = Stack (Stack)
  • Lpush +rpop=Queue
  • Lpush + LTRIM =Capped Collection
  • Lpush + brPOP =Message Queue

4.Set

Collection type is also used to store multiple strings of elements, but unlike the list is not allowed to have a repeat element in the set, and the elements in the collection is unordered, not available by index subscript elements, redis in addition to support within a collection to add and delete, as well as support for multiple set intersection, and set, difference set, and the use of reasonable good collection types, Can solve many practical problems in the actual development.

Collections are implemented by hashing tables, so adding, deleting, and searching are O(1) complexity.

The maximum number of members in a collection is 232-1 (4294967295, each collection can store more than 4 billion members).

  • Example:
Redis 127.0.0.1:6379> sadd runoob redis (integer1 redis 127.0.0.1:6379> sadd runoob mongodb (integer1 redis 127.0.0.1:6379> sadd runoob rabitmq (integer1 redis 127.0.0.1:6379> sadd runoob rabitmq (integer) 0
redis 127.0.0.1:6379> smembers runoob

1) "redis"
2) "rabitmq"
3) "mongodb"
Copy the code
  • Usage scenarios

Label (tag) : collection types more typical usage scenarios, such as a user interested in entertainment, sports comparison, another possible to news of interest, the interest is the label, with these data can get the same label, the label and the common interest of the user, the data for the user experience, and has been strong user viscosity is more important. (The maintenance of the relationship between users and labels should be carried out in one thing to prevent data inconsistency caused by partial command failure)

Sadd =tagging spop/ srandMember =random item sadd+ Sinter =social Graph

5.Zset(sorted set)

The ordered set is necessarily related to the set, which retains the feature that the set cannot have duplicate members, but the difference is that the elements in the ordered set can be sorted, but it is different from the list that uses index subscript as the sorting basis, but it sets a score for each element as the sorting basis. (The elements of an ordered set cannot be repeated, but the csore can be repeated, just as the student numbers of a class cannot be repeated, but the test scores can be the same).

  • Using the instance
Redis 127.0.0.1:6379> zadd runoob 0 redis (integer1 redis 127.0.0.1:6379> zadd runoob 0 mongodb (integer) 1
redis 127.0.0.1:6379> zadd runoob 0 rabitmq
(integer) 1
redis 127.0.0.1:6379> zadd runoob 0 rabitmq
(integer) 0
redis 127.0.0.1:6379> > ZRANGEBYSCORE runoob 0 1000
1) "mongodb"
2) "rabitmq"
3) "redis"
Copy the code
  • Usage scenarios

Leaderboards: An ordered collection of classic usage scenarios. For example, a video website needs to make a list of videos uploaded by users. The maintenance of the list may be in many aspects: by time, by play quantity, by the number of likes obtained, etc.

  • Comparison of different types

Publish and subscribe

Redis provides a “publish and subscribe” message mechanism, in which message subscribers and publishers do not communicate directly, but publishers publish messages to a specified channel, and each client subscribing to the channel can receive messages.

Redis mainly provides publishing messages, subscribing to channels, unsubscribing, and subscribing and unsubscribing to patterns.

1. Publish and subscribe commands

  • news
publish channel:test "hello world

Copy the code
  • Subscribe to news
subscrible channel:test

Copy the code
  • Viewing subscriptions
pubsub numsub channel:test

Copy the code
  • unsubscribe
unsubscribe channel:test

Copy the code
  • Subscribe by pattern and unsubscribe by pattern
psubscribe ch* 

punsubscribe ch*
Copy the code

2. Application scenarios

  • 1. Toutiao Subscription account, wechat subscription public account, Sina Weibo attention, email subscription system
  • 2, even the communication system
  • 3. Group chat tribal system (wechat group)

Redis persistence

Redis is an in-memory database that supports persistence, that is, Redis needs to constantly synchronize data in memory to disk to ensure persistence, which can avoid data loss caused by process exit.

1. Persistence

RDB Persistence The process of saving the snapshot (.rdb) file of the current process data to the hard disk.

  • Manual trigger

The save command: blocks the current Redis until the RDB persistence process is complete. it is not recommended in online environments if large memory instances cause long blocking.

Bgsave command: the redis process forks to create a sub-thread, which is optimized for save. The process blocks in a short time (microseconds), and automatically executes the bgsave command if AOF persistence is not enabled when redis is shutdown

  • Automatic trigger

Conf setting: appendonly yes (not enabled by default, no) Default filename: appendfilename “appendone.aof”

2. BgSave Running process

The schematic diagram of the operation process is as follows:

3.RDB file operations

  • Example Set the path for saving RDB files
config set dir /usr/local   Dump. Rd to /usr/local/
Copy the code
  • Start persisting data
bgsave
Copy the code
  • Restore data

Dump. RDB into the same redis installation directory as redis. Conf and restart Redis

Advantages:

1. Compressed binary text, suitable for backup and full copy, and disaster recovery 2. Data recovery in RDB mode is much faster than that in AOF mode

Disadvantages:

1. Real-time persistence cannot be achieved, and child processes need to be created every time. Frequent operation costs are too high. The saved binary file is incompatible with the RDB file of the new version.

4. AOF persistence

For RDB is not suitable for real-time persistence, Redis provides AOF persistence to solve the problem

  • Start persisting

Redis. Conf setting: appendonly yes (not enabled by default, no) Default filename: appendfilename “appendone.aof”

  • AOF persistence process

1. All write commands (set hset) are appended to the aof_buf buffer

2. Synchronize the AOF buffer to the hard disk

3. As AOF files become bigger and bigger, it is necessary to rewrite AOF files periodically to achieve compression

4. When the Redis service restarts, load the AOF file for restoration

  • Description of AOF configuration parameters
Appendonly yes // Enable aOF persistence#appendfsync always force write to disk as soon as a write command is received, slowest, but ensures full persistence, not recommendedAppendfsync everysec // forces writes to disk once per second, a compromise between performance and persistence, recommended#appendfsync no // fully dependent on OS, best performance, persistence not guaranteed (OS own synchronization)No-appendfsync-on-rewrite yes // Do you want to stop synchronizing aof auto-aof-rewrite-percentage 100 // Rewrite aof when the file size increases by 100% compared to the last time it was rewritten Auto-aof -rewrite-min-size 64mb //aof file, rewritten when at least 64mbCopy the code
  • AOF recovery

Set appendonly yes. 2. Put appendonly.aof in directory 3 specified by dir. When you start Redis, the file appendone.aof is automatically loaded

  • Load sequence of AOF and RDB

If both AOF and RDB files exist, load AOF preferentially. 2. If AOF is disabled, load RDB file 3. The AOF/RDB is loaded successfully, and the REDis is restarted successfully. 4. An error message is displayed indicating that the AOF/RDB fails to be started