Introduction and simple use of redis

table of Contents

1. Introduction

2. Introduction to the 5 basic data types of redis

1. String String type:

2. List list:

3. Hash:

4. Collection set:

5. Ordered set zset:

Three, redis as a cache will encounter problems

1. Cache penetration:

2. Cache breakdown:

3. Cache avalanche:


1. Introduction

Redis is a completely open source non-relational key_value database. Many companies also call it a cache database. Why is it so called? It is because many companies use highly available redis clusters to achieve the purpose of caching data on the project.

Why use redis as a cache? Some companies use memcached. First of all, redis has a very fast read and write speed. Second, redis has a variety of data structures to choose from. Not only that, Redis also has a native cluster mode;

For data that has a large amount of data, is rarely modified and frequently queried, it can be placed in the cache. For example, the permissions of the most commonly used user roles in our system are often placed in the cache.

In addition to being used as a cache, in distributed systems, redis is often used as a distributed lock to control the execution of distributed programs.

As a cache database, redis has the following characteristics:

1. Redis supports persistence, you can put the data in the memory to the disk, even if the redis restarts, the data exists, you can load it and continue using it.

2. There are 5 data types supported by redis. In addition to the basic key_value data, there are also 4 data structure forms such as list, set, zset, and hash.

3. Redis can also set the data expiration time, and the data will be automatically cleared after expiration.

4. Redis also supports master-slave backup. Master_slave mode can be used to achieve master-slave backup.

5. The performance of Redis is extremely high-the theoretical read speed is 110000 times/s, and the write speed is 81000 times/s

6. Disaster recovery, redis data loss can be recovered by using aof


2. Introduction to the 5 basic data types of redis

We first add some data to redis, and then use the desktop management tool RedisDesktopManager to see the style of the data

1. String String type:

Features: A sequence composed of bytes is the most basic type.

redis 127.0.0.1:6379> SET string_demo "hello world"
OK
redis 127.0.0.1:6379> GET string_demo 
"hello world"

2. List list:

Features: an ordered and repeatable sequence composed of multiple string values, linked list structure.

redis 127.0.0.1:6379> lpush listdemo redis
(integer) 1
redis 127.0.0.1:6379> lpush listdemo mongodb
(integer) 2
redis 127.0.0.1:6379> lpush listdemo rabbitmq
(integer) 3
redis 127.0.0.1:6379> lrange listdemo 0 10
1) "rabbitmq"
2) "mongodb"
3) "redis"

3. Hash:

Features: A container similar to Map<String,String> can store multiple key-value-like data in a specified key. Each hash can store 4294967295 key-value pairs.

redis 127.0.0.1:6379> HMSET hashdemo book1 "abc" book2 "def"
"OK"
redis 127.0.0.1:6379> HGET hashdemo book1 
"abc"
redis 127.0.0.1:6379> HGET hashdemo book2 
"def"

4. Collection set:

Features: disorder and non-repeatable

redis 127.0.0.1:6379> sadd runoob redis
(integer) 1
redis 127.0.0.1:6379> sadd runoob php
(integer) 1
redis 127.0.0.1:6379> sadd runoob java
(integer) 1
redis 127.0.0.1:6379> smembers runoob
1) "php"
2) "redis"
3) "java"

5. Ordered set zset:

Features: Similar to set, no duplication is allowed, the feature is that each member will have a score associated with it

redis 127.0.0.1:6379> zadd zsetdemo 70 go
(integer) 1
redis 127.0.0.1:6379> zadd zsetdemo 80 redis
(integer) 1
redis 127.0.0.1:6379> zadd zsetdemo 90 java
(integer) 1
redis 127.0.0.1:6379> zadd zsetdemo 100 mysql
(integer) 0


So far, the five basic data structures of redis have been introduced. However, using redis in actual projects is not so simple. It is necessary to decide which data structure to choose for caching according to the specific business type.

Three, redis as a cache will encounter problems

1. Cache penetration:

Simply put, every time you query the redis cache, it is not found, which leads to database queries such as mysql, which increases the pressure on the database.

2. Cache breakdown:

Very high concurrent access to a cached value in a short time, but the cache just expired, resulting in a large number of requests to access the database.

3. Cache avalanche:

In a certain period of time, the centralized cache expires.

In fact, the above three problems are, in the final analysis, that the cache does not play its due role, resulting in increased pressure on the database, which may cause the database to crash or crash.

There are many solutions to the above problems on the Internet, and interested partners should try it out.

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_37488998/article/details/112692429