MongoDB vs DynamoDB: Which NoSQL database is right for you?
Tags: nosql, database, webdev
Published: 2022-04-21 17:53:48

Data storage flexibility can be one of the most important keys to your productivity as a software developer, and **NoSQL databases** are a great facilitator of that flexibility. They allow for high-speed data management and scalability, which makes them ideal for a variety of data structures and modern use cases, such as web applications and cloud-based services.

Today, we’ll go over the pros and cons of two services and discuss which you should use depending on your project goals and backend use cases. Given the number of NoSQL database services available, we’re narrowing our focus to two of the most popular: **MongoDB and Amazon’s DynamoDB**.

**We'll cover**:
* [What Is a NoSQL database?](#nosql)
  * [Types of NoSQL databases](#types)
* [What Is MongoDB?](#mongodb)
* [What Is DynamoDB?](#dynamodb)
* [Key differences](#differences)
* [Wrapping up and next steps](#next-steps)

<br>
<a name="nosql"></a>

## What is a NoSQL database?
NoSQL stands for “non-SQL” or “not only SQL.” **Structured Query Language** (SQL) is a programming language developed in the early 1970s to allow programmers to interface with relational database management systems (RDBMS). This interface allowed them to organize data into tables that were then linked based on shared data.

In the era of Big Data, however, increasing demand for faster and more disparate use of large data sets led to the development of NoSQL or non-relational databases. Without the need to connect tables, developers could use NoSQL databases to divide search-query and data-storage workloads across **multiple database servers**, instead of relying on a single server. NoSQL databases have not rendered relational databases obsolete but simply provided a viable alternative for relevant use cases.

NoSQL databases are also considered **distributed**, meaning that data is stored on multiple local or remote servers. This means that, if some piece of the database is taken offline, the rest of the database remains functional and accessible.

<br>
<a name="types"></a>

### Types of NoSQL databases
Broadly speaking, there are four major types of NoSQL databases: 
* **Key-value**: Considered the simplest type, this is a schema-less data model that houses data in a dictionary of key-value pairs, meaning each item has a key and a value. You often see this kind of database used in web applications that need to store and cache user-session data, such as for gaming or shopping apps.
* **Document-oriented**: This model stores data as documents, usually in JSON, XML, or BSON (binary JSON) formats, with each document containing pairs of fields and values. These values come in various types, such as arrays, Booleans, numbers, objects, and strings. This type ensures that data stays together when used in applications. It also gives you increased flexibility, as you don’t need to make sure data schemas are uniform across all documents.
* **Wide-column**: This database arranges data into columns, which allows you to access only the data you need without wasting memory. This method of organization is considered more complex than other types of NoSQL databases. These can be useful for storing data across multiple servers. 
* **Graph**: This model organizes data in nodes and edges. Nodes are any person, place, or object in the database, and edges define the relationship between nodes. For example, if you were to put your family tree in a graph database, an edge could categorize the relationship between you and your brother as “siblings.”

<br>
<a name="mongodb"></a>

## What is MongoDB?
MongoDB is an **open-source, document-oriented** NoSQL database from MongoDB Inc. that began development in 2007. It uses BSON-formatted documents and files to store and manage data. MongoDB supports field, range query, and regular-expression search functions through this system. 

Like other document-oriented databases, MongoDB doesn’t require matching data schemas across documents, which can provide flexibility that other databases lack. In addition, MongoDB Atlas is **cloud-agnostic**, meaning you could feasibly deploy it on any cloud provider (AWS, Google Cloud, or Microsoft Azure) or on premise without much issue. MongoDB is also compatible with a **wide range of programming languages**, including C, C#, Python, Ruby on Rails, Rust, and Scala, increasing provisioning options.

MongoDB is ideal if your project might need to scale quickly to meet increasing demands. Being schema-less, MongoDB supports adding document data to the database rapidly, which can speed up development. This is great if your project’s schema is prone to frequent changes, as it means you don’t have to reformat every document in the database after every change.   

MongoDB is not without its drawbacks. Its schema-free design is flexible but also means you might experience some **difficulty performing complex data transactions**. In some cases, this can lead to data loss or corruption. And its document size is limited to 16MB, meaning larger data sets have to be broken up to fit in the database properly. 

<br>
<a name="dynamodb"></a>

## What is DynamoDB?
DynamoDB is Amazon’s proprietary NoSQL database service. First released in 2012, DynamoDB supports two types of NoSQL data: key-value and document-oriented. It has similar functionality to MongoDB, though it is **only usable through Amazon Web Services** (AWS), making it less than ideal for users who aren’t working with AWS services.  

Given how common AWS is, there are plenty of reasons to use Amazon DynamoDB. Though less flexible in where you can deploy it, DynamoDB has **smoother integration with other tools and services found in AWS**. MongoDB allows users tighter control over their databases, but it also requires users to actively manage their configurations and infrastructure, which can take up precious development time. DynamoDB, however, uses AWS to manage updates, scaling, and availability, making the setup process quick and simple. You also won’t have to worry about keeping servers and backups running. Finally, AWS DynamoDB’s **out-of-the-box security features are solid**, thanks to being part of AWS and not being directly accessible via the internet. 

Unfortunately, DynamoDB has more issues than just its limited deployment capability. Its querying capabilities aren’t great, and it **only allows three data types for primary keys**: string, number, and binary. (It does support many different data types for other attributes within a table.)

Furthermore, DynamoDB uses a throughput model for pricing and data processing by default. With this model, you estimate your read/write capacity needs before the service can appropriate the resources. In cases where you don’t really know your expected read/write volumes, you might under- or overestimate your needs, which can lead to batch-processing failures. Of course, you can use the on-demand model to work around this, and DynamoDB will automatically accommodate your workloads as they ramp up or down. But that comes with the risk of **bill spikes** if your project’s traffic ends up higher than initially expected.

<br>
<a name="differences"></a>

## Key differences
Let’s take a look at some of the differences between MongoDB and Amazon DynamoDB.

**MongoDB**
* Can be hosted on premise or on any public or private cloud
* Difficult to set up, configure, and maintain
* Disables authentication process by default, which can lead to security issues if not configured properly
* Available on Linux, OS X, Solaris, and Windows operating systems
* Written in C++ and supports a wide range of programming languages
* Allows server-side scripting through JavaScript
* Maximum document size of 16 MB
* Has its own query language that provides advanced querying capabilities
* Offers a variety of data types

**DynamoDB**
* Can only be run on AWS cloud. No on-premise options
* Simple to set up and configure through AWS
* Accessible only through AWS’s Identity and Access Management (IAM) feature, which makes it very secure by default
* Available only through AWS hosting
* Written in Java and supports a much smaller number of programming languages
* Allows server-side processing through DynamoDB Streams and the AWS Lambda service 
* Maximum document size of 400 KB
* `Query` operation allows key-value and range queries, and scans provide another option for reading items in a table
* Offers three data types for primary keys and other data types for non-key attributes

<br>
<a name="next-steps"></a>

## Wrapping up and next steps
We’ve talked a lot today about the pros and cons of MongoDB and Amazon DynamoDB and differences between the two services, but the ultimate question is, “Which one is right for you?” That answer depends on your project’s specific needs and resources.

**If you’re already using AWS, DynamoDB's simpler setup process and better integration tools make it the ideal choice in most scenarios.** While MongoDB can be deployed on AWS the same as on any other cloud provider, it isn’t designed with AWS in mind like DynamoDB is. DynamoDB is also simply easier to use. While feature-rich, MongoDB has a bit of a learning curve, which can take up precious development time.

**If you want to build a multi-cloud infrastructure or simply use the database on-premise, MongoDB is the best choice between the two.** In truth, it’s the only choice in these cases since DynamoDB is only usable through AWS.

That said, this was only a sampling of these NoSQL databases. There’s so much more to learn and explore beyond MongoDB and DynamoDB through Educative courses and learning paths. If you want to discover more about DynamoDB and AWS, be sure to check out our [**Become an AWS Professional**](https://www.educative.io/path/become-an-aws-professional?eid=5082902844932096) learning path. And our [**MongoDB: The Complete Guide**](https://www.educative.io/courses/mongodb-complete-guide?eid=5082902844932096) course is ideal if you want to become an expert in all things Mongo. 

*Happy learning!*

### Continue learning about databases on Educative

* [What is a database query? SQL and NoSQL queries explained](https://www.educative.io/blog/what-is-database-query-sql-nosql?eid=5082902844932096)
* [MongoDB with Docker: Get started in 5 minutes](https://www.educative.io/blog/mongodb-with-docker?eid=5082902844932096)
* [Database design tutorial](https://www.educative.io/blog/database-design-tutorial?eid=5082902844932096)

### Start a discussion
Which NoSQL database do you use? Was this article helpful? Let us know in the comments below!