Managing Databases
1. Selecting Database
| Command | Description |
SELECT index | Switch current connection to DB index (0..15 default) |
redis-cli -n 3 | Connect directly to DB 3 |
2. Getting Database Size
| Command | Returns |
DBSIZE | Number of keys in current DB |
INFO keyspace | Per-DB key/expire/avg-ttl stats |
3. Flushing Single Database
| Command | Effect |
FLUSHDB | Delete all keys in current DB (sync) |
FLUSHDB ASYNC | Delete in background, non-blocking |
FLUSHDB SYNC | Force synchronous flush |
4. Flushing All Databases
FLUSHALL # all DBs, sync
FLUSHALL ASYNC # all DBs, background
FLUSHALL SYNC # force blocking
Warning: Disable in production via rename-command FLUSHALL "".
5. Moving Keys Between Databases
| Command | Behavior |
MOVE key db | Atomic move; fails if key exists in target |
COPY src dst DB n REPLACE | Copy across DBs, optionally overwrite 6.2+ |
6. Swapping Databases
| Command | Effect |
SWAPDB a b | Atomically swap two DB indexes; instant cutover for blue/green data |
Example: Zero-downtime data refresh
SELECT 1
# load fresh data into DB 1 ...
SWAPDB 0 1 # production reads now hit fresh dataset
FLUSHDB ASYNC # clean up old data
7. Using Async Flushing
| Variant | Behavior |
FLUSHDB ASYNC | Reclaim memory in background thread |
UNLINK key ... | Non-blocking key delete (preferred over DEL for large values) |
lazyfree-lazy-user-flush | Config to make FLUSH commands async by default |
8. Configuring Database Count
| Setting | Notes |
databases 16 | Default; raise to use more logical DBs |
| Cluster mode | Only DB 0 is allowed |