Securely install keys using StackExchange.Redis, allowing you to delete

I am trying to use Redis as the cache that is in front of the SQL database. At a high level, I want to implement the following operations:

  • Read the value from Redis if it does not exist, then generate the value using an SQL query and paste it into Redis, so we do not need to calculate it again.
  • Writing the value to Redis because we just made some changes to our SQL database, and we know that we already cached it, and now it is invalid.
  • Delete the value, because we know that the value in Redis is now deprecated, we suspect that no one wants it, but it works too much to double-check now. We are fine, letting the next client who performs operation number 1, will calculate it again.

My task is to understand how to implement # 1 and # 3 if I try to do this using StackExchange.Redis. If I naively implement # 1 with a simple key reading and push, it’s possible that between me the calculation of the value from SQL and its pushing is that any number of other SQL operations could happen, and also tried to push their values ​​to Redis through # 2 or # 3. For example, consider this ordering:

  • Client # 1 wants to perform operation # 1 [Read] from above. He tries to read the key, does not see it there.
  • Client # 1 calls the SQL database to generate the value.
  • # 2 - SQL, # 2 [Write] . Redis.
  • № 3 , SQL №3 [] Redis, , - , .
  • # 1 ( ) Redis.

, №1? Redis WATCH, , , # 1, StackExchange.Redis - , . , , ", ", , . / , ? , .

: , , , StackExchange.Redis , kludgy.

+4
1

, , Redis. - Redis WATCH/ MULTI - , db , . №3 :

, . , , №1,

, read update . , :

  • , 2 №1? Redis SQL-, Redis. , , ?
  • ( 3)?

concurrency , , . , . , , . , .

OCC wikipedia, :

- . , , , , .

Redis WATCH/MULTY - , - , , .

?

, , - - , . .

. ? :

  • - , . Redis TTL - , X-, X + Y, Y - .
  • - .
  • , , - "" - . , .

( " " ):

  • ( Redis SETEX - ).
  • - (, - ).
  • SQL- .
  • Redis .
  • - "".
  • .

( №2, №3):

  • "".
  • /ttl, .

  • . , - , .
  • .
  • .

!

, - . , :

  • .
  • SQL .
  • "" (/) .

?

. . - , - . .

+2

Source: https://habr.com/ru/post/1625247/


All Articles