Running a MongoDB-Compatible Database with Amazon DocumentDB - Design Patterns and Scaling
Select the right instance class for MongoDB-compatible DocumentDB and ensure scalability with sharding through Elastic Clusters. This article also covers backup strategies.
DocumentDB Features and Differences from MongoDB
DocumentDB is a managed document database that provides a MongoDB-compatible API. You can use MongoDB drivers, ODMs (such as Mongoose), and management tools (mongosh, Compass) as-is. However, its internal architecture differs from MongoDB, using a storage engine similar to Aurora. Storage automatically replicates six copies across three AZs and auto-scales in 10 GB increments up to 128 TiB. The biggest benefit is eliminating the need to manage MongoDB replica sets and sharding. On the other hand, some MongoDB features (such as client-side field-level encryption and certain $graphLookup patterns) are not supported, so compatibility verification is necessary before migration.
Instance Design and Scaling
The db.r6g instance class (memory-optimized, Graviton2) is the standard choice for DocumentDB. The primary instance handles writes, and up to 15 read replicas distribute reads. Read replicas also serve as failover targets, automatically promoting when the primary fails. For large-scale workloads, use Elastic Clusters. Elastic Clusters automatically distribute data based on a shard key, handling petabyte-scale data and millions of operations per second. Adding and removing shards can be done online. Shard key selection directly impacts performance, so it is important to choose a field with high cardinality that aligns with your query patterns.
Backup and High Availability
DocumentDB automatically takes continuous backups to S3, enabling point-in-time restore to any second within a retention period of up to 35 days. Manual snapshots can be stored without retention limits and copied across regions for disaster recovery. In a multi-AZ deployment, the primary and read replicas are placed in different AZs, and failover during an AZ failure typically completes within 30 seconds. With global clusters, you can place read replicas in up to five regions and promote a replica in another region to primary during a regional failure. Replication lag is typically under one second. For practical MongoDB know-how, related books on Amazon are also a good reference.
DocumentDB Pricing
DocumentDB pricing consists of instance hours, storage, and I/O. A db.r6g.large costs approximately $0.277 per hour (about $199/month). Storage costs approximately $0.10 per GB/month, and I/O on Standard clusters costs approximately $0.20 per million requests. I/O-Optimized clusters have approximately 25% higher storage costs but no I/O charges, reducing total costs for I/O-intensive workloads. Elastic Clusters (sharding) cost approximately $0.1386 per vCPU-hour and scale with the number of shards.
Summary
DocumentDB is a service for running document databases as a managed service with a MongoDB-compatible API. Its Aurora-based storage engine delivers high durability and availability, and Elastic Clusters handle large-scale workloads. While pre-migration compatibility verification is important when migrating from MongoDB, it significantly reduces operational management overhead.