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


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

(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?

Memory.

5. What is the full name of Redis?

Remote Dictionary Server.

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

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)

Allkeys-lru: Attempts to reclaim the least-used key (LRU) to make room for 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 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 codis.

Currently, the most commonly used cluster scheme 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.

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

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?

(1) Session Cache

One of the most common scenarios for 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 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 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 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!

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?

To set the 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?

MULTI, EXEC, DISCARD, and WATCH

28. How to set the expiration time and permanent validity of Redis key respectively?

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.

We have the Spring, MyBatis, Netty source code analysis, high concurrency, high performance, distributed, microservice architecture principle, JVM performance optimization, distributed architecture and other technologies interested in friends can add my Java backend community: 586446657, which will share the necessary knowledge system to become an architect, mainly for Java developers to improve themselves, break the bottleneck, I believe you to learn, there will be improvement and harvest. There will be content you need in this group friends please hurry to join in.