Redis is currently one of the most mainstream cache technology, Redis based on memory operation and has a strong performance, can reach 100,000 requests per second, can be said to be a very powerful cache technology.

This paper is divided into three parts:

  • Introduction to Basic Knowledge
  • Common technical explanation and caching mechanism
  • Usage scenarios and cache issues

Introduction to Basic Knowledge

No overview

What is NoSQL?

NoSQL = Not Only SQL

Relational databases: tables, rows, columns

Non-relational databases: there is no fixed query language, key-value storage, column storage, document storage

With the birth of Web2.0 Internet! Traditional relational databases can hardly cope with the web2.0 era! In particular, very large, highly concurrent communities.

No characteristic

1, easy to expand

2. High performance with large data volume (Redis writes 80,000 times and reads 110,000 times per second)

3. Data types are diverse

There are four types of NoSQL

KV key pair: Redis, for example, is mainly used for content caching, mainly to handle high data access load

Document database: For example, MongoDBMongoDB is a database based on distributed file storage

Column storage Database: For example, an HBase distributed file system stores data in a column cluster

Graph relational database: for example, Neo4j does not store graphs, but puts relations, such as: circle of friends, social network, advertising recommendation!

Introduction of Redis

What is Redis?

Redis (Remote Dictionary Server) is an open source NoSQL database written in ANSI C with excellent performance, network support, and persistent key-value memory.

What can Redis do?

1. Memory storage and persistence.

2, high efficiency, can be used for caching

3. Publish and subscribe system

4, timer, page views!

5,…

Redis benefits

Mainly from the “high performance” and “high concurrency” these two points.

By caching database data, requests are read directly from memory without passing through the database, reducing database stress and improving performance.

Common technical explanation and caching mechanism

Redis mainly has five data types, including String, List, Set, Zset and Hash, which meet most of the requirements

String

  • String: session, object, small file (store file stream byte array, faster than disk IO)?
  • Int: seckill, current limiting, and count
  • Bitmap:

Scenario 1. Setbit and bitcount count the number of days in a year in which a user performs operations, and getbit count the number of days in a year in which a user performs operations

Scenario 2. Permission control. For example, each permission corresponds to a bit

list

Instead of collections in the Java JVM, they can be shared as data that cannot be shared or not shared well across multiple processes in Java

hash

You can make the Redis key smaller, similar to objects.

Scenario 1. The product details page, the corresponding collection number and inventory of the product are placed in Redis because it is atomic, and the multi-place access is real-time

Scenario 2. Aggregation scenario: An object may have different attributes in different tables in the database and can be aggregated into the same object in Redis

set

Set performance is slow, can be a separate redis instance

Scenario 1. The SRANDMEMBER or spop command can be used to draw prizes

Scenario 2. Random events

Scenario 3. Mutual Friends (Intersection)

Scenario 4. Recommend friends (Bad set)

sorted_set

Ordered set, when the number is small, the bottom is zipList compression table, more data skiplist

Scenario 1. Leaderboards

Scenario 2. Ordered events

Scenario 3. Comment pages

In addition to the five most common data types, there are three special data types in Redis

You can run the help command to query descriptions of related commands, for example:

help @string help @list1
Copy the code

The transaction

Redis transaction essence: a collection of commands! All commands in a transaction are serialized and executed in sequence during the execution of the transaction!

Redis is single imperative to preserve atomicity, but transactions do not guarantee atomicity!

Set k1 v1 set k2 v2 get k2 # exec 1Copy the code

Redis persistence

Persistence is to write the data in memory to disk to prevent the loss of memory data when the service is down.

Redis provides two persistence mechanisms: RDB (default) and AOF.

1, RDB,

RDB is Redis DataBase snapshot, the default persistence mode. The memory data is saved as a snapshot to the hard disk at a certain time. The generated data file is dump. RDB

triggering

(1) When the rules of SAVE are met

(2) Run the flushall command

(3) Exit redis, RDB file will also be generated

2, AOF:

AOF persistence (Append Only File persistence) records every write command executed by Redis to a separate log File. When Redis restarts, data will be recovered from the persistent log File.

The three AOF policies (1) always (2) Everysec (default) (3) No always

During application, select RDB or AOF based on your actual requirements. In fact, if you want to ensure data security, you can enable both methods. However, simultaneous I/O operations of the two persistence methods will seriously affect server performance, so you have to make a choice sometimes.

Redis primary/secondary replication

The concept of master-slave replication refers to the replication of data from one Redis server to other Redis servers. The former is called the master node (master/leader) and the latter is called the slave node (slave/follower). The replication of data is one-way and can only go from the master node to the slave node.

Advantages :(1) read/write separation (2) backup

Disadvantages: The primary server is down and needs to be manually started

The guard mode

Sentinel mode is a special mode, first of all Redis provides sentinel command, sentinel is an independent process, as a process, it will run independently. The idea is that sentry monitors multiple instances of Redis running by sending commands and waiting for the Redis server to respond.

Usage scenarios and cache issues

1. Cache of hotspot data

When the number of project users reaches a certain number, the reasonable use of cache can not only improve the speed of project access, but also greatly reduce the pressure of database.

2, business statistics, ranking

In order to keep the data realistic, such as the number of visits to the project, each view has to be +1. When the concurrency is high, it can be challenging and stressful to request database operations every time

3. Application of time-limited business

Service scenarios such as daily check-in and restricted login

4. Message queues

Provides basic publish-subscribe functionality, but not at the professional level of message queues

Cache avalanche

Cause: A large number of Redis keys fail at the same time, resulting in a large number of requests to access the database, the database server breaks down, and a large number of online services report errors.

Solutions:

(1) Redis is highly available

(2) Queuing with lock, traffic limiting degradation

(3) Cache failure time is evenly distributed

The cache to penetrate

Cause: Data that is not in the cache or the database, causing all requests to fall on the database, causing the database to collapse under a large number of requests in a short period of time.

Solution: (1) add check on interface layer (2) use Bloom filter

Cache breakdown

Cause: There is no data in the cache but there is data in the database (usually the cache time expires). At this time, there are too many concurrent users who read data from the cache and fetch data from the database at the same time. As a result, the pressure of the database increases suddenly, causing excessive pressure. For example, weibo is popular.

Solutions:

(1) Set no expiration time for the cache of hotspot data

(2) Add mutex

Ok, that’s it. You can try it yourself. Welcome to pay attention to the public number: Java treasure to receive more tutorial welfare