[Java interview question summary 14] Redis interview question summary (with answers)

1. What is Redis?

Redis is a key-value storage system. It supports more stored value types, including string, list, set, zset (sorted set) and hash. These data structures all support push/pop, add/remove, intersection, union and difference, and richer operations, and these operations are all atomic. On this basis, Redis supports a variety of different ways of sorting. In order to ensure efficiency, data is cached in memory, Redis will periodically write updated data to disk or write modification operations to additional record files, and on this basis, it realizes master-slave (master-slave) synchronization .

2. What are the usage scenarios of Redis?

  1. Redis is a memory-based nosql database, which can be persisted in the form of new threads, without affecting Redis single-threaded read and write operations
  2. Get the latest N data through the list
  3. Simulate a scenario similar to a token that requires an expiration time to be set
  4. Publish and Subscribe Message System
  5. Timer, counter

3. What are the functions of Redis?

1. Cache based on native memory

When calling the api to access the database, if the process takes 2 seconds, if each request has to access the database, it will cause huge pressure on the server. If the result of this sql query is stored in Redis, when you request again, directly from Instead of accessing the database, the efficiency will be greatly improved. Redis can update the data regularly (for example, 1 minute).

2. If the computer restarts, will the data written to the memory become invalid? At this time, Redis also provides the function of persistence.

3. Sentinel and copy

Sentinel can manage multiple Redis servers, and it provides monitoring, reminding and automatic failover functions;

Replication is to allow the Redis server to be equipped with a backup server;

Redis also uses these two functions to ensure the high availability of Redis;

4. Cluster

There is always an upper limit on the resources of a single server. CPU and IO resources can be read and written through master-slave replication, which transfers part of the CPU and IO pressure to the slave server, but what about the memory resources, the master-slave mode is only for data Backup, and memory cannot be expanded;

Now we can scale horizontally so that each server is only responsible for a part of the task, and then these servers form a whole. To the outside world, this group of servers is like a cluster.

4. What data types does Redis support?

  1. String
  2. hash
  3. list
  4. set
  5. zset

Five, Redis value storage problem

1. Take out the Redis connection pool first

JedisPool pool = new JedisPool(new JedisPoolConfig(),"127.0.0.1"); Jedis jedis = pool.getResource();

2. Access value

jedis.set("key","value");jedis.get("key");jedis.del("key");//给一个key叠加valuejedis.append("key","value2");//此时key的值就是value + value2;//同时给多个key进行赋值:jedis.mset("key1","value1","key2","value2");

3. Operate the map

Map<String,String> user = new HashMap();user.put("key1","value1");user.put("key2","value2");user.put("key3","value3");//存入jedis.hmset("user",user);//取出user中key1 List<String> nameMap = jedis.hmget("user","key1");//删除其中一个键值jedis.hdel("user","key2");//是否存在一个键jedis.exists("user");//取出所有的Map中的值:Iterator<String> iter = jedis.hkeys("user").iterator();while(iter.next()){    jedis.hmget("user",iter.next());}

6. Why is Redis single-threaded?

  1. The code is clearer and the processing logic is simpler;
  2. There is no need to consider various lock issues, there is no lock and release operation, and there is no performance problem due to possible deadlock;
  3. There is no CPU consumption due to multi-thread switching;
  4. Cannot take advantage of the multi-core CPU, but it can be improved by opening several Redis instances;

7. How many ways are there for Redis persistence?

Redis provides two persistence methods, RDB (Redis DataBase) and AOF (Append Only File).

RDB, in short, is to generate snapshots of data stored in redis at different points in time and store them on disks and other media;

AOF is a different perspective to achieve persistence, that is, to record all the write instructions that redis has executed. When redis is restarted next time, as long as these write instructions are executed again from front to back, the data can be realized. recovered.

In fact, RDB and AOF can also be used at the same time. In this case, if redis restarts, the AOF method will be preferred for data recovery, because the AOF method has a higher data recovery integrity.

