In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
How does redis achieve consistency between data storage and caching? In view of this problem, this article gives the corresponding analysis and answers, hoping to help more friends who want to solve this problem to find a more simple and feasible way.
Method 1:
The data is saved in the database. After redis is not started by persist redis, the data loaded from the database does not require strong consistent real-time read requests. All read requests requiring strong consistent real-time performance are processed by redis. There are two processing methods for write requests handled by the database, and the database handles them.
The application writes the database first, and then updates redis
The application writes the database first, and then synchronizes other daemon to redis
Advantages: redis startup does not have to deal with redis data and database inconsistencies
Disadvantages: redis startup puts a lot of read pressure on the database
Mode 2:
The database and redis respectively deal with different data types. The database handles data that requires strong consistent real-time performance, such as financial data and transaction data. Redis processing does not require strong consistent real-time data, such as the most popular list of websites.
Redis and MySQL data synchronization, the code level can be roughly as follows: read: read redis- > No, read mysql- > write mysql data back to redis write mysql- > successful, write redis
The concurrency is not high: read: read redis- > No, read mysql- > write mysql data back to redis, and sometimes directly from redis; write: write mysql- > succeed, then write redis
High concurrency: read: read redis- > No, read mysql- > write mysql data back to redis, and some words directly from redis; write: asynchronous words, first write to redis cache, then return directly; save data to mysql on a regular basis or specific actions, you can update it many times and save it at once.
-- Note: if you want to write a transaction in redis that uses redis:
127.0.0.1 INCR idQUEUED127.0.0.1:6379 6379 > WATCH idOK127.0.0.1:6379 > MULTIOK127.0.0.1:6379 > INCR idQUEUED127.0.0.1:6379 > EXEC1) (integer) 342183127.0.0.1 INCR idQUEUED127.0.0.1:6379 6379 >
Mode 3:
Use lua script: redis allows only one script to execute when using lua, which is consistent with the atomicity of the transaction, but a lua script cannot be executed for too long, otherwise it will block
EVAL
The EVAL command evaluates the execution of the Lua script.
Syntax:
EVAL script numkeys key [key...] Arg [arg...] The content of the script lua script notes that the script should not be a Lua function. Numkeys represents the number of parameters for the specified key name. Key [key...] Indicates that the list of key values corresponding to the script can start at 1 in the script using KEYS [1] KEYS [2] KEYS [3] keys [n] n. Arg [arg...] The list of parameters passed in a named line can start at 1 in the script using ARGV [1] ARGV [2] ARGV [3] ARGV [n] n.
An example is worth a thousand words of explanation.
Eval "return {KEYS [1], KEYS [2], ARGV [1], ARGV [2]}" 2 id name 3 mytest
Execute the above script to return
1) "id" 2) "name" 3) "2" 4) "mytest" on redis to achieve data storage and cache consistency method to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.