In 2019 AWS released a new tool for its cloud services that makes it easier to use a document database in just a few clicks, without having to worry about provisioning, patching, or managing the underlying infrastructure.
This database service was said by AWS teams to be 80% compatible with MongoDB and thus can be used as a replacement for MongoDB for serverless applications. With Amazon DocumentDB, you can run the same application code and use the same drivers and tools that you use with MongoDB.
DocumentDB and other document databases
A document database is a type of NoSQL database designed to store, retrieve, and manage data in a semi-structured or unstructured format. Unlike traditional relational databases, which organize data in rows and columns, document databases store data in collections of documents, typically using a format like JSON, BSON, or XML.
AWS DocumentDB is a scalable, fully managed document database service compatible with MongoDB. It allows you to work with unstructured or semi-structured data, ideal for modern web and mobile applications.
Amazon DocumentDB (with MongoDB compatibility) like other document databases is well-suited for applications dealing with unstructured or semi-structured data, such as content management systems, IoT applications, web and mobile apps, or analytics. Its compatibility with MongoDB makes it an excellent choice for developers familiar with MongoDB’s ecosystem.
Setting up DocumentDB on AWS
Unlike other AWS dtatbases like DynamoDB, Amazon Aurora, etc. setting up DocumentDB can be relatively difficult and its architecture can be more expensive especially if you have to connect to it from Lambda.
- Navigate to the Amazon DocumentDB service by searching for “DocumentDB” in the search bar or by finding it under the “Database” section
- Click on the “Create” button to start the cluster creation process.
- Choose the desired cluster type: “Cluster” for a multi-node cluster or “Cluster from snapshot” if you want to restore from an existing snapshot.
- Cluster Specifications: Select the appropriate cluster version, compute type (instance class), and instance count.
Cluster Identifier: Provide a unique name for your cluster.
Credentials: Set the master username and password for your cluster.
Network Settings: Choose the VPC, subnet group, and security group for your cluster.
Backup and Maintenance: Configure backup settings, maintenance window, and deletion protection if desired.
Encryption: Decide whether you want to enable encryption at rest and choose the appropriate encryption key. - Review the cluster configuration details and click “Create cluster” to initiate the cluster creation process.
- In the AWS Management Console, you can monitor the cluster’s performance, manage backups, scale the cluster up or down, and perform other administrative tasks.
DocumentDB should be set up in a private subnet for best security practices.
Connecting to DocumentDB
Once the cluster is ready, you can connect to it using various methods:
MongoDB Shell: Connect using the mongo
shell and the cluster endpoint.
mongo --host <cluster-endpoint> --port 27017 -u <master-username> -p <master-password> --authenticationDatabase admin
Replace <cluster-endpoint>
, <master-username>
, and <master-password>
with the appropriate values for your cluster.
Application: Connect from your application using the appropriate MongoDB driver and the cluster endpoint.
AWS Lambda Function: Connect from an AWS Lambda function within the same VPC.
How to Query DocumentDB
Amazon DocumentDB’s query capabilities work similarly to MongoDB, as it is designed to be compatible with the MongoDB query language and API. Here’s a brief explanation of how querying works in DocumentDB:
- Query Syntax: DocumentDB supports the standard MongoDB query syntax, including find, update, insert, and delete operations. Queries can be written using the MongoDB query language, which provides a rich set of operators and expressions for filtering, sorting, and manipulating data.
- Collections and Documents: Just like MongoDB, DocumentDB stores data in collections, which are analogous to tables in relational databases. Each collection can contain multiple documents, which are JSON-like objects with key-value pairs.
- Indexes: DocumentDB supports indexing, which is essential for efficient querying. You can create indexes on one or more fields in a collection, similar to MongoDB. Indexes help improve query performance by allowing DocumentDB to quickly locate the relevant documents based on the indexed fields.
DocumentDB uses very similar query language to MongoDB, example are some query for DocumentDB.
db.example.find( {} ).pretty()
The query above gets all the documents in the examplecollection.
db.example.find( { "Item": "Pen" } ).pretty()
This query retrieves all documents where the Item has value “Pen”.
db.example.find({"Inventory": {
"OnHand": 47,
"MinOnHand": 50
}
}).pretty()
This query retrieves documents that match an embedded document.
Advantage
There are many advantages to using AWS DocumentDB which will be listed below.
- Fully Managed Service: DocumentDB is a fully managed service, which means AWS handles the provisioning, patching, and maintenance of the underlying infrastructure, including hardware and software updates. This eliminates the need for manual database management tasks, allowing you to focus on application development and data modeling.
- High Availability and Durability: DocumentDB automatically provisions and maintains a highly available cluster with replicas across multiple Availability Zones (AZs) within an AWS Region. This ensures data durability and fault tolerance, reducing the risk of data loss or downtime due to infrastructure failures.
- Scalability: DocumentDB allows you to scale your cluster’s compute and storage resources up or down with a few clicks, making it easy to accommodate changing workload demands. This scalability is achieved through the use of storage and compute instances that can be added or removed independently.
- Automatic Backup and Point-in-Time Recovery: DocumentDB provides automatic backup and point-in-time recovery capabilities, enabling you to restore your data to any point within the specified backup retention period. This helps protect against accidental data loss or corruption.
- MongoDB Compatibility: DocumentDB is compatible with the MongoDB query language and APIs, allowing you to use existing MongoDB drivers, tools, and expertise. This can simplify the migration process from self-managed MongoDB databases to DocumentDB.
- AWS Integration: DocumentDB seamlessly integrates with other AWS services, such as AWS Lambda, AWS CloudFormation, and AWS CloudTrail, enabling serverless architectures, automated provisioning, and monitoring.
- Cost Optimization: With DocumentDB, you pay only for the resources you consume, without the overhead of managing and maintaining the underlying infrastructure. This can lead to cost savings compared to self-managing a MongoDB database, especially for workloads with varying or unpredictable demands.
- Performance and Reliability: DocumentDB is designed to provide consistent and reliable performance by leveraging AWS’s high-performance storage and networking infrastructure, as well as features like read replicas and storage auto-scaling.
Disadvantage
My bad for calling this a disadvantage but I like to see it as a factor you would want to consider before using DoucmentDB, these factors might have a negative effect on the long-term success of your project if not thought through properly.
- Limited Compatibility: Although DocumentDB is designed to be compatible with MongoDB, it doesn’t support all MongoDB features and functionalities.
- Vendor Lock-in: By using DocumentDB, you become dependent on AWS for your database needs. This can lead to vendor lock-in, making it more challenging to migrate your data and applications to another cloud provider or an on-premises environment in the future. You cannot use DocumentDB for multi-cloud because it is an AWS-owned service.
- Limited Customization: As a fully managed service, DocumentDB provides limited options for customization and fine-tuning. You cannot directly access or modify the underlying database servers or configurations, which may be a limitation for certain use cases that require advanced customizations.
- Cost Considerations: While DocumentDB can be cost-effective for many workloads, the pricing model may not be optimal for all use cases.
- Limited Control over Upgrades: AWS manages the upgrades and patching of DocumentDB clusters, which can be beneficial for security and stability but may also introduce breaking changes or compatibility issues with your applications. You have limited control over the timing and testing of these upgrades.
- Learning Curve: While DocumentDB is compatible with MongoDB, there may still be a learning curve for developers and administrators who are new to the AWS ecosystem and the DocumentDB service. Understanding the nuances, best practices, and integration with other AWS services can require additional training and resources.
- Limited Community Support: Compared to the open-source MongoDB community, the DocumentDB community and ecosystem may be more limited, which can impact the availability of third-party tools, libraries, and resources.
Take Note and My experience
I recently started using DocumentDB on AWS, and as a complete newbie, I went through a bit of a learning curve. While I successfully configured it on private subnets and set up VPC connections, I wasn’t prepared for the costs associated with Elastic IPs and NAT Gateways.
This unexpected expense hit me hard. It made me realize that using DocumentDB requires careful consideration of the services involved and their pricing. Additionally, setting it up with subnets, VPCs, security groups, and subnet groups can be complex compared to more serverless database options.
Conclusion
DocumentDB is a fully managed NOSQL database developed by AWS to be compatible to MongoDB. It is best suited for content management systems, IoT applications, web and mobile apps workloads.
Make a comment in the comment section below.