Skip to main content

What is a database index in Redis?

In Redis, a database index is the number of a logical database inside a single Redis instance. It has nothing to do with SQL indexes (searching by field), it's just a simple system for splitting keys into independent logical namespaces.

1. What this means

A Redis server can hold several databases, each a separate hash table (dictionary) storing its own key → value pairs. Every database gets an index, an integer starting at 0.

By default:

javascript
db0, db1, db2, ...

The number of databases is set via a config parameter:

bash
databases 16

(16 databases by default: db0 through db15).

2. Switching between databases

Redis doesn't support querying several databases in one request. The client connects to the server and picks a database with:

bash
SELECT <index>

Example:

bash
SELECT 0 SET key "value" SELECT 1 GET key

Result: GET key returns (nil), because the key was written into a different database (db0 and db1 are independent).

3. How Redis stores these databases

In memory, every database (db) is represented by its own structure:

javascript
dict *dict → the key/value store dict *expires → key lifetimes

Every database lives in an array:

javascript
server.db[0], server.db[1], ..., server.db[N-1]

These are fully isolated hash tables. Deletion, TTL, LRU, and other mechanisms all run separately per database.

4. Important characteristics

  • Databases have no names, only numeric indexes (0, 1, 2, …).
  • Databases have no schema, all of them store data the same way.
  • There's no access-rights isolation: these aren't "multi-user databases", just logical partitions.
  • During replication, persistence (RDB/AOF), and cleanup (FLUSHALL), every database's data is processed together.

5. Clearing and inspecting

Clear a specific database:

bash
FLUSHDB

Clear every database at once:

bash
FLUSHALL

See the active database:

bash
CLIENT INFO

(the db=N field shows which database the client is working with).

6. Uses

  • Splitting data between applications or environments (e.g. test / production).
  • Isolating temporary from permanent data.
  • Storing different kinds of cache in different databases.

However, in production, a single database (db0) is usually used, and data is split via key prefixes instead (user:1, session:xyz, order:42), because Redis Cluster supports only one database (db0).

7. Summary

A database index in Redis is:

  • simply the number of a logical database,
  • inside a single Redis instance,
  • indicating which hash table a key's data lives in.

That is:

javascript
db0 → its own dict of keys db1 → its own dict of keys db2 → its own dict of keys

This has nothing to do with SQL search indexes, they're logical partitions of Redis's memory, not accelerated-search structures.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.