Redis is an open source key-value storage database written in C language. It can be used in scenarios such as caching, event publishing and subscription, and high-speed queues. And supports rich data types: string (string), hash (hash), list (list), set (unordered set), zset (sorted set: ordered set)
Application scenarios of Redis in projects
1. Caching data
is the most commonly used, for queries that often need to be queried and changes are not very frequent. The data is often called hot data.
2. Message queue
is equivalent to a message subscription system, such as ActiveMQ and RocketMQ. If you have higher consistency requirements for data, it is still recommended to use MQ)
3, counter
, such as counting click-through rates and likes rate, redis is atomic and can avoid concurrency problems
4. E-commerce website information
Cache of initialization page data of large e-commerce platforms . For example, when purchasing a flight ticket on Qunar.com, the price on the home page will be different from the price you click on.
5. Hotspot data
#For example, real-time hot spots on news websites, hot searches on Weibo, etc. need to be updated frequently. When the total amount of data is relatively large, querying directly from the database will affect performance
Application scenarios of Redis data types
As mentioned earlier, Redis supports five There are a variety of rich data types, so how should we choose in different scenarios?
1. String
String is the most commonly used data type. It can store any type of string, including binary, JSONized objects, even base64 encoded images. The maximum capacity of a string in Redis is 512MB, which can be said to be omnipotent.
2. Hash
is often used to store structured data. For example, it can be used to store user IDs, nicknames, and avatars in forum systems. Points and other information. If you need to modify the information, you only need to take out the Value through the Key, deserialize it, modify the value of an item, and then serialize and store it in Redis. The Hash structure is stored because the Hash structure will be modified when a single Hash element is less than a certain number. Compressed storage, so you can save a lot of memory. This does not exist in the String structure.
3、List
List is implemented as a two-way linked list, which can support reverse search and traversal, making it more convenient to operate. However, it brings some additional memory overhead. Many implementations within Redis, including sending buffer queues, etc. This data structure is used. In addition, you can use the lrange command to implement Redis-based paging function, which has excellent performance and good user experience.
4. Set
The external function provided by set is similar to the function of list. The special thing is that set can automatically arrange Seriously, when you need to store a list of data and don't want duplicate data to appear, you can choose to use set at this time.
5. Sort Set
can be sorted according to the weight of a certain condition. For example, a ranking data application can be made based on the number of clicks. .
Redis features:
1. Redis is a pure memory operation, and we need to manually persist it to the hard disk when needed
2. Redis is single-threaded, thus avoiding the frequent context switching operations in multi-threads.
3. Redis data structure is simple, and the operation of data is also relatively simple
4. The underlying models used are different, and the underlying implementation between them is The method and application protocol for communication with the client are different. Redis directly builds its own VM mechanism, because if the general system calls system functions, it will waste a certain amount of time to move and request
5. Use multi-channel I/O multiplexing model, non-blocking I/O
For more Redis related knowledge, please visit the Redis usage tutorial column!
The above is the detailed content of What type of data does redis generally store?. For more information, please follow other related articles on the PHP Chinese website!