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
0to15and, by default, you connect to database0when you connect to your Redis instance. However, you can change the database you’re using with theselectcommand after you connect:
127.0.0.1:6379> select 15If you’ve selected a database other than
0, it will be reflected in theredis-cliprompt:
127.0.0.1:6379[15]>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
swapdbcommand.
127.0.0.1:6379> swapdb 6 8swapdbwill swap the data held in database6with that in database8, and any clients connected to either database will be able to see changes immediately.swapdbwill returnOKif 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
127.0.0.1:6379> migrate 203.0.113.0 6379 key_1 7 8000Additionally, 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, themigrateoperation 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 thekeyscommand to migrate any key that matches a pattern.
Commands
renamewill rename the specified key. If it’s successful, it will returnOK:
127.0.0.1:6379> rename old_key new_keyrandomkeywill return a random key from the currently selected database:
127.0.0.1:6379> randomkeytypeto determine what type of data the given key holds. This command’s output can be eitherstring,list,hash,set,zset, orstream:
127.0.0.1:6379> type key_1If the specified key doesn’t exist, type will return none instead.
moveto move an individual key to another database in your Redis instance with the.movetakes the name of a key and the database where you want to move the key as arguments.
127.0.0.1:6379> move key_1 8move will return OK if moving the key was successful.
delis used to delete one or more keys of any data type
127.0.0.1:6379> del key_1 key_2If this command deletes the key(s) successfully it will return (integer) 1. Otherwise, it will return (integer) 0.
unlinkcommand performs a similar function asdel, with the difference being thatdelblocks 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 fordelto 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 thenunlinkfunctions the same way asdelby the key immediately while also blocking the client. However, if there’s a high cost to deallocate memory for a key,unlinkwill delete the key asynchronously by creating another thread and incrementally reclaim memory in the background without blocking the client.
127.0.0.1:6379> unlink key_1Since 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.
flushdbto delete all the keys in the selected database,
flushdbflushallto delete all the keys in every database on a Redis server (including the currently selected database),
flushallBoth 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
savecommand:
saveThis 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.
bgsavecommand tells Redis to fork the database: the parent will continue to serve clients while the child process saves the database before exiting:
bgsaveNote that if clients add or modify data while the bgsave operation is occurring, these changes won’t be captured in the snapshot.
Last updated
Was this helpful?