DynamoDB vs MongoDB: Battle of the NoSQL Databases

by | Mar 15, 2017


Here at Optimal, we are currently working on some microservice management software. Part of my work on this project has been implementing some database classes for message brokers to interact with. So far I’ve implemented classes for DynamoDB and MongoDB: two NoSQL databases with their own strengths and weaknesses.
NoSQL is pretty much what it sounds like: databases that don’t use SQL. NoSQL databases are not relational, that is, there are no connections between tables in a database.
In this blog, we’ll look at factors of cost, security and ease-of-use.
Let’s get ready to rumble!

Setting Up

DynamoDB

Setting up Dynamo was a breeze. Because it’s an Amazon Web Service, it’s just a matter of going to the AWS console and tottering through a wizard to create your database.

MongoDB

Welcome to hell, kids. Setting up a Mongo database is a sea of terminal text and unhelpful authentication errors. I had a tonne of problems getting started with Mongo, to the extent that it took me over a day to get it going. Sometimes fixing a problem was as small as correcting a typo, but at other times is was a matter of uninstalling and starting from scratch.
MongoDB has some instructions for getting set up which are helpful if you’re fluent in the Ubuntu terminal:

However, there are a few things missing from these guides that required some intense googling. Tim and I have put together a guide for installing MongoDB, which may turn into a blog. Watch this space.
However, you can pay for MongoDB Atlas which is cloud hosted and, according to their website, “The easiest way to run MongoDB”. I haven’t tried this out, so perhaps it puts Mongo on a similar level to Dynamo here. You could easily argue that we’re comparing apples and oranges in this case.

Programming

Both of these database tools have libraries for use in C#. I worked with each of them.

DynamoDB

Have you ever wanted to throw your PC out of the window of a multi-storey building? Try using the Dynamo library and you will.
I found programming with the DynamoDB library to be unintuitive and frustrating. Nothing works the way you expect it to. If you are planning on programming synchronously, you’re in for a ride. All of Dynamo’s basic methods like creating tables and adding data to them are asynchronous.
Here’s the code for creating a table with MongoDB:
[code language=”csharp”]this.collection = db.GetCollection<BsonDocument>(tableName);[/code] vs creating a table with DynamoDB:
[code language=”csharp”] public void CreateTable(string tableName) {
try {
Task<CreateTableResponse> response = client.CreateTableAsync(new CreateTableRequest {
TableName = tableName,
AttributeDefinitions = new List<AttributeDefinition>() {
new AttributeDefinition {
AttributeName = "Id",
AttributeType = "S"
}
},
KeySchema = new List<KeySchemaElement>() {
new KeySchemaElement {
AttributeName = "Id",
KeyType = "HASH"
}
},
ProvisionedThroughput = new ProvisionedThroughput {
ReadCapacityUnits = readCapacity,
WriteCapacityUnits = writeCapacity
}
});
WaitTillTableCreated(response.Result, tableName);
} catch(AggregateException e) {
OLog.Info("Cannot create table as it already exists.");
}
}
private void WaitTillTableCreated(CreateTableResponse response, string tableName) {
TableDescription tableDescription = response.TableDescription;
string status = tableDescription.TableStatus;
// Wait until table is created.
while(status != "ACTIVE") {
System.Threading.Thread.Sleep(1000); // Wait 1 second.
try {
Task<DescribeTableResponse> res = client.DescribeTableAsync(new DescribeTableRequest {
TableName = tableName
});
status = res.Result.Table.TableStatus;
}
// Try-catch to handle potential eventual-consistency issue.
catch(ResourceNotFoundException) {}
}
}
[/code] Have you punched a hole in your monitor yet?
To make things worse, there’s a heap of outdated documentation floating around, which makes troubleshooting a minefield. You think you’ve found the method to save you, but try to use it and it comes up deprecated.

Running queries can be a pain too. An ordinary query can only take a primary key as a parameter. As in, you can only use a query to get data about an item if you know what the item is.

“But what if I want to search for all items with a certain property?” I hear you cry.

Well, you can use a scan, but it’s not very smart. It iterates over each row in the table and checks for matches. Pretty slow stuff.

Alternatively, you can use a global secondary index, but that’s a bit of a learning curve in itself. Amazon does provide some good tutorials, however.

MongoDB

The Mongo library was a comparative pleasure to work with. All basic methods exist in both synchronous and asynchronous form. All the methods that should exist, do exist. Querying is as simple is making a list of rules to filter by. It was so intuitive and usable that all but one of my unit tests passed on the first go.

Cost

Both Dynamo and Mongo are freemium, meaning that the services are free up until a certain limit (such as storage space or read/write capacity) or with limited features.
For the paid versions of these services, Dynamo has increasing cost for higher read/write capacity, while Mongo does not. However, Mongo is more expensive to upscale in terms of storage.
Get more info on pricing for DynamoDB and MongoDB.

