High Concurrency Architecture Series: Basic introduction to Redis, analysis of five data types and application scenarios

1. Introduction to the basic concepts and features of Redis

1.1 Introduction to the basic concepts of Redis

High Concurrency Architecture Series: Basic introduction to Redis, analysis of five data types and application scenarios

 

1. Redis is remote, with a client and a server, we generally say the server;

2. Redis is based on memory , so it is much faster than MySQL based on hard disk, but it eats memory very much

3. Redis is a non-relational database. It is also a database in essence, but a data dictionary must be defined when MySQL relational database is stored, while Redis does not need it.

1.2 Comparison of Redis and Memcached

High Concurrency Architecture Series: Basic introduction to Redis, analysis of five data types and application scenarios

 

Redis data types all support push/pop, add/remove, intersection, union and difference, and richer operations, and these operations are all atomic.

Like Memcached, in order to ensure efficiency, data is cached in memory. The difference is that Redis periodically writes updated data to disk or writes modification operations to additional record files, and on this basis, it realizes master-slave (master-slave) synchronization.

1.3 Redis supports master-slave synchronization.

Data can be synchronized from the master server to any number of slave servers, and the slave server can be the master server associated with other slave servers. This allows Redis to perform single-level tree replication. Save to disk can write data intentionally or unintentionally. Since the publish/subscribe mechanism is fully implemented , it can subscribe to a channel and receive the complete message publishing record of the master server when synchronizing the tree from the database anywhere. Synchronization is very helpful for the scalability and data redundancy of read operations.

Two, Redis application scenarios

2.1 Cache

High Concurrency Architecture Series: Basic introduction to Redis, analysis of five data types and application scenarios

 

Redis is often used as a data cache .

When some system interfaces are slow, we can cache some data in Redis. Next time we fetch data, we won’t perform very time-consuming SQL operations. We will fetch data directly from the cache to improve system performance. One of the methods;

2.2 Queue

Push and pop operations ensure the realization of atomicity;

2.3 Data storage

All additions, deletions, modifications, and checks are performed in Redis. Redis has a hard disk persistence mechanism, which is stored regularly to ensure data integrity and security.

  • View the redis client: which redis-cli
  • Login: redis-cli (login to port 6379 of this machine by default)
  • info command to view redis information

Three, Redis data type

In redis, in addition to \n and spaces can not be used as the component content of the name, other content can be used as the name part of the key. The length of the name is not required.

High Concurrency Architecture Series: Basic introduction to Redis, analysis of five data types and application scenarios

 

High Concurrency Architecture Series: Basic introduction to Redis, analysis of five data types and application scenarios

 

3.1 String type operation

High Concurrency Architecture Series: Basic introduction to Redis, analysis of five data types and application scenarios

 

Function: Provide string or integer operations.

set key value(string/int/float)
get keyincr string [value]//自增decrby string [value] //自减

String is the most basic type of redis

The redis string can contain any data. Including jpg images (binary) or serialized objects.

The maximum upper limit of a single value is 1G bytes.

If only the string type is used, redis can be regarded as memcache with persistence features.

High Concurrency Architecture Series: Basic introduction to Redis, analysis of five data types and application scenarios

 

3.2 List linked list type

High Concurrency Architecture Series: Basic introduction to Redis, analysis of five data types and application scenarios

 

Function: storage sequence collection, can be used for queue, push and pop operations

lpush left in 
rpop and right out llen key to see how many elements in the list

List type is first-in, first-out, suitable for queues; List does not require the elements in it to be unique

(1) Introduction

Get the latest 10 login user information: select * from user order by logintime desc limit 10;

The above sql statement can meet user needs, but when there is a lot of data, all data will be affected, and the load on the database is relatively high. If necessary, it is also necessary to set an index for the key field (id or logintime), which also consumes system resources.

If the above functions are achieved through the list linked list, only the latest 10 data can be kept in the list linked list, and each new data will be deleted every time a new data comes in. Every time you can get the required data directly from the linked list. Greatly save all aspects of resource consumption

(2) Application

The list type is actually a doubly linked list. Add and delete elements from the head or tail of the linked list through push and pop operations.

