MongoDB has long been one of the biggest names in NoSQL, and with Atlas, they have made it easier to get MongoDB in the cloud. Meanwhile, DynamoDB has fast become the favorite AWS-native NoSQL database. Both are fully managed serverless services, meaning that you don't need to worry about maintenance, updates, or hardware. So how do they compare, and when should you use one over the other?
Celebrating ten years in existence in 2022, DynamoDB has been the de-facto NoSQL solution for AWS developers, and with good reason. It's low cost, lightning-fast, works smoothly with your AWS Lambda Functions, and has native integration with Step Functions. For anyone getting started with serverless, it's where all the tutorials are going to take you in terms of data persistence, and it's pretty easy to get started with.
How does it all stack up against MongoDB Atlas, though? Three years its senior, MongoDB would have been considered the default NoSQL option for some time. For those outside of the AWS cloud or who need a multi-cloud solution, it still is. Released on November 30, 2020, MongoDB Atlas is a managed, cloud-hosted service for MongoDB. The big difference between it and DynamoDB is that it can be hosted on any cloud provider (AWS, Azure, and Google Cloud). That flexibility is key, if you're unsure on which provider to use, or simply want to avoid vendor lock-in, it's a compelling choice.
Rick Houlihan, one of the biggest names in DynamoDB, and the creator of Single-Table Design, is known for his regular gold standard talks at re:Invent, AWS's annual conference. In late 2021, Rick actually moved to MongoDB to take on the role of Director Developer Relations for Strategic Accounts.
Which is more popular as you read this? Well, if you look at db-engines.com it's clear that MongoDB has it and always has. In fact, if you look at the overall ranking, MongoDB is the fifth most popular database overall, with the four big relational databases (Oracle, MySQL, MSSQLServer, and PostgreSQL) the only databases that are more popular.
This article isn't so much about which one is better or who should win, though. Let's take a look through both DynamoDB and MongoDB Atlas, highlighting where they are similar, their differences, and the places where one should be used over the other.
While DynamoDB provides point-in-time and on-demand backup options, the analogous features in MongoDB Atlas are continuous backups and cloud-provider snapshots.
DynamoDB protects data at rest and data in transit. Data is encrypted at rest using encryption keys stored in AWS Key Management Service (KMS), either with an AWS-owned key (default), an AWS-managed key, or a customer-managed key. Access control for DynamoDB is managed using AWS Identity and Access Management (IAM).
MongoDB Atlas provides encryption at rest on all cloud platforms using their transparent disk encryption. Similar to DynamoDB, customers can provide their own keys and utilize the key management system available on their chosen cloud provider. MongoDB Atlas also provides role-based access rules to control access to the data.
DynamoDB has two capacity modes - on-demand and provisioned. In the case of less predictable workloads, on-demand capacity mode will manage capacity for you and only charge for what is used. While this is extremely useful, it obviously has the downside of making pricing less predictable.
When you know exactly what you need in terms of reads and writes per second in your application, it makes sense to use provisioned capacity. Auto-scaling ensures that the table can increase its provisioned capacity to handle sudden increases in traffic.
MongoDB also has auto-scaling functionality allowing you to automatically scale cluster tier and/or storage capacity, with the option to control costs by specifying limits for your cluster sizes. However, MongoDB in dedicated mode doesn't enforce rate limits, so it makes sense that it wouldn't have an on-demand option. MongoDB Atlas has introduced a serverless option (currently in preview mode) which is more in line with the DynamoDB on-demand, allowing you to pay only for the operations that you run.
Data Access Patterns
No matter which solution you use, understanding the difference between NoSQL databases and relational databases is key.
For DynamoDB, adopting Single Table Design, and understanding your access patterns well in advance will help you create a database that is built to last. In summary, Single Table Design means that you're not going to be using SQL joins, which tend to be expensive operations anyway, and instead you prepare your data into item collections. Your queries then work through the primary key, which can have 2 attributes at most.
Meanwhile, accessing data from MongoDB can be done by queries based on keys, ranges, joins, graph traversals, and geospatial queries.
While this might make it seem that MongoDB Atlas makes more sense to use, bear in mind that DynamoDB's limited access patterns are by design to ensure lightning-fast data access.
When it comes to datatypes, MongoDB has a much wider range of types. The most commonly discussed difference is the lack of a date type in DynamoDB - this can be overcome by using the timestamp data type, but it isn't as neat as in MongoDB. Even though both databases can store binary data, DynamoDB has a limit of 400KB for any item, while MongoDB can support documents up to 16MB in size.
MongoDB's document model lends itself to a wide variety of use cases, including key-value pairs, geospatial, time series, and graphs. DynamoDB is primarily a key-value store but with support for JSON to include document-like structures.
DynamoDB is only available on AWS, while MongoDB Atlas can run on AWS, Azure, or Google Cloud Platform. Additionally, MongoDB itself can run anywhere - on your laptop, on a server in your datacenter or a virtual instance in the cloud. To be 100% clear, you can still run DynamoDB on your local machine for testing before deploying to AWS, so you don't need to hop straight onto the cloud!
TCP Socket Connections
Applications use HTTPS endpoints to access DynamoDB tables, while MongoDB requires TCP socket connections. Depending on your cluster type, there is a set limit to the number of concurrent TCP connections allowed (see here). At the time of writing, the entry-level (M0/M2 cluster tier) is 500 per node.
AWS Native Integrations
When you use AWS Step Functions, you can really unleash the power of serverless technologies in the AWS ecosystem, and DynamoDB is a key part of this. You can easily store results from an AWS Lambda function.
You can use API Gateway to allow access to DynamoDB data through HTTP endpoints, or you can trigger an AWS Lambda function when any changes happen in a DynamoDB table. There are also simple-to-use integrations Cognito for identity pools, Redshift for data analysis, and S3 for exporting table data.
Obviously, MongoDB Atlas does not have the advantage of all of these native integrations, but you can easily write your own AWS Lambda function that accesses your MongoDB Atlas database and bring it into step functions. Working with DynamoDB is more straightforward, but it's not impossible to get close to this with a bit of coding effort.
Comparing pricing between MongoDB Atlas and DynamoDB is complicated as both have very different models.
For DynamoDB (pricing calculator), you need to take a number of factors into account, such as:
- Provisioned or on-demand
- Data storage required
- Number of reads and writes
- Backup costs
In the case of MongoDB Atlas (pricing page), the costs for the dedicated service are calculated based on:
- Storage / RAM and vCPUs (which together are termed as a Cluster Tier)
- Cloud Provider
The new serverless tier will require you to consider read and write operations, data storage, and backups.
Luckily, both databases have free tiers where you can experiment with the settings that make sense for your use case. In general, for large-scale applications, MongoDB Atlas will work out as the cheaper option.
When to Use Which
DynamoDB is a great database choice in cases where you have unpredictable workloads. If you can work out your application's data access patterns in advance, and these don't vary too much, using DynamoDB will give you faster query times. If you are already fully invested in AWS services, the native integrations, particularly with Step Functions, make it the first-class choice.
MongoDB is perfect if you need to store large amounts of binary data or your application requires geospatial queries. If you are running across multiple cloud platforms or dedicated to only Azure/Google Cloud Platform, MongoDB Atlas will be your default choice.
Is DynamoDB the same as MongoDB?
They are both NoSQL databases, but the similarities end there. Data access patterns are different and the range of data types varies a lot.
Is DynamoDB compatible with MongoDB?
Can DynamoDB replace MongoDB?
It depends on your use case. In many cases it will, but you will need to keep in mind that how you query data will be different. Some cases will prevent you from choosing DynamoDB, such as the need to store binary data > 400KB or using it as a geospatial database.
Is DynamoDB based on MongoDB?
Is DynamoDB better than MongoDB?
It depends who you ask! It's an apples to oranges comparison, as there are some cases that don't suit one or the other. For example, during Prime Day 2021, DynamoDB handled trillions of API calls, peaking at 89.2 million requests per second.
Is DynamoDB faster than MongoDB?
DynamoDB is faster than MongoDB, mainly due to how queries can be expressed.
Which is cheaper, DynamoDB or MongoDB?
In the long run, MongoDB probably ends up being cheaper for mature applications. Again, it depends on the use case. Don't forget that large organizations such as Zoom, Disney, Snap, and Dropbox lean on DynamoDB, so it is far from being a hobbyist solution.
© 2022 Dynobase