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
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
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 countingNumber of participants in the 2019 Spring Festival
key: active:spring2019:total value: 3045 Operation: incr
Time expiredA person can only sign in once a day
key: active: checkin: userId: 10000: day: 20190101 value: check-in time stamp operation: expire
A distributed lockThe 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
push, pop
Order newspush, pop
Implement the producer and consumer modelsBlocking access to BRPOP and BLPOP commands
set
Number of participants in the 2019 Spring Festival
key: active: spring2019: users value: 100010100 20 operations: a lot
The labelUser 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
intersectionUser A, user B
And setUser A, user B any task completed by 1
Difference setUser A has yet to complete the task
Get random elements1 gift was randomly obtained from set
hash
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
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
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 jammingconclusion