This allows the list to be used both as a stack and as a queue.

Top in, top out: stack

Top in and bottom out: queue

High Concurrency Architecture Series: Basic introduction to Redis, analysis of five data types and application scenarios

 

High Concurrency Architecture Series: Basic introduction to Redis, analysis of five data types and application scenarios

 

  1. lrange key 0 -1 Take out all elements.
  2. lrem key n/-n X Delete the specified element n times.
  3. linsert key after/before key1 key2 Insert key2 after/before key1.
  4. rpoplpush key1 key2 puts the right key of key1 into the key2 linked list, atomicity .

High Concurrency Architecture Series: Basic introduction to Redis, analysis of five data types and application scenarios

 

Function: Unordered collection, each element is different .

High Concurrency Architecture Series: Basic introduction to Redis, analysis of five data types and application scenarios

 

3.3 Set type

The set type stores multiple different elements in an unordered manner, and can quickly add, search and delete elements ; the same element is only counted as one .

The set of redis is an unordered collection of string type.

The set element can contain up to (2 to the 32th power-1) elements.

Regarding the set collection type, in addition to the basic add and delete operations,

Other useful set of operations further comprises taking the union (union), the intersection (intersection),  the difference set (difference). Through these operations, the friend recommendation function in sns can be easily realized .

Note: Each element in each set cannot be repeated.

High Concurrency Architecture Series: Basic introduction to Redis, analysis of five data types and application scenarios

 

This type of application: Recommended by QQ friends.

Tom circle of friends (friends with XX): mary jack xiaoming wang5 wang6 
linken circle of friends (friends with XX): yuehan daxiong luce wang5 wang6

High Concurrency Architecture Series: Basic introduction to Redis, analysis of five data types and application scenarios

 

3.4 hash type

High Concurrency Architecture Series: Basic introduction to Redis, analysis of five data types and application scenarios

 

Function: The key-value structure is stored, and the key must be a string

Often used to store more complex structures, Hash only requires different keys, such as name, age, etc.

Insert/modify: hset hash1 key1 12 
get: hash1 key1 view hash length: hlen key one-time get: hmget hash1 key1 key2

High Concurrency Architecture Series: Basic introduction to Redis, analysis of five data types and application scenarios

 

High Concurrency Architecture Series: Basic introduction to Redis, analysis of five data types and application scenarios

 

3.5 Sort Set Sort Set Type

High Concurrency Architecture Series: Basic introduction to Redis, analysis of five data types and application scenarios

 

Function: Storage is an ordered collection of scores, often used for leaderboards .

sort set is often used for ranking such as class performance ranking

Storage, like hash, is a mapping. What is stored is the mapping between scores and elements. It can be seen that it is the leaderboard.

The value must be globally unique.

score is a floating point type.

Sort set type operation, if the scores of the two elements are the same, they are arranged in byte order

Add/modify: zadd zset1 10.1 val1 
View number: zcard zset1 View ranking: zrange zset1 0 2 withscores View a certain value ranking: zrank zset1 val2

Like set, sorted set is also a collection of string type elements, the difference is that each element is associated with a weight . The elements in the set can be obtained in order by weight.

The Sort set type is suitable for occasions:

Get popular post (response volume) information: select * from message order by backnum desc limit 5;

(The above requirements can be achieved through simple sql statements, but sql statements consume mysql database resources)

Case: Use sort set to obtain the top 5 most popular posts

High Concurrency Architecture Series: Basic introduction to Redis, analysis of five data types and application scenarios

 

Each element in the sorted set is a combination of value and weight (each element of the previous set collection type is just a value)

High Concurrency Architecture Series: Basic introduction to Redis, analysis of five data types and application scenarios

 

We only make a sort set, and only keep 5 elements in it. The 5 elements are the most replies

When each post is replied, there is a chance to enter the collection, but only the top 5 posts with the highest number of replies will exist in the collection, and the ones with low replies will be deleted.

At last

Friends who read this can forward and follow, and more series of articles will be updated later to share and read!

Guess you like

Origin blog.csdn.net/python8989/article/details/108524328