redis interview questions (summary)

  1. What is Redis? Please briefly describe its main features and uses.

    Redis (Remote Dictionary Server) is an open source, in-memory data storage system. Its main features include fast read and write operations, support for multiple data structures, persistence support, data expiration strategy and publish-subscribe model. Redis is commonly used in applications such as caching, session storage, real-time statistics, and message queues.

  2. What is the data storage method of Redis? What data structures does it support?

    Redis uses a key-value pair (Key-Value) data storage method, where the key is a unique identifier associated with the value. Redis supports the following main data structures:

    • String
    • Hash table (Hash)
    • List
    • Set
    • Sorted Set
    • Bitmap
    • HyperLogLog
  3. What is the difference between Redis and traditional databases?

    The differences between Redis and traditional databases include:

    • Storage method: Redis is based on memory storage, while traditional databases are usually based on disk.
    • Data model: Redis supports a variety of data structures, while traditional databases use tables and schemas.
    • Persistence: Redis provides persistence support, but data is usually stored in memory, while traditional database data is persisted on disk.
    • Query language: Redis does not have a SQL query language and only provides basic data operation commands.
    • Purpose: Redis is mainly used for caching and real-time data processing, while traditional databases are used for persistent data storage.
  4. How to set keys and values ​​in Redis? Please provide examples.

    Key-value pairs can be SETset using commands, for example:

    sqlCopy code
    SET mykey "Hello, Redis!"
    

    This will create a key named in Redis mykeyand "Hello, Redis!"associate the string value with it.

  5. What is Redis's data expiration policy? What strategies are available?

    Redis's data expiration policy is used to automatically delete expired keys. The main expiration strategies include:

    • Timed deletion (TTL): Each key can have a survival time set, and once the time expires, the key will be automatically deleted.
    • Lazy deletion: Redis deletes an expired key when trying to access it.
    • Periodic deletion: Redis periodically scans expired keys and deletes them. This process does not happen all the time, but is performed on demand.
  6. How is Redis persistence implemented? What are the two main ways of persistence it has?

    Redis persistence is achieved by writing data to disk. There are two main ways of persistence:

    • Snapshotting: Redis regularly writes snapshots of data in memory to disk to create point-to-point backups of data (RDB files).
    • Log Append (Append-Only File, AOF): Redis appends each write operation to a log file to record data modification operations. During recovery, Redis can re-perform these operations to restore the data.
  7. Please explain the String data type and related operations in Redis.

    Redis strings are simple key-value pairs that can store text or binary data. Common operations on strings include:

    • SET key value: Set the value of the key.
    • GET key: Get the value of the key.
    • INCR key: Increment the value of the key.
    • DECR key: Decrement the value of the key.
    • APPEND key value:Append the value to the value of the key.
    • MSET key1 value1 key2 value2 ...: Set multiple key-value pairs in batches.
  8. How to store and operate hash tables (Hash) in Redis?

    A hash table is a collection of key-value pairs, where each key corresponds to a value, similar to an associative array. Hash table operations in Redis include:

    • HSET key field value: Set the value of the specified field in the hash table.
    • HGET key field: Get the value of the specified field in the hash table.
    • HDEL key field1 field2 ...: Delete one or more fields in the hash table.
    • HGETALL key: Get all fields and values ​​in the hash table.
    • HMSET key field1 value1 field2 value2 ...: Set the values ​​of multiple fields in batches.
  9. How do lists in Redis work? Give an example to illustrate its use.

    A Redis list is an ordered collection of string elements that supports insertion and deletion operations at both ends of the list. Common list operations include:

    • LPUSH key value: Adds a value to the left side of the list.
    • RPUSH key value: Adds the value to the right side of the list.
    • LPOP key: Removes and returns a value from the left side of the list.
    • RPOP key: Removes from the right side of the list and returns a value.
    • LRANGE key start stop: Get the elements in the specified range in the list.
  10. What are Sets and Sorted Sets? What's the difference?

    A set is a unique set of unordered elements, while an ordered set is a unique set of elements, each of which is associated with a score. The difference is that sets are not sorted, whereas ordered sets are sorted based on scores. Common operations include:

    • Collection: SADD, SREM, SMEMBERSetc.
    • Ordered set: ZADD, ZREM, ZRANGE, ZSCOREetc.
  11. How to use Redis's Bitmap data type?

    A Redis bitmap is a special string in which each character represents a binary bit. Bitmaps are often used to record user online status, activity time, etc. Common operations include:

    • SETBIT key offset value: Sets the bit at the specified offset in the bitmap to the specified value (0 or 1).
    • GETBIT key offset: Get the value of the bit at the specified offset in the bitmap.
    • BITCOUNT key: Counts the number of bits with a value of 1 in the bitmap.
    • BITOP operation destkey key1 key2 ...: Perform bit operations on multiple bitmaps.
  12. What is HyperLogLog? What is its use in Redis?

    HyperLogLog is a data structure used to estimate cardinality (the number of unique elements). In Redis, HyperLogLog can be used to estimate the number of unique elements in a collection without storing each element. Common operations include:

    • PFADD key element1 element2 ...: Add elements to HyperLogLog.
    • PFCOUNT key: Estimate the number of unique elements in HyperLogLog.
  13. How to improve the performance of Redis? List several suggestions for performance optimization.

    Recommendations for improving Redis performance include:

    • Use high-performance hardware such as SSDs, fast CPUs, and lots of memory.
    • Configure appropriate memory usage and data expiration policies.
    • Use Redis cluster to spread the load.
    • Use connection pooling to reduce connection overhead.
    • Reasonable use of persistence mechanisms, such as AOF and RDB.
  14. What are the memory elimination strategies for Redis? Please explain LRU (least recently used) policy.

    Redis’ memory elimination strategies include:

    • LRU (Least Recently Used): Eliminates the keys that have been accessed least recently. Redis maintains an approximate LRU list, but it is not completely accurate.
  15. What is Redis cluster? How does it improve availability and performance?

    Redis cluster is a distributed system of multiple Redis instances used to improve availability and performance. It shards data into multiple nodes, with each node responsible for a portion of the data, thus providing horizontal scalability and fault tolerance.

  16. How to avoid key conflicts in Redis?

    To avoid key conflicts, you can take the following measures:

    • Use meaningful, unique key names.
    • Use namespace prefixes to differentiate between different datasets.
    • Use data structures such as hash tables (Hash) to store complex data instead of storing all data under top-level keys.
  17. What is the Redis publish and subscribe (Pub/Sub) model? Please explain its purpose.

    The Redis publish and subscribe model is a messaging model used to achieve decoupling between message publishers and subscribers. The publisher sends a message to the channel, and all subscribers subscribed to the channel receive the message. It is used to implement functions such as message queues and real-time notifications.

  18. How to implement transactions in Redis? How is its atomicity guaranteed?

    Redis transactions allow a set of commands to be executed sequentially in one execution without being interfered by operations from other clients. Atomicity is guaranteed through the MULTI, EXEC and DISCARD commands. The commands in the transaction will be executed in order. If the transaction fails to execute, it will be rolled back.

  19. How does Redis handle concurrent access and race conditions? Please discuss optimistic locking and WATCH command.

    The way Redis handles concurrent access and race conditions includes optimistic locking and WATCH commands. The WATCH command is used to monitor multiple keys. If any of the monitored keys is modified during transaction execution, the transaction will fail. Optimistic locking uses version numbers or timestamps to avoid race conditions and detect whether data has been modified by other clients.

  20. What is Lua script and how to use Lua script in Redis?

    Lua script is a script executed in Redis that can implement complex operations. Use the EVAL command in Redis to execute Lua scripts. The script can access Redis data and commands and is atomic.

  21. How to configure master-slave replication (Replication) in Redis?

    To configure Redis master-slave replication, you need to specify information such as roles, master node addresses, and ports in the configuration files of the master node and slave nodes. The master node will copy data to the slave node to achieve data backup and read load balancing

Guess you like

Origin blog.csdn.net/LSW1737554365/article/details/132860018