If you don't have the need for data persistence, you can also turn off RDB and AOF. In this case, redis will become a pure memory database, just like memcache.

8. What is the difference between Redis and memecache?

1. Redis has more data structures and supports richer data operations than memecache.

(1) Redis supports key-value, and the commonly used data types mainly include String, Hash, List, Set, Sorted Set.

(2) Memecache only supports key-value.

2. Comparison of memory usage. Redis uses a hash structure for key-value storage. Due to its combined compression, its memory usage will be higher than that of memecache.

3. Performance comparison: Redis only uses single core, memecache uses multiple cores.

4. Redis supports disk persistence, but memecache does not.

Redis can swap some values ​​that have not been used for a long time to disk through the swap method.

5. Redis supports distributed clusters, but memecache does not.

9. What are the java clients supported by Redis?

Redisson, Jedis, lettuce, etc., Redisson is officially recommended.

10. What is the difference between jedis and redisson?

Both Jedis and Redisson are packages of Redis operations in Java. Jedis simply encapsulates the Redis API library and can be regarded as a Redis client. Its method is very similar to Redis commands. Redisson not only encapsulates redis, but also encapsulates support for more data structures, as well as functions such as locks, which are larger than Jedis. But Jedis is more native and more flexible than Redisson.

11. What is cache penetration? How to deal with it?

1. Cache penetration

The general cache system caches queries according to the key. If there is no corresponding value, you should go to the back-end system to find it (such as a DB database). Some malicious requests will deliberately query non-existent keys, and the large amount of requests will cause a lot of pressure on the back-end system. This is called cache penetration.

2. How to solve it?

The query result is also cached when the query result is empty, and the cache time is set to be shorter, or the data corresponding to the key is inserted after the cache is cleared.
 
Filter keys that must not exist. You can put all possible keys in a big Bitmap, and pass the Bitmap filtering when querying.

3. Cache avalanche

When the cache server restarts or a large number of caches fail in a certain period of time, when it fails, it will put a lot of pressure on the back-end system and cause the system to crash.

4. How to solve it?

  1. After the cache is invalid, the number of threads that read the database and write the cache is controlled by locking or queueing. For example, only one thread is allowed to query data and write cache for a certain key, and other threads wait;
  2. Do the second level cache;
  3. Set different expiration times for different keys to make the cache invalidation time as even as possible;

12. How to ensure the consistency of cache and database data?

1. Eliminate the cache

If the data is more complex, the update operation of the cache will become extremely complicated. Therefore, it is generally recommended to choose to eliminate the cache instead of updating the cache.

2. Choose to eliminate the cache first, and then update the database

If the database is updated first, and then the cache is eliminated, if the elimination of the cache fails, then subsequent requests will get dirty data until the cache expires.

If the cache is eliminated first and then the database is updated, if the update of the database fails, only one cache penetration will occur. In comparison, the latter has no essential impact on the business.

3. Delayed double deletion strategy

The following scenario: At the same time, there is a request for A to perform an update operation, and another request for B to perform a query operation.

  1. Request A to write and delete the cache
  2. Request B to query and find that the cache does not exist
  3. Request B to query the database to get the old value
  4. Request B to write the old value to the cache
  5. Request A to write the new value to the database

The data inconsistency problem occurred every time. It can be solved by adopting the delayed double deletion strategy.

public void write(String key,Object data){    redisUtils.del(key);    db.update(data);    Thread.Sleep(100);    redisUtils.del(key);}

In this way, the dirty data in the cache caused within 1 second can be deleted again. This time setting can be adjusted according to Russian business scenarios.

4. Scenario where the database reads and writes are separated

Two requests, one request A to perform an update operation, and the other request B to perform a query operation.

  1. Request A to write and delete the cache
  2. Request A to write data to the database,
  3. Request B to query the cache and found that the cache has no value
  4. Request B to query from the library, at this time, the master-slave synchronization has not been completed, so the query is the old value
  5. Request B to write the old value to the cache
  6. The database completes the master-slave synchronization, and the slave database becomes the new value

The delayed double deletion strategy is still used to solve this problem.

13. Redis, what is cache penetration? How to deal with it?

1. Cache penetration

The general cache system caches queries according to the key. If there is no corresponding value, you should go to the back-end system to find it (such as a DB database). Some malicious requests will deliberately query non-existent keys, and the large amount of requests will cause a lot of pressure on the back-end system. This is called cache penetration.

2. How to solve it?

The query result is also cached when the query result is empty, and the cache time is set to be shorter, or the data corresponding to the key is inserted after the cache is cleared.
 
Filter keys that must not exist. You can put all possible keys in a big Bitmap, and pass the Bitmap filtering when querying.

3. Cache avalanche

When the cache server restarts or a large number of caches fail in a certain period of time, when it fails, it will put a lot of pressure on the back-end system and cause the system to crash.

4. How to solve it?

  1. After the cache is invalid, the number of threads that read the database and write the cache is controlled by locking or queueing. For example, only one thread is allowed to query data and write cache for a certain key, and other threads wait;
  2. Do the second level cache;
  3. Set different expiration times for different keys to make the cache invalidation time as even as possible;

14. How does Redis implement distributed locks?

Use Redis to implement distributed locks

redis command: set users 10 nx ex 12 atomic command

//使用uuid,解决锁释放的问题@GetMappingpublic void testLock() throws InterruptedException {    String uuid = UUID.randomUUID().toString();    Boolean b_lock = redisTemplate.opsForValue().setIfAbsent("lock", uuid, 10, TimeUnit.SECONDS);    if(b_lock){        Object value = redisTemplate.opsForValue().get("num");        if(StringUtils.isEmpty(value)){            return;        }        int num = Integer.parseInt(value + "");        redisTemplate.opsForValue().set("num",++num);        Object lockUUID = redisTemplate.opsForValue().get("lock");        if(uuid.equals(lockUUID.toString())){            redisTemplate.delete("lock");        }    }else{        Thread.sleep(100);        testLock();    }}

Remarks: Lua scripts can be used to ensure the atomicity of distributed locks.

15. What are the disadvantages of Redis distributed locks?

Redis distributed locks cannot solve the timeout problem. Distributed locks have a timeout period. If the execution of the program exceeds the lock timeout period, problems will occur.

Several problems that Redis is prone to:

  1. The lock was not released
  2. Lock B was released by Lock A
  3. Database transaction timeout
  4. The lock has expired and the business has not been executed yet
  5. Redis master-slave replication problem

16. How does Redis optimize memory?

1. Shorten the length of the key value

  1. Shortening the length of the value is the key. If the value is a large business object, you can serialize the object into a binary array;
  2. First, the business should be streamlined, unnecessary attributes should be removed, and some useless data should be avoided;
  3. The second is the choice of serialization tools. More efficient serialization tools should be selected to reduce the size of the byte array;
  4. Taking JAVA as an example, the built-in serialization method is not satisfactory in terms of speed or compression ratio. At this time, you can choose more efficient serialization tools, such as: protostuff, kryo, etc.

2. Shared object pool

Object shared pool refers to the integer object pool [0-9999] maintained internally by Redis. Creating a large number of integer types redisObject has memory overhead, and the internal structure of each redisObject occupies at least 16 bytes, which even exceeds the space consumption of the integer itself. Therefore, Redis memory maintains an integer object pool of [0-9999] to save memory. In addition to integer value objects, other types such as list, hash, set, and zset internal elements can also use integer object pools. Therefore, under the premise of meeting the requirements, use integer objects to save memory as much as possible during development.

3. String optimization

4. Coding optimization

5. Control the number of keys

Highlights from the past issue:

Summary of Java knowledge system (2021 version)

Summary of basic knowledge of Java multithreading (absolutely classic)

[Summary of the most complete Java framework in the full stack] SSH, SSM, Springboot

Super detailed springBoot study notes

Summary of common data structures and algorithms

Java design patterns: a comprehensive analysis of 23 design patterns (super detailed)

Summary of Java interview questions (with answers)