Java Development Interview--Redis Zone

1. What is Redis? What are its main features?

答:

Redis is an open source, memory-based, high-performance key-value storage system. It is mainly used in scenarios such as caching, data storage and message queues.

  1. High performance : Redis stores data in memory and uses a single-threaded method to process requests, making its read and write speed very fast, capable of reaching 100,000+ read and write operations per second.
  2. Rich data structures : Redis supports a variety of data structures, including strings, lists, hash tables, sets, ordered sets, etc. The flexibility of these data structures allows Redis to meet the needs of various scenarios.
  3. Persistence support : Redis provides two persistence methods, namely RDB and AOF. RDB saves a snapshot of the current data to the disk, while AOF appends the operation log to the file. These two methods can be selected and configured according to needs.
  4. High concurrent access : Redis has built-in transaction functions and optimistic locking mechanisms, which can effectively handle concurrent access issues. At the same time, Redis also provides some atomic operation instructions, such as INCR, DECR, etc., which can ensure the atomic execution of multiple operations.
  5. Distributed support : Redis provides a cluster mode that can distribute data across multiple nodes. Through data sharding and replication mechanisms, high availability and scalability of data are achieved.
  6. Publish/subscribe mode : Redis supports the publish/subscribe mode, which can realize the broadcast and subscription functions of messages. This is useful for building real-time messaging systems or event-driven applications.

2. What is the difference between Redis and other common relational databases (such as MySQL)?

答:

There are significant differences between Redis and relational databases (such as MySQL) in many aspects. The following is an introduction to some of the main differences:

  1. Data model : Redis is a key-value storage system that uses simple key-value pairs to store data. MySQL is a database based on a relational model, using tables to store structured data.
  2. Storage : Redis stores data in memory, which allows it to read and write data quickly. MySQL usually stores data on disk, and access speed is relatively slow.
  3. Data query : Redis query operations mainly rely on keys, and the corresponding values ​​are obtained through keys. MySQL supports complex SQL query language and can use a variety of conditions and associations to query data.
  4. Transaction processing : Redis supports simple transaction processing (multi/exec/discard), but does not support rollback operations. MySQL supports complex transaction processing, including support for ACID features, which can implement more complex transaction logic.
  5. Data persistence : Redis provides a data persistence mechanism, including RDB and AOF, which can save data to disk. MySQL saves data on disk by default and provides a variety of persistence methods, such as InnoDB's transaction log and binary log.
  6. Scalability : Because Redis stores data in memory and uses a single thread to handle requests, it can easily scale to multiple nodes. The expansion of MySQL often needs to be achieved through master-slave replication or sharding.
  7. Data consistency : Redis stores data in memory by default, and there is no guarantee of strong consistency. MySQL uses ACID features to ensure data consistency.

3. What are the data structures of Redis? Please introduce their uses respectively.

答:

  1. String : String is the most basic data structure of Redis. It can store values ​​of strings, integers, floating point numbers and other types. Strings are widely used in Redis, such as storing user information, cached data, counters, etc.
  2. List : A list is an ordered collection of string elements that can be quickly inserted and deleted at both ends of the list. Lists can be used to implement data structures such as queues and stacks, and can also be used to store logs, message queues, etc.
  3. Hash table (Hash) : A hash table is a collection of key-value pairs, where the keys and values ​​are both string types. Hash tables are suitable for storing and reading object attributes, such as storing user information, product information, etc. Through the hash table, individual attributes can be easily read and written.
  4. Set : A set is an unordered and non-repeating collection of string elements. Sets can perform operations such as intersection, union, and difference. You can also deduplicate sets and determine whether an element exists. Collections are often used to store tags, friend relationships , etc.
  5. Sorted Set : An ordered set is an ordered set of string elements. Each element is associated with a score and is sorted by the score. Ordered collections are often used in scenarios such as rankings and latest news lists . Range queries can be performed based on scores, and rankings can also be obtained based on elements.
  6. Geospatial : Redis supports geographical location data structure, which can store longitude and latitude coordinates, and perform distance calculations and range queries on coordinates. The geographical location data structure is suitable for scenarios such as nearby people and location recommendations .

4. What are the data persistence methods supported by Redis? Please describe their differences.

答:

Two data persistence methods supported by Redis :

  1. RDB (Redis Database) : RDB is the default persistence method of Redis . It saves the data to disk in a binary format snapshot , which contains the data of all key-value pairs in the current database. The advantage of RDB is that data is stored in compact binary files, which is very efficient for large-scale data sets and regular backups. At the same time, RDB loads faster when restoring data. However, RDB saves snapshot data, so if Redis unexpectedly shuts down, the data after the last snapshot may be lost.
  2. AOF (Append-Only File) : AOF is another Redis data persistence method. It recovers data by recording operations on the database by appending each write command to the end of the file. The advantage of AOF is that it can ensure that the data is more durable. Even if Redis unexpectedly crashes, the data can be restored by replaying the log. In addition, AOF also supports different persistence strategies, such as synchronization once every second, synchronization once every modification, etc. However, because AOF records each write command in text form , AOF files are usually larger and the recovery speed is relatively slower than RDB.

There are some differences in use between these two data persistence methods :

  • RDB is suitable for data backup and fast recovery . It generates compact binary files and is suitable for scenarios where backup is scheduled and data integrity requirements are not very high.
  • AOF is suitable for scenarios with high data persistence requirements . It achieves persistence by recording every write command, which can provide better data security and disaster recovery capabilities. However, since each write command needs to be appended to the end of the file, AOF may have slightly worse writing performance than RDB.

5. How to use Redis for caching and data storage in Java development?

答:

  1. Add dependencies : First, you need to add dependencies on the Redis client library. In the Maven project, you can add dependencies on some framework integrations such as Jedis or Lettuce libraries or Sa-Token.
  2. Create a Redis client connection : Use a Redis client such as Jedis or Lettuce to create a connection and set the connection information (such as host, port, password, etc.).
  3. Use Redis to cache data : After serializing the Java object into a string, store it in the Redis cache through the set method. When reading data, use the get method to obtain the data in the cache and deserialize it into a Java object.
  4. Use Redis to store data : Similar to caching, after a Java object is serialized into a string, it can be stored in Redis through the set method. However, it should be noted here that the expiration time (such as 20 minutes, 1 hour, etc.) needs to be set when storing data to prevent the data from occupying memory indefinitely.
  5. Proper optimization of cache and data storage : You can use data structures such as hash tables, lists, and sets provided by Redis to store different types of data to improve access efficiency. At the same time, you can also set the cache expiration time and LRU policy reasonably to control memory usage and cache hit rate.

6. What are the usage scenarios of Redis? Please give an example.

答:

  1. Caching : One of the most common usage scenarios for Redis is as a cache. By storing hotspot data in Redis memory, the system's read speed and performance can be greatly improved.

    Example: In an e-commerce website, frequently accessed data such as product information and user session information can be stored in the Redis cache to reduce the load on the database and improve page response speed.

  2. Session Store : Redis can also be used as a session store database, especially for distributed or microservice architectures. Storing session data in Redis provides fast read and write capabilities and supports distributed management of sessions.

    Example: In an online multiplayer game application, user login status and game session data can be stored in Redis to quickly identify users and share game status.

  3. Message queue : Redis provides a publish-subscribe function, allowing developers to implement a simple message queue system. It can be used to decouple and process tasks asynchronously.

    Example: In an e-commerce system, when a user places an order, the order information can be published to the Redis message queue, and then the order processing system asynchronously consumes the order data from the message queue for processing.

  4. Real-time rankings : Redis provides data structures such as Sorted Sets and counters, which can be used to implement real-time rankings. By storing and updating scores, you can quickly obtain top scores or rankings.

    Example: In a game application, player scores can be stored as scores in a Redis ordered collection, and ranking information can be quickly obtained based on the scores.

  5. Distributed lock : Redis provides atomic operations and expiration time settings, which can implement a distributed lock mechanism to control access to shared resources.

    Example: In a distributed system, multiple nodes need mutually exclusive access to a resource. You can use Redis's distributed lock to ensure that only one node can obtain access to the resource.

7. How to handle concurrent access to Redis? What are the solutions?

答:

In a high-concurrency environment, access to Redis needs to consider the security and performance issues of concurrent reading and writing.

Solution :

  1. Using Transaction : Redis supports transaction operations, which can be implemented through commands such as MULTI, EXEC, and WATCH. Transactions can be used to package a group of operations together and ensure that these operations are executed in order, making multiple operations atomic.
  2. Using Lock : You can use Redis's distributed lock to achieve concurrency control. By acquiring the lock, it is ensured that only one thread can access key resources at the same time, and other threads need to wait for the lock to be released.
  3. Use Optimistic Locking : Use the version number (or timestamp) to identify whether the data has been modified by other threads. After reading the data, check the version number again. If it changes, it means that concurrent modification has occurred and you need to try again.
  4. Use distributed locks to implement rate limiting : Redis distributed locks can be used to control the frequency of access to a certain resource to limit the number of concurrent requests. For example, you can set the maximum number of requests allowed within a time window and try to acquire a lock as each request arrives, rejecting the request if the lock is already occupied by another request.

8. What are the data elimination strategies of Redis? Please briefly describe their principles and application scenarios.

答:

Redis is an in-memory database. When memory is insufficient, a data elimination strategy needs to be used to determine which data should be cleared out of memory to make room for new data.

Here are several common data retirement strategies :

  1. LRU (Least Recently Used) : Least Recently Used . The LRU algorithm sorts keys according to their most recent access time. When memory is insufficient, the least recently accessed data will be eliminated first.

    Principle: This strategy is based on the idea that "if the data has been accessed recently, the probability of being accessed in the future is also higher."

    Application scenario: It is suitable for scenarios where the access pattern is relatively concentrated and there are obvious hot spots of data . It can retain data with high access frequency.

  2. LFU (Least Frequently Used) : Least frequently used . The LFU algorithm sorts keys according to the number of times they have been accessed. When memory is insufficient, the data with the least access times will be eliminated first.

    Principle: This strategy is based on the idea that "if data is accessed more times, the probability of being accessed in the future is also higher."

    Application scenario: It is suitable for scenarios where the access pattern is relatively average and the number of times each data is accessed is not much different. It can retain frequently accessed data.

  3. Random : Random elimination . This strategy randomly selects a portion of data for elimination without clear sorting rules.

    Principle: This strategy is simple and direct, randomly selecting data for elimination.

    Application scenario: Suitable for scenarios that have no special requirements for data access mode and no specific requirements for the order of data elimination.

  4. TTL (Time To Live) : time to live . Use TTL to set the expiration time. After the expiration time is reached, the data will be automatically eliminated.

    Principle: This strategy is based on setting the life cycle of data and eliminating data by defining the expiration time.

    Application scenario: It is suitable for data with a clear life cycle, such as cached data, temporary data, etc., and can flexibly control the storage time of data according to needs.

9. In terms of bytes, why is Redis so fast?

答:

  1. In-memory storage : Redis is a memory-based database that stores data in memory instead of on disk. Compared with traditional disk storage, memory access is faster and therefore can provide higher performance.
  2. Single-threaded model : Redis adopts a single-threaded model to improve performance by avoiding competition and synchronization overhead between multiple threads. Due to the single-threaded nature, Redis can make full use of the CPU cache and reduce the overhead of thread switching and synchronization.
  3. Efficient data structures : Redis has a variety of built-in efficient data structures, such as strings, lists, hash tables, sets, and ordered sets. These data structures are carefully optimized in the underlying implementation and can quickly perform various operations, such as insertion, deletion, search, etc.
  4. Asynchronous IO : Redis uses asynchronous IO technology. By using event-driven model and non-blocking IO operations, it can handle a large number of concurrent requests and maintain efficient response speed. This allows Redis to handle IO operations without blocking the execution of other requests.
  5. Highly optimized : Redis is highly optimized for various operations in the underlying implementation. For example, it uses the MurmurHash algorithm for fast hash calculations, uses data structures such as compressed lists and skip tables to save memory space, and refines the critical path. Optimization.

10. How do Redis and MySQL ensure data consistency?

答:

First of all, it needs to be made clear that Redis and MySQL are two different types of databases, and they have different mechanisms and strategies for ensuring data consistency.

In Redis , in order to ensure data consistency, the following methods can be used:

  1. Persistence of write operations : By configuring the persistence mechanism of Redis, data is written to the hard disk to prevent data loss when the system crashes or is powered off. Redis provides two persistence methods: RDB snapshot and AOF log. RDB snapshots save the state of the database at a certain point in time to disk, while AOF logs append each write operation to a file. These persistence methods can restore data consistency after Redis restarts.
  2. Master-slave replication : Redis supports a master-slave replication mechanism. After a write operation is performed on the master node, the data will be synchronized to the slave node. By configuring appropriate replication topology and replication strategies, data can be synchronized between master and slave nodes, improving data availability and consistency.
  3. Redis transaction : Redis supports transaction processing, which can package a set of operations into an atomic operation, either all of them are executed successfully or all of them fail. Using Redis transactions can ensure the atomicity of a series of operations and ensure data consistency.

