First: what is Redis?

Redis is a memory-based, persistent, logging, key-value database high-performance storage system, and provides multiple language apis.

Second: the background

There are more and more Data Structure requirements, but memcache does not have them, which affects the development efficiency

Performance requirements need to be addressed as the number of read operations increases. The process is as follows: Database read/write separation (M/S) – > Database use multiple slaves – > Add Cache (memcache) – > Transfer to Redis

Solve the problem of writing: horizontal split, split the table, put some users in this table, some users in another table;

Reliability Requirements Cache avalanches challenge Cache recovery

The maintenance cost of Cache and DB consistency is getting higher and higher. The development needs to keep up with the continuous influx of product requirements. The most expensive hardware cost is the database level machine, which is basically several times more expensive than the front-end machine. It is mainly IO intensive and consumes much hardware.

Complex maintenance consistency maintenance costs are increasing; BerkeleyDB uses B tree, it will always write new, no internal file reorganization; This will result in larger and larger files; Big time need to file, file operation to do regularly; In this way, there needs to be a certain down time;

Based on the above considerations, Redis is selected

Third, the application of Redis in Sina Weibo

Introduction of Redis

  1. Five data structures are supported

Supports strings, hashes, Lists, sets, sorted Sets String is a good way to store, used for counting storage. Sets are great for building index libraries;

  1. K-v storage vs K-V cache

At present, 98% of the applications used by Sina Weibo are persistent applications, and 2% are cache applications. When 600+ servers are used, there is no big difference between persistent applications and non-persistent applications: non-persistent is 80,000-90,000 TPS, so the persistence is about 70,000-80,000 TPS; When using persistence, you need to consider the ratio of persistence and write performance, that is, to consider the ratio of redis memory size to disk write rate.

  1. The community is active

Redis currently has more than 30000 lines of code, the code written by streamlining, there are a lot of clever implementation, the authors have technical cleanliness Redis community activity is very high, this is an important indicator to measure the quality of open source software, the early stages of the open source software is generally no commercial technical service support, if there is no active community, in the event of problems are no distress;

Redis fundamentals

Redis persistent (AOF) append online file: write log(AOF), then merge with memory. Append and append data to disks in sequence, which has little impact on performance

  1. Single instance single process

Redis uses a single process, so an instance is configured using only one CPU; To maximize CPU usage, you can configure the number of Redis instances corresponding to the number of cpus, and the number of Redis instances corresponding to the number of ports (8-core CPU, 8 instances, and 8 ports) to improve concurrency. In a single-machine test, a single piece of data is 200 bytes, and the test result is 80,000-90,000 TPS.

  1. Replication

Procedure: Data is written to master – > Master stores the RDB to slave – >slave loads the RDB to memory. Storage point (Save point): When the network is disconnected, after connecting, continue to upload. Master-slave The first synchronization is full, followed by incremental synchronization. ,

  1. Data consistency

There is possibility of inconsistency between multiple nodes after long-term operation; Two tools were developed: 1. For the data with a large amount of data, periodic full check was performed; 2. 2. Check whether incremental data is consistent in real time.

The inconsistency caused by the master library not synchronizing the slave library in time is called delay problem. For scenarios with less stringent consistency requirements, we just need to ensure final consistency; For the delay problem, it is necessary to analyze the characteristics of business scenarios and add policies from the application level to solve this problem. For example: 1. The newly registered user must query the master database first. 2. After the registration is successful, data synchronization is performed in the background.

Fourth: the architecture design of distributed cache

1. Architecture design

Because Redis is a single point, it needs to be used in projects and must be distributed itself. The basic architecture diagram is as follows:

2. Distributed implementation

To achieve the distribution of redis nodes corresponding to key.

Implementation of consistent hashing:

Hash calculation: Supports MD5 and MurmurHash. MurmurHash is used by default for efficient hash calculation.

Implementation of consistency: Through Java TreeMap to simulate the ring structure, to achieve uniform distribution

3. The client’s choice

The main modification for Jedis is the modification of partition module, so that it supports partitioning according to BufferKey, according to different Redis node information, can initialize different ShardInfo, and also modified the underlying implementation of JedisPool. Make its connection pool pool support the construction method of key and value according to different ShardInfos, create different Jedis connection client, achieve the effect of partition, for the application layer to call

4. Module description

Dirty data processing module that handles failed cache operations.

Shielding monitoring module, for abnormal monitoring of JEDis operation, when a node is abnormal, it can control redis node resection and other operations.

The whole distributed module excites abnormal REDis nodes through Hornetq. The addition of new nodes can also be achieved through the reload method. (This module can also be easily implemented for new nodes).

The implementation of the above distributed architecture meets the requirements of the project. In addition, some redis nodes can be set separately for cache data of some important purposes and set specific priorities. In addition, the design of the cache interface can also be based on the requirements to achieve the basic interface and some special logic interface. Cas related operations and some things operations can be realized through its Watch mechanism.