Recently, I interviewed several companies and all of them asked questions about Redis. When PREPARING for the interview, I neglected this aspect, and the interview result was predictable. After the interview, I collected the questions about Redis that my friends met in the interview and made a summary, so as to record them for reference in the interview.

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

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. Redis’s greatest appeal is the ability to store multiple data structures, with a maximum value limit of 1GB, unlike memcached, which can store 1MB of data, so 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. All memcached values are simple strings. Redis is an alternative to these, supporting richer data types

2. Redis is much faster than memcached

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?

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 can be stored.

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 quite stable, and a large number of users, 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 cluster, which is characterized by the concept of hash slot rather than consistent hash algorithm, and its own support for setting nodes to slave nodes. 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 hash layer code, including alternative algorithm scheme after node failure, automatic script recovery after data flapping, instance monitoring and so on.

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. I have collected a set of systematic architecture and technology system by myself, which is aimed at the technical requirements of current Internet companies and combined with mainstream technologies. You may have been exposed to these things in your daily work, but you lack comprehensive and systematic learning. Join the back-end development group: 943918498, or follow the wechat public account: Java Information library, reply to “Architecture” for free architecture information.

12. What are the suitable scenarios for Redis?

(1) Session Cache

One of the most common scenarios for using Redis is sessioncache. The advantage of using Redis to cache sessions 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 Cache (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) Queue

One advantage of Reids in the memory storage engine world is that it provides list and set operations, which makes Redis a good message queue platform to use. 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) Leaderboards/counters

Redis does a great job of incrementing or decrementing numbers in memory. Set and SortedSet 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 sorted set — we call it “user_scores” — we just need to do the following:

Of course, this assumes that you are doing an ascending order based on your users’ scores. If you want to return users and their scores, you need to do this:

ZRANGE user_scores 0 10 WITHSCORES

A good example is Agora Games, implemented in Ruby, whose leaderboards are stored in Redis, as you can see here.

(5) Publish/subscribe

Last (but certainly not least) is Redis’s publish/subscribe capabilities. 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!

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. After each key is verified by CRC16, the model of 16384 is taken to determine which slot to place.

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?

The one-shot request/response server implements the ability 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 feature, 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 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.

blog.csdn.net/qq_42894896…