Redis FAQ

Gangster article

What is Cache avalanche

Usually when we carry a lot of requests to do caching layer to protect the data layer, but
later when you redis inside the key values of a large number of expired (Double Twelve For example, I immediately began to spike when I set the expiration time of 1 hour then one hour of these commodities the information is not a collective failure) at the same time a large number of requests at this time direct access to redis at this time when there is no cache of surface, these requests directly concurrent requests to the database when the database may be too large to collapse because of the pressure that's off cache avalanche

Solutions:

  • Limiting downgrade when a large number of requests directly access my past I only make a request to the database query, the queried data into redis
  • Cache warming can cache reload mechanism, advance to update the cache, then long before the big concurrent access to manually trigger different loads the cache key, set a different expiration time (say you are selling a good product my impending expiration time set I bought a little general design of a little shorter), so that the point in time of a cache miss as even as possible.

Cache penetration

What we want is a cache penetrate our normal process, when we send a request to the front desk when you come to reduce the database cache layer by redis pressure at this time if you did not request a value in redis go in it that you are not a database query , and this time there are some malicious people will go to find you redis inside by brute-force method does not cache a large number of data requests directly down to your database, but the database does not pressure you to data when the database is not very large (it crazy but then you find that they can not find a lot of requests to the database directly exhausted) which is penetrated cache

  • Solutions we can be resolved by the Bloom filter

When you do not have a database data when (as you check the order number if I do not have the order number I gave him a status of 1 or 0)
At this point you request access to my redis data I saw a state is 0 for my database does not I can not be directly rejected your request to tell you do not have that piece of data

  • Or if direct brute that I have not found a null return data directly into redis set the expiration time is a little short, say 60 seconds so you next time to check when I tell you this is null

Cache breakdown

What is the breakdown cache is a hot key when expired as being heavily accessed directly penetrated the protective cover

Solution:

  • I was not you hot data can be simple and crude, but he set on a timer or you set your task to build this I set the timer expires this task to update your health this time in 29 minutes in 30 minutes is more limited if the value of the key is still relatively fixed feasibility
  • Save the key cache expiration time (absolute time) went to the cache (can be spliced, you can add new fields, save a separate key can be used ... in whatever form, as long as the two established a good relationship on the line). In each execution after the get operation, we will get out of the cache expiration time and the current system time to make a comparison, if the cache expiration time - current system time <= 1 min (from a defined value), then update the cache this ensures that the active buffer. data is always current (and as a program, so that data never expires.), but if there is an extreme case, the cache is that you immediately expired, is not requested at this time to come you can not get to call and then update the cache, etc. your time is over, key expired, and immediately direct you to a wave of high concurrency finished
  • Use locks to achieve
 //创建一个锁
static Lock reenLock = new ReentrantLock();

    public List<String> getData04() throws InterruptedException {
        List<String> result = new ArrayList<String>();
        // 从缓存读取数据
        result = getDataFromCache();
         //判断若是没有数据
        if (result.isEmpty()) {
             //判断是否获取到锁
            if (reenLock.tryLock()) {
                try {
                    System.out.println("我拿到锁了,从DB获取数据库后写入缓存");
                    // 从数据库查询数据
                    result = getDataFromDB();
                    // 将查询到的数据写入缓存
                    setDataToCache(result);
                } finally {
                    reenLock.unlock();// 释放锁
                }

            } else {
                result = getDataFromCache();// 先查一下缓存
                if (result.isEmpty()) {
                    System.out.println("我没拿到锁,缓存也没数据,先小憩一下");
                    Thread.sleep(100);// 小憩一会儿
                    return getData04();// 再来一此
                }
            }
        }
        return result;
    }

redis persistence strategy

  • RDB (data snapshot mode), regular storage, preservation of the data itself, the file is stored in a compact

  • AOF (append mode), each time the data is modified, synchronize to the hard disk (write logs), save that change record data

  • If you only want the data stored in memory, then both kinds of strategies can be closed

  • You can also open simultaneously both types of strategies when Redis restart, AOF will be used to reconstruct the original data file

    RDB scheduled backup data set in memory. Server startup, you can recover the data set from the RDB file.

advantage

  • The file is stored in a compact
  • Suitable for backup and easy recovery of data in different versions
  • Suitable for disaster recovery, backup files can be restored in other servers
  • To maximize the performance of Redis, when a backup is initiated child thread, the parent process does not require an IO operation is to save a snapshot of the current thread is saved if the file is too Assembly blocked thread (redis user experience is single-threaded performance so bad bgsave created by using a fork to slowly engage sub-thread blocked thread microseconds)
  • Data retention faster than AOF

Shortcoming

1 Redis because if not properly shut down and stop working, the data between the last save point will be lost
2 due to the need for frequent fork child thread to the backup operation, if the amount of data is large, then, fork time-consuming, if cpu performance is not enough, the server may be Caton. When a large amount of data belonging to a Redis server not to deploy multiple services.

