Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Redis uses indexes to get the data?

Tags:

node.js

redis

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?

like image 606
Raz Buchnik Avatar asked Dec 23 '22 02:12

Raz Buchnik


2 Answers

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

like image 84
Urosh T. Avatar answered Dec 25 '22 15:12

Urosh T.


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.

like image 23
mihai Avatar answered Dec 25 '22 16:12

mihai