What is redis?

Source: Internet
Author: User
Tags allkeys

Labels: software uses Gen to improve highlights and improve implementation of many large websites that do not execute

1. What is redis?

 

Redis is a memory-based high-speed cache database developed by Salvatore sanfilippo (Network Name: antirez. Redis is called remote dictionary server (Remote Data Service). This software is written in C language. redis is a key-value storage system that supports a wide range of data types, such: string, list, set, zset (sorted set), and hash.

 

2. redis features

 

Redis uses memory as the data storage medium, so the efficiency of reading and writing data is extremely high, far exceeding the database. Taking setting and obtaining a 256-byte string as an example, the read speed can reach 110000 times/s, and the write speed can reach 81000 times/s.

 

Redis and memcache are different in that the data stored in redis is persistent and will not be lost after power failure or restart. Redis storage is divided into three parts: memory storage, disk storage, and log files. After restarting, redis can re-load data from the disk to the memory, these can be configured through the configuration file, because in this way, redis can achieve persistence.

 

Redis supports the master-slave mode and supports cluster configuration, which is more conducive to supporting large-scale projects, which is also a highlight of redis.

 

3. What can redis do in application scenarios?

 

Redis is supported in many languages. Because redis exchange data fast, it is often used on servers to store data that requires frequent access, this can greatly save the I/O overhead of the system to directly read the disk to obtain data. More importantly, it can greatly increase the speed.

 

Take a large website as an example. For example, a website has 1 million people accessing the homepage one day, one of which is recommendation news. If you directly query data from the database, you need to consume 1 million more database requests a day. As mentioned above, apsaradb for redis supports a wide range of data types, so this can be done with apsaradb for redis. This hot data can be stored in redis (memory). When used, it can be directly retrieved from the memory, this greatly improves the speed and saves the server overhead.

1. What are the advantages of using redis?
(1) fast because the data is stored in memory. Similar to hashmap, the advantage of hashmap is that the time complexity of searching and operations is O (1)

(2) supports a wide range of data types, including string, list, set, sorted set, and hash.

(3) supports transactions and operations are atomic. The so-called atomicity means that changes to data are either executed in full or not executed in full. (4) rich features: they can be used for caching, message, set the expiration time by key, and will be automatically deleted after expiration

2. What are the advantages of redis over memcached?
(1) All memcached values are simple strings. redis, as its replacement, supports richer data types.
(2) redis is much faster than memcached
(3) redis can persist its data
3. redis common performance problems and solutions:
(1) It is recommended that the master not do any persistence work, such as RDB memory snapshots and aof log files.
(2) If data is important, an slave enables aof to back up data and sets the policy to synchronize data once per second.
(3) In order to Speed Master-slave replication and connection stability, it is best for master and slave to be in the same LAN
(4) try to avoid adding slave databases to the master database with great pressure
(5) do not use a graph structure for master-slave replication. The one-way linked list structure is more stable, that is, master <-slave1 <-slave2 <-slave3...
This structure is convenient for solving single point of failure (spof) and implementing slave's replacement of master. If the master node fails, you can immediately enable slave1 as the master node.

4. MySQL has million data records and redis only stores 20 million data records. How can we ensure that redis data is always hot data?
Related Knowledge: When the redis memory dataset size increases to a certain size, a data elimination policy will be implemented. Redis provides six data elimination policies:
Voltile-LRU: selects the least recently used data from the data set with an expiration time (server. DB [I]. expires)
Volatile-TTL: select the data to be expired from the data set with an expiration time (server. DB [I]. expires)
Volatile-random: removes any data from a dataset (server. DB [I]. expires) with an expiration time set.
Allkeys-LRU: selects the least recently used data from the dataset (server. DB [I]. dict)
Allkeys-random: Any data elimination from the dataset (server. DB [I]. dict)
No-enviction: Data eviction prohibited

5. What are the differences between memcache and redis?
1) Storage Method
Memecache stores all data in the memory. After the power is down, the data cannot exceed the memory size.
Some redis instances are stored on the hard disk to ensure data persistence.
2) supported data types
Memcache supports relatively simple data types.
Redis has complex data types.
3) use different underlying models
The underlying implementation methods and application protocols for communication between them and clients are different.
Redis directly built its own VM mechanism, because the general system calls system functions, it will waste a certain amount of time to move and request.
4), value size
Redis can reach up to 1 GB, while memcache is only 1 MB


6. What are common redis performance problems? How can this problem be solved?
1 ). when the master writes a memory snapshot, the SAVE command schedules the rdbsave function, which will block the work of the master thread. When the snapshot is large, the performance will be greatly affected and the service will be suspended intermittently, therefore, it is recommended that the master node not write memory snapshots.
2 ). master aof persistence. If the aof file is not overwritten, this persistence method has the least impact on the performance, but the aof file will keep increasing. The aof file will affect the recovery speed of Master restart after the Assembly. It is best for the master not to perform any persistence work, including memory snapshots and aof log files. In particular, do not enable memory snapshots for persistence. If the data is critical, a server Load balancer will enable aof to back up data, the policy is synchronous once per second.
3). When the master calls bgrewriteaof to rewrite the aof file, aof will occupy a large amount of CPU and memory resources during rewriting, resulting in high service load and transient service suspension.
4). redis master-slave replication performance problems, in order to speed master-slave replication and connection stability, slave and master are best in the same LAN
7. The most suitable scenario for redis
Redis is most suitable for all data in-momory scenarios. Although redis also provides persistence functions, it is actually a disk-backed function, there is a big difference from the persistence in the traditional sense, so you may be wondering, it seems that redis is more like an enhanced version of memcached, so when Will memcached be used and when will redis be used?
If we simply compare the difference between redis and memcached, most people will get the following points:
1. redis not only supports Simple K/V data, but also provides storage of data structures such as list, set, zset, and hash.
2. redis supports data backup, that is, data backup in master-slave mode.
3. apsaradb for redis supports data persistence. Data in the memory can be stored on the disk, and can be loaded and used again upon restart.
(1) session cache)
The most common scenario for using redis is session cache ). The advantage of using redis cache sessions over other storage systems (such as memcached) Is that redis provides persistence. When maintaining a cache that does not strictly require consistency, most people will be unhappy if all the information about the user's shopping cart is lost. Will they still be like this?
Fortunately, with the improvement of redis over the years, it is easy to find out how to properly use redis to cache session documents. Magento, a well-known commercial platform, also provides redis plug-ins.
(2) full-page cache (FPC)
In addition to basic session tokens, redis also provides a simple FPC platform. Return to the consistency problem. Even if the redis instance is restarted, the page loading speed will not be decreased due to disk persistence. This is a great improvement, similar to the local FPC of PHP.
Taking magento as an example, magento provides a plug-in to use redis as the full-page cache backend.
In addition, for Wordpress users, Pantheon has a very good plug-in WP-redis, which can help you load the pages you have browsed as quickly as possible.
(3) queue
Reids provides list and set operations, which enables redis to be used as a good message queue platform. The Operations used by redis as a queue are similar to the push/pop operations on the list by local programming languages (such as Python.
If you quickly search for "redis queues" in Google, you will be able to find a large number of open-source projects that are designed to use redis to create excellent backend tools, to meet various queue requirements. For example, celery uses redis as the broker in the background. You can check it here.
(4), ranking/Counter
Redis is able to increment or decrease numbers in memory. Set and sorted set make it easy to execute these operations. redis only provides these two data structures. Therefore, we need to get the top 10 users from the sorting set-we call it "user_scores". We just need to execute the command as follows:
Of course, it is assumed that you are performing incremental sorting based on your user scores. If you want to return the user and user scores, You need to execute the following command:
Zrange user_scores 0 10 withscores
Agora games is a good example. It is implemented in Ruby, and its ranking is to store data using redis. You can see it here.
(5) Publish/Subscribe
The last (but certainly not least) is the redis release/subscription function. There are many scenarios for publishing/subscription. I have seen people use it in social network connections. It can also be used as a publish/subscribe-Based Script trigger, or even use redis's publish/subscribe function to build a chat system! (No, this is true. You can verify it ).
Among all the features provided by redis, I feel that this is the least popular, although it provides users with this function.

(Switch) What is redis?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.