Managing Databases

1. Selecting Database

CommandDescription
SELECT indexSwitch current connection to DB index (0..15 default)
redis-cli -n 3Connect directly to DB 3

2. Getting Database Size

CommandReturns
DBSIZENumber of keys in current DB
INFO keyspacePer-DB key/expire/avg-ttl stats

3. Flushing Single Database

CommandEffect
FLUSHDBDelete all keys in current DB (sync)
FLUSHDB ASYNCDelete in background, non-blocking
FLUSHDB SYNCForce 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

CommandBehavior
MOVE key dbAtomic move; fails if key exists in target
COPY src dst DB n REPLACECopy across DBs, optionally overwrite 6.2+

6. Swapping Databases

CommandEffect
SWAPDB a bAtomically 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

VariantBehavior
FLUSHDB ASYNCReclaim memory in background thread
UNLINK key ...Non-blocking key delete (preferred over DEL for large values)
lazyfree-lazy-user-flushConfig to make FLUSH commands async by default

8. Configuring Database Count

SettingNotes
databases 16Default; raise to use more logical DBs
Cluster modeOnly DB 0 is allowed