Multiple Databases

Difficulty: Beginner

Question

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?

Answer

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.

How logical databases work

  • Each DB is its own key dictionary, so 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.

Why they are not real isolation

ConcernLogical DBsSeparate instances
Memory limitOne shared maxmemory; one DB can evict another's keysIndependent
CPUOne shared event loop; a slow command in DB 5 blocks DB 0Independent
ConfigSame eviction policy, persistence, timeoutsTuned per workload
SecurityACLs cannot restrict a user to a DBSeparate credentials
Redis ClusterOnly DB 0 is supportedWorks normally
MonitoringMixed metricsClear 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.

What to use instead

  • Key prefixes such as 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.
  • Separate instances or managed clusters when workloads need different eviction policies, persistence or scaling, for example a no-eviction instance for queues and an LRU instance for cache.

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.

Where multiple DBs are still fine

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.

Code examples

Switching and inspecting databases

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.

Key points

  • Default is 16 databases (0 to 15), controlled by the databases setting
  • SELECT is per connection; libraries usually set it from a db option or URL
  • All DBs share memory, CPU, maxmemory and eviction policy
  • Redis Cluster supports only DB 0
  • Prefer key prefixes or separate instances for real separation

Common follow-up questions

  • What does SWAPDB do and when is it useful?
  • Why can a cache in one DB evict data from another DB?
  • How would you split a cache and a job queue that currently share one Redis?
  • Why does Redis Cluster not support SELECT?

Interview tips

  • Answer the number and the command quickly, then spend your time on why prefixes or instances are better
  • Mention the Cluster limitation; it is the strongest practical argument
  • Use the shared eviction example to show you understand operational risk

Concepts covered

Logical databases, SELECT, databases config, Key prefixes, Redis Cluster DB 0

More Redis & Caching questions