


Detailed explanation of the application scenarios, advantages and disadvantages of Redis
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!
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

Redis uses hash tables to store data and supports data structures such as strings, lists, hash tables, collections and ordered collections. Redis persists data through snapshots (RDB) and append write-only (AOF) mechanisms. Redis uses master-slave replication to improve data availability. Redis uses a single-threaded event loop to handle connections and commands to ensure data atomicity and consistency. Redis sets the expiration time for the key and uses the lazy delete mechanism to delete the expiration key.

Steps to solve the problem that redis-server cannot find: Check the installation to make sure Redis is installed correctly; set the environment variables REDIS_HOST and REDIS_PORT; start the Redis server redis-server; check whether the server is running redis-cli ping.

To view all keys in Redis, there are three ways: use the KEYS command to return all keys that match the specified pattern; use the SCAN command to iterate over the keys and return a set of keys; use the INFO command to get the total number of keys.

To view the Redis version number, you can use the following three methods: (1) enter the INFO command, (2) start the server with the --version option, and (3) view the configuration file.

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

Redis Ordered Sets (ZSets) are used to store ordered elements and sort by associated scores. The steps to use ZSet include: 1. Create a ZSet; 2. Add a member; 3. Get a member score; 4. Get a ranking; 5. Get a member in the ranking range; 6. Delete a member; 7. Get the number of elements; 8. Get the number of members in the score range.
