In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to understand the usage scenario of Redis". In the daily operation, I believe many people have doubts about how to understand the usage scenario of Redis. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to understand the usage scenario of Redis"! Next, please follow the editor to study!
What is Redis?
Redis is an in-memory cache database. Redis full name: Remote Dictionary Server (remote data Service), written in C language, Redis is a key-value storage system (key storage system), supporting a wide range of data types, such as: String, list, set, zset, hash.
Redis is a storage system that supports multiple data structures such as key-value. Can be used for caching, event publishing or subscribing, high-speed queuing and other scenarios. Support network, provide string, hash, list, queue, collection structure direct access, memory-based, persistent.
Official information
Redis official website: http://redis.io/
Redis official document: http://redis.io/documentation
Redis tutorial: http://www.w3cschool.cn/redis/redis-intro.html
Redis download: http://redis.io/download
Why use Redis
The usage scenario of a product must be based on the characteristics of the product, first list the characteristics of Redis:
Excellent reading and writing performance
Redis can read at a speed of 110000 times per second and write at a speed of 81000 times per second (see the next section for test conditions).
Rich data types
Redis supports Strings, Lists, Hashes, Sets and Ordered Sets data type operations for binary cases.
Atomicity
All operations of Redis are atomic, and Redis also supports the atomicity of several operations after a full merge.
Rich features
Redis supports publish/subscribe, notification, key expiration and other features.
Persistence
Redis supports RDB, AOF and other persistence methods.
Publish and subscribe
Redis supports publish / subscribe model
Distributed system
Redis Cluster
(PS: understand it in combination with the following usage scenarios)
Here are the performance tests obtained by the official bench-mark based on the following conditions (110000 reads per second and 81000 writes per second)
The test completed 50 concurrent execution of 100000 requests.
The value set and obtained is a 256-byte string.
Linux box is running Linux 2.6, which is X3320 Xeon 2.5 ghz.
Text execution uses the loopback interface (127.0.0.1).
Usage scenarios of Redis
Summary of redis application scenarios A lot of places are used in redis. Here is a summary of the application scenarios we have learned:
Cache of hot spot data
Caching is the most common application scenario for Redis, and all of it is used this way, mainly because of the excellent read and write performance of Redis. And gradually replace memcached as the preferred server-side caching component. Moreover, Redis supports transactions internally, which can effectively ensure the consistency of data when in use.
When used as a cache, there are generally two ways to save data:
Before reading, read the Redis first. If there is no data, read the database and pull the data into the Redis.
When inserting data, Redis is written at the same time.
Plan 1: it is easy to implement, but there are two things to pay attention to:
Avoid cache breakdown. The database does not have the data that needs to be hit, so Redis has no data all the time, and has been hitting the database all the time. )
The real-time performance of the data is relatively poor.
Scheme 2: the data is real-time, but it is not easy to deal with it uniformly during development.
Of course, the two ways are applicable according to the actual situation. For example: scheme 1 is suitable for scenarios where the real-time requirements of data are not particularly high. The second scheme is suitable for data storage with dictionary table and small amount of data.
The use of time-limited business
In redis, you can use the expire command to set the lifetime of a key, and redis will delete it when the time expires. This feature can be used in time-limited preferential activity information, mobile phone CAPTCHA and other business scenarios.
Counter related problems
Redis incrby command can be used to increase atomicity, so it can be used in highly concurrent flash sale activity, distributed serial number generation, and specific business, such as limiting the number of SMS messages sent by a mobile phone number, the number of requests per minute, the number of calls per day, and so on.
Distributed lock
This is mainly carried out by using the setnx command of redis. Setnx: "set if not exists" means that if it does not exist, successfully set the cache and return 1, otherwise return 0. This feature is used in the background where you go far away, because our server is clustered, and scheduled tasks may run on both machines, so set a lock through setnx in the scheduled task first, and execute it if it is set successfully. If it is not set successfully, the scheduled task has been executed. Of course, combined with the specific business, we can add an expiration time to the lock, such as a scheduled task executed every 30 minutes, then the expiration time can be set to a time less than 30 minutes, which is related to the cycle of the scheduled task and the time consumed by the scheduled task execution.
In the scenario of distributed lock, it is mainly used in such as second kill system and so on.
Delay operation
For example, after the production of the order, we occupy the inventory, 10 minutes later to check that the user is enough to buy, if there is no purchase, the document will be set invalid, and restore the inventory at the same time. Since redis has provided Keyspace Notifications since 2.8.0, it allows customers to subscribe to Pub/Sub channels to receive events that affect the Redis dataset in some way. So we can use the following solution for the above requirements. When we order production, we set a key and set it to expire 10 minutes later. We implement a listener in the background to listen for the effectiveness of the key, and add the follow-up logic when the key fails.
Of course, we can also use the delay queue service of message middleware such as rabbitmq and activemq to achieve this requirement.
Questions related to the ranking
The query speed of relational database is generally slow in terms of ranking, so we can sort hot data with the help of redis's SortedSet.
For example, make a SortedSet, then use the user's openid as the username above, and the number of likes as the score above, and then make a hash for each user. Through zrangebyscore, you can get the ranking according to the number of likes, and then obtain the user's hash information according to username. This performance experience is also quite good in practical application at that time.
Storage of relationships such as likes, friends, etc.
Redis makes use of some commands of sets, such as finding intersection, union, subtraction, and so on.
In the Weibo application, the people followed by each user exist in a collection, so it is easy to find a common friend of two people.
Simple queue
Because Redis has commands such as list push and list pop, it is easy to perform queue operations.
Reference article
Https://baike.baidu.com/item/Redis/6549233?fr=aladdin
Https://zhuanlan.zhihu.com/p/29665317
Https://www.jianshu.com/p/40dbc78711c8
Knowledge system
Knowledge system
Related articles
First of all, we learn the conceptual basis of Redis to understand the scenarios in which it applies.
Getting started with Redis-Redis concepts and basics
Redis is a storage system that supports multiple data structures such as key-value. Can be used for caching, event publishing or subscribing, high-speed queuing and other scenarios. Support network, provide string, hash, list, queue, collection structure direct access, memory-based, persistent.
Secondly, these applicable scenarios are based on the data types supported by Redis, so we need to learn the data types it supports; at the same time, we also need to understand the underlying data structure in redis optimization, so we also need to understand the design and implementation of some underlying data structures.
Getting started with Redis-data types: details of 5 basic data types
All key (keys) of Redis are strings. When we talk about the basic data structure, we are talking about the data types for storing values, which mainly include five common data types, namely: String, List, Set, Zset, Hash.
Getting started with Redis-data types: details of 3 special types
In addition to the five basic data types mentioned above, Redis also has three special data types, namely HyperLogLogs (cardinality Statistics), Bitmaps (Bitmap) and geospatial (Geographic location)
Getting started with Redis-data types: Stream details
Redis5.0 also adds a data structure Stream, which draws lessons from the design of Kafka and is a new and powerful persistent message queue that supports multicast.
Redis Advanced-underlying data structure: detailed explanation of object Mechanism
In the previous article, we have described the detailed explanation of the five basic data types of Redis, namely, string, list, hash, set, zset, and the detailed explanation of Redis Stream structure in version 5.0. so how is the underlying layer of these basic types implemented? In fact, each kind of object in Redis is composed of object structure (redisObject) and corresponding encoded data structure. This paper mainly introduces the part of object structure (redisObject).
Redis Advanced-underlying data structure: detailed explanation of underlying data structure
The previous article is the first part of the underlying design: a detailed description of the object mechanism, this paper mainly introduces the underlying data structure.
Redis Advanced-underlying data structure: detailed explanation of the corresponding relationship between redis objects and coding (underlying structure)
After learning the underlying data structure, we can finally explain the relationship between redis objects and coding combined with the previous content.
In addition, you need to learn the core features supported by Redis, including persistence, messaging, transactions, high availability; high availability, master-slave, sentry, etc.; and high scalability, such as slicing mechanism.
Redis Advanced-persistence: a detailed explanation of RDB and AOF mechanisms
In order to prevent data loss and recover data when the service is restarted, Redis supports data persistence, which is mainly divided into two ways, namely RDB and AOF;. Of course, these two mixed modes will also be used in actual scenarios.
Redis advance-messaging: a detailed explanation of the publish and subscribe model
Redis publish subscription (pub/sub) is a mode of message communication: the sender (pub) sends the message and the subscriber (sub) receives the message.
Redis Advanced-event: a detailed explanation of Redis event Mechanism
Redis uses event-driven mechanism to deal with a large number of network IO. Instead of using mature open source solutions such as libevent or libev, it implements a very concise event-driven library, ae_event.
Redis Advanced-transaction: detailed explanation of Redis transaction
The essence of a Redis transaction is a collection of commands. Transactions support the execution of multiple commands at a time, and all commands in a transaction are serialized. During transaction execution, commands in the queue are serialized sequentially, and command requests submitted by other clients are not inserted into the transaction execution command sequence.
Redis Advanced-High availability: master-Slave replication details
We know that in order to avoid a single point of failure, that is, to ensure high availability, we need to provide cluster services in a redundant (replica) manner. On the other hand, Redis provides the master-slave library mode to ensure the consistency of data copies, and the master-slave libraries are separated from each other by reading and writing. This paper mainly describes the master-slave replication of Redis.
Redis Advanced-High availability: detailed explanation of Sentinel Mechanism (Redis Sentinel)
On the basis of the master-slave copy above, what if the note node fails? In the Redis master-slave cluster, the sentry mechanism is the key mechanism to realize the automatic switching of the master-slave database, and it effectively solves the problem of failover in the master-slave replication mode.
Redis Advanced-highly Extensible: detailed explanation of sharding Technology (Redis Cluster)
In the previous two articles, master-slave replication and sentinel mechanism ensure high availability. For read-write separation, although slave nodes expand the read concurrency capability of master-slave nodes, the write capacity and storage capacity cannot be expanded, so it can only be the upper limit of the capacity of master nodes. If you are faced with massive data, it is necessary to build a cluster between master (master node sharding) and absorb high availability (master-slave replication and sentry mechanism) capabilities, that is, each master sharding node also needs to have slave nodes, which is the embodiment of typical vertical expansion (cluster sharding technology) in distributed systems. Therefore, the corresponding design in Redis version 3.0 is Redis Cluster.
Finally, it is the specific practice and the problems encountered in practice and solutions: there are different characteristics in different versions, so we also need to understand the version; and performance optimization, large factory practice and so on.
Redis Advanced-caching issues: consistency, penetration, penetration, avalanche, pollution, etc.
One of the most commonly used scenarios in Redis is as a cache. This article mainly discusses what problems there may be in practice as a cache. Such as consistency, penetration, avalanche, pollution, etc.
Redis Advanced-release Features: Redis4.0, 5.0,6.0 feature collation
When learning the Redis knowledge system, we will inevitably need to look at the differences between version implementations. This article mainly collates the features of the newer versions of Redis.
Redis Advanced-Operation and maintenance Monitoring: detailed explanation of Redis Monitoring
The actual combat of Redis includes development, cluster and operation and maintenance. Whether Redis is used well or not, and how to make it better, is what operation and maintenance should do. This article mainly helps you build your understanding of redis operation and maintenance / monitoring system in terms of Redis's own status and commands, visual monitoring tools, and Redis monitoring system, which is a prerequisite for performance optimization.
At this point, the study on "how to understand the usage scenario of Redis" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.