Create a snapshot of the following five forms:

  • The client sends BGSAVE command, the server will fork a child thread will be written to disk snapshot
  • The client sends a SAVE command, the server address operation is performed in the main thread. It does not often use, generally used only when enough memory to perform the BGSVAE
  • SAVE is set configuration items, such as SAVE 300 100, then when "there is written 100 times in 300 seconds", Redus automatically triggers BGSAVE command. If there are multiple configuration items, any one meet, will trigger a backup
  • When Redis by the SHUTDOWN (shutdown) the received command requests the server to shut down, or signal TERM, a SAVE instruction is executed, this time will block all clients, it does not execute any command sent by the client
  • When a Redis Redis server to connect to another server, and send each other like SYNC (sync) command to start a copy operation, if the primary server is not currently in execution BGSAVE operation, or just executing the master server, the master server will perform GBSAVE

AOF(append only file)

AOF record all writes servers. When the server restarts, and will re-execute all writes again, in order to achieve data backup. When the write operation is set too large (larger than the original data set), Redis will rewrite the write operation sets.

advantage

  • AOF mode using more flexible, because you can have different strategies fsync
  • AOF is added to a log file, all unwanted position, even if there is no damage to the power outage problem, even if the end of the file is written to a half of those orders, redus-check-aof tool can easily repair
  • When a lot of AOF documents, Redis will automatically be rewritten in the background. Rewrite is absolutely safe because Redis is to continue to append the old file which is used to create the current minimum set of operations required data sets to create a new file, once created, Redis will switch to a new file, start perform additional operations to a new file
  • AOF include one after another operation command, easy to understand and resolve

Shortcoming

For the same data set, AOF files are usually larger than the RDB file
AOF may be slower than the RDB, depending on fsync strategy. Usually fsync (synchronization policy) is set to once per second, then performance is still very high, if you turn off sfync, even at high loads and also RDB as fast. However, even under heavy write loads, RDB can still provide a good maximum delay assurance
AOF1 by incrementally update data snapshot is created from scratch RDB, RDB will be more robust and stable (it is suitable for backup)

Redis data types supported?

String string:
format: the SET Key value
String type is binary safe. Redis meaning of string can contain any data. Such as jpg image or serialized object.
Redis is the most basic type of string data type can store a maximum 512MB key.

Hash (hash)
format: name hmset VALUE1 key1 key2 value2
the Redis the hash is a key (key => value) pairs.
Redis hash field is a string type and the value of the mapping table, hash is particularly suited for storing objects.

List (list)
the Redis list is a simple list of strings, sort insertion order. You can add an element to the head of the list (on the left) or tails (on the right)
Format: lpush name value
corresponding to the list of head string element is added in key
formats: rpush name value
corresponding to the end of the list to add a string element in the key
format : lrem name index
key corresponding to the list and delete a count value like elements
format: llen name
returns the key corresponding to the length of the list

Set (collection)
Format: Sadd name value
Redis is a string Set the type of unordered collection.
Collection is achieved through a hash table, so add, delete, search complexity is O (1).

zset (sorted set: an ordered collection)
Format: Score Zadd name value
Redis zset and is set as a collection of string type elements, and does not allow duplicate members.
The difference is that a double score will be associated with each type of element. It is to redis from small to large order of collection of member passing score.
zset member is unique, but the score (score) it can be repeated.

Redis Cluster

From then redis 3.0 Supported redis-cluster clustering, Redis-Cluster-free central structure, each of the data storage node and the entire state of the cluster, every node and all other nodes are connected.
Features:
1, no center architecture (which affect the performance bottleneck node does not exist), less a proxy layer.

2, according to the data slot is stored in a plurality of distributed nodes, inter-node data sharing, data distribution can be dynamically adjusted.

3, scalability, linearly expandable to 1000 nodes, the nodes may be dynamically added or deleted.

4, high availability, the partial node is not available, the cluster is still available. Make backup copies of data by increasing the Slave

5, the failure to achieve automatic failover, between nodes exchange status information through gossip protocol, complete with Slave to the voting mechanism to enhance the role of Master.

Disadvantages:

1, resource isolation is poor, the situation is prone to influence each other.

2, asynchronous data replication, does not guarantee strong data consistency

Redis used to achieve distributed lock

Details Gangster description

Setnx scramble to acquire a lock, then grabbed, and then add a lock to expire expiration prevent lock forget released.

If you perform before expire after setnx process to restart unexpectedly crash or maintained, then what happens?

There are very complex instruction set of parameters, this should be able to setnx and expire at the same time a synthesis instruction to use!

Used Redis what to do asynchronous queue, how do you use? What are the disadvantages?

Structure is generally used as a queue list, rpush message production, lpop consumption message. Lpop when no message to a proper sleep will try again.

Disadvantages:

In the case of the consumer off the assembly line, production will be lost messages, message queues have to use professional as rabbitmq and so on.

Many times a consumer can not produce it?

Use pub / sub topic subscriber mode, can achieve the 1: N message queue.

Published 23 original articles · won praise 2 · Views 927

Guess you like

Origin blog.csdn.net/metjoyful/article/details/103723547