1. What is Redis? Describe its advantages and disadvantages.

Redis stands for: Remote Dictionary.server 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. The main appeal of Redis is the ability to store multiple data structures, and with a maximum value of 1GB, unlike memcached, Redis can be used to implement many useful functions. 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. Memcached all values are simple strings, redis as a substitute, Redis is much faster than memcached. Redis can persist its data. Redis can persist its data. String, List, Set, Sorted Set, hashes

4. What physical resources do Redis mainly consume? Memory.

5. What kinds of data elimination strategies does Redis have? The memory limit is reached and the client is attempting to execute commands that will allow more memory to be used. 2. Allkeys-lru: Try to reclaim the least-used key (LRU) to make room for the newly added data. Volatile – LRU: Attempts to reclaim the least used key (LRU), but only the key in the expired set, so that newly added data has room to store. 4. Allkeys-random: Retrieve random keys to make space for newly added data. Volatile -random: Reclaim random keys to make room for newly added data, but only for keys in expired collections. 6. Volatile – TTL: Reclaim the keys that are in the expired set, and preferentially reclaim the keys with a shorter TTL so that newly added data can be stored.

6. Why doesn’t Redis officially provide Windows version? Because the current Linux version has been fairly stable, and the number of users is large, there is no need to develop a Windows version, but will bring compatibility issues.

7. What is the maximum capacity of a string value? 512M

8. 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. Therefore, Redis has the characteristics of fast and persistent data. If data is not stored in memory, disk I/O speed seriously affects redis performance. As memory becomes cheaper and cheaper, Redis will become more and more popular. If maximum memory usage is set, new values cannot be inserted after the number of existing records reaches the memory limit.

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

1.codis

2. The cluster scheme most commonly used at present has basically the same effect as Twemproxy, but it supports the data of the old node to be restored to the new hash node when the number of nodes changes. Redis cluster3.0 comes with its own clustering feature, which is characterized by the concept of hash slot instead of consistent hash algorithm, and self-supporting node setting slave node. See the official documentation for details. 3. 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 the hash layer code, including alternative algorithms after node failure, automatic script recovery after data flapping, instance monitoring, and so on. Welcome Java engineers who have worked for one to five years to learn and exchange advanced Java architecture: 952124565, group provides free Java architecture learning materials (which have high availability, high concurrency, high performance and distributed, Jvm performance tuning, Spring source code, MyBatis, Netty, Redis, Kafka, Mysql, Zookeeper, Tomcat, Docker, Dubbo, Nginx) architecture of multiple knowledge, such as the rational utilization of their every minute and second time to learn to improve yourself, don’t use “no time” to conceal his lazy idle thoughts! While young, hard to fight, to the future of their own account!

10. 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.

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. In fact, in addition to Redis, many companies attach great importance to high concurrency and high availability technology, especially first-line Internet companies, distributed, JVM, Spring source code analysis, micro services and other knowledge points are the interview must test questions.

12. What are the suitable scenarios for Redis?

Session Cache is one of the most commonly used scenarios in which Redis is used. Sessioncache has an advantage over other storage (such as Memcached) in 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 example 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) leaderboard/counter Redis in memory to increase or decrease the operation of the number to achieve very good. Sets and ordered sets 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. If you want to return users and their scores, you need to do something like this: ZRANGE user_scores 0 10 WITHSCORES Agora Games is a good example of this. It is implemented in Ruby, and its leaderboards are stored in Redis, as you can see here. 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 to build chat systems!

What are the Java clients supported by Redis? Which is the official recommendation? Redisson, Jedis, lettuce, etc. Redisson is officially recommended.

14. 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).

15. 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.

Redis hash slot The Redis cluster does not use consistent hash, but introduces the hash slot concept. The Redis cluster has 16384 hash slots, and each key determines which slot to place after CRC16 verification. Each node in the cluster is responsible for part of the hash slot.

17. 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.

19. How are Redis clusters replicated? Asynchronous replication

What is the maximum number of nodes in a Redis cluster? 16384

21. How to select database for Redis cluster? The Redis cluster cannot make database selection at present, default is 0 database.

22. 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, so that multiple commands can be sent to the server without waiting for a reply, which can finally be 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 capability, greatly speeding up the process of downloading new messages from the server.

23, How to understand Redis transaction? A transaction is a single isolated operation: all commands in the transaction are serialized and executed sequentially, without being interrupted by command requests from other clients. A transaction is an atomic operation: all or none of the commands in a transaction are executed.

What are the Redis transaction related commands? MULTI, EXEC, DISCARD, and WATCH

25. How to set the expiration time and permanent validity of Redis key respectively? EXPIRE and PERSIST commands

26, Redis how to 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.

27. 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 a large amount of memory being used (for example, the intersection of a large set is saved to a new key), it won’t take long for the memory limit to be exceeded by this memory usage.

28. Locking mechanism

Let’s look at the picture above. Now some client needs to be locked. If the client is facing a Redis cluster, it first selects a machine based on the Hash node. Note here that only one machine is selected! This is crucial! Next, a lua script is sent to Redis, which looks like this:

Why use lua scripts? Because a large chunk of complex business logic can be encapsulated in lua scripts and sent to Redis to ensure atomicity of the execution of this complex business logic.

So what does this Lua script mean? RLoc k lock = redisson.getLock(“myLock”); RLoc k lock = redisson.getLock(“myLock”); Here you set the lock key to be “myLock”.

ARGV[1] represents the default lifetime of the lock key, which is 30 seconds. ARGV[2] represents the idd of the locked client, like 8743C9c0-0795-4907-87FD-6C719a6b4586:1

