What is done can not be undone from Redis

Why choose single-threaded Redis

Single-threaded thread context switching and reducing lock contention.

Network IO IO model uses multiplexing, using EPOLL registered literacy event notification, synchronization non-blocking.

How to play the single-threaded multi-core CPU advantage Redis

Redis multiple instances running on a single server. Use taskset command, each core cpu redis instance and bind

Redis Distributed Lock achieve

setnx key value if the key does not exist, it is created and assigned. Successful return 1 else return 0. At the same time set an expiration time: expire key seconds

You can also use the set command options with NX EX-time setup SET key value EX 10086 NX

Cache avalanche breakdown cache, the cache penetration

Cache avalanche : a large number of KEY expiration time is too concentrated, resulting in an instantaneous lot of cache invalidation, which may lead to a sharp increase in pressure in the database.
Solution : The time to failure were randomly disrupted, such as setting the expiration time discrete certain extent when the system starts to warm up.

Cache breakdown : a certain cache KEY expire, if not at this time have a large number of requests come KEY hit the cache, the cache layer is like drilling a hole in a large number of database queries as inflow
solution : double check the way from the database data is read into the cache. Double check: After the first layer of the query cache failed to enter the critical section to ensure that only one request thread read the database, try again to enter the critical buffer zone, still did not hit the database query.

Cache penetration : external requests continue to query data in a system does not exist, the service can not hit the cache instead each attempt from the database query.
Solution :

  1. The query result set is null null key cache buffer space for sacrificing response time.
  2. All illegal key is mapped to a bitmap by bitmap interception. "Bloom filter" principle

How to delete expired Redis key

  1. Initiative to remove: redis default at regular intervals to check expired key to delete, or insufficient memory mechanism is triggered automatically deleted

  2. Inert delete: and then check whether the key expire when there is a request to read and write key, then delete the expired

How Redis persistence

AOF

AOF (Append Only File): redis recorded for each write command, such as a key update 10 times, recording 10 written instructions. You can set after each write once per second or additional action occurs. Advantages: persistent high frequency, abnormal data loss less machine down. Disadvantages: file size, relatively time-consuming recovery.

Redis default RDB persistence, persistence AOF can be open simultaneously, as follows

appendonly yes
appendfilename "appendonly.aof"

AOF rewrite

AOF will be rewritten when the file size is too large. Rewriting action is a command to record key for recording a plurality of commands in place of the previous key pair. As a key increment 100 times the original file records 100 instructions, will rewrite the original instructions merged into a new, AOF thus greatly compressed file size.

Rewrite the use of the assignment (Copy On Write) principles when writing, do not directly manipulate the original file.

RDB

RDB: Snapshot persistence. Features Save time at the point of the whole amount of data. Advantages: recovery snapshot files directly loaded into memory and speed. Disadvantage: Since the total amount of data is large, the frequency is generally lower persistence setting. Last Change data will be lost to the shutdown time when persistent abnormal shutdown.

As RDB way configuration

save 900 1 #在900s内如果有1条数据被写入,则产生一次快照。 
save 300 10#在300s内如果有10条数据被写入,则产生一次快照   
save 60 10000 #在60s内如果有10000条数据被写入,则产生一次快照  
stop-writes-on-bgsave-error yes    # 如果为yes则表示,当备份进程出错的时候,   主进程就停止进行接受新的写入操作,这样是为了保护持久化的数据一致性的问题。

bgsave

Command can also be triggered manually by persistent snapshots basave. bgsave fork a child process, when write-write replication mechanism. Do not block the main process of work (compare the save command).

AOF and RDB can be used simultaneously with, the open AOF, redis start rebuilding from AOF default log file cache.

Redis clustering solutions

  1. Sentinel: Sentinel mode, mainly to achieve high availability. Redis solve the original master-slave synchronization mode, causing defects in the entire cluster does not work when the Master instance failure. Sentinel is a stand-alone process, testing Redis cluster status, such as Master because of the elections in the cluster initiate a fault in the machine offline elect a new Master node.
  2. Cluster: Limited to address a single host memory capacity. Redis hash cluster definition has 16,384 slots, each node is responsible for some. Each key by the CRC16 checksum of modulo 16384 to determine which slices are placed.

Redis and a database inconsistency

You need to trigger the cache to update the database update

If you delete the cache and then update the database, the database may be updated before the new request comes in because there is no cache hit read the old data from the database. If you first delete the cache database update, delete the cache that may arise after the failure of the database write is successful. It causes the cache is still the old data.

Circulated on the Internet double deletion strategy: to delete the cache -> database writer - "delete the cache again.

Benefit is redis working state can pre-check before data can be updated, but the key question: When you delete cached fails after writing may still exist. The fundamental solution should increase retry mechanism when deleting failed after writing, or rollback database updates.

Redis out of memory strategies

Redis memory using memory triggers elimination strategy beyond maxmemory configured size.

  • noeviction: do not delete the policy, direct memory reaches the upper limit returns an error message
  • allkeys-random: for all of the key, delete some random
  • allkeys-lru: for all of the key, delete the least used priority
  • volatile-random: a key for setting an expiration time, delete some random
  • volatile-ttl: a key for setting the expiration time, the expiration of the fastest delete key priority

Guess you like

Origin www.cnblogs.com/linwenbin/p/11725753.html