Search code examples
redissnapshot

Is snapshot of Redis atomic


I used the sorted set of Redis to store some data in RAM. The scores of elements in the sorted set keep updating.

My question is if we may get such a case:

  1. Element A is at the position 3 in the sorted set;
  2. A Redis snapshot is writing "Element A is at the position 3";
  3. Element A moves to the position 100;
  4. The same Redis snapshot is writing "Element A is at the position 100".

At last, in the snapshot, A appears two times in the same sorted set.

If the Redis snapshot is atomic, I should never see A twice in shapshot.


Solution

  • Yes, it's atomic.

    When Redis creates a snapshot, it forks a child process to do the dump work, and parent process does copy-on-write for the data set in memory.

    So the case you described should not happen, i.e. you never see A twice in snapshot. Instead, once it creates a child process to dump snapshot, new writes in parent process won't be written to the snapshot.