Redis is completely open source and free, complies with the BSD protocol, and is a high-performance key-value database. It is one of the most popular NoSql databases at present, and is also called a data structure server.
#So why use Redis?
The reason is simple, hurry up!
This problem must be considered in websites with large concurrency and high load. All data in the redis database is stored in memory. Since the read and write speed of memory is much faster than that of hard disk, Redis has a very obvious advantage in performance compared with other databases based on hard disk storage.
The use of Redis in the project is mainly considered from two perspectives: performance: concurrency. Of course, Redis also has other functions such as distributed locks, but if it is just for other functions such as distributed locks, there are other middleware instead, and it is not necessary to use Redis. Therefore, this question is mainly answered from two perspectives: performance and concurrency.
Performance:
When we encounter SQL that takes a particularly long time to execute and the results do not change frequently, it is especially suitable to put the running results into the cache, so , subsequent requests are read from the cache, and the requests can be responded to quickly.
Concurrency:
In the case of large concurrency, all requests directly access the database, and a connection exception will occur in the database. At this time, you need to use Redis to perform a buffering operation so that the request can access Redis first instead of directly accessing the database.
Advantages of redis:
1. It runs in memory and is fast. The official claims to support concurrent 11-watt read operations and 8-watt concurrent write operations, which can be said to be quite impressive. Awesome.
2. Although the data is in the memory, it provides persistence support, that is, the data in the memory can be written asynchronously to the hard disk without affecting the continued provision of services.
3, Support Rich data structures (string (string), list (linked list), set (set), zset (sorted set - ordered set)) and Hash (hash type, the string encrypted by md5)
What can Redis do?
Caching, there is no doubt that this is the most well-known usage scenario of Redis today, and it is very effective in improving server performance.
1. Ranking list, if you use a traditional relational database, it is very troublesome, but using the SortSet data structure of Redis can be very convenient;
2. Calculator/speed limiter , using the atomic auto-increment operation in Redis, we can count the number of user likes, the number of user visits, etc. If MySQL is used for such operations, frequent reading and writing will bring considerable pressure; the speed limiter is more typical The usage scenario is to limit the frequency of a user's access to a certain API. Commonly used ones include panic buying to prevent unnecessary pressure from users' crazy clicks;
3. Friend relationships, using some commands in the collection, such as requesting Intersection, union, difference set, etc. can facilitate functions such as common friends and common hobbies;
4. Simple message queue, in addition to Redis's own publish/subscribe mode, we can also use List to Implementing a queue mechanism, such as arrival notification, email sending and other requirements does not require high reliability, but will bring a lot of DB pressure. List can be used to complete asynchronous decoupling;
5. Session sharing, taking PHP as an example, the default Session is saved in the server's file. If it is a cluster service, the same user may be on different machines, which will cause users to log in frequently; after using Redis to save the Session, regardless of the user The corresponding Session information can be obtained on that machine.
The above is the detailed content of Why use redis. For more information, please follow other related articles on the PHP Chinese website!