Redis as the current mainstream NoSql database, is not impossible, in the interview is also very high frequency, must not lose points in this link, whether it is learning, or interview, the following knowledge points, are necessary to master. Bloggers will continue to update, I hope you can support me.
1. What is Redis?
Redis is an open source (BSD licensed) store of in-memory data structures that can be used as databases, caches, and message brokers, and is a NoSQl database based on key-value pairs.
2. Redis feature?
- Speed is fast
- Key-value pair-based data structure server
- Rich functionality, rich data structures
- Simple and stable
- Multiple client languages
- persistence
- A master-slave replication
- High can & distributed
3. Suitable application scenarios for Redis?
- The cache
- list
- counter
- Distributed session
- A distributed lock
- The social network
- The latest list
- The messaging system
4. Besides Redis, what other NoSQL databases do you know?
MongoDB, MemcacheDB, Cassandra, CouchDB, Hypertable, Leveldb.
5. The difference between Redis and Memcache?
Different storage types are supported. Memcached supports only a simple K/V structure. Redis supports more types of storage structure types (see question 6).
While memcached data cannot be recovered, Redis can persist data to disk.
The new version of Redis directly builds its own VM mechanism, the normal system calls system functions, will waste a certain amount of time to move and request.
When the physical memory is used up, redis can swap values that have not been used for a long time to disks.
6. How many data types does Redis have?
Basics: String, hash, list, set, zset.
Also HyperLogLog, flow, geographic coordinates, etc.
7. What are the advanced features of Redis?
Message queuing, automatic expiration deletion, transactions, data persistence, distributed locks, nearby people, slow query analysis, Sentinel and clustering.
8. Have you ever installed Redis?
1. Download the Redis source code installation package to the current directory.
-
Unzip the Redis source code installation package.
-
Create a redis directory soft link pointing to the decompression package.
-
Go to the redis directory
-
compile
-
The installation
Children’s boots with Docker are easier.
docker pull redis
9. How many major executables are there in Redis? Is respectively?
10. How many ways can I start Redis?
1. Default configuration:
./redis-server
2. Run and start: Redis-server plus the configuration name and value to be changed (can be multiple pairs), the default configuration will be used if there is no configuration.
For example, redis-server — port 7359
3. Specify the configuration file to boot:
./redis-server /opt/redis/redis.conf
11.Redis configuration needs to be written? How to configure it?
There is a redis. Conf template configuration in the redis directory. So just copy the template configuration and modify it.
In general, most production environments start Redis with a specified configuration file.
12. How to execute Redis client commands?
1. Interaction mode:
Redis -cli -h 127.0.0.1 -p 6379Copy the code
Once connected to Redis, the commands that follow can be implemented interactively.
2. Cli:
Redis -cli -h 127.0.0.1 -p 6379 get valueCopy the code
13. How do I stop the Redis service?
Kill -9 PID (do not use this command because the data will not be persisted and the cache will not be closed gracefully)
You can use redis’ shutdown command to persist data before shutting it down.
redis-cli shutdown nosave|save
Copy the code
14. How do I check whether the current key exists?
exists key
15. How do I delete data?
del key
16. Why is Redis fast? Single thread?
- Redis uses a single-threaded architecture and the I/O multiplexing model model.
- Pure memory access.
- Because it is a single thread, it avoids the resource consumption associated with thread context switching.
17. What is the maximum value of a string?
512MB
18. How many databases does Redis have by default?
16
19. How many ways to persist Redis?
RDB, AOF, mixed persistence.
20.RDB persistence?
Redis DataBase (RDB) persistence is a process of storing snapshots of current process data on hard disks.
Tips: Write to disk in binary mode.
21. How is persistence of the RDB triggered?
Manual trigger:
Save: block the current Redis server until the RDB process is complete, which can cause a long block if the data is large.
Not recommended online.
Bgsave: The redis process forks the child process. The child process is responsible for the persistence
The fork phase, half the time is very short.
Automatic trigger:
save xsecends n:
RDB persistence is triggered when at least n keys change in x seconds. That is, when the condition is met, persistence is triggered.
flushall :
The primary and secondary synchronization is triggered
22. What are the advantages of RDB?
- The RDB is a compact binary file that represents a snapshot of Redis data at a point in time.
- Ideal for backup, full copy scenarios, useful for disaster recovery.
- Redis loads RDB recovery data much faster than AOF.
23. Disadvantages of RDB?
- RDB cannot persist in real time. Data will be lost for a period of time if it is terminated unexpectedly.
- The RDB requires fork() to create the child process, which is a heavyweight operation and can cause Redis to stall for several seconds.
24. How do I disable persistence?
Generally speaking, the build environment is not used, so it is good to know.
config set save ""
Copy the code
25.AOF Persistence?
AOF(Append only file) To solve the RDB can not live persistence problem, AOF to fix. Record each command in a separate log to an AOF file.
26. How do I check whether AOF is enabled?
config get appendonly
Copy the code
27. How do I enable AOF?
Cli: Takes effect in real time but takes effect after restart.
config set appendonly
Copy the code
Configuration file: The configuration file takes effect only after being restarted.
appendonly yes
Copy the code
28.AOF workflow?
1. All write commands are appended to the aOF_buf buffer.
2. The AOF buffer synchronizes data to disks based on the corresponding policy.
3. As AOF files become larger and larger, AOF files need to be rewritten periodically to achieve compression.
4. When the Redis server restarts, the AOF file can be loaded for data recovery.
29. Why does AOF append commands to the cache (aOF_buf) first?
Redis uses single-threaded response commands, and if each write file command is appended directly to the disk, performance will depend on the load on the disk. If you use buffers, Redis provides a variety of buffer policies to balance performance and security.
30. How is AOF persistence triggered?
Automatic trigger: satisfies the set policy and satisfies the rewrite trigger.
Policy :(configured in the configuration file)
Manual trigger :(execute command)
bgrewriteaof
Copy the code
31. AOF advantages?
- AOF provides three save strategies: save per second, system policy, and save per operation. The real-time performance is relatively high. Generally speaking, it will choose to save every second, so at most one second of data will be lost when an accident occurs.
- The file is appended to write, so the file is seldom damaged. If the data is not written unexpectedly, the redis-check-aof tool can be used to repair it.
- Since AOF is in text form, it adopts the protocol format directly, avoiding the overhead of secondary processing and being flexible for modification.
32. AOF weaknesses?
- AOF files are larger than RDB files.
- AOF cold preparation is not as fast as RDB.
- Performance is not as good as RDB at high load due to high execution frequency.
33. Mixed Persistence? The advantages and disadvantages?
In general, we use mixed persistence online. New hybrid persistence methods have been added since Redis4.0.
Advantages:
- While loading quickly, you avoid losing more data.
Disadvantages:
- Due to mixing the two formats, the readability is poor.
- Compatibility requires 4.0 and later support.
Redis Java client official recommendation? Actual choice?
There are three official recommendations: Jedis, Redisson and lettuce.
In general use more: Jedis | Redisson.
Jedis: Lighter weight, brief, no read/write separation required us to implement, less documentation. The API provides comprehensive Redis command support.
Redisson: Based on Netty, provides high performance and supports asynchronous requests. Many distributed related operation services are provided. More advanced features, more documentation, but more practical complexity. Compared with Jedis, the function is relatively simple, does not support string operations, does not support sorting, transaction, pipeline, partition and other Redis features.
35. Redis transactions?
Transactions provide a mechanism to package multiple command requests and execute multiple commands at once and sequentially. And while the transaction is executing, the server does not interrupt the transaction to execute another client command request, it does
36. How many stages do Redis transactions start to finish?
- Open the transaction
- The command team
- Execute transaction/abandon transaction
Redis key expiration operation?
Set the key lifetime to n seconds
expire key nseconds
Copy the code
Set the life time of the key to nmilliseconds
pxpire key milliseconds
Copy the code
Set the expiration time to timestamp of the specified number of seconds
expireat key timespamp
Copy the code
Set the expiration time to timestamp in milliseconds
pexpireat key millisecondsTimestamp
Copy the code
38.Redis expiration key deletion policy?
Scheduled deletion: At the same time as the set expiration time, create a timer to delete the key immediately when the expiration time comes.
Lazy deletion: An expired key is left alone, but each time a key is retrieved from the key space, the retrieved key is checked for expiration, deleted if expired, and returned if not.
Periodic deletion: Deletes the expiration key every once in a while and reduces the impact of the deletion on the CPU time by taking the deletion duration and frequency first.
39. What is Pipeline? Why do you want it?
Command batch technology, which assembles commands and then executes multiple commands at once.
This saves Round Trip Time (RTT).
Tested and verified:
- Pipeline execution is generally faster than line-by-line execution.
- The larger the network extension between client and service, the more obvious the effect of pipeline.
40. How to obtain the current maximum memory? How to set it dynamically?
Get maximum memory:
config get maxmemory
Copy the code
Set maximum memory:
Command Settings:
config set maxmemory 1GB
Copy the code
Redis memory overflow control?
When Redis reaches the maxMemory limit, the corresponding overflow strategy is initiated.
42.Redis memory overflow policy?
Noeviction: Rejects all write operations and returns client error OOM Command not allowed when used memory, responds only to read operations.
- Volatile – LRU: Deletes keys with expire attributes based on the LRU algorithm until sufficient space is available. If there are no key objects to remove, retreat to the Noeviction policy.
- Allkeys-lru: Deletes keys according to the LRU algorithm, regardless of whether the timeout attribute is set, until enough space is made.
- Allkeys-random: randomly remove allkeys until enough space is left.
- Volatile -random: Deletes expired keys randomly until sufficient space is available.
- Volatile – TTH Deletes recently expired data based on the TTL property of the key value object. If not, retreat to Noeviction strategy.
43.Redis High availability solution?
Redis Sentinel automates fault detection and migration.
44.Redis cluster solution?
Twemproxy, Redis Cluster, Codis.
45.Redis Cluster slot scope?
0 ~ 16383
46.Redis lock implementation idea?
Setnx (set if not exists) : If the lock is created successfully, the lock is obtained.
Setnx lock true Creates the lock
Del lock Releases the lock
What if I crash and can’t release the lock?
At this point you need to consider the issue of timeout. For example: Expire Lock 300
Since the command is non-atomic, it will still be deadlocked, how to resolve?
Redis supports the set and timeout function.
Example: Set lock true ex 30 Nx
47. What is a Bloom filter?
It was proposed by Bloom in 1970. It’s actually a long binary vector and a series of random mapping functions. Bloom filters can be used to retrieve whether an element is in a collection. The advantage of this algorithm is that the space efficiency and query time are much better than the general algorithm, but the disadvantage is that there is a certain error recognition rate and deletion difficulty.
Tips: When the judgment must exist, it may misjudge. When the judgment does not exist, it must not exist.
48. What is cache penetration? Deal with the problem?
Cache penetration: the cache layer is not hit, and the storage layer is not hit.
Solution 1: Cache empty objects, but this time will occupy more memory space, so according to your service characteristics to set the timeout period to control the memory occupation problem.
Treatment mode 2: Bloom filter.
49. What is cache warming?
After the system goes online, the relevant data will be loaded into the cache system in advance to avoid users to check the database first and then cache.
50. What is cache avalanche? Deal with the problem?
Cache avalanche: Because the cache layer is overloaded with requests, it effectively protects the storage layer. However, if the storage layer cannot provide services for some reason, calls to the storage layer surge and the storage layer breaks down.
Processing:
- Ensure high availability of cache layer services.
- The cache system to do real-time monitoring, alarm, etc.
- Dependency isolation components limit and degrade back-end traffic.
- Make it persistent for fast data recovery.
Reference:
-
Redis Deep Adventures: Core Principles and Practical Applications
-
Redis Development and Operation
-
Redis Design and Implementation
-
https://redis.io/
-
Baidu encyclopedia
This article is formatted using MDNICE
New blogger for three, at the same time pay attention to my public account [Java small coffee show] reply interview can be a white whone “Java full level engineering lion interview questions. PDF” continue to update, adhere to ✊✊✊