Search code examples
redisnode-redis

How do I persistently and asynchronously save Redis database in NodeJS?


How can I use BGSAVE redis function in NodeJS?

import { createClient } from 'redis';

const client = await createClient()
  .on('error', err => console.log('Redis Client Error', err))
  .connect();

await client.set('key', 'value');
const value = await client.get('key');
await client.disconnect();

is it simply await client.BGSAVE with a callback?

Is the data persistent upon reboot? In which file is it saved?


Solution

  • It's that simple but you don't need a callback. Strictly speaking, you don't even need to await the Promise as Redis creates a new thread to do the background saving.

    By default, the file will be called dump.rdb but you can change the filename in redis.conf.