7

If I am doing this:

client.get("foo", (err, res) => {
  console.log(res);
});

And there billions of keys stored in the Redis server, would it return the data as quick as if there stored only just couple of keys?

OR that I should use index (if there indexes in Redis), just like querying the DB in MongoDB?

Raz Buchnik
  • 7,753
  • 14
  • 53
  • 96

2 Answers2

10

Redis GET command has a O(1) complexity, meaning that it will always get the data in the same amount of time (not counting network latency and bandwith) if there are 10, 10k or a 10 million entries. This is the case where you are using Redis a simple key-value store. In-memory retrieval + unique keys allows it to do that.

If you have more complex data structures, like hashes, then you could do secondary indexing. See more here

Urosh T.
  • 3,336
  • 5
  • 34
  • 42
3

Redis doesn't have indexes for keys. Each typical query for a key will have constant execution time, O(1), so it doesn't matter if you have one or billions of keys in the database.

However if you plan to search for anything other than the key, you might want to check the docs for indexing patterns

Sorted sets as indexes
Lexicographically encoded indexes
Geospatial indexes
IP range indexes
Full text search indexes
Partitioned indexes

But again, these are non-key indexes.

mihai
  • 37,072
  • 9
  • 60
  • 86
  • Hashes are key indexes? – Raz Buchnik Nov 27 '18 at 08:35
  • yes, this is how they're typically implemented, but keys are hashed automatically by redis so you don't have to do anything. You can however store your own hashes in the db if you want to. – mihai Nov 27 '18 at 08:38