Home  >  Article  >  Database  >  Detailed explanation of the application scenarios, advantages and disadvantages of Redis

Detailed explanation of the application scenarios, advantages and disadvantages of Redis

藏色散人
藏色散人forward
2020-11-03 15:41:452308browse

The following column Redis Tutorial will introduce the application scenarios and advantages and disadvantages of Redis to you. I hope it will be helpful to friends in need!

Detailed explanation of the application scenarios, advantages and disadvantages of Redis

As a typical non-relational database, Redis is currently widely used in enterprise-level applications. It is very suitable for scenarios with little storage and huge access volume. All data is in-memory to ensure high-speed access to data. As a type of noSQL, what are the differences and advantages between it and memcached? Let us take a look.

1. Disadvantages of Memcached and Redis

The storage architecture such as MySQL Memcached is commonly used in the industry, but as the number of visits continues to increase, there are many problems that follow.

1. MySQL needs to constantly tear down databases and tables, and Memcached also needs to continue to expand;

2. There is a certain data inconsistency between Memcached and MySQL;

3. If the hit rate of Memcached becomes low easily, it will penetrate directly to MySQL, causing the database to be unable to support;

2. Features of Redis

Speaking of Redis, many developers only We know that it is a non-relational database like Memcached and can directly store data in K-V form, but compared to Memcached, it also has many differences.

1. Redis not only supports simple K-V form of data storage, but also supports the storage of collection data such as list, set, hash, zset, etc.;

2. Redis supports real-time Data backup, timely downtime, data can also be restored;

3. Redis supports data persistence, and the data stored in the memory can be directly saved on the disk;

3 , Redis commonly used data types

String, Hash, Set, List, SortedSet, pub/sub, Transactions.

1. String: Strings is a variable stored in the simplest Key-Value form. Where Value can be either a number or a string. The implementation is to store a string by default inside Redis, which is referenced by redisObject. When a digital operation such as increment, decr, etc. is detected, it is automatically converted into a number for calculation. After the calculation is completed, it is converted into a String for storage. stand up.

2. Hash: Hash storage is the value of key-value pairs. That is Key-Hash, and Hash is a k-v structure. If you use Memcached, you need to package the entire Hash and store it in the memory. If you need to query a certain value, you must take out the entire Hash and then find the corresponding value. Redis can obtain Value directly through commands, which greatly improves performance. Its implementation principle: When there are few members, Redis will use compact storage similar to a one-dimensional array in order to save memory. When there are many objects, it will be directly converted to HashMap storage.

3. Set: Set is an unordered natural deduplication set, that is, Key-Set. In addition, it also provides a series of methods for directly operating collections such as intersection and union, which is particularly convenient for the functions of seeking common friends and common concerns. The bottom layer is implemented by HashMap, where value is null;

4. List: List is an ordered and repeatable collection, which follows the principle of FIFO. The bottom layer is implemented by relying on a doubly linked list, so it supports normal Double search forward and reverse. Through List, we can easily obtain functions such as the latest reply.

5. SortedSet: Similar to TreeSet in java, it is a sortable version of Set. In addition, priority sorting is also supported, and a score parameter is maintained to achieve this. Its bottom layer mainly relies on HashMap to achieve sorting by maintaining the mapping of inserted values ​​​​and Score priorities.

6. pub/sub: publish and subscribe, similar to message queue mq. You can choose to subscribe to a certain Key. Once this key publishes some messages, all objects subscribed to this Key can receive this message. It can mainly be used in real-time messaging systems, such as chat.

7. Transactions: NoSQL does not support transactions, but it provides the function of package execution, that is, all commands in this package must be executed together. In addition, a certain Key can be locked. When executing commands in a package, if If it is detected that this Key has changed, it will be rolled back directly.

4. Common application scenarios of Redis

Redis uses memory to provide storage and saves data through the persistence function. And it is a single-threaded operation for request. This series of features makes redis have very magical functions.

1. Find the latest reply.

If you are using a traditional relational database, you need to use select * from table where name="" order by time desc limit 100; this consumes database performance, but through Redis, you can create it directly through Id in Redis A List with a specified length of 1w. When searching is needed, the last 100 records of the list are directly output.

2. Ranking issues

Common ranking issues, such as the hottest topics, game rankings, etc., can be easily achieved through Redis and can be obtained directly using ZRank.

3. Delete expired data

Redis is not a truly persistent database. You can add a valid time to the data. When the valid time exceeds, Redis will automatically delete the corresponding data.

The above is the detailed content of Detailed explanation of the application scenarios, advantages and disadvantages of Redis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete