When to use Redis over MongoDB?


First difference: Redis is a key-value store, while MongoDB is a document store. I think naming pretty much cuts it, but to give more details - Redis is better suited for storing simple values while MongoDB should store whole documents (structures with many fields).

Second difference: Redis lives in the memory. It is tremendously fast because of that, but it's also limited by the amount of RAM you have on your server. And please remember that RAM is probably most expensive part of any server setup. When data in Redis starts to exceed the available memory, strange things start to happen: either it stops accepting writes, or oldest keys get pruned silently to make space for newer ones. MongoDB on the other hand uses your disk which is much cheaper but also slower.

So, if you plan to store simple things (strings, numbers etc.) and you need very fast access, you should probably choose Redis. If it's some large amount of data you need to store, Redis won't be a good choice. Probably neither will MongoDB, but that's a whole different story...

Show all replies

Matthew Cory that's true. But remember that it's not necessary a good idea. From Redis docs:

Very long keys are not a good idea. For instance a key of 1024 bytes is a bad idea not only memory-wise, but also because the lookup of the key in the dataset may require several costly key-comparisons. Even when the task at hand is to match the existence of a large value, hashing it (for example with SHA1) is a better idea, especially from the perspective of memory and bandwidth.

Reply to this…

Take me to the question