Interviewer: I haven't been able to figure out distributed locks. Can I still be familiar with Redis?

Interviewer: I haven't been able to figure out distributed locks. Can I still be familiar with Redis?

 

Interviewer: What are the advantages of Redis?

Programmer Ali:

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 O(1).

Support rich data types, support string, list, set, zset and hash.

Support transactional. Operations are all atomic. The so-called atomicity means that all changes to the data are executed or not executed at all. (Note that this piece is different from the previous MySQL)

Rich features, can be used for cache, message queue, set expiration time by key, it will be deleted automatically after expiration.

Interviewer: What are the data structures of Redis?

Programmer Ali:

Redis supports five Value Types, and there are 8 types of encoded data structures implemented at the bottom:

  • SDS-simple synamic string-byte array that supports automatic dynamic expansion
  • list-plain linked list
  • dict-A dictionary implemented with a double hash table that supports smooth expansion
  • zskiplist-Skip list with back pointer attached
  • intset-own structure for storing a collection of integer values
  • ziplist-A data structure similar to TLV in implementation, but more complex than TLV, used to store an ordered sequence of arbitrary data
  • quicklist-a double-linked list structure with ziplist as the node
  • zipmap-A lightweight dictionary structure used in small-scale occasions to bridge the "underlying data structure" and "Value Type". It is another data structure implemented by Redis: redisObject

The Key and Value in Redis are both instances of redisObject on the surface, so the structure has a so-called "type", that is, ValueType. For each redisObject of Value Type type, its bottom layer supports at least two different bottom data structures to achieve. To cope with the operating efficiency or memory usage of Redis in different application scenarios.

Interviewer: What scenarios is Redis suitable for?

Programmer Ali:

Session sharing (single sign-on), page caching, queues (such as asynchronous queues used in projects), rankings/counters, publish/subscribe (implement message flow)

  • What should I pay attention to when using Redis?
  • Redis transactional
  • What are the current Redis cluster cluster methods, their advantages and disadvantages, and scenarios
  • The principle of Memcache, which data is suitable to be placed in the cache?
  • What are the advantages of Redis over memcached? The main difference between the two?
  • How to solve the problem of concurrent competition in Redis? Do you understand the CAS operation of Redis transactions?
  • Redis persistence mechanism, the difference between AOF and RDB
  • Memory recycling of Redis objects
  • Know which Redis optimized operations
  • Principle of Redis's master-slave replication mechanism
  • What is the threading model of Redis?
  • The difference between set and zset in Redis
  • Distributed usage scenarios (storage session)
  • How to ensure the consistency of the cache and the database?
  • Why does Redis use skiplist instead of balanced tree?
  • Redis distributed lock implementation

The first is to use the watch command of redis to implement.

The watch instruction provides CAS behavior in redis transactions. In order to detect whether the watched keys have conflicts caused by multiple clients changing at the same time, these keys will be monitored. If at least one monitored key is modified by other clients before executing the exec command, the entire transaction will be rolled back without performing any action, thus ensuring atomic operation, and executing exec will get a null reply.

Specific working mechanism: The watch command will monitor each given key. When exec, if any of the monitored keys has changed since the watch is called, the entire transaction will be rolled back and no action will be performed. Note that the watch key is valid for the entire connection, as well as the transaction. If the connection is disconnected, monitoring and transactions will be automatically cleared.

The second is to use the setnx command of redis to achieve.

Let's take a look at this related command first.

 SETNX key value 

If the key does not exist, set the key corresponding to the string value. In this case, the command is the same as SET. When the key already exists, nothing is done. SETNX is "SET if Not eXists".

expire KEY seconds 

Set the expiration time of the key. If the key has expired, it will be deleted automatically.

del KEY 

Delete key

Because when a key does not exist, SETNX will set the key. And because Redis uses a single-process single-thread model, there is no need to worry about concurrency issues. Then, you can use the features of SETNX to maintain a key. When it exists, the lock is held by a thread; when it does not exist, no thread holds the lock.

And you can set the expiration time of the key as the lock timeout, and you can delete the key directly after releasing the lock.

  • Problems and shortcomings encountered by Redis
  • Redis usage scenarios of various data types
  • Redis data elimination strategy
  • The concept of Redis hash slots
  • Redis's cache avalanche
  • Redis cache penetration
  • The advantages of Redis SDS over char[]

Interviewer: I haven't been able to figure out distributed locks. Can I still be familiar with Redis?

Interviewer: I haven't been able to figure out distributed locks. Can I still be familiar with Redis?

Interviewer: I haven't been able to figure out distributed locks. Can I still be familiar with Redis?

Interviewer: I haven't been able to figure out distributed locks. Can I still be familiar with Redis?

For the information in the text, scan the QR code on the left side of the homepage or click the hyperlink below the QR code to receive it for free!

Guess you like

Origin blog.csdn.net/EnjoyEDU/article/details/107766703