search

Home  >  Q&A  >  body text

用通俗点的语句告诉我Redis的操作的原子性是什么意思?

最近在官网的教程上看到Redis的很多操作都是原子性的。
实在不理解啥意思。

伊谢尔伦伊谢尔伦2762 days ago507

reply all(3)I'll reply

  • PHPz

    PHPz2017-04-24 09:12:21

    The atomicity of the operation ensures that the operation cannot be divided again, and it is either completely executed or not executed completely.

    You need to learn the principles of operating systems to be atomic. The time is a little long and it may not be accurate. Let me give a common example that has nothing to do with Redis.

    There is a variable A=10, I want to add 5 to it. Then my "add 5" operation is divided into several steps:

    • Read A
    • Read result +5
    • The result is written back to A

    There is another person who wants to add 3 to it. He doesn't know that I also want to operate the A variable. His "add 3" operation needs to be divided into several steps:

    • Read A
    • Result of reading +3
    • The result is written back to A

    If our operations ("add 3" and "add 5") are not atomic, the instructions between each other will be arranged randomly, and the results are unpredictable. Maybe he has completed the writeback of +3 first. When I read it, it was already It is 13, so we got the correct result of +3 and then +5. It is also possible that he did not write back during the +3 operation. I happened to read 10, so when writing back, it may be 13 or 15.

    If our operation is atomic, it can ensure that I cannot insert it when he performs the step of adding 3. I can only start my operation after he completes the operation, thus ensuring that the data is reliable.

    reply
    0
  • PHPz

    PHPz2017-04-24 09:12:21

    Similar to database transactions, a set of operations either succeeds or is not executed at all

    reply
    0
  • PHPz

    PHPz2017-04-24 09:12:21

    If you want to do it, just do it, if you don’t want to do it, don’t do it at all

    reply
    0
  • Cancelreply