background

Redis is an open source memory data structure storage system.

Can be used as database, cache, and messaging middleware.

Support for multiple types of data structures.

Redis is built with replication, LUA scripting, LRU eviction, transactions, and different levels of disk persistence.

High availability through automatic partitioning of Redis Sentinel and Redis clusters.

Basic data types

Strings 1. The expiration time of a string is cleared after the value is reset

127.0.0.1:6379> set hello 3ok127.0.0.1:6379 > get hello"3" 127.0.0.1:6379> TTL hello(integer) -1127.0.0.1:6379> expire Hello 3000 (integer) 1 127.0.0.1:6379> set hello 4ok127.0.0.1:6379 > TTL hello(integer) -1Copy the code

2. Set a string value to override any other type

127.0.0.1:6379> sadd settest 1,2 (integer) 1 127.0.0.1:6379> type setTestSet127.0.0.1:6379 > set settest HelloOK127.0.0.1:6379 > type setteststring127.0.0.1:6379 > sadd settest a, b (error) WRONGTYPE Operation against a key holding the wrong kind of valueCopy the code

Hashes

Lists

Redis lists are implemented based on Linked Lists. Head and tail operation speed, slow retrieval

Sets

Sorted sets that support range lookups

The sorting of ordered collections defaults to lexicographical order

bitmaps

hyperloglogs

Support geospatial query by radius index

Application scenarios

string

Cache data

Both simple and complex data can be converted directly to string storage.

Key: active: Spring2019 :title value: “2019 Spring Festival Activity “Operation: set

Commodity information, provincial information, activity configuration and a series of cold data cache that does not change often

Very popular data cache, game rankings, background updates every second data

Simple counting

Number of participants in 2019 Spring Festival Activities

Key: active: Spring2019: Total Value: 3045 Operation: INCR

A person can only check in once a day

Key: active: the checkin: userId: a 10000 – day: 20190101 value: check-in timestamp operation: the expire

The code below distributed lock is not strict, nX can be concurrent

127.0.0.1:6379> set lockkey 1 nxok127.0.0.1:6379 > set lockkey 1 nx(nil)Copy the code

list

Users queue push, pop

Ordered messages push, POP

Implement producer and consumer model blocking access to BRPOP and BLPOP commands

set

De-list the number of participants in the 2019 Spring Festival activity

Key: active:spring2019:users value: 100010,10020 operations: many

Label User label

Merchants label

There are five abcDE tasks in the Spring Festival activity. User A has completed A and B, and user B has completed C and D

Intersect The tasks completed by both users A and B

Combine the tasks completed by user A and user B

Task that user A has not completed

Obtain a random gift from the gift library set

hash

Users with different attributes of the same resource received different kinds of prizes during the campaign

Key: active:spring:g’ifts:user:10010 value: {“giftA”:2,”giftB”:5

Incr can be performed on giftA directly

zset

Ranking user consumption ranking, likes ranking, etc

Key: active: Spring :star:rank value: user ID, score: number of likes Operation: many

Get the top 10 according to the score

Query the score of a user

Query users with scores between 90 and 100

Sometimes our score is not determined by a certain business value, can be sorted by two business value, such as look at the user’s actual score, look at the user level, then we can use before the decimal point in design when the score values mean score, the level of value after the decimal point, if there are other special requirements, You can also think about the score plus some maximum.

Matters needing attention

Each key should have a reasonable expiration time

The expiration time of string will be overwritten after the value is reset

A string set operation can override a type

Use appropriate data structures

Do not use list to store a large amount of data and retrieve the number of reasonably planned keys to determine whether users have participated or not. Set should be used instead of one key per user

Business data isolation User RedIS business Redis activity Redis should be distinguished and the activity redis can be cleaned freely after the activity

Use pipes, Lua scripts, and Redis transactions wisely to improve performance, especially when using Redis in scripts

On Reids online systems with a large number of keys, disable the keys * operation in the main library to prevent jams