Redis learning notes [interview questions]

Introduction to redis

The redis database is different from the traditional database. The data of redis is stored in memory, and the reading and writing speed is very fast.

Redis can be applied in the following directions: caching, distributed locks, transactions, persistence, Lua scripts, LRU driven events, and various clustering schemes.

Why redis

High performance:

High concurrency:

Why not use map as cache

Cache is divided into local cache and distributed cache.

Map:

Redis or memcached:

Redis thread model

https://www.javazhiyin.com/22943.html

Redis internally uses the file event handler, filter event handler, which is single threaded. Therefore, redis is called a single threaded model.

The IO multiplexing mechanism is used to monitor multiple sockets at the same time, and the corresponding event processor is selected for processing according to the events on the socket.

Four parts of the file processor:

Multiple sockets may generate different operations concurrently, and each operation corresponds to different file events. However, the IO multiplexer will listen to multiple sockets and queue the events generated by sockets into the queue. The event dispatcher will take one event from the queue each time and hand the event to the corresponding event processor for processing.

Differences between redis and memcached

Analysis of common data structures and usage scenarios of redis

String

Common operations: set, get, decr, incr, mget

String data structure is a simple key value type, which is applied to conventional caching applications: number of microblogs, number of fans, etc.

Hash

Common operations: hget, hset, hgetall

A mapping table of string type field and value. Hash is suitable for storing objects. During subsequent operations, you can directly modify the value of only one field in the object. For example:

key=user122
value={
    "id": 1,"name":"summerday","age":20
}

List

Common operations: lpush, rpush, lpop, rpop, lrange

Two way linked list can support two-way search and traversal, and can realize the of message list and fan attention list. Lrange command can realize how many elements to read from a certain element, and can realize simple high-performance paging.

Set

Common operations: Sadd, spop, smembers, Sunion

It provides a function similar to list, but the elements in set are automatically de duplicated. In addition, the operation of intersection, union and difference set can easily realize the function of common concern and common fans.

Sorted Set

Common operations: zadd, zrange, zrem, zcard

A weight coefficient score is added on the basis of set, so that the elements in the set can be arranged orderly according to score, and various ranking lists can be realized.

Redis setting expiration time function

Generally speaking, login information such as verification code and token will have time limit. According to the processing method of traditional database, you may need to judge whether it has expired, which will affect the performance of some projects.

Redis has the function of setting the expiration time. When setting a key, set an expiration time to specify the key survival time through the expiration time.

Redis can delete keys in two ways:

If we set a large number of expired keys, many keys are missed during periodic deletion, and the system does not check this key in time, it will lead to a serious situation: a large number of expired keys accumulate in memory, leading to the depletion of redis memory.

At this time, the memory elimination mechanism of redis is needed.

Redis provides data elimination strategy

Added after version 4.0:

Persistence mechanism of redis

Redis supports two kinds of persistent operations, while memcached does not support persistent operations.

Redis transaction

Redis implements transaction functions through multi, exec, watch and other commands.

In the traditional relational database, acid is often used to test the reliability and security of transaction function.

In redis, transactions always have atomicity, consistency and isolation, and transactions also have durability when redis runs in a specific persistence mode.

Redis cache avalanche

If the cache fails in a large area at the same time, the subsequent requests will fall to the database, causing the database to bear a large number of requests and collapse in a short time.

[solution]

Cache penetration

A large number of requested keys do not exist in the cache at all, resulting in requests directly to the database without going through the cache layer at all.

You can use show variables like '% max'_ connections%'; Command to view the maximum number of connections to MySQL. If there are too many concurrent requests, the database can easily hang up.

[solution]

How to ensure data consistency when caching and database double write

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>