In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail what the interview questions about redis caching are, and the editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
What is the difference between redis cached interview question 1, redis and memcached? Why is it that single-threaded redis is sometimes more efficient than multithreaded memcached in high concurrency?
Difference:
Memcached caches pictures and videos. Redis supports more data structures except KBE.
Redis can use virtual memory, redis can be persisted and aof disaster recovery, and redis supports data backup through master and slave.
3.redis can be used as message queue.
Reason: the memcached multithreading model introduces cache consistency and locking, which leads to performance loss.
2. How is redis master-slave replication implemented? How to implement the cluster mode of redis? How is redis's key addressed?
Master-slave replication implementation: the master node makes a snapshot of the data in its own memory, sends the snapshot to the slave node, and the slave node restores the data to memory. After that, each time the new data is added, the master node sends the statement to the slave node in a binary log similar to mysql, and the statement sent from the slave node to the master node is replayed.
Slicing method:
Client slicing
Agent-based fragmentation
Twemproxy
Codis
Routing query fragmentation
Redis-cluster body provides the ability to automatically distribute data to different nodes in RedisCluster, where a subset of the entire data set is stored is transparent to the user)
Redis-cluster slicing principle: there is a 16384-length slot (virtual slot) in Cluster, numbered 0-16383 respectively. Each Master node is responsible for a part of the slot. When a key is mapped to a slot responsible for a Master, then the Master is responsible for providing services for the key. As for which Master node is responsible for which slot, it can be specified by the user or automatically generated during initialization. Only Master owns the slot. The Master node maintains a sequence of 16384and8 bytes of bits, and the Master node uses bit to identify whether it owns or not for a slot. For example, for slots numbered 1, Master only needs to determine whether the second bit of the sequence (the index starts at 0) is 1. This structure makes it easy to add or remove nodes. For example, if I want to add a new node D, I need to get some slots from nodes A, B, C to D.
[related recommendation: Redis video tutorial]
3. How to design distributed locks using redis? Can you tell me how to achieve it? Is it okay to use zk? How to achieve it? What's the difference between the two?
Redis:
Thread Asetnx (timestamp tl when the locked object times out), and if true is returned, the lock is acquired.
Thread B acquires T1 with get, compares it with the current timestamp to determine whether it times out, does not time out false, and executes step 3 if it times out.
Calculate the new timeout T2, use the getset command to return T3 (this value may have been modified by other threads), if t1==t3, acquire the lock, if T1 timeout T3 indicates that the lock was acquired by another thread.
After acquiring the lock, process the business logic, and then determine whether the lock has timed out. If the lock has not timed out, there is no need to handle it (to prevent the lock of other threads from being deleted).
Zk:
When the client locks a method, a unique instantaneous ordered node node1 is generated on the zk under the directory of the specified node corresponding to the method.
The client acquires all the created child nodes under this path, and if it finds that the sequence number of the node1 it has created is the lowest, it is considered that the client has acquired the lock.
If you find that the node1 is not the smallest, listen to the largest node with a smaller sequence number than the one you created and wait.
After acquiring the lock, you can delete the node1 you created after processing the logic. Difference: zk has poor performance, high overhead, and simple implementation.
4. Do you know the persistence of redis? How is it implemented at the bottom? What are the advantages and disadvantages?
RDB (RedisDataBase: synchronize snapshots generated by redis data to disk and other media at different points of time): memory to hard disk snapshots, updated regularly. Disadvantages: time-consuming, performance-consuming (fork+io operation), easy to lose data.
AOF (AppendOnlyFile: record all the instructions executed by redis, and the next time redis restarts, just execute the instructions): write a log. Disadvantages: large volume, slow recovery.
Bgsave persists the image in full, and aof does incremental persistence. Because bgsave takes a long time and is not real-time enough, it will lead to a lot of data loss during downtime, which requires the cooperation of aof. When the redis instance is restarted, aof is preferred to restore the state of memory. If there is no aof log, the rdb file will be used to recover. Redis will do regular aof rewrites to compress the log size of aof files. After Redis4.0, there is the function of mixed persistence, which combines the total amount of bgsave and the increment of aof, which not only ensures the efficiency of recovery, but also takes into account the security of data. According to the principle of bgsave, fork and cow,fork means that redis performs bgsave operations by creating child processes, and cow refers to copyonwrite. After the child process is created, the parent process shares data segments, and the parent process continues to provide read and write services. Writing dirty page data will be gradually separated from the child process.
5. What are the expiration policies for redis? Do you know the LRU algorithm? Write the java code implementation?
Expiration Policy:
Timed expiration (a key must be a timer), lazy expiration: determine whether the key has expired only when using key, and clear if it expires. Periodic expiration: a compromise between the first two.
LRU:newLinkedHashMap (capacity,DEFAULT_LOAD_FACTORY,true); the third parameter is set to true, which means that linkedlist is sorted by access order and can be used as LRU cache; if set to false, it is sorted by insertion order and can be used as FIFO cache
Implementation of LRU algorithm:
This is achieved through a two-way linked list, and new data is inserted into the header of the linked list.
Whenever the cache hits (that is, the cache data is accessed), the data is moved to the header of the linked list
When the list is full, discard the data at the end of the list.
The combination of LinkedHashMap:HashMap and a two-way linked list is called LinkedHashMap. HashMap is unordered, and LinkedHashMap ensures iteration order by maintaining an additional bi-directional linked list. The iteration order can be either the insertion order (the default) or the access order.
6. Cache penetration, cache breakdown, cache avalanche solutions?
* * Cache traversal: * * refers to querying a data that must not exist. If the data cannot be found from the storage layer, it will not be written to the cache. This will cause the non-existent data to be queried at DB every time, which may cause the DB to fail.
Solution:
The data returned by the query is empty. The empty result will still be cached, but the expiration time will be relatively short.
Bloom filter: hash all possible data into a large enough bitmap, and a certain non-existent data will be intercepted by this bitmap, thus avoiding the query of DB.
* * Cache breakdown: * * for a key with an expiration time, when the cache expires at a certain point in time, there are a large number of concurrent requests for the Key. These requests find that the cache expiration will generally load the data from the backend DB and reset it to the cache. At this time, large concurrent requests may crush the DB instantly.
Solution:
Use mutex: when the cache expires, do not immediately Ioaddb, first use a setnx such as Redis to set a mutex, and then perform the Ioaddb operation and reset the cache when the operation returns successfully, otherwise retry the get cache method.
Never expire: physically does not expire, but logically expires (background asynchronous thread to refresh). Cache avalanche: the cache is set with the same expiration time, causing the cache to expire at a certain time, and all requests are forwarded to DB,DB instantaneous overpressure avalanche. The difference between avalanche and cache breakdown: avalanche is a lot of key, breakdown is a certain key cache.
Solution:
Spread the cache expiration time, for example, add a random value to the original expiration time, such as 1-5 minutes random, so that the repetition rate of each cache expiration time will be reduced, and it will be difficult to cause collective failure events.
7. When selecting cache, when to choose redis and when to choose memcached
When you select redis:
Complex data structures, value data are hashes, lists, collections, ordered collections, etc. In this case, redis will be selected, because memcache can not meet these data structures, the most typical use scenarios are user order lists, user messages, post comments, etc.
Data persistence function is required, but be careful not to use redis as a database. If redis fails, memory can quickly recover hot data without putting pressure on the database instantly. There is no cache preheating process. Persistent storage can be used for scenarios with low requirements for read-only and data consistency.
High availability, redis supports clustering, can achieve active replication, read-write separation, while for memcache to achieve high availability, you need to carry out secondary development.
The content of storage is relatively large, and the maximum value of memcache storage is 1m.
Select the scene of memcache:
Pure KV, for businesses with a very large amount of data, it is more appropriate to use memcache because:
The memory allocation of memcache adopts the management mode of pre-allocated memory pool, which can save the time of memory allocation. Redis is a temporary application space, which may lead to fragmentation.
Virtual memory is used. Memcache stores all data in physical memory. Redis has its own vm mechanism, which theoretically can store more data than physical memory. When the data exceeds, swap is triggered and cold data is refreshed to disk. From this point, when the amount of data is large, memcache is faster.
In the network model, memcache uses the non-blocking 10-multiplexing model, and redis also uses the non-blocking I. Reuse model, but redis also provides some sorting outside of non-KV storage, aggregation functions, complex CPU calculations, which block the entire I0 scheduling, from this point, because redis provides more functions, memcache is faster
In the threading model, memcache uses multithreading, the main thread listens, and the worker child thread accepts requests and performs read and write. There may be lock conflicts in this process. Redis uses single thread, although there are no lock conflicts, but it is difficult to take advantage of the multi-core feature to improve throughput.
8. What if the cache is inconsistent with the database?
Assume that the main memory separation, read-write separation of the database
If a thread A first deletes the cached data and then writes the data to the master database, at this time, the synchronization between the master library and the slave database is not completed, and thread B fails to read the data from the cache, reads the old data from the library, and then updates it to the cache. At this time, the old data is in the cache.
The reason for the above inconsistency is that the master-slave database data is inconsistent. After adding the cache, the time of master inconsistency has been lengthened.
Processing idea: after the data from the database is updated, the data in the cache is also updated at the same time, that is, when the data update from the database occurs, delete it to the cache to eliminate the old data written during this period of time.
9. How to solve the inconsistency between master and slave databases?
Scene description: for the master-slave library, read-write separation, if the master-slave library update synchronization time difference, it will lead to the inconsistency of the master-slave library data
Ignore this data inconsistency, in a business that does not require high data consistency, it may not need to be consistent all the time
Force to read the main library, use a highly available main library, the database reads and writes in the main library, add a cache to improve the performance of data reading.
Selectively read the master database, add a cache, which is used to record the data that must be read from the master library, which library, which table, which primary key, as the cache key, set the cache expiration time as the master slave synchronization time, if there is this data in the cache, read the master library directly, if there is no such primary key in the cache, then read it from the corresponding slave library.
10. Common performance problems and solutions of Redis
Master is best not to do persistence work, such as RDB memory snapshots and AOF log files
If the data is important, a slave enables AOF backup, and the policy is set to synchronize once per second.
For the speed of master-slave replication and the stability of connection, master and Slave are better located in a local area network.
Try to avoid adding slave libraries to the stressed master library.
Master-slave replication should not use mesh structure, but linear structure as far as possible, Master
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.