This is the fifth day of my participation in Gwen Challenge

I. Concept and advantages

  • concept
  • High availability
    • Memory access is faster than disk access
  • High concurrency
    • QPS: indicates the number of queries that the server can perform per second
    • Database 1W (4-core 8G) <<—->> Redis 10W or 30W
  • Redis and map
    • Redis can use tens of gigabytes of memory for caching, Map can’t, and a few gigabytes of data is usually enough for a JVM
    • Redis cache can persist, Map is an in-memory object, the data is lost when the program restarts
    • Redis can implement distributed cache, Map can only exist in the program that created it
    • Redis can handle millions of concurrent requests per second and is a professional caching service. Map is just an ordinary object
    • Redis cache has expiration mechanism, Map does not have this function
    • Redis has a rich API, Map is much simpler

Cache classification and common cache

1.Redis(6379)

  • Redis is installed on Windows

Blog.csdn.net/baidu_33465…

  • Redis inLinuxInstallation under service
    • Docker install Redis and map configuration files:

www.cnblogs.com/934827624-q… www.jb51.net/article/203…

docker run -p 6378:6379 --name redis01 -v /root/redis/redis01/conf/redis.conf:/etc/redis/redis.conf -v /root/redis/redis01/data:/data -d redis redis-server /etc/redis/redis.conf --appendonly yes
Copy the code

2.Memcache(11211)

  • A high-performance distributed memory cache server
  • Objective: To improve the speed and scalability of dynamic Web applications by caching database query results and reducing database access times.

Third, Redis

1. Redis single-threaded mode

2. Data structure

2.1 String String

set key1 value1
get key1
Copy the code

2.2 list

rpush/lpush myList value1 value2 
lpop myList
Copy the code

2.3 the hash hash

hset userInfo name 'ss' age 30
hget userInfo name 
Copy the code

2.4 set the set

sadd mySet value1 value2
smembers mySet
Copy the code

2.5 Ordered set sortset

Zadd myzSet 3.0 value1 2.0 value2 1.0 value3 zrange myzSet 0-1 -- > value3 value2 value3 zrange myzSet 0 1 -- > value3 value2 zrevrange myzSet 0 1 -- > value1 value2Copy the code

3. The RDB and AOF

3.1. Data persistence

3.2. RDB

  • RDB :(snapshotting, RDB) dump. RDB snapshot (redis default mode)
Save 900 1 # After 900 seconds (15 minutes), Redis automatically triggers the BGSAVE command to create a snapshot if at least one key has changed. After 300 seconds (5 minutes), Redis automatically triggers the BGSAVE command to create a snapshot if at least 10 keys have changed. After 60 seconds (1 minute), Redis automatically triggers the BGSAVE command to create a snapshot if at least 10,000 keys have changed. Trigger time: Automatic trigger: (1) When the service is shut down. (2) Manually trigger when the preceding conditions are met: The save command uses the redis main process to write dump. RDBCopy the code

3.3. AOF

  • AOF: (append-only file, AOF) appendonly. AOF parameter file append (increasingly mainstream – advantages: better real-time)
    • TIPS:
      • The AOF rewrite is not based on the last AOF file, but on the data currently in Redis memory
      • AOF generates a new AOF file, which has the same data state as the original AOF file, but is smaller.
        • This function is implemented by reading database key-value pairs, and does not operate on existing AOF files:
          • (1)Redis server maintains an AOF rewrite buffer
          • (2) The child thread completes the creation of AOF file
Appendfsync always # write to the AOF file every time a data change occurs, which seriously slows down Redis' speed. Appendfsync no # let the operating system decide when to set the synchronization parameters: appendonly YesCopy the code

3.4 other

  • Redis 4.0Start to support RDB and AOFMixed persistence(The aof-use-rdb-preamble configuration item is disabled by default.)
    • Advantages: When AOF is rewritten, the content of RDB is directly written to the beginning of AOF file, which combines the advantages of RDB and AOF, fast loading
    • Disadvantages: THE RDB part of AOF is compressed format is no longer AOF format, poor readability

4. Common cache problems

1. Cache penetration

  • [A large number of request keys do not exist in the cache, resulting in a database check, lost the significance of Redis cache]
  • The solution
    • (1) Cache invalid key(Cache invalid keys to Redis and set expiration time)
      • Table name: column name: primary key name: primary key value
    • (2) Bloom filter
      • All possible data is placed in the Bloom filter, and the user requests it and makes a judgment in the Bloom filter;
      • Bloom filter says there is a small probability of misjudging the presence of an element;
      • The Bloom filter says that an element is not there, so that element must not be there.

2. Cache avalanche

  • A large area of cache failure at the same time, resulting in a large number of direct requests to the database
  • The solution
    • When Redis is not available:
      • (1) Adopt REDis cluster to avoid single machine
      • (2) Limit the flow to avoid processing a large number of requests at the same time
    • For hot spot failure:
      • (1) Set different cache expiration times, such as random
      • (2) Set the cache to be permanently invalidated

3. Consistency between database and cache data

  • The delete cache policy is generally used rather than update the cache
  • 3.1 Update the database before deleting the cache
Thread A queries the database, obtains an old value thread B writes the new value to the database thread B deletes the cache thread A writes the found old value to the cacheCopy the code
  • 3.2 Delete the cache and then update the database
Thread A deletes the cache thread B's query and finds that the cache no longer exists. Thread B goes to the database and gets the old value. Thread B writes the old value to the cache and thread A writes the new value to the databaseCopy the code
  • We can find that both strategies have their own advantages and disadvantages:
    • Delete the cache first, then update the database
      • Poor performance at high concurrency, excellent performance when atomicity is broken
    • Update the database, then delete the Cache.
      • Good at high concurrency, bad at atomicity destruction

Reference Address:

  • JavaGuide – Redis interview
  • [3Y] Learn Redis from Zero [Bronze]
  • Let’s Go into Dachang series – Redis Based
  • Redis command reference
  • Bloom filter principle