Security

MongoDB

Security for MongoDB has been in the news a lot lately as it has been at the centre of some ugly database breaches. This is not the fault of MongoDB itself so much as it is MongoDB being used incorrectly.
MongoDB installs with authentication off by default. This is a very bad idea for most users of MongoDB as any data can be changed by anyone at any time. The easiest way to remedy this is to enable authentication via username and password. This can be quick and easy to set up and get going if you know what you’re doing, and only affects the initial connection to the database – none of your other code needs changing. Once this is set, MongoDB has no issues being a nice and secure home for all your data.
Here is a good checklist to make sure you are working securely with MongoDB:
https://docs.mongodb.com/manual/administration/security-checklist/

DynamoDB

DynamoDB’s security is mostly provided by the normal AWS security measures. Access to DynamoDB is granted with IAM through access/secret key pair or roles from the machine the code is running on. Both of these systems are secure and allow easy access to your data!
(Big thanks to Tim for writing this section for me as I had to return to university before I was able to finish it!)

Verdict

And thus the fight comes to an end. And the winner is… subjective?
My personal preference is for MongoDB. It gave me far fewer headaches in the long-run. The ease of using its C# library trumps the difficulty of setting up the free version for me.
However, it’s clear that DynamoDB has its benefits too. If security and plentiful storage are your organisation’s biggest concerns, and then this is the one to go for. Make sure your developers are well-versed in asynchronous programming, however.
It would be nice if the results were as clear as those of a wrestling match, but unfortunately, this is real life, and everything is tinted with shades of grey.
Any choice of software is a matter of what’s the most important to you. Get ready to rumble with your priorities.
Sarah – The tea-drinking programmer
Sarah blogs about her learning experiences as she navigates the road from student to professional.
Want to read more? Try … Getting Started with a Web API or more from Sarah.
We run regular business intelligence courses in both Wellington and Auckland.

11 Comments
  1. Kevin

    You stated “A query can only take a primary key as a parameter.” with regard to DynamoDB. Are you aware of global secondary indexes?

    Reply
  2. Sarah Dobie

    Hi Kevin, thanks for your comment. I wasn’t aware of global secondary indexes! Thanks very much for pointing that out. I’m still fairly new to the wonderful world of NoSQL. I will update the blog to mention them.

    Reply
  3. Guillaume Meister

    Hi Sarah, nice article indeed. Yet comparing DBaaS and locally hosted DB is indeed comparing oranges and apples, why not compare DynamoDB with the free version of Mongo Atlas ? it’s limited to 500Mb but I believe it’s alright for your test process.

    Reply
  4. Sarah Dobie

    Hi Guillaume, that’s a great suggestion! I wasn’t aware of a free version of Atlas – possibly it wasn’t available at the time of writing, or I just didn’t come across it. I may give that a go and update this blog in the future.
    Thanks for reading. 🙂

    Reply
  5. aaron r

    Could you make a blog about setting up a MongoDB on and AWS server? I’ve been struggling with that for a while. Thanks!

    Reply
  6. amit rao

    “Setting up a Mongo database is a sea of terminal text and unhelpful authentication errors”… I dont agree with this. MongoDB documentation is very clear on what needs to be done and is just a couple of steps setting this up.

    Reply
  7. Lohit Peesapati

    Good article, it’s brief. I was checking Dynamodb for the past week, and I have to agree that it’s a pain in many ways tough it also has advantages.
    “A query can only take a primary key as a parameter” – The primary key is also known as a Hash key. Also, you can use the sort key to filters the queries further which is much more useful. Now, the reason for this being, we don’t have a database and collections in Dynamodb, instead there are tables. So, we have to have a way of organizing the data which is dynamo’s way of saying that primary key is just a collection in which you store your data. It’s also much more performant.
    From their docs
    “The partition key of an item is also known as its hash attribute. The term “hash attribute” derives from DynamoDB’ usage of an internal hash function to evenly distribute data items across partitions, based on their partition key values.
    The sort key of an item is also known as its range attribute. The term “range attribute” derives from the way DynamoDB stores items with the same partition key physically close together, in sorted order by the sort key value.”
    And as stated in the above comment by Kevin, we have secondary indexes.
    You have mappers like dynogels and dynamoose which are similar to mongoose for Mongo. They ease the pain a bit. Still, the querying and managing the data is a nightmare right now. Hope they improve it soon 🙂

    Reply
  8. Tim Gray

    Hi Small Potato
    Thanks for commenting!
    You have linked the Java SDK and the code in this blog is written in .Net C#, but it is always good to know about changes for frameworks!
    Cheers
    Tim

    Reply
Leave a Reply to Franck Leveneur Cancel reply

Your email address will not be published. Required fields are marked *