Tue Nov 01 2022

Redis vs MongoDB - which is better for your next project?

DatabaseFeatured7910 views
Redis vs MongoDB - which is better for your next project?

MongoDB

It's an open-source document database and leading NoSQL database. MongoDB is written in C++. It's a cross-platform document-oriented database program. It's developed by MongoDB Inc. and licensed under the Server Side Public License (SSPL). MongoDB uses JSON-like documents with the schema. It began developing in 2007 as a component of a planned platform as a service product.

Redis

According to the official homepage, Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports various data structures such as Strings, Hashes, Lists, Sets, etc. It has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence. It provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster. Redis is written in ANSI C and works in most POSIX systems like Linux, *BSD, OS X without external dependencies. The Redis project began when Salvatore Sanfilippo, nicknamed antirez, the original developer of Redis, was trying to improve the scalability of his Italian startup, developing a real-time web log analyzer. It was developed in 2009.

Redis vs MongoDB

  • MongoDB automatically takes care of balancing data and load across a cluster, redistributing documents automatically, and routing user requests to the correct machines.

  • The data model available within MongoDB allows you to represent hierarchical relationships, to store arrays, and other more complex structures more easily.

  • MongoDB environments are very scalable. Companies across the world have defined clusters with some of them running 100+ nodes with around millions of documents within the database.

  • In MongoDB, each database contains collections which in turn contains documents. Each document can be different with a varying number of fields. The size and content of each document can be different from each other.

  • In MongoDB, the document structure is more in line with how developers construct their classes and objects in their respective programming languages.

  • With MongoDB, Developers will often say that their classes are not rows and columns but have a clear structure with key-value pairs.

  • MongoDB supports search by field, range queries, and regular expression searches. Queries can be made to return specific fields within documents.

  • MongoDB can provide high availability with replica sets. A replica set consists of two or more MongoDB instances. Each replica set member may act in the role of the primary or secondary replica at any time.

  • MongoDB uses the concept of sharding to scale horizontally by splitting data across multiple MongoDB instances.

  • MongoDB can run over multiple servers, balancing the load and/or duplicating data to keep the system up and running in case of hardware failure.

  • MongoDB provides horizontal scalability as part of its core functionality. Automatic sharding distributes data across a cluster of machines, while replica sets can provide eventually-consistent reads for low-latency deployments

  • Redis is fairly simple to set up, use and learn.

  • Redis excels a well-known caching solution. It isn’t a plain cache solution – it also has advanced data structures that provide many powerful ways to save and query data that can’t be achieved with a vanilla key-value cache.

  • Redis provides persistence that you can opt to set up, so cache warming in the event of a crash is hassle-free.

  • Redis doesn’t have inbuilt encryption on the wire. It isn’t a seamless, mature clustering solution. It can be a pain to deploy in large-scale cloud deployments with Redis.

  • Redis performs considerably better for reads for all sorts of workloads, and better for writes as workloads increase.

  • Redis is single-threaded, it (mostly) gets more done with running on one core than MongoDB does while saturating all the cores.

  • Redis, for non-trivial data sets, uses a lot more RAM compared to MongoDB to store the same amount of data.

  • For MongoDB, CPU was saturated by 32 threads onward. Greater than 300% usage with single-digit idle percentages. For Redis, at 128 threads, runs failed often with read-timeout exceptions.

  • For Redis, CPU utilization never crossed 95%. So, Redis was consistently performing better than MongoDB while running on a single thread, while MongoDB was saturating all the cores of the machine.

  • MongoDB has a single-master replication with built-in auto-election. This means you can have a second database set up and it can be auto-elected if the primary database becomes unavailable.

  • Redis offers master-slave replication. Using Redis Enterprise, you can have a cluster manager and shared-nothing cluster architecture.

  • MongoDB is better for prototyping and is helpful when you need to get data into a database and you’re not certain of the final design from the beginning.

  • For applications that need real-time data, Redis can be great because it can look up information about specific keys quickly.