1. What is Redis?

Redis is a high performance key-value database that is completely open source and free and complies with the BSD protocol.

Redis and other key-value caching products have the following three characteristics:

Redis supports data persistence, saving data in memory to disk, which can be reloaded for use upon restart.

Redis not only supports simple key-value type data, but also provides the storage of list, set, zset, hash and other data structures.

Redis supports data backup, namely, data backup in master-slave mode.

Redis optimal potential

High performance – Redis can read 110,000 times /s and write 81,000 times /s.

Rich data types – Redis supports binary case Strings, Lists, Hashes, Sets and Ordered Sets data type operations.

Atomic – All operations in Redis are atomic, meaning that they either succeed or fail at all. Individual operations are atomic. Multiple operations also support transactions, namely atomicity, wrapped in MULTI and EXEC instructions.

Rich features – Redis also supports publish/subscribe, notifications, key expiration, and more.

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 memory is a tradeoff for high-speed reads and writes to different data sets, since the amount of data cannot be greater 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.

2. Redis data type?

A: Redis supports five data types: String, hash, list, set and Zsetsorted set.

If you are an advanced user in Redis, you also need to add the following data structures HyperLogLog, Geo, Pub/Sub.

If you mention that you’ve also played with Redis modules like BloomFilter, RedisSearch, and Redis-ML, the interviewer’s eyes will light up.

3. What are the benefits of using Redis?

1, fast, because the data is stored in memory, similar to HashMap, HashMap has the advantage of O1 time complexity for both lookup and operation.)

2, support rich data types, support string, list, set, Zset, hash, etc

3, support transactions, operations are atomic, the so-called atomic data changes are either all executed or not executed at all

4, rich features: can be used for cache, message, by key to set expiration time, expiration will be automatically deleted

4. What advantages does Redis have over Memcached?

Redis is an alternative to Memcached, where all values are simple strings

Redis is faster than Memcached. 3. Redis can persist its data

3, more interview questions focus on wechat public account: Java2B

5. What are the differences between Memcache and Redis?

1. Memecache Stores all data in memory. It will hang up after power failure. Redis is partially stored on hard disk to ensure data persistence

Memcache’s support for data types is relatively simple. Redis has complex data types.

3. Different underlying models are used. The underlying implementation modes between them and the application protocols for communication with clients are different. Redis directly built the VM mechanism itself, because normal system calls to system functions would waste a certain amount of time moving and requesting.

Redis is single-process single-thread?

A: Redis is single-process, single-thread. Redis uses queue technology to turn concurrent access into serial access, eliminating the overhead of traditional database serial control.

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

A: 512 m

8. What is the persistence mechanism of Redis? What are their strengths and weaknesses?

Redis provides two persistence mechanisms, RDB and AOF:

1, RDBRedis DataBase) persistence mode: it refers to the semi-persistent mode of data set snapshot) records all the key value pairs of REDis DataBase, and writes the data to a temporary file ata certain point in time. After the persistence, the temporary file is used to replace the last persistent file to achieve data recovery.

Advantages:

1, only one file dump. RDB, convenient persistence.

2, good disaster recovery, a file can be saved to a safe disk.

To maximize performance, fork the child process to complete the write operation and let the main process continue processing the command, so maximize IO. A separate sub-process is used for persistence, and the main process does not perform any IO operations, ensuring the high performance of Redis. When the data set is large, the startup efficiency is higher than that of AOF.

Disadvantages:

1. Low data security. RDB is persisted for a period of time. If REDis fails during the persistence, data will be lost. So this approach is more suitable when data requirements are not strict.)

2, aofAppend-only file) persistence mode: all command line records are stored in the redis command request protocol format) as aOF files.

Advantages:

Data security: In aOF persistence, you can configure appendfsync and always to record every command operation to the AOF file once.

2. Write files in Append mode. Even if the server breaks down during the process, you can use the redis-check-aOF tool to solve data consistency problems.

Rewrite model of AOF mechanism. You can delete some of the AOF files (such as flushall) before they are rewrite (merge commands when the file is too big)

Disadvantages:

1. The AOF file is larger than the RDB file, and the recovery speed is slow.

2. When the data set is large, the startup efficiency is lower than RDB.

9. Redis Common performance Problems and solutions:

1. Do not write a memory snapshot for the Master. If the Master writes a memory snapshot, the save command will schedule the rdbSave function, which blocks the main thread

2. If the data is important, a Slave enables AOF backup, and the policy is set to synchronize data once per second

3. For the speed of Master/Slave replication and connection stability, it is better for Master and Slave to reside on the same LAN

4. Try to avoid adding slaves to stressed main libraries

5, Master/slave replication do not use graph structure, use one-way linked list structure is more stable, that is: Master < -slave1

< – Slave2 < – Slave3… 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.

Delete redis expiration key?

1, timed delete: when setting the expiration time of the key, create a timer timer. Set the timer to delete the key as soon as the key expires.

2, lazy delete: let the key expire regardless, but each time from the key space to obtain the key, check whether the obtained key is expired, if expired, delete the key; If not, the key is returned.

3, regular deletion: every once in a while the program will check the database, delete the expired key inside. It is up to the algorithm to determine how many expired keys to delete and how many databases to check.

11. Redis recycling Strategy (elimination strategy)

Volatile – lRU: Selects the least recently used expires data from a set with an expiration date (server.db[I].expires)

Volatile – TTL: Selects expired data from a set (server.db[I].expires) to be discarded

Volatile -random: Selects any data from a set with an expiration date (server.db[I].expires) to be discarded

Allkeys-lru: Culls the least recently used data from the dataset (server.db[I].dict)

Allkeys-random: Random selection of data from a dataset (server.db[I].dict)

No-enviction: Data expulsion is prohibited

Note the six mechanisms here. Volatile and AllKeys specify whether to eliminate data from a set with an expiration date or from the entire set. Lru, TTL, and RANDOM are the three different elimination strategies. Plus a no-enviction never recycle policy.

Using policy rules:

1. If the data has a power-law distribution, that is, some data are accessed with high frequency and some data are accessed with low frequency, then allkeys-LRU is used

2. If the data presents equal distribution, that is, all data access frequency is the same, allkeys-random is used

12. Why does EDis need to put all data in memory?

A: Redis reads all 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.

13. Do you know the synchronization mechanism of Redis?

A: Redis can use master slave synchronization and slave slave synchronization. During the first synchronization, the primary node performs a BGSave and records subsequent modifications to the memory buffer. After the synchronization is complete, the RDB file is fully synchronized to the replication node, and the replication node loads the RDB image to the memory. After the loading is complete, the master node is notified to synchronize the modified operation records to the replication node for replay.

14. What are the benefits of Pipeline? Why do we use Pipeline?

A: Multiple IO round trips can be reduced to one, provided that there is no causal correlation between the instructions executed by the pipeline. When using Redis-Benchmark for pressure measurement, it can be found that an important factor affecting the PEAK QPS of Redis is the number of pipeline batch instructions.

15. Have you used Redis cluster? What is the principle of cluster?

  1. Redis Sentinal focuses on high availability and will automatically upgrade slave to Master if master goes down.

  2. Redis Cluster focuses on scalability. When a single Redis memory is insufficient, Cluster is used to fragment storage.

16. When will Redis cluster solution make the whole cluster unusable?

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

17, What are the Java clients supported by Redis? Which is the official recommendation?

A: Redisson, Jedis, lettuce, etc., are officially recommended.

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

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

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

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

20, What about the Redis hash slot?

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

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

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

Will Redis write operations be lost? Why is that?

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

23. How are Redis clusters replicated?

A: Asynchronous replication

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

Answer: 16,384.

How to select database for Redis cluster?

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

26, How to test the connectivity of Redis?

A: Run the ping command.

27, How to understand Redis transaction?

A:

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

2) A transaction is an atomic operation: all or none of the commands in a transaction are executed.

What are the Redis transaction related commands?

A: MULTI, EXEC, DISCARD, WATCH

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

Answer: EXPIRE and PERSIST commands.

30, How does Redis do memory optimization?

A: 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. More interview questions follow the wechat official account: Java2B

How does the Redis recycle process work?

A: A client runs a new command and adds 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.

32, What are some ways to reduce Redis memory usage?

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

What happens when Redis runs out of memory?

A: Redis write commands will return an error message if the upper limit is reached (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.

34, How many keys can a Redis instance hold? List, Set, Sorted Set How many elements can they store at most?

A: Redis can theoretically 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.

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

A: When Redis memory data sets grow to a certain size, a data obsolescence strategy is implemented. What to know: Redis offers 6 data elimination strategies:

Volatile – lRU: Picks the most recent expires from a set of data sets (server.db[I].expires)

Less used data obsolescence

Volatile – TTL: Selects expired data from a set (server.db[I].expires) to be discarded

Volatile -random: Selects any data from a set with an expiration date (server.db[I].expires) to be discarded

Allkeys-lru: Culls the least recently used data from the dataset (server.db[I].dict)

Allkeys-random: Random selection of data from a dataset (server.db[I].dict)

No-enviction: Data expulsion is prohibited

What is the most suitable scene for Redis?

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

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!

37. Suppose there are 100 million keys in Redis and 10W of them start with a fixed known prefix. What if you could find all of them?

A: Use the keys command to scan out a list of keys for a given pattern.

If the redis is providing services to online businesses, what is the problem with using keys?

This is the time to answer a key redis feature: Redis single threaded. The keys command causes the thread to block for a period of time and the online service to pause until the command is executed. In this case, scan command can be used. Scan command can extract the key list of the specified mode without blocking, but there will be a certain probability of repetition. It is ok to perform deduplication on the client, but the overall time will be longer than that of direct keys command.

38. If a large number of keys need to be set to expire at the same time, what should I pay attention to?

A: If a large number of key expiration times are set too centrally, redis may experience a temporary lag at that point in time. It is generally necessary to add a random value to the time to spread out the expiration time.

Have you ever used Redis for asynchronous queues? How do you use it?

A: You typically use the list structure as a queue, rpush to produce messages, and LPOP to consume messages. When there are no LPOP messages, sleep for a while and try again.

What if the other person asks if it’s ok not to sleep?

List also has a directive called blPOP, which blocks when there is no message until it arrives. If the other party asks can produce once consume many times? Using the PUB/SUB topic subscriber pattern, a 1:N message queue can be implemented.

What are the disadvantages of pub/sub?

In the event that the consumer goes offline, the production message is lost and a professional message queue such as RabbitMQ is used.

If the other party asks how redis realizes the delay queue?

I bet right now you’d want to beat the interviewer to death if you had a baseball bat in your hand. But you are very restrained and calmly answer: Use sortedSet, take timestamp as score, call Zadd for message content as key to produce message, and the consumer uses ZrangebyScore command to get data polling N seconds ago for processing. At this point, the interviewer has secretly given you a thumbs-up. But what he doesn’t know is that right now you’re sticking your middle finger up behind your chair.

Have you ever used Redis distributed lock? What is it?

Use setnx to fight for locks, and use expire to add an expiration time to locks in case they are forgotten to release.

You will be told that you answered well, and then asked what happens if the process crashes after setnx before expire or if maintenance needs to be restarted?

This is where you respond with surprise: Oh, yeah, the lock will never be released. The next thing you need to do is scratch your head, pretend to think about it for a moment, as if you were thinking about the next result on your own initiative, and reply: I remember that the set directive has very complicated parameters. It should be possible to combine setnx and EXPIRE into one directive. The other party will show a smile at this time, the heart began to recite: press, this boy is not bad.

41. How to implement session shared storage in a cluster?

Session is run on one server, all the access will go to our unique server, so we can obtain the Session according to the sessionID sent by the client. Or create a new Session when the corresponding Session does not exist (the Session life cycle expires/the user logs in for the first time). However, if we are in A clustered environment and we have two servers A and B, the user’s request will be forwarded by the Nginx server (as well as other schemes). When the user logs in, Nginx forwards the request to server A, which creates A new session. When the user browses other pages, the client verifies the login status, and Nginx forwards the request to server B. Since there is no session with the corresponding session ID sent by the client on SERVER B, a new session is created. In addition, the new sessionID is returned to the client. In this way, we can imagine that the user has 1/2 probability to log in again for each operation, which not only has a very bad user experience, but also makes the session on the server surge, increasing the operating pressure of the server.

To solve the seesion sharing problem in a clustered environment, there are four solutions:

  1. Sticky session

Sticky session means that Ngnix forwards all requests of the same user to the same server every time, that is, the user is bound to the server.

  1. Server Session Replication

That is, every time a session changes, its creation or modification is broadcast to all servers in the cluster so that all servers have the same session.

  1. Sharing session

Caching sessions, using Redis, memcached. 4. The session persistence

Store sessions in the database as if they were data.

42. What is the difference between memcached and Redis?

1. Redis not only supports simple K/V type data, but also provides the storage of list, set, zset, hash and other data structures. Memcache supports only simple data types, requiring clients to handle complex objects themselves

2. Redis supports persistent data, which can keep the data in memory in disk, and can be reloaded for use when restarting (PS: persistent in RDB, AOF).

benefits

More interview questions focus on BROTHER B’s official account: Java2b (wechat search)