Data lost after redis server restart - redis

Im using redis 2.8.3 server to store key value pairs in redis.
redis.conf
port 6378
bind 127.0.0.1
databases 16
After restarting the redis-server
redis-server /home/redis.conf
Im loosing all the keys which i have already stored in redis.Can anyone help me to solve this.

If you run a 'BGSAVE' before you shut down the server does that help?
The shutdown script should always run that....

Use this configuration settings, which will help you to sync data using a background process:
# appendfsync always
appendfsync everysec
# appendfsync no

to prevent removing data from redis after restarting redis service in windows, you should update redis.windows-service.conf.
Redis SAVE command is used to create backup of current redis database
Save the DB on disk: save seconds changes
Will save the DB if both the given number of seconds and the given
number of write operations against the DB occurred
In the example below the behaviour will be to save:
after 1sec if at least 1 key changed
after 100sec if at least 50 keys changed
like in the following example, in SNAPSHOTTING section:
################################ SNAPSHOTTING ################################
save 1 1
save 100 50
after making changes, restart redis service,
you can download last version of redis for windows

Related

Redis data is not persistent

I am new to using Redis and I am playing around a little bit with it. I have noticed that after a little time, let's say 10 minutes all the keys that I inserted just go away.
I just did the default installation showed in the documentation. I didn't configure anything with a redis.config. Is there any configuration that I need to do so my data can persist?
Environment
Redis Server
Redis server v=6.2.6 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=557672d61c1e18ba
Redis-cli
redis-cli 6.2.6
Ubuntu 18.08 VM.
I have also been using redisInsight to insert the keys.
there are two mechanisms for persisting the data to disk:
snapshotting
append-only file (aof)
if you want to use snapshotting, you need to add the following settings in redis.conf file
dir ./path_for_saving_snapshot
dbfilename "name_of _snapshot.rdb"
save 60 1000
with this configuration, redis will dump the data to disk every 60 seconds if at least 1,000 keys changed in that period.
if you want to use aof, you need to add the following settings in redis.conf file
appendonly yes
appendfilename "your_aof_file.aof"
appendfsync everysecond
everysecond is the default FYSNC policy. you have also other options.
You can configure your Redis instance to use either of the two mechanisms or a combination of both.

How to delete all keys from redis cluster

I want to delete all keys from redis cluster by using fast process. I know how to delete the keys by using "redis-cli FLUSHALL". But this command can be slow when the data set is large. I heard that all keys can be cleared from redis cache by re-starting the redis service. I am testing this process on my local mac laptop. I am performing following steps:-
Setting many number of keys on my local redis server by using example command redis-cli SET mykey1 "Hello"
Then Re-starting the redis service "brew services restart redis" in the hope that all keys will be deleted when the service will be back up
Then getting the keys by giving "redis-cli KEYS '*'" command
I still see the keys after step-3
The keys are gone only when I give this command--> redis-cli FLUSHALL? How I can clear the keys by re-starting the redis service locally on my mac laptop first then I will try on QA servers?
You see the keys after restart because there is either RDB or AOF persistence enabled. See https://redis.io/topics/persistence.
RDB is enabled by default. To disable persistence, you need to edit your redis.conf or start as redis-server --save "" --appendonly no
See Is there a way to flushall on a cluster so all keys from master and slaves are deleted from the db on how to use redis-cli to send the command to all cluster nodes.
As dizzyf indicates, use FLUSHALL ASYNC to have the deletion performed in the background. This will create fresh hash maps for each database, while the old ones are deleted (memory reclaimed) progressively by a background thread.
In redis 4.0 and greater, the FLUSHALL ASYNC command was introduced, as a way to delete all the keys in a non-blocking manner. Would this solve your issue?
https://redis.io/commands/flushall
Thank you for links. These were very helpful. I was able to achieve the result by making changes to my redis.conf file with--> redis-server --save "" and --appendonly no. So after these changes, when I now re-start redis service nothing is saved.

How can you disable snapshotting on a running Redis for certain DBs?

How to disable Save for some DBs and allow for the others in the Redis
You cannot. An RDB snapshot is a single file that contains the data of all dbs.
You can send a FLUSHDB on the dbs you do not want to restore after the RDB is loaded.
If you'll use a dedicated Redis process for each db you could configure each one differently with a dedicated redis.conf file, and a SAVE and BGSAVE commands will only create a snapshot of the Redis process it was issued on.

How to do a redis FLUSHALL without initiating a sentinel failover?

We have a redis configuration with two redis servers. We also have 3 sentinels to monitor the two instances and initiate a fail over when needed.
We currently have a process where we periodically have to do a FLUSHALL on the redis server. This is a blocking operation that takes longer than the time we have allotted for the sentinels to timeout. In other words, we have our sentinel configuration with:
sentinel down-after-milliseconds OurMasterName 5000
and doing a redis-cli FLUSHALL on the server takes > 5000 milliseconds, so the sentinels initiate a fail over.
We acknowledge that doing a FLUSHALL isn't great and we also know that we could increase the down-after-milliseconds to but for the purposes of this question assume that neither of these are options.
The question is: how can we do a FLUSHALL (or equivalent operation) WITHOUT having our sentinels initiate a fail over due to the FLUSHALL blocking for greater than 5000 milliseconds? Has anyone encountered and solved this problem?
You could just create new instances: if you are using something like AWS or Azure than you have API for creating a new Redis cluster. Start it, load it with data and once ready just modify the DNS, again with API call -so all these can be handled by some part of your application. But on premises things can get more complex because it will require some automation with ansible/chef/puppet.
The next best option you currently have to is to delete keys in batches to reduce the amout of work to at once. You can build a list, assuming you don't have one, using scan Then delete in whatever batch size works for you.
Edit: as you are not interested in keeping data, disable persistence, delete the RDB file, then just restart the instance. This way you do t have to update sentinel like you would if you take the provision new hosts.
Out of curiosity, if you're just going to be flushing all the time and don't care about the data as you'll be wiping it, why bother with sentinel?

Is it recommended to run redis using Supervisor

Is it a good practice to run redis in production with Supervisor?
I've googled around, but haven't seen many examples of doing so. If not, what is the proper way of running redis in production?
I personally just use Monit on Redis in production. If Redis crash Monit will restart it but more importantly Monit will be able to monitor (and alert when a threeshold is reach) the amount of RAM that Redis currently takes (which is the biggest issue)
Configuration could be something like this (if maxmemory was set to 1Gb in Redis)
check process redis
with pidfile /var/run/redis.pid
start program = "/etc/init.d/redis-server start"
stop program = "/etc/init.d/redis-server stop"
if 10 restarts within 10 cycles
then timeout
if failed host 127.0.0.1 port 6379 then restart
if memory is greater than 1GB for 2 cycles then alert
Well..it depends. If I were do use redis under daemon control I would use runit. I do use monit but only for monitoring. I like to see the green light.
However, for redis to exploit the true power, you dont run redis as a deamon esp a master. If a master goes down, you will have to switch a slave to a master. Quit simply, I just shoot the node in the head and I have a chef recipe bring up a new node.
But then again....it also depends on how often you snapshot. I do not snapshot thus no need for deamon control.
People use reids for brute force speed. that means not writing to disk and keep all data in ram. If a node goes down...and you dont snapshot...data is lost.