Difficulty: Beginner
How many logical databases does a Redis server have by default, how do you switch between them, and should you use them to separate data?
A standalone Redis server has 16 logical databases by default, numbered 0 to 15, set by the databases directive in redis.conf. Each connection starts on DB 0 and switches with SELECT <index>. They are separate keyspaces only; they share the same memory, CPU, configuration and persistence, so most teams prefer key prefixes or separate instances instead.
user:1 in DB 0 and user:1 in DB 3 are different keys.SELECT is per connection. If your pool hands out connections, every connection must select the right DB; most client libraries take a db option or a URL like redis://host:6379/3 and do this for you on connect.MOVE key db moves a key to another DB on the same server, and SWAPDB 0 1 (4.0+) swaps two databases atomically, which is useful for loading a fresh dataset into DB 1 and switching it live.INFO keyspace shows key counts per DB, for example db0:keys=1200,expires=900.| Concern | Logical DBs | Separate instances |
|---|---|---|
| Memory limit | One shared maxmemory; one DB can evict another's keys | Independent |
| CPU | One shared event loop; a slow command in DB 5 blocks DB 0 | Independent |
| Config | Same eviction policy, persistence, timeouts | Tuned per workload |
| Security | ACLs cannot restrict a user to a DB | Separate credentials |
| Redis Cluster | Only DB 0 is supported | Works normally |
| Monitoring | Mixed metrics | Clear per-workload metrics |
The eviction point matters most. If a cache in DB 1 and a job queue in DB 2 share an allkeys-lru instance, a cache traffic spike can evict queued jobs.
cache:product:991, session:ab12, ratelimit:ip:1.2.3.4. They work everywhere, including Cluster, and make SCAN MATCH and memory analysis per namespace easy.Redis Cluster supports only DB 0, and SELECT with any other index fails there. Valkey 9.0 added support for multiple databases in cluster mode, but code that relies on it is not portable to Redis Cluster.
Local development and test suites. Running each test worker on its own DB index and calling FLUSHDB between tests is a cheap way to isolate tests on one local server. Pub/Sub channels, by contrast, are global and ignore the selected DB.
CONFIG GET databases # how many DBs this server has
SET greeting "hello from db0"
SELECT 3
GET greeting # nil, different keyspace
SET greeting "hello from db3"
INFO keyspace
# Connect straight to DB 3
# redis-cli -n 3
# or in a URL: redis://localhost:6379/3
Each DB is a separate key dictionary on the same server, which is why the same key name holds different values.
Logical databases, SELECT, databases config, Key prefixes, Redis Cluster DB 0