pythonredispersistence

The redis command 'save' doesn't store immediately the snapshot on permanent memory


I'm trying to force the storing of a Redis snapshot on permanent memory by the command save. The instance of Redis is configured to use RDB persistence with the configuration:

appendonly no
save 30 1
dbfilename dump.rdb

so, normally, the data in RAM are saved to the file dump.rdb every 30 seconds if at least a key is changed.

I'm using python-redis and the snippet of code with problem is the following:

import redis
redis_client = redis.Redis(host="127.0.0.1", port=6379, encoding="utf-8", decode_responses=True)
redis_client.flushdb()
redis_client.save()
print("Save command executed!")

Previous code deletes all keys from the dataset in RAM (by flushdb()) and, after that, should request the save of the (empty) snapshot to the permanent memory.

If I execute previous code and wait the execution of the last instruction print("Save command executed!") instruction, I can see the message Save command executed! on standard output and the following log on the journald of the redis service:

> journalctl -u redis -xf

Jun 05 12:23:23 <hostname> redis-server[325]: 325:M 05 Jun 2025 12:23:23.055 * 1 changes in 30 seconds. Saving...
Jun 05 12:23:23 <hostname> redis-server[325]: 325:M 05 Jun 2025 12:23:23.056 * Background saving started by pid 2269
Jun 05 12:23:23 <hostname> redis-server[2269]: 2269:C 05 Jun 2025 12:23:23.071 * DB saved on disk
Jun 05 12:23:23 <hostname> redis-server[2269]: 2269:C 05 Jun 2025 12:23:23.075 * RDB: 0 MB of memory used by copy-on-write
Jun 05 12:23:23 <hostname> redis-server[325]: 325:M 05 Jun 2025 12:23:23.157 * Background saving terminated with success
Jun 05 12:23:25 <hostname> redis-server[325]: 325:M 05 Jun 2025 12:23:25.680 * DB saved on disk

The last line of the log (DB saved on disk) occurs for the execution of the redis_cli.save() instruction.

If I switch off the system (unfortunately I can't do a correct shutdown but I have to brutally switch off) immediately after (about in a second) the log line appears on the journald of the redis service, the snapshot is not saved on permanent memory; in fact on the next boot the dataset of the redis server is not empty.

In this context why the snapshot is not correct save to the permanent memory? It depends from the flushdb() method execution before the method save()?


Solution

  • Thanks to the comment of @Guy Royse I have verified that the problem was not on a particular configuration of Redis, but on the delay introduced by the file system.

    I have solved the problem adding the execution of the Linux command sync in the python code after the execution of the save command. So the code becomes:

    import redis
    import subprocess
    
    redis_client = redis.Redis(host="127.0.0.1", port=6379, encoding="utf-8", decode_responses=True)
    redis_client.flushdb()
    redis_client.save()
    command_output = subprocess.run("sync", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8")
    
    print(f"SAVE command executed; sync command executed: returncode =  {command_output.returncode}")
    

    By this adding the file dump.rdb is correctly saved to permanent memory even if I switch-off immediately after the execution of the Redis command flushdb and save.