preface

First time with a title like that. Anyway, this is a Blog about Redis related knowledge points, will be asked in the interview (proportion will be relatively large ha), continue to collect updates

What is Redis?

This is rarely asked, usually to ask where you are using Redis in real development, but it’s good to know; Redis is the mainstream key-value pair storage database (NoSQL), usually all data is in memory (one of the main reasons why it is so fast!). Redis supports rich data types (for most scenarios), and Redis is single-threaded (no thread-safety issues, no locks).

Data types supported by Redis

Before we look at what Redis can do, let’s take a look at the data types that Redis supports

Six common data types

  • Binary Redis Key Any binary sequence can be used as a Key, from characters to the contents of JPEG files. The maximum value allowed for blank characters is 512 MB
  • String String
  • List the List
  • Zest/Sorted Sets
  • The Set collection
  • The Hash of a dictionary

The advanced

  • Bitmap/Bitmap
  • HyperLogLogs/Computes probabilistic data structures for unique things

What can Redis do?

This is the most practical question, and one that Redis frequently asks in interviews

  • Cache/cache: Accelerates the query speed, stores the session cache of hotspot data, records the number of online users by timeliness, and incR

  • MySQL is not limited to I/O operations, supports transactions, and supports rich data structures. It would be a pity not to do DB storage. A String of incr

  • Message queue/Message Broker

    Redis is also a pub-sub server that subscribes with three commands, UNSUBSCRIBE and PUBLISH implement information subscription publishing service official has a practice case multi-user high performance Web chat, using EventMachine and Redis: link

What are the advantages over memcached?

The difference between Spring and Spring Boot, MyBatis and Hibernate, Redis is generally compared with NoSQL

  • Memcached supports only characters, while Redis supports rich data types
  • Redis supports persistent data. Memcached does not. It can only be stored in memory
  • Memcached is not as flexible as Redis. The default Key is 250 bytes and the Value is 1MB, but memcached is designed to store small chunks of arbitrary data (strings, objects). This data can be the result of database calls, API calls, or page renders.

Redis persistence strategy

Redis persistence policy is a frequently asked question in interviews. Redis provides two persistence policies. You can choose one of them based on service requirements, or enable or disable both of them

  • RDB persistence (Reids periodically dump in-memory database records to disk for RDB persistence)
  • AOF persistence (operation logs of Reids are appended to files)

AOF

AOF persistence records every write and delete operation processed by Redis service in the form of log and saves it in text. When Redis restarts, it preferentially recovers data from AOF files and executes commands in AOF logs one by one during server startup

RDB

Performs a point-in-time snapshot of the data set at a specified interval. Redis saves a snapshot of the data set on disk in a file called dump.rdb. If there are at least M changes in the dataset, Redis can be configured to generate a snapshot, or the SAVE or BGSAVE command can be invoked manually. Here is an example of RDB persistence configuration:

Configure the following two lines in the Redis configuration file to generate a snapshot if one of them is satisfied
save 60 1000  Every minute, if there are 1000 write commands, a snapshot is generated
save 3600 10000 Every hour, 1W write commands are used to create snapshots
Copy the code
  • Redis can manually use the SAVE command. Executing the SAVE command will use the main process to perform the snapshot operation, which means blocking the main process during the SAVE process. With BGSAVE, Redis forks a child process to perform the snapshot without affecting the main process.

  • RDB is a very compact, single-file point-in-time representation of Redis data. RDB files are great for backup. For example, we want to archive RDB files hourly within the last 24 hours. RDB is very suitable for disaster recovery. It can transfer a single compressed file to a remote data center. Compared with AOF, RDB is more suitable for Redis with a large amount of data, and can restart Redis faster.

The relevant point of cache failure

First of all, the following database pressure problem is based on high concurrency as the background, relatively speaking, this kind of scene and business is not met with many iron juice, so do not worry about your server will hang the problem

Cache avalanche

First of all, the following database pressure problem is high concurrency as a background, relatively speaking, this kind of scene and business is not met many iron juice, so do not worry about directly go to the database, service is not available

How to solve cache avalanche problem?

The first thing we need to know is, how do we deal with avalanches

  • Prevention:
    1. Redis service outages, we can build a highly available cache architecture, such as sentinel + master-slave architecture or cluster, to avoid a single Redis failure resulting in cache service unavailability
    2. Since the fear of database pressure and downtime, then do DB cluster, share the pressure
    3. The system can access cache middleware (memcached) other than Redis. If Redis is unavailable, the system can use cache services through other middleware
    4. Monitor Redis service operation, related alarm mechanism
  • What if there is an avalanche? Services are degraded and traffic limiting to prevent MySQL downtime
  • How to limit the damage after an avalanche? O&m engineers restart Redis and recover cached data using the persistence policy

Cache breakdown

The difference between cache penetration and cache avalanche is that cache avalanche is a large number of Key failures, while penetration is a hot Key failure, a large number of requests to access the DB, resulting in service unavailability

The cache to penetrate

Query a data do not exist, because the cache is hit, not passive, and for the sake of fault tolerance, do not check if the storage layer data is not written to the cache, this will lead to this there is no data every request to go to the storage layer query, lost the meaning of the cache In the concurrent, DB may hang up pressure is too high, If someone frequently attacks our system services through this vulnerability, the consequences are unimaginable

reference

Redis:WiKi memcached:GitHub