Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The Scheme of data consistency in redis

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Background:

Now redis is widely used in interconnection, and its use varies from business to business, but there are generally problems with the consistency of data storage and caching. Here are some more mature and practical solutions. I hope it will be helpful to you. If you have any good plans and suggestions, you are welcome to leave me a message.

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 first writes the database, and then other daemon synchronizes to redis advantages: redis startup does not have to deal with redis data and database inconsistencies disadvantages: redis startup puts great read pressure on the database

Mode 2:

Database and redis respectively deal with different data types. Database processing requires strongly consistent real-time data, such as financial data and transaction data. Redis processing does not require strong consistent real-time data, such as website hottest list.

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 purl 6379 > WATCH id

OK

127.0.0.1 purl 6379 > MULTI

OK

127.0.0.1 purl 6379 > INCR id

QUEUED

127.0.0.1 purl 6379 > EXEC

1) (integer) 342183

127.0.0.1purl 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"

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report