1. What is Redis?

Redis is essentially a key-value in-memory database, much like memcached. The entire database is loaded into memory for operation, and periodically flushed to hard disk asynchronously. Because it is a pure memory operation, Redis has excellent performance and can process more than 100,000 read and write operations per second, making it the fastest key-value DB known. It’s not just performance that makes Redis great. One of Redis’s biggest attractions is the ability to store multiple data structures. In addition, the maximum limit for a single value is 1GB, unlike memcached, which can only store 1MB of data. For example, using his List to do FIFO bidirectional linked List, to achieve a lightweight high performance message queue service, with his Set can do high-performance tag system and so on. Redis can also set expire times for stored key-values, so it can be used as a more powerful version of memcached. The main disadvantage of Redis is that the database capacity is limited by physical memory, so it cannot be used for high-performance read and write of massive data. Therefore, Redis is mainly suitable for high-performance operations and operations with small data volume.

What are the advantages of Redis over memcached?

(1) All memcached values are simple strings, and Redis, as an alternative, supports richer data types (2) Redis is much faster than memcached and (3) Redis can persist its data

3. What data types does Redis support?

String, List, Set, Sorted Set, hashes

4. What physical resources do Redis mainly consume?

Redis is a high performance database based on memory – it depends on memory.

5. What is the full name of Redis?

Remote Dictionary Server

6. What kinds of data elimination strategies does Redis have?

Allkees-lru: An attempt to reclaim the least used key (LRU) makes room for newly added data when the memory limit is reached and the client tries to execute commands that will allow more memory to be used (most write instructions, but DEL and a few exceptions). Volatile – LRU: Attempts to reclaim the least-used key (LRU), but only the key in the expired set, so that newly added data has space to store. Allkeys-random: Retrieves random keys to make room for newly added data. Volatile -random: Retrievals random keys to make room for newly added data, but only for keys in expired collections. Volatile – TTL: Retrievals the keys in the expired set, and prioritized the keys with a shorter TTL to make room for newly added data.

7. Why doesn’t Redis officially provide Windows version?

Because the current Linux version has been quite stable, and a large number of users, there is no need to develop a Windows version, but will bring compatibility issues.

What is the maximum capacity of a string value?

512M

9. Why does Redis need to put all data in memory?

Redis reads data to memory for the fastest read/write speed and writes data to disk asynchronously. So Redis is characterized by fast and persistent data. If data is not kept in memory, disk I/O speed severely affects Redis performance. As memory gets cheaper and cheaper, Redis will become more and more popular. If the maximum memory usage is set, new values cannot be inserted after the number of existing records reaches the memory limit.

10. What should Redis cluster solution do? What are the options?

1. The general concept of TwemProxy is that it is similar to a proxy method, and the use method is no different from ordinary Redis. After setting up multiple Redis instances under twemProxy, twemProxy should be connected where redis should be connected. It receives the request as a proxy and, using a consistent hash algorithm, forwards the request to the concrete Redis and returns the result to twemProxy. Easy to use (only need to modify the connection port relative to REDis), the first choice for the expansion of old projects. Problem: The pressure of twemproxy’s own single-port instance and the change of the calculated value when the number of Redis nodes changes after using consistent hash, the data cannot be automatically moved to the new node. 2. Codis, the most widely used cluster scheme, has basically the same effect as Twemproxy, but it supports data of old nodes to be restored to new hash nodes when the number of nodes changes. 3. Redis Cluster3.0 comes with its own cluster, which is characterized by the concept of hash slot rather than consistent hash algorithm and its support for setting slave nodes. See the official documentation for details. 4. Implement in the business code layer, set up several unrelated Redis instances, hash the key in the code layer, and then operate the data in the corresponding Redis instances. This method has high requirements on hash layer code, including alternative algorithm scheme after node failure, automatic script recovery after data flapping, instance monitoring and so on.

11. When will Redis cluster solution make the whole cluster unavailable?

In A cluster with three nodes A, B, and C, if node B fails without the replication model, the whole cluster will be unavailable as it lacks slots in the range 5501-11000.

12, MySQL has 2000W data, redis only 20W data, how to ensure that redis data is hot data?

When the redis memory data set grows to a certain size, a data obsolescence strategy is implemented. ##13. What are the suitable scenarios for Redis? Session Cache One of the most common scenarios using Redis is Session Cache. The advantage of caching sessions with Redis over other stores, such as Memcached, is that Redis provides persistence. When maintaining a cache that is not strictly consistent, most people would be unhappy if all of their shopping cart information was lost. Now, would they be? Fortunately, as Redis has improved over the years, it’s easy to figure out how to use Redis appropriately to cache documents for sessions. Even Magento, a popular commercial platform, offers plug-ins for Redis. (2) Full page Caching (FPC) In addition to the basic session token, Redis also provides a very simple FPC platform. Returning to the consistency issue, even if the Redis instance is restarted, users will not see a drop in page loading speed due to disk persistence, which is a huge improvement, similar to PHP native FPC. Again, Magento provides a plug-in to use Redis as a full-page caching back end. Also, for WordPress users, Pantheon has a great plugin called WP-Redis that will help you load pages you’ve viewed as quickly as possible. (3) One of the advantages of queue Reids in the field of memory storage engines is that they provide list and set operations, which makes Redis a good platform for message queuing. The operation Redis uses as a queue is similar to the push/pop operation of a list in a native programming language such as Python. If you do a quick Google search for “Redis Queues”, you will immediately find a number of open source projects designed to use Redis to build great back-end tools for queues. For example, Celery has a background that uses Redis as a broker, which you can view from here. (4), the leaderboard/counter Redis in memory to increase or decrease the operation of the number to achieve very good. Set and Sorted Set also make it very easy to perform these operations. Redis just provides these two data structures. So, to get the top 10 users from the sort set — we call it “user_scores” — we simply do as follows: this assumes, of course, that you sort incrementally by your users’ scores. ZRANGE user_scores 0 10 WITHSCORESAgora Games is a good example of this, implemented in Ruby, whose leaderboards are stored in Redis, as you can see here. Publish/subscribe Last (but certainly not least) is Redis’s publish/subscribe feature. There are a lot of publish/subscribe usage scenarios. I’ve seen people use it for social networking connections, as publish/subscribe based script triggers, and even Redis’s publish/subscribe feature for chat systems! (No, it’s true, you can check it out).

14. What Java clients are supported by Redis? Which is the official recommendation?

Redisson, Jedis, lettuce, etc. Redisson is officially recommended.

15. What does Redis have to do with Redisson?

Redisson is an advanced distributed coordination Redis customer service, Can help users in the distributed environment to easily implement some Java objects (Bloom filter, BitSet, Set, SetMultimap, ScoredSortedSet, SortedSet, Map, ConcurrentMap, List, ListMultimap, Queue, BlockingQueue, Deque, BlockingDeque, Semaphore, Lock, ReadWriteLock, AtomicLong, CountDownLatch, Publish/Subscribe, HyperLogLog).

16. What are the advantages and disadvantages of Jedis versus Redisson?

Jedis is the client of Java implementation of Redis. Its API provides comprehensive support for Redis commands. Redisson implements distributed and extensible Java data structures. Compared with Jedis, Redisson has relatively simple functions. It does not support string manipulation, sorting, transaction, pipeline, partitioning and other Redis features. The goal of Redisson is to promote a separation of focus from Redisso that users can focus more on processing business logic.

17. How to set and verify the password in Redis?

Set password: config set requirepass 123456 Authorization password: auth 123456

18, What about the Redis hash slot?

The Redis cluster does not use consistent hash, but introduces the hash slot concept. The Redis cluster has 16384 hash slots. After each key is verified by CRC16, the model of 16384 is taken to determine which slot to place.

19. What is the master/slave replication model of Redis cluster?

In order to make the cluster usable even if some nodes fail or most nodes fail to communicate, the cluster uses a master-slave replication model, with n-1 replicas per node.

Will Redis write operations be lost? Why is that?

Redis does not guarantee strong data consistency, which means that in practice the cluster may lose writes under certain conditions.

21. How are Redis clusters replicated?

Asynchronous replication

22. What is the maximum number of nodes in Redis cluster?

16384.

Redis cluster how to select a database?

The Redis cluster cannot make database selection at present, default is 0 database.

24, How to test the connectivity of Redis?

ping

25. What is the use of pipes in Redis?

A one-time request/response server can be implemented to process new requests even if the old one has not yet been responded to. This allows you to send multiple commands to the server without waiting for a reply, which is finally read in one step. This is pipelining, a technology that has been widely used for decades. For example, many POP3 protocols have been implemented to support this feature, greatly speeding up the process of downloading new messages from the server.

26, How to understand Redis transaction?

A transaction is a single isolated operation: all commands in the transaction are serialized and executed sequentially. The transaction will not be interrupted by command requests from other clients during execution. A transaction is an atomic operation: all or none of the commands in a transaction are executed.

What are the Redis transaction related commands?

How to set the expiration time and permanent validity of the Redis key for MULTI, EXEC, DISCARD, WATCH ##28 and Redis key? EXPIRE and PERSIST commands.

29, How does Redis do memory optimization?

Use hashes whenever possible. Hashes use very little memory, so you should abstract your data model into a hash as much as possible. For example, if you have a user object in your Web system, do not set a separate key for the user’s name, last name, email address, and password. Instead, store all of the user’s information in a hash table.

How does the Redis recycle process work?

A client runs a new command to add new data. Redi checks the memory usage, and if it exceeds the maxMemory limit, reclaims it according to the preset policy. A new command is executed, etc. So we keep crossing the boundary of the memory limit, by constantly reaching the boundary and then constantly reclaiming back below the boundary. If the result of a command is that a large amount of memory is used (for example, the intersection of a large set is saved to a new key), it does not take long for the memory limit to be exceeded by this memory usage. **

31. What algorithm does Redis recycle?

* * LRU algorithm

32. How does Redis do mass data insertion?

Redis2.6 Start Redis – CLI supports a new mode called Pipe mode for performing large data inserts.

33, Why do Redis partition?

Partitioning allows Redis to manage more memory, and Redis will be able to use all the machine’s memory. Without partitions, you can only use the memory of one machine. Partitioning allows Redis computing power to be multiplied by simply adding computers, and Redis network bandwidth to be multiplied by adding computers and network cards.

34, Do you know what Redis partition implementation scheme?

Client partitioning is where the client determines which Redis node data will be stored or read from. Most clients already implement client partitioning. Proxy partitioning means that the client sends requests to the proxy, which then decides which node to write or read data to. The broker decides which Redis instances to request based on partitioning rules and then returns the Redis response to the client. A proxy implementation of Redis and memcached is Twemproxy Query Routing, which means that the client requests any random Redis instance and Redis forwards the request to the correct Redis node. Redis Cluster implements a hybrid form of query routing, but rather than directly forwarding requests from one Redis node to another Redis node, it redirects directly to the correct Redis node with the help of the client.

35. What are the disadvantages of Redis zoning?

Operations involving more than one key are generally not supported. For example, you can’t intersect two collections, because they might be stored in different Redis instances (there are ways to do this, but you can’t use intersection directives directly). You cannot use Redis transactions if you operate on multiple keys simultaneously. The granularity used for partitioning is key, You cannot store a data set using a partitioning granularity is The key, So it is not possible to shard a dataset with a single huge key like a very big sorted set). Data processing can be very complicated when using partitions, for example you have to collect RDB/AOF files from different Redis instances and hosts simultaneously for backup. Dynamic capacity expansion or reduction while partitioning can be complex. Redis clusters can transparently rebalance data to users by adding or removing Redis nodes at run time, but some other client or proxy partitioning methods do not support this feature. However, there is a pre-slice technology can also better solve this problem.

Redis persistent data and cache how to do capacity expansion?

If Redis is used as a cache, use consistent hashing for dynamic scaling and scaling. If Redis is used as a persistent store, a fixed keys-to-nodes mapping must be used, and the number of nodes cannot be changed once determined. Otherwise (that is, if the Redis nodes need to change dynamically), you must use a system that can rebalance data at run time, which currently only the Redis cluster can do.

37, distributed Redis is early or late scale to do a good job? Why is that?

Since Redis is so lightweight (a single instance uses only 1M memory), it is best to start many instances at first to prevent future expansion. Even if you only have one server, you can start with Redis running in a distributed fashion, using partitions and launching multiple instances on the same server. Setting up a few more Redis instances to start with, such as 32 or 64 instances, may seem cumbersome to most users, but the sacrifice is worth it in the long run. That way, when your data is growing and you need more Redis servers, all you need to do is simply migrate Redis instances from one service to another (without worrying about repartitioning). Once you add another server, you need to migrate half of your Redis instances from the first machine to the second machine.

38. What is Twemproxy?

Twemproxy is a proxy system maintained by Twitter for Memcached ASCII and Redis protocols. It is a single-threaded program written in C and runs very fast. It is open source software under the Apache 2.0 license. Twemproxy supports automatic partitioning, so if one of its proxy Redis nodes is unavailable, it will automatically exclude that node (this will change the original keys-Instances mapping, so you should only use Twemproxy when caching Redis). There is no single point of Twemproxy itself, because you can start multiple Twemproxy instances and then have your clients connect to any one of them. Twemproxy is an intermediate layer between Redis client and server, so it should not be complicated and should be reliable to handle partitioning functionality.

Which clients support consistent hashing?

Redis-rb, Predis, etc.

40. How is Redis different from other key-value stores?

Redis has more complex data structures and provides atomic operations on them, which is a different evolutionary path from other databases. Redis data types are transparent to programmers while being based on basic data structures without additional abstractions. Redis runs in memory but can persist to disk, so there is a memory tradeoff for high-speed reads and writes to different data sets, since the amount of data cannot be larger than hardware memory. Another advantage of an in-memory database is that it is much easier to operate in memory than the same complex data structures on disk, allowing Redis to do a lot of things with a lot of internal complexity. At the same time, they are compact in terms of disk formats and are produced in an appending manner because they do not require random access.

What is the memory usage of Redis?

To give you an example: 1 million key-value pairs (the keys are 0 to 999999 and the values are the string “Hello world”) used 100MB on my 32-bit Mac laptop. The same data can be put into a key for only 16MB, because the key value has a high overhead. Executing on Memcached gives a similar result, but is less expensive than Redis, which records type information, reference counts, and so on. Of course, the ratio is much better for big key pairs. 64-bit systems require more memory overhead than 32-bit systems, especially since key-value pairs are smaller because Pointers take up eight bytes on 64-bit systems. But, of course, 64-bit systems support more memory, so more or less a 64-bit system is needed to run a large Redis server.

What are some ways to reduce the memory usage of Redis?

If you are using a 32-bit Redis instance, you can make good use of the Hash,list,sorted set,set, etc., as many small key-values can often be grouped together in a more compact way.

##43, What command is used to check the status of Redis? Info44, What happens when Redis runs out of memory? If the upper limit is reached, Redis write commands will return an error message (but read commands will return normally). Or you can use the configuration flush mechanism by using Redis as a cache, flushing out old content when Redis reaches its memory limit. ## 45, Redis is a single thread, how to improve the utilization of multi-core CPU? You can deploy multiple instances of Redis on the same server and use them as different servers. At some point, one server is not enough anyway, so if you want to use more than one CPU, you can consider shards.

46, How many keys can a Redis instance hold?

List, Set, Sorted Set How many elements can they store at most? In theory Redis can handle up to 232 keys and has been tested in practice with at least 250 million keys stored in each instance. We are testing some larger values. Any list, set, and sorted set can have 232 elements. In other words, the storage limit of Redis is the amount of available memory in the system.

Redis common performance problems and solutions?

(1) It is better for the Master not to do any persistent work, such as RDB memory snapshots and AOF log files. (2) If the data is important, a Slave enables the AOF backup data, and the policy is set to synchronize data once per second. (3) For the speed of Master/Slave replication and connection stability, Master and Slave should be in the same LAN. (4) Avoid adding Slave libraries to the Master library under great pressure. (5) Do not use graph structure for Master and Slave replication. In this way, the single point of failure can be easily solved and the Slave can replace the Master. If the Master fails, you can immediately enable Slave1 as Master.

48. What persistence methods does Redis provide?

RDB persistence allows you to take snapshots of your data at specified intervals. The AOF persistence mode records each write operation to the server. When the server is restarted, these commands will be executed again to restore the original data. The AOF command saves each write operation to the end of the file using the Redis protocol. Redis can also rewrite AOF files in the background so that AOF files are not too large. If you only want your data to exist as long as the server is running, you can do so without any persistence. You can also enable both persistence methods. In this case, when Redis restarts, AOF files will be loaded first to recover the original data, since AOF files usually hold more complete data sets than RDB files. The most important thing to understand is the difference between RDB and AOF persistence, so let’s start with RDB persistence.

49, How to choose the appropriate persistence mode?

In general, if you want to achieve data security comparable to PostgreSQL, you should use both persistence features. If you care deeply about your data, but can still afford to lose it within minutes, you can use RDB persistence only. Many users use AOF persistence only, but this is not recommended: periodic SNAPSHOTS are very convenient for database backups, RDB can restore data sets faster than AOF, and RDB can avoid the aforementioned AOF bugs.

50. Does Redis take effect in real time without restarting the configuration?

For running instances, there are many configuration options that can be modified through the CONFIG SET command without any kind of reboot. Starting with Redis 2.2, you can switch from AOF to RDB’s snapshot persistence or other means without rebooting Redis. Retrieve the ‘CONFIG GET *’ command for more information. Occasional reboots are necessary, such as when you need to update the Redis program to a new version, or when you need to modify some configuration parameters that are not currently supported by the CONFIG command.