The “exists myLock” command is used to determine if the lock key does not exist. How do you lock it? Very simple, with the following command: hset myLoc k

8743c9c0-0795-4907-87Fd-6c719a6b4586:1 1. Run this command to set a hash data structure. After this command is executed, a data structure similar to the following is displayed:

The above represents “8743C9C0-0795-4907-87FD-6C719a6B45866:1” this client to “myLock” this lock key completed lock. The “pexpire myLock 30000” command is executed to set the lifetime of the lock key myLock to 30 seconds. Okay, that’s it. Ok, lock is done.

29. Lock mutual exclusion mechanism

So at this point, what if client 2 tries to lock and executes the same Lua script? Simply put, the first if judgment executes “exists myLock” and finds that the lock key myLock already exists. The hash data structure for the myLock lock key contains the ID of client 2, but it clearly does not, because it contains the ID of client 1.

Therefore, client 2 will get a number returned by PTTL myLock that represents the remaining lifetime of the lock key myLock. Let’s say you have 15,000 milliseconds to live. Client 2 then enters a while loop, continuously trying to lock.

30. Watch Dog automatic delay mechanism

The default lifetime of the lock key on client 1 is 30 seconds. What if client 1 wants to keep the lock for longer than 30 seconds?

Simple! Once client 1 successfully locks, a watch dog will be started, which is a background thread that will check every 10 seconds. If client 1 still has the lock key, the lifetime of the lock key will be continuously extended.

31. Reentrant locking mechanism

What happens if client 1 already holds the lock, resulting in reentrant locking? For example, code like this:

Let’s take a look at the above Lua script. The first if judgment is definitely not true. “exists myLock” indicates that the lock key already exists. The second if statement is true because the hash data structure of myLock contains the ID of client 1, which is “8743C9c0-0795-4907-87FD-6C719a6b45866:1”.

The logic for reentrant locking is executed with:

Incrby myLock 8743c9c0-0795-4907-87fD-6c71a6b4586:1 1. Run this command to add the lock count of client 1 to 1. The myLock data structure now looks like this:

As you can see, the client ID in the Hash data structure of myLock corresponds to the number of times the lock is held

32. Lock release mechanism

If lock.unlock() is executed to release a distributed lock, the business logic is very simple. In other words, the number of times the myLock data structure is locked is reduced by one. If the lock count is 0, the client no longer holds the lock. In this case, the “del myLock” command is used to delete the key from redis. Then, the other client 2 can try to complete the lock. This is the implementation mechanism of the open source Redisson frame for the so-called distributed lock.

In general, we can use the class library provided by Redisson framework to add and release distributed locks based on Redis in the production system.

33. Disadvantages of Redis distributed lock mentioned above

In fact, the biggest problem with the above scheme is that if you write a value like myLock to a Redis master instance, it will be asynchronously copied to the corresponding master slave instance. However, in this process, once the Redis M Aster breaks down and the master/slave switchover occurs, the Redis slave becomes the Redis Master.

The result is that when client 2 tries to lock, the lock is done on the new Redis Master, and client 1 thinks it has successfully locked. This will cause multiple clients to complete the lock on a distributed lock. At this time, the system will certainly have problems in business semantics, resulting in the production of all kinds of dirty data.

Therefore, this is the biggest disadvantage of redis distributed locking caused by the Redis cluster, or the asynchronous replication of the Redis master-slave architecture: when the Redis master instance goes down, it may cause multiple clients to complete locking at the same time.

34. Have you ever used Redis distributed lock and how is it implemented?

Use setnx to fight for locks, and use expire to add an expiration time to locks in case they are forgotten to release. What happens if a process crashes unexpectedly or needs to restart maintenance after executing EXPIRE after setnx? The set directive has very complex parameters. It should be possible to combine setnx and EXPIRE into one directive.

35. Have you ever used Redis for asynchronous queues and how? What are the disadvantages?

The list structure is typically used as a queue, with RPUSH producing messages and LPOP consuming messages. When there are no LPOP messages, sleep for a while and try again. Disadvantages: Production messages can be lost when the consumer goes offline, using professional message queues such as RabbitMQ. Can we produce once and consume many times? Using the PUB/SUB topic subscriber pattern, a 1:N message queue can be implemented.

36. What is cache penetration? How to avoid it? What is cache avalanche? How to avoid it?

The cache penetrates the general cache system, which caches the query according to the key. If there is no corresponding value, go to the back-end system (such as DB) to find it. Some malicious requests intentionally look for nonexistent keys, and the volume of requests can cause a lot of stress on the backend system. This is called cache penetration. How to avoid it?

1: Cache if the query result is empty, set the cache time to be shorter, or clear the cache after the data corresponding to the key is inserted.

2: Filters non-existent keys. You can put all possible keys into a large Bitmap and filter the query through this Bitmap. Cache avalanche When the cache server is restarted or a large number of caches fail at a certain time, the failure will put a lot of pressure on the backend system. The system crashes. How to avoid it?

1: Controls the number of threads that can read the database write cache by locking or queuing after the cache is invalid. For example, only one thread is allowed to query data and write to the cache for a certain key, while other threads wait.

2: Level 2 cache. A1 is the original cache and A2 is the copy cache. When A1 fails, access A2

3: Set different expiration times for different keys, so that the cache expiration time is as uniform as possible.

Recently read a Redis combat analysis PDF, feel good, need to pay attention to the public number [story programmers] to obtain ~