Managing Database
A Redis instance supports 16 logical databases. These databases are effectively siloed off from one another, and when you run a command in one database it doesn’t affect any of the data stored in other databases in your Redis instance.
Redis databases are numbered from
0
to15
and, by default, you connect to database0
when you connect to your Redis instance. However, you can change the database you’re using with theselect
command after you connect:
If you’ve selected a database other than
0
, it will be reflected in theredis-cli
prompt:
Transfering the data from one database to another
To swap all the data held in one database with the data held in another, use the
swapdb
command.
swapdb
will swap the data held in database6
with that in database8
, and any clients connected to either database will be able to see changes immediately.swapdb
will returnOK
if the swap is successful.
Keys
Migrate
If you want to move a key to a different Redis instance, you can run
migrate
. This command ensures the key exists on the target instance before deleting it from the source instance.When you run
migrate
, the command must include the following elements in this order:The hostname or IP address of the destination database
The target database’s port number
The name of the key you want to migrate
The database number where you want to store the key on the destination instance
A timeout, in milliseconds, which defines the maximum amount of idle communication time between the two machines. Note that this isn’t a time limit for the operation, just that the operation should always make some level of progress within the defined length of time
Additionally, migrate
allows the following options which you can add after the timeout argument:
COPY
: Specifies that the key should not be deleted from the source instanceREPLACE
: Specifies that if the key already exists on the destination, themigrate
operation should delete and replace itKEYS
: Instead of providing a specific key to migrate, you can enter an empty string (""
) and then use the syntax from thekeys
command to migrate any key that matches a pattern.
Commands
rename
will rename the specified key. If it’s successful, it will returnOK
:
randomkey
will return a random key from the currently selected database:
type
to determine what type of data the given key holds. This command’s output can be eitherstring
,list
,hash
,set
,zset
, orstream
:
If the specified key doesn’t exist, type
will return none
instead.
move
to move an individual key to another database in your Redis instance with the.move
takes the name of a key and the database where you want to move the key as arguments.
move
will return OK
if moving the key was successful.
del
is used to delete one or more keys of any data type
If this command deletes the key(s) successfully it will return (integer) 1
. Otherwise, it will return (integer) 0
.
unlink
command performs a similar function asdel
, with the difference being thatdel
blocks the client as the server reclaims the memory taken up by the key. If the key being deleted is associated with a small object, the amount of time it takes fordel
to reclaim the memory is very small and the blocking time may not even be noticeable.However, it can become inconvenient if, for example, the key you’re deleting is associated with many objects, such as a hash with thousands or millions of fields. Deleting such a key can take a noticeably long time, and you’ll be blocked from performing any other operations until it’s fully removed from the server’s memory.
unlink
, however, first determines the cost of deallocating the memory taken up by the key. If it’s small thenunlink
functions the same way asdel
by the key immediately while also blocking the client. However, if there’s a high cost to deallocate memory for a key,unlink
will delete the key asynchronously by creating another thread and incrementally reclaim memory in the background without blocking the client.
Since it runs in the background, it’s generally recommended that you use unlink
to remove keys from your server to reduce errors on your clients.
flushdb
to delete all the keys in the selected database,
flushall
to delete all the keys in every database on a Redis server (including the currently selected database),
Both flushdb
and flushall
accept the async
option, which allows you to delete all the keys on a single database or every database in the cluster asynchronously. This allows them to function similarly to the unlink
command, and they will create a new thread to incrementally free up memory in the background.
To create a backup of the currently selected database, you can use the
save
command:
This will export a snapshot of the current dataset as an .rdb
file, which is a database dump file that holds the data in an internal, compressed serialization format.
save
runs synchronously and will block any other clients connected to the database. Hence, the save
command documentation recommends that this command should almost never be run in a production environment.
bgsave
command tells Redis to fork the database: the parent will continue to serve clients while the child process saves the database before exiting:
Note that if clients add or modify data while the bgsave
operation is occurring, these changes won’t be captured in the snapshot.
Last updated