In MySQL , data consistency is mainly ensured through the following methods:

  1. Transaction : MySQL supports transactions with ACID characteristics, which can encapsulate a series of operations in a transaction and use the isolation level of the transaction to control concurrent access. Through the transaction commit and rollback mechanism, the consistency of data between multiple operations can be guaranteed.
  2. Locking mechanism : MySQL controls concurrent access through the locking mechanism, including row-level locks and table-level locks. Through appropriate lock granularity and lock strategy, data conflicts and inconsistencies can be avoided.
  3. Master-slave replication : Similar to Redis, MySQL also supports master-slave replication mechanism. After a write operation is performed on the master node, the data will be synchronized to the slave node to ensure data consistency across multiple nodes.

11. Does Redis have thread safety issues? Why?

答:

In Redis, there are thread safety issues in some specific cases . Mainly include the following points:

  1. Atomicity of commands : Although Redis is single-threaded, when executing certain commands, multiple operation steps may be involved. For example, when operating on a certain key, you may need to obtain the value of the key first, and then perform calculations or Modify, and finally write the results back. This process is not an atomic operation, so in a multi-threaded environment, race conditions or data inconsistencies may occur.
  2. Race conditions : Although Redis uses a single-threaded model internally to avoid multi-thread synchronization problems, in some cases, race conditions may occur. For example, when multiple clients write to the same key at the same time, data inconsistency may occur if appropriate synchronization control is not implemented.
  3. Consistency in a distributed environment : In a distributed deployment of Redis, due to data sharding and communication between nodes, distributed consistency problems may be faced. For example, in a cluster environment, when a node goes down or a network partition occurs, data inconsistency or loss may occur.

To resolve these thread safety issues, the following measures can be taken :

  1. For operations that need to ensure atomicity, use the transaction function of Redis to package a set of operations into an atomic operation.
  2. In a multi-threaded environment, synchronize the locking of the shared resources involved to avoid race conditions and data inconsistencies.
  3. In the distributed deployment of Redis, data consistency and high availability are ensured through appropriate replication mechanisms, failover and data synchronization strategies.

12. Please tell me about your understanding of distributed locks and the implementation of distributed locks?

答:

Distributed locks are a mechanism used to control access to shared resources in distributed systems. Its role is to ensure mutually exclusive access to the same resource between multiple nodes or processes in a distributed environment, thereby ensuring data consistency and avoiding race conditions.

For the implementation of distributed locks , the following are common methods:

  1. Database-based : Use database transactions and unique constraints to implement distributed locks. By creating a special table or row record in the database to represent the status of the lock, when it is necessary to acquire the lock, try to insert the row record or update a specific field, and use the unique constraints of the database to ensure that only one thread or process successfully acquires it. Lock.
  2. Cache-based : Use distributed caches such as Redis or Memcached to implement distributed locks. By setting a specific key as a lock in the cache, and using the cache's atomic operations to acquire and release the lock. For example, you can use the SETNX command in Redis to try to acquire a lock. If the return value is 1, it means that the acquisition is successful; otherwise, it means that the lock has been acquired by another process and you need to wait or retry.
  3. Based on ZooKeeper : Use distributed coordination services such as ZooKeeper to implement distributed locks. ZooKeeper provides the function of ordered temporary nodes. You can create temporary nodes to obtain locks and use ZooKeeper's sequential characteristics to determine the order of nodes. By monitoring the deletion event of the previous node, you can determine whether you have acquired the lock.

It should be noted that when implementing distributed locks, the following points need to be considered:

  • Deadlock and livelock : When designing the lock acquisition and release mechanism, it is necessary to avoid the occurrence of deadlock and livelock, that is, a situation where all participants are unable to move forward or fall into an infinite loop.
  • Lock timeout mechanism : In order to prevent the lock from being released due to a node failure or abnormal situation, an automatic lock timeout mechanism can be introduced , that is, setting a reasonable lock timeout and forcing the lock to be released after this time is exceeded.
  • Fault tolerance and high availability : In a distributed system, issues such as node failures and network partitions need to be considered, and a fault-tolerant and highly available distributed lock solution needs to be implemented to ensure that the lock can work normally and have recovery capabilities.

13. Let’s talk about the understanding of Redis cache avalanche, cache penetration and cache breakdown, and how to avoid it?

答:

  1. Cache avalanche : refers to a situation where at a certain point in time, a large amount of data in the cache becomes invalid or expires at the same time, causing a large number of requests to hit the database directly, causing a sharp increase in database pressure, and even causing a database crash. The main reason may be that the cache expiration time is set too centrally or the cache server fails.

Ways to avoid cache avalanches:

  • Set a reasonable cache expiration time to avoid all cache expiration at the same time.
  • Use a multi-level cache structure, such as local cache and distributed cache, to reduce the risk of single points of failure.
  • Implement a cache preheating mechanism to proactively update or load data before the cache expires to avoid sudden failure in times of high concurrency.
  1. Cache penetration : refers to querying a data that does not exist in the cache, causing each request to directly access the database and consuming database resources. This may be a malicious attack or when querying very rare data. Attackers construct specific requests to bypass the cache and directly access the database, thereby increasing the load on the database.

Ways to avoid cache penetration:

  • For requests that return empty results, they are also cached and a short expiration time is set to avoid repeated queries to the database.
  • Use technologies such as Bloom Filter to pre-filter non-existent data to reduce query pressure on the database.
  1. Cache breakdown : refers to the sudden failure or expiration of a certain hotspot data. At this time, a large number of requests pour in at the same time. Since they cannot obtain the data from the cache, they will directly hit the database, causing a surge in database pressure.

Ways to avoid cache hits:

  • Set never-expiry or longer expiration time for hotspot data to avoid simultaneous expiration during high concurrency.
  • Using a mutex or distributed lock, when the cache expires, only one thread loads data into the cache, and other threads wait to obtain the data in the cache.

14. Tell us about the understanding of Redis’ master-slave sentinel and cluster?

答:

  1. Master-slave replication of Redis (master-slave sentinel) : Master-slave replication refers to the redundant backup and reading of data by copying the data of one Redis instance (called the master node) to other Redis instances (called the slave node). Write separation. The master node is responsible for processing write operations and synchronizing modified data to the slave node, while the slave node is only responsible for providing read operations and does not accept client write operations.

Advantages of master-slave replication:

  • Improve the reliability and disaster tolerance of the system. When the master node fails, it can quickly switch to the slave node to continue providing services.
  • Supporting read-write separation, the slave node can bear part of the read requests, reducing the pressure on the master node and improving the concurrency performance of the system.
  1. Redis cluster : Redis cluster refers to distributing data on multiple Redis nodes to form a logical cluster, achieving high availability and horizontal expansion of data through data sharding and data migration. In the Redis cluster, each node is independent of each other and cooperates with each other to complete data storage, read and write operations.

Features of Redis cluster:

  • Data distribution: Redis fragments data according to certain rules and stores data dispersedly on different nodes, improving storage capacity and performance.
  • High availability: Redis cluster adopts master-slave replication and failover mechanism. When a node fails, master-slave switching can be automatically performed to ensure continuous availability of data.
  • Horizontal expansion: By increasing the number of nodes, Redis cluster can achieve horizontal expansion and provide higher concurrent processing capabilities.

15. Let’s talk about the understanding of Redis cache warm-up, cache update, and cache downgrade?

答:

  1. Cache preheating : Cache preheating refers to loading some commonly used data into the cache in advance before system startup or peak period to improve system performance and response speed. By preheating the cache, you can avoid a large number of requests from hitting the database directly when the system first starts, reducing the load pressure on the database.

Implementation steps for cache warm-up:

  • Before system startup or peak period, the program actively loads frequently used data into the cache.
  • Set a reasonable expiration time so that the data in the cache becomes invalid within a limited time so that the latest data can be obtained in a timely manner.
  1. Cache update : Cache update means that when the data changes, the data in the cache needs to be updated in a timely manner to ensure the consistency between the cache and the database. When the data is updated, the corresponding data in the cache needs to be updated so that the latest data can be obtained the next time it is read.

Commonly used cache update strategies :

  • Update cache: When data changes, obtain the latest data from the database and update it to the cache to maintain data consistency. You can use hook functions or triggers to update the cache synchronously when data is updated.
  • Delete cache: When the data changes, the corresponding data is deleted directly from the cache, and the latest data will be reloaded from the database the next time it is read.
  1. Cache downgrade : Cache downgrade means that when the cache fails or the cache service is abnormal, in order to ensure the availability of the system, the cache is temporarily abandoned and data is obtained directly from the database or other data sources. Cache degradation prevents the entire system from being unavailable due to cache failure.

Common cache downgrade strategies:

  • Set an appropriate cache expiration time, and when the cache expires, obtain data from the database or other data sources in a timely manner.
  • Introduce a circuit breaker mechanism. When the cache fails, an alternative solution or default value is used to process requests to ensure the normal operation of the system.

盈若安好,便是晴天

Guess you like

Origin blog.csdn.net/qq_51601665/article/details/132847589