Jiew Meng
Jiew Meng

Reputation: 88217

Multiple redis calls or single call to Mongo or another DB engine

I understand redis is fast and I find that I can implement many things using just redis. But at the expense of making multiple queries for example. In I use Mongo, I might have a model/schema like:

Chatrooms (Mongo)

With redis I need something abit more complex

To retrieve chatroom details for mongo is straightforward, with mongo I need to incur 2 queries in this case. In a more complex use case, maybe more

So I wonder from a performance point of view, which is more efficient? From a development point of view, definitely, its simpler to use Mongo for example.

Upvotes: 0

Views: 1002

Answers (3)

Matías Fidemraizer
Matías Fidemraizer

Reputation: 64943

So I wonder from a performance point of view, which is more efficient? From a development point of view, definitely, its simpler to use Mongo for example.

Redis supports Lua scripting using the EVAL/EVALSHA commands. What you call multiple queries can be simplified to just one of them, since Lua scripts are executed in Redis itself, and then you can call a cached Lua script by SHA from your Redis client.

In terms of performance, it will depend on your own code, but you can be sure that Redis should beat Mongo since Redis is an in-memory storage and it's asynchronously-persisted to disk (actually it creates snapshots overtime or based on some conditions).

About the simplicity, there's not something more simpler than using data structures like sets, hashes or lists, but anyway, simplicity is a subjective concept...

About Lua scripts

Pay attention to the fact that Lua scripts are atomically-executed, and since Redis can execute a command at once (an atomic operation is like a single command), you should not implement heavy operations in Lua because it may decrease the overall Redis performance.

Usually you implement Lua scripts to atomically write data like "set a string key, add a member to some set and remove if from who knows what sorted set". Since you don't want to have a chance of corrupting your data, you use Lua scripting or MULTI command to implement atomic operations.

Upvotes: 5

thinker
thinker

Reputation: 211

It depends on what you want to index. If you just want to index by <id>, I don't see why you cannot get all the data in one query from redis. What you can do is to encode the name and users into, say, a JSON string, and retrieve it with just one query.

In general performance comparison is tricky. Less queries don't necessarily mean faster. I normally prefer redis or memcached type of in-memory caching if the data is not complex and does not require index over many fields.

Upvotes: 0

Itamar Haber
Itamar Haber

Reputation: 49982

That's one tricky question - you're basically asking to compare performance between two databases and contrast that with ease of use.

In terms of ease of use, I definitely believe MongoDB is more suitable for newcomers and, perhaps, developers who just want to use a database w/o understanding the pros/cons/strengths/weaknesses of each technology. MongoDB is an amazing piece of tech that is extremely accessible to a wide range of engineers at different levels - that's both its power and its downfall IMO: while you can do a lot with it, it is too generic for my taste to be really effective (sort of like a new Oracle).

Redis, OTOH, provides the basic building blocks - you need to understand how it works and how to put the blocks together to get at the needed result. This requires more effort for a developer - both for learning the Redis way and then implementing the solution with it.

So while MongoDB is easier to start using, I think Redis is more flexible in the long term and allows to have fine-grained control over every aspect of your database.

In terms of performance, I'm positive that Redis will outdo MongoDB any time. Yes, performance is affected by the "number of calls" but that's hardly a conclusive measure. The example provided (chatrooms) can be easily encapsulated in one or a few "calls" (e.g. use a Hash to contain all your objects and id) and that just one approach of doing it. Even assuming the OP's proposed "schema" and doing multiple calls to get the same data from Redis as one call from MongoDB provides, I'm sure that the data will come back faster from Redis than from any other database simply because Redis uses RAM whereas MongoDB is disk-based.

I suggest that in order to choose the right tech/solution, you take a minute to define and consider the requirements - if you need performance, Redis is your best bet.

Upvotes: 2

Related Questions