Redis interview questions
1.What is
Redis?.
2. What is the
data type of Redis?
3. What are the
benefits of using Redis?
4. What are the
advantages of Redis over Memcached?
5. What are the
differences between Memcache and Redis?
6. Is Redis
single-process and single-threaded?
7. What is the
maximum storage capacity of a string type value?
8. What is the
persistence mechanism of Redis? Their advantages and disadvantages?
9. Redis common
performance problems and solutions:
10. What is the
deletion strategy of redis expired keys?
11. Redis
recycling strategy (elimination strategy)?
12. Why does
edis need to put all data in memory?
13. Do you
understand the synchronization mechanism of Redis?
14. What are
the benefits of Pipeline? Why use pipeline?
15. Have you
used Redis cluster? What is the principle of cluster?
16. Under what
circumstances will the Redis cluster solution cause the entire cluster to be
unavailable?
17. What are
the Java clients supported by Redis? Which one is the official recommendation?
18. What are
the advantages and disadvantages of comparing Jedis and Redisson?
19. How to set
password and verify password for Redis?
20. Tell me
about the concept of Redis hash slots?
21. What is the
master-slave replication model of Redis cluster?
22. Will there
be write operation loss in Redis cluster? Why?
23. How are
Redis clusters replicated?
24. What is the
maximum number of nodes in a Redis cluster?
25. How to
choose a database for Redis cluster?
26. How to test
the connectivity of Redis?
27. How to
understand Redis transaction?
28. What are
the commands related to Redis transactions?
29. How to set
the expiration time and permanent validity of Redis key?
30. How does
Redis optimize memory?
31. How does
the Redis recycling process work?
32. Are there
any ways to reduce the memory usage of Redis?
33. What
happens when Redis runs out of memory?
34. How many
keys can a Redis instance store at most? How many elements can be stored in
List, Set, Sorted Set?
35. There are
2000w data in MySQL and only 20w data in redis. How to ensure that the data in
redis are all hot data?
36. What
scenario is Redis most suitable for?
37. If there
are 100 million keys in Redis, 10w of them start with a fixed, known prefix.
What if you can find them all?
38. If there
are a large number of keys that need to be set to expire at the same time, what
should be paid attention to?
39. Have you
used Redis as an asynchronous queue? How do you use it?
40. Have you
ever used Redis distributed lock? What is it?
Redis is
completely open source and free, complies with the BSD protocol, and is a
high-performance key-value database.
Redis and other
key-value caching products have the following three characteristics:
(1) Redis
supports data persistence. The data in the memory can be saved on the disk, and
it can be loaded again for use when restarting.
(2) Redis not
only supports simple key-value type data, but also provides storage for list,
set, zset, hash and other data structures.
(3) Redis
supports data backup, that is, data backup in master-slave mode.
Redis advantage
(1) High
performance-Redis can read 110,000 times/s and write speed is 81,000 times/s.
(2) Rich data
types-Redis supports Strings, Lists, Hashes, Sets and Ordered Sets data type
operations in binary cases.
(3) Atomic-All
Redis operations are atomic, which means that they are executed successfully or
not executed at all if they fail. A single operation is atomic. Multiple
operations also support transactions, that is, atomicity, packaged by MULTI and
EXEC instructions.
(4) Rich
features-Redis also supports publish/subscribe, notification, key expiration
and other features.
How is Redis
different from other key-value stores?
(1) Redis has a
more complex data structure and provides atomic operations on them, which is an
evolutionary path different from other databases. Redis data types are based on
basic data structures and are transparent to programmers without additional
abstraction.
(2) Redis runs
in memory but can be persisted to disk, so memory needs to be weighed when
reading and writing different data sets at high speed, because the amount of
data cannot be greater than hardware memory. Another advantage of the in-memory
database is that it is very simple to operate in memory compared to the same
complex data structure on the disk, so that Redis can do a lot of internally
complex things. At the same time, in terms of disk format, they are generated
in a compact manner because they do not require random access.
2. What is the data type of Redis?
Answer: Redis
supports five data types: string (string), hash (hash), list (list), set
(collection) and zsetsorted set: ordered collection).
The most
commonly used in our actual projects is string and hash. If you are an advanced
user of Redis, you also need to add the following data structures HyperLogLog,
Geo, Pub/Sub.
If you say that
you have played Redis Module, such as BloomFilter, RedisSearch, Redis-ML, the
interviewer's eyes will start to shine.
3. What are the benefits of using
Redis?
(1) Fast speed,
because the data is stored in memory, similar to HashMap, the advantage of
HashMap is that the time complexity of search and operation is O1)
(2) Support
rich data types, support string, list, set, Zset, hash, etc.
(3) Support
transactions, operations are all atomic. The so-called atomicity means that all
data changes are executed or not executed at all.
(4) Rich
features: can be used for cache, message, set expiration time according to key,
it will be deleted automatically after expiration
4. What are the advantages of Redis
over Memcached?
(1) All values
of Memcached are simple strings. Redis, as its replacement, supports richer
data types.
(2) Redis is
much faster than Memcached
(3) Redis can
persist its data
5. What are the differences between
Memcache and Redis?
(1) Storage
method Memecache stores all the data in the memory, and it will hang up after a
power failure, and the data cannot exceed the memory size. Part of Redis is
stored on the hard disk, which can ensure the durability of the data.
(2) Data
support type Memcache supports relatively simple data types. Redis has complex
data types.
(3) The use of
the underlying model is different, and the underlying implementation methods
between them and the application protocol for communication with the client are
different. Redis directly built the VM mechanism by itself, because the general
system calls system functions, it will waste a certain amount of time to move
and request.
6. Is Redis single-process and
single-threaded?
Answer: Redis
is single-process and single-threaded. Redis uses queue technology to turn
concurrent access into serial access, eliminating the overhead of traditional
database serial control.
7. What is the maximum storage capacity
of a string type value?
Answer: 512M
8. What is the persistence mechanism of
Redis? Their advantages and disadvantages?
Redis provides
two persistence mechanisms, RDB and AOF mechanisms:
1. RDBRedis
DataBase) persistence mode:
Refers to the
semi-persistent mode of using a snapshot of the data set) to record all the
key-value pairs of the redis database, and write the data to a temporary file
at a certain point in time. After the end of the persistence, replace the last
persistent file with this temporary file File to achieve data recovery.
advantage:
(1) There is
only one file dump.rdb, which is convenient for persistence.
(2) Good
disaster tolerance, a file can be saved to a safe disk.
(3) To maximize
performance, fork the child process to complete the write operation and let the
main process continue to process commands, so IO is maximized. Use a separate
child process for persistence, the main process will not perform any IO
operations, ensuring the high performance of redis)
(4) When the
data set is large, the startup efficiency is higher than that of AOF.
shortcoming:
Data security
is low. RDB is persisted at intervals. If redis fails between persistence, data
loss will occur. So this method is more suitable when the data requirements are
not rigorous
2. AOFAppend-only
file) persistence method:
It means that
all command line records are completely persistently stored in the format of
the redis command request protocol) and saved as an aof file.
advantage:
(1) Data
security, aof persistence can be configured with appendfsync attribute, there
is always, every time a command operation is performed, it will be recorded in
the aof file once.
(2) Write files
through append mode, even if the server is down in the middle, you can use the
redis-check-aof tool to solve the data consistency problem.
(3) The rewrite
mode of the AOF mechanism. Before the AOF file is rewrite (when the file is too
large, the command will be merged and rewritten), you can delete some of the
commands (such as the incorrectly operated flushall))
shortcoming:
(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 that of rdb.
9. Redis common performance problems
and solutions:
(1) Master is
better not to write memory snapshots. If Master writes memory snapshots, the
save command schedules the rdbSave function, which will block the work of the
main thread. When the snapshot is relatively large, the performance impact will
be very large, and the service will be suspended intermittently
(2) If the data
is more important, a Slave turns on AOF to back up the data, and the strategy
is set to synchronize once per second.
(3) For the
speed of master-slave replication and the stability of the connection, it is
best for Master and Slave to be in the same LAN
(4) Try to
avoid adding slaves to the stressful master library
(5) Do not use
a graph structure for master-slave replication. It is more stable to use a
singly linked list structure, namely: Master <- Slave1 <- Slave2 <-
Slave3... This structure is convenient to solve the single point of failure
problem and realize the replacement of Slave to Master. . If the master hangs
up, you can immediately enable Slave1 as the master, and the others remain unchanged.
10. What is the deletion strategy of
redis expired keys?
(1) Timing
deletion: While setting the key expiration time, create a timer timer). Let the
timer execute the key deletion operation immediately when the key expiration
time comes.
(2) Lazy deletion:
let the key expire, but every time you get a key from the key space, check
whether the obtained key expires, if it expires, delete the key; if it does not
expire, return the key.
(3) Periodic
deletion: The program checks the database at regular intervals and deletes the
expired keys. As for how many expired keys to delete and how many databases to
check, it is up to the algorithm.
11. Redis recycling strategy
(elimination strategy)?
Volatile-lru:
select the least recently used data from the data set (server.db[i].expires)
with an expiration time set to be eliminated
Volatile-ttl:
select the data to be expired from the data set (server.db[i].expires) that has
set expiration time
Volatile-random:
arbitrarily select data to be eliminated from the data set
(server.db[i].expires) for which the expiration time has been set
allkeys-lru:
select the least recently used data from the data set (server.db[i].dict) to
eliminate
allkeys-random:
arbitrarily select data to eliminate from the data set (server.db[i].dict)
no-enviction
(eviction): prohibit eviction of data
Pay attention
to the six mechanisms here. Volatile and allkeys specify whether to eliminate
data from a data set with an expiration time set or to eliminate data from all
data sets. The following lru, ttl, and random are three different elimination
strategies, plus one A no-enviction strategy of never recycling.
Use policy
rules:
(1) If the data
exhibits a power-law distribution, that is, part of the data is accessed with
high frequency and part of the data is accessed with low frequency, use
allkeys-lru
(2) If the data
is equally distributed, that is, all data access frequencies are the same, use
allkeys-random
12. Why does edis need to put all data
in memory?
Answer: In
order to achieve the fastest read and write speed, Redis reads the data into
the memory and writes the data to the disk in an asynchronous manner. So redis
has the characteristics of fast and data persistence. If you don't put the data
in memory, the disk I/O speed will seriously affect the performance of redis.
Today, when memory is getting 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 data records reaches the memory limit.
13. Do you understand the
synchronization mechanism of Redis?
Answer: Redis
can use master-slave synchronization and slave-slave synchronization. During
the first synchronization, the master node performs a bgsave, and at the same
time records the subsequent modification operations to the memory buffer. After
completion, the RDB file is fully synchronized to the replication node. After
the replication node accepts the completion, the RDB image is loaded into the
memory. After the loading is completed, the master node is notified to
synchronize the operation records modified during the period to the replication
node for replay, and the synchronization process is completed.
14. What are the benefits of Pipeline?
Why use pipeline?
Answer: The
time of 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 stress testing, it can be found that an important
factor that affects 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. When the master is down, it will
automatically upgrade the slave to the master and continue to provide services.
(2) Redis
Cluster focuses on scalability. When a single redis memory is insufficient,
Cluster is used for shard storage.
16. Under what circumstances will the
Redis cluster solution cause the entire cluster to be unavailable?
Answer: A
cluster with three nodes A, B, and C. Without a replication model, if node B
fails, the entire cluster will think that it lacks slots in the range of
5501-11000 and is unavailable.
17. What are the Java clients supported
by Redis? Which one is the official recommendation?
Answer:
Redisson, Jedis, lettuce, etc., Redisson is officially recommended.
18. What are the advantages and
disadvantages of comparing Jedis and Redisson?
Answer: Jedis
is the client of Redis's Java implementation. Its API provides a more
comprehensive support for Redis commands; Redisson implements a distributed and
scalable Java data structure. Compared with Jedis, it has simpler functions and
does not support strings. Operation, does not support Redis features such as sorting,
transactions, pipes, and partitions.
The purpose of
Redisson is to promote the separation of concerns from users to Redis, so that
users can focus more on processing business logic.
19. How to set password and verify
password for Redis?
Set password:
config set requirepass 123456
Authorization
password: auth 123456
20. Tell me about the concept of Redis
hash slots?
Answer: Redis
cluster does not use consistent hash, but introduces the concept of hash slot.
Redis cluster has 16384 hash slots. After CRC16 check, each key is modulo 16384
to determine which slot to place. Each node is responsible for part of the hash
slot.
21. What is the master-slave
replication model of Redis cluster?
Answer: In
order to make the cluster still available when some nodes fail or most nodes
cannot communicate, the cluster uses a master-slave replication model, and each
node will have N-1 replicas.
22. Will there be write operation loss
in Redis cluster? Why?
Answer: Redis
does not guarantee strong data consistency, which means that in practice, the
cluster may lose write operations under certain conditions.
23. How are Redis clusters replicated?
Answer:
Asynchronous replication
24. What is the maximum number of nodes
in a Redis cluster?
Answer: 16,384.
25. How to choose a database for Redis
cluster?
Answer: Redis
cluster currently cannot be used for database selection, the default is 0 database.
26. How to test the connectivity of
Redis?
Answer: Use the
ping command.
27. How to understand Redis
transaction?
answer:
(1) The
transaction is a separate isolated operation: all commands in the transaction
will be serialized and executed in order. During the execution of the
transaction, it will not be interrupted by the command request sent by other
clients.
(2) A transaction
is an atomic operation: either all commands in the transaction are executed or
none of them are executed.
28. What are the commands related to
Redis transactions?
答:MULTI、EXEC、DISCARD、WATCH
29. How to set the expiration time and
permanent validity of Redis key?
Answer: EXPIRE
and PERSIST commands.
30. How does Redis optimize memory?
Answer: Use
hash tables as much as possible. The memory used by hash tables (which means
that the number stored in a hash table is small) is very small, so you should
abstract your data model into a hash table 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, surname, email, or password, but store all the user's information
in a hash table.
31. How does the Redis recycling
process work?
Answer: A
client ran a new command and added new data. Redi checks the memory usage, and
if it is greater than the maxmemory limit, it will be recycled according to the
set strategy. A new command is executed, and so on. So we continue to cross the
boundary of the memory limit, by continuously reaching the boundary and then
continuously reclaiming back below the boundary. If the result of a command
causes a large amount of memory to be used (for example, the intersection of a
large set is saved to a new key), it will not take long for the memory limit to
be exceeded by this memory usage.
32. Are there any ways to reduce the
memory usage of Redis?
Answer: If you
are using a 32-bit Redis instance, you can make good use of collection type
data such as Hash, list, sorted set, set, because usually many small Key-Values
can be stored together in a more compact way.
33. What happens when Redis runs out of
memory?
Answer: If the
set upper limit is reached, Redis write commands will return error messages (but
read commands can also return normally.) Or you can use Redis as a cache to use
the configuration elimination mechanism. When Redis reaches the memory limit,
it will flush out the old ones. content.
34. How many keys can a Redis instance
store at most? List, Set, Sorted Set, how many elements can they store at most?
Answer: In
theory, Redis can handle up to 232 keys, and has been tested in practice. Each
instance stores at least 250 million keys. We are testing some larger values.
Any list, set, and sorted set can contain 232 elements. In other words, the
storage limit of Redis is the available memory value in the system.
35. There are 2000w data in MySQL and
only 20w data in redis. How to ensure that the data in redis are all hot data?
Answer: When
the size of the Redis memory data set rises to a certain size, a data
elimination strategy will be implemented.
Related
knowledge: Redis provides 6 data elimination strategies:
Volatile-lru:
select the least recently used data from the data set (server.db[i].expires)
with an expiration time set to be eliminated
Volatile-ttl:
select the data to be expired from the data set (server.db[i].expires) that has
set expiration time
Volatile-random:
arbitrarily select data to be eliminated from the data set
(server.db[i].expires) for which the expiration time has been set
allkeys-lru:
select the least recently used data from the data set (server.db[i].dict) to
eliminate
allkeys-random:
arbitrarily select data to eliminate from the data set (server.db[i].dict)
no-enviction
(eviction): prohibit eviction of data
36. What scenario is Redis most
suitable for?
1. Session
Cache
The most
commonly used scenario for using Redis is the session cache. The advantage of
using Redis to cache sessions over other storage (such as Memcached) is that
Redis provides persistence. When maintaining a cache that does not strictly
require consistency, most people will be unhappy if all the user's shopping
cart information is lost. Now, will they still be like this? Fortunately, as
Redis has improved over the years, it is easy to find out how to properly use
Redis to cache session documents. Even Magento, a well-known commercial
platform, provides plugins for Redis.
2. Full page
cache (FPC)
In addition to
the basic session token, Redis also provides a very simple FPC platform. Going
back to the consistency issue, even if the Redis instance is restarted, users
will not see a drop in page loading speed because of disk persistence. This is
a great improvement, similar to the PHP local FPC. Taking Magento as an example
again, Magento provides a plug-in to use Redis as a full-page cache backend. In
addition, for WordPress users, Pantheon has a very good plugin wp-redis, which
can help you load the pages you have browsed as quickly as possible.
3. Queue
A major
advantage of Reids in the field of memory storage engines is to provide list
and set operations, which makes Redis a good message queue platform to use. The
operation used by Redis as a queue is similar to the push/pop operation of a
local programming language (such as Python) on a list. If you quickly search
for "Redis queues" in Google, you will immediately find a large
number of open source projects. The purpose of these projects is to use Redis
to create very good back-end tools to meet various queue needs. For example,
Celery has a backend that uses Redis as a broker. You can check it from here.
4.
Leaderboard/Counter
Redis
implements the operation of incrementing or decrementing numbers in memory very
well. Set and Sorted Set also make it very easy for us to perform these
operations. Redis just provides these two data structures. So, we need 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 based on
the scores of your users Ascending sort. If you want to return users and user
scores, you need to do this: ZRANGE user_scores 0 10 WITHSCORES Agora Games is
a good example, implemented in Ruby, and its leaderboard uses Redis to store
data, you can go here See.
5.
Publish/Subscribe
Last (but
certainly not the least important) is the publish/subscribe feature of Redis.
There are indeed many usage scenarios for publish/subscribe. I have seen people
use it in social network connections, can also be used as a script trigger
based on publish/subscribe, and even use the publish/subscribe function of
Redis to build a chat system!
37. If there are 100 million keys in
Redis, 10w of them start with a fixed, known prefix. What if you can find them
all?
Answer: Use the
keys command to scan out the key list of the specified mode.
The other party
then asked: If this redis is providing services to online businesses, what are
the problems with using the keys command?
At this time,
you have to answer a key feature of redis: redis's single thread. The keys
instruction will cause the thread to block for a period of time, and the online
service will be paused. The service cannot be restored until the instruction is
executed. At this time, you can use the scan command. The scan command can
extract the key list of the specified mode without blocking, but there will be
a certain probability of repetition. It is enough to do the deduplication once
on the client-side, but the overall time will be more than direct use. The keys
instruction is long.
38. If there are a large number of keys
that need to be set to expire at the same time, what should be paid attention
to?
Answer: If the
expiration time of a large number of keys is set too concentrated, redis may
have a short period of time when it expires. It is generally necessary to add a
random value to the time to make the expiration time more scattered.
39. Have you used Redis as an
asynchronous queue? How do you use it?
Answer:
Generally, the list structure is used as a queue, rpush produces messages, and
lpop consumes messages. When there is no message from lpop, you need to sleep
for a while and try again. If the other party asks, can I not sleep? The list
also has a command called blpop. When there is no message, it will block until
the message arrives. If the other party asks, can he produce once and consume
many times? Using the pub/sub topic subscriber mode, a 1:N message queue can be
realized.
If the other
party asks what are the disadvantages of pub/sub?
In the case of
consumers going offline, the produced messages will be lost, and a professional
message queue such as RabbitMQ must be used.
If the other
party asks how redis implements the delay queue?
I guess now you
really want to beat the interviewer to death. If you have a baseball bat in
your hand, why do you ask in such detail? But you are very restrained, and then
replied with an attitude: Use sortedset, use the timestamp as the score, and
the message content as the key to call zadd to produce the message. Consumers
use the zrangebyscore command to obtain the data polled N seconds ago for
processing. At this point, the interviewer secretly gave you a thumbs up. But
what he didn't know was that you raised your middle finger at the moment,
behind the chair.
40. Have you ever used Redis
distributed lock? What is it?
First use setnx
to compete for the lock, and then use expire to add an expiration time to the
lock to prevent the lock from forgetting to release it.
In response to the knowledge points asked in the above interview, I have summarized most of the interview questions and answers involved in the Internet company’s Java programmer interview. Documents and architecture materials are shared with everyone. I hope to help you before the interview. Reviewing and finding a good job also saves everyone's time to search for information on the Internet to learn.
Comments
Post a Comment