How to use Redis wisely in projects

  • 2020-06-19 12:01:31
  • OfStack

An overview of the

As a memory database with excellent performance, Redis has a variety of application scenarios in Internet companies. This article will introduce how TO use Redis reasonably in projects.

background

Redis is an open source memory data structure storage system. It can be used as database, cache, and messaging middleware. Supports multiple types of data structures. Redis has built-in replication (replication), LUA scripting (Lua scripting), LRU driven events (LRU eviction), transactions (transactions) and different levels of disk persistence (persistence). Provides high availability (high availability) through automatic partitioning of Redis sentry (Sentinel) and Redis cluster (Cluster).

Basic data type

String (strings)

1. The expiration time of string will be cleared after reset


127.0.0.1:6379> set hello 3
OK
127.0.0.1:6379> get hello
"3"
127.0.0.1:6379> ttl hello
(integer) -1
127.0.0.1:6379> expire hello 3000
(integer) 1
127.0.0.1:6379> set hello 4
OK
127.0.0.1:6379> ttl hello
(integer) -1

2. Set the value of string to override any other type


127.0.0.1:6379> sadd settest 1,2
(integer) 1
127.0.0.1:6379> type settest
set
127.0.0.1:6379> set settest hello
OK
127.0.0.1:6379> type settest
string
127.0.0.1:6379> sadd settest a,b
(error) WRONGTYPE Operation against a key holding the wrong kind of value
Hash (hashes) List (lists)

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

Set (sets) Ordered collections that support scope lookup (sorted sets)

Sorted collections default to lexicographic ordering

bitmaps hyperloglogs Geospatial support by radius index (geospatial)

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 Activities "Operation: set

Product information, provincial information, activity configuration 1 series of infrequently changing cold data cache

Very hot data cache, game ranking, background update data once per second

Simple counting

Number of participants in the 2019 Spring Festival

key: active:spring2019:total value: 3045 Operation: incr

Time expired

A person can only sign in once a day

key: active: checkin: userId: 10000: day: 20190101 value: check-in time stamp operation: expire

A distributed lock

The following code is not rigorous, nx can put concurrency


127.0.0.1:6379> set lockkey 1 nx
OK
127.0.0.1:6379> set lockkey 1 nx
(nil)

list

Users in line

push, pop

Order news

push, pop

Implement the producer and consumer models

Blocking access to BRPOP and BLPOP commands

set

To weight list

Number of participants in the 2019 Spring Festival

key: active: spring2019: users value: 100010100 20 operations: a lot

The label

User label

Merchants label

Spring Festival activity 1 has 5 tasks of abcde. User A has completed a,b, and user B has completed c and d

intersection

User A, user B

And set

User A, user B any task completed by 1

Difference set

User A has yet to complete the task

Get random elements

1 gift was randomly obtained from set

hash

Different attributes of the same 1 resource

During the activity 1, users got different kinds of prizes

key: active:spring:g' user:10010 value: {"giftA":2,"giftB":5} Operation: many

You can perform the incr operation directly on giftA

zset

list

User consumption ranking, thumb up ranking, etc

key: active: spring: star: rank value: user ID score: thumb up number operation: a lot

top 10 is obtained by score

Query a user's score

Query users with scores between 90 and 100

Sometimes our score is not decided by a 1 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 when designing score decimal values mean score before and after the decimal point value level, if there are other special requirements, also can consider to score with a great value to deal with.

Matters needing attention

Each key should have a reasonable expiration time The expiration time of string is overwritten when reset An set operation of type string can override the type Use appropriate data structures [

Do not use list to store and retrieve large amounts of data

] Plan the quantity of key reasonably [

set should be used to determine whether the user is participating or not. key should not be 1 for each user

] Environmental data isolation Business data Isolation User redis Business redis activity redis should be differentiated and active redis is free to clean up after the event 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 key, disable the keys * operation in the main library to prevent jamming

conclusion


Related articles: