DynamoDB vs Azure Cosmos DB - The Ultimate Comparison
Written by Mapitigama Samaranayake
Published on May 18th, 2022
Time to 10x your DynamoDB productivity with Dynobase [learn more]
DynamoDB and Azure Cosmos DB are widely used as cloud-based NoSQL database services in modern web and mobile development. However, choosing one isn't easy since each service has unique features to offer.
So, in this article, I will compare and contrast DynamoDB and Cosmos DB with their use cases to help you choose the right one for your project.
An Overview of DynamoDB and Cosmos DB
DynamoDB
DynamoDB is a fully managed, highly available, NoSQL database service provided by AWS. It ensures millisecond range latency at any scale and supports key-value and document data structures. Apart from the core features of a database, DynamoDB provides some amazing additional features like backups, high availability, and scalability to make developers' work easier. DynamoDB also offers built-in support for ACID transactions, which ensures that all operations within a transaction are completed successfully before committing the changes.
Cosmos DB
Cosmos DB is another highly available, horizontally scalable NoSQL database service. It is offered by Azure and is known as a multi-model database service. It enables customers to instantly develop and distribute their apps across Azure data centers without any prior configuration. In addition, it is available in all Azure regions and replicates its data across multiple data centers in the network because it is part of Azure. Cosmos DB supports multiple APIs, including SQL, MongoDB, Cassandra, Gremlin, and Table, making it versatile for various use cases.
Shared Attributes for DynamoDB and Cosmos DB
Both DynamoDB and Cosmos DB allow you to create highly available, reliable, and scalable serverless NoSQL databases. In addition, they both provide fully managed services and make it easier for the developer to configure the database and proceed with their applications with minimal intervention.
On the other hand, DynamoDB models data as key-value and documents, whereas Cosmos DB models data as key-value, documents, graph DBMS, and wide column stores. Also, Cosmos DB is designed to be a drop-in replacement for a typical relational database that supports SQL queries, whereas DynamoDB doesn't support ANSI SQL.
Let's compare and contrast some of the highlighted aspects of DynamoDB and Cosmos DB to get a better understanding.
Performance
DynamoDB
DynamoDB is a high-performance database service, and it can handle more than 10 trillion requests per day and 20 million requests per second at its peak.
DynamoDB utilizes the high IO rates of the SSDs to minimize the latency and reaction time when reading and writing data. In addition to that, you can use DynamoDB Accelerator (DAX) to improve the read and write performance by tenfold.
Regardless of the table size, DynamoDB ensures high performance and maintains the latency within the millisecond range. You can further improve these numbers with efficient indexes and partitioning. DynamoDB also supports adaptive capacity, which automatically adjusts the provisioned throughput for your tables based on the workload.
Cosmos DB
Azure Cosmos DB ensures 99.999% availability for reading and writing data with the facility to replicate across multiple regions. It also ensures low latencies for both read and write operations. Usually, the read latency is below 10 milliseconds, and the write latency is below 15 milliseconds. Cosmos DB is the world's first globally distributed database service, with comprehensive service level agreements (SLAs) for throughput, latency, availability, and consistency.
Similar to DynamoDB, Cosmos DB also has an SSD-backed storage system with low-latency order-of-millisecond response times. Furthermore, it supports consistency levels such as eventual, consistent prefix, session, bounded-staleness, and strong consistency, allowing developers to choose the right consistency model for their application needs. In addition, the reserved throughput model in Cosmos DB allows you to think about reads/writes rather than CPU/memory/IOPs of the underlying hardware.
Security
DynamoDB
One of the primary reasons people prefer AWS for their databases is its high security. DynamoDB supports user-based authentication through AWS IAM and encryption at rest using AWS KMS.
Encryption at Rest: All user data stored in tables, indexes, streams, and backups are encrypted at rest by DynamoDB using encryption keys stored in AWS Key Management Service (AWS KMS). This adds an extra layer of data security by preventing unauthorized access to the underlying storage.
IAM user roles authentication: You can create separate users, user roles, and policies using AWS IAM and assign them to users based on the requirements. With proper configurations, you can easily allow different users to access different tables.
Cosmos DB
Cosmos DB adheres to a hash-based message authentication for authorization purposes of the applications and uses SSL/TLS enforcement for client-service interactions.
Apart from that, Cosmos DB uses encryption at rest for all data stored. Nothing needs to be configured, and you have the same latency, throughput, availability, and functionality as before, with an additional assurance that your data is safe and secure. Cosmos DB also supports role-based access control (RBAC) and integrates with Azure Active Directory (AAD) for enhanced security management.
Scalability
DynamoDB
DynamoDB tables are highly scalable, and it provides 2 capacity models to choose from when creating a table.
- Provisioned capacity mode - Users need to define the minimum and the maximum number of capacity units in this mode. Tables will only operate between those limits.
- On-Demand - In this mode, DynamoDB will automatically increase and decrease read and write capacity units based on the traffic. This mode is well suited for applications with ad hoc traffic.
Cosmos DB
Cosmos DB can be scaled horizontally for reads and writes operations to serve hundreds of millions of transactions per second. The throughput (T) for Cosmos DB is scaled as 0.1*Tmax= Tmax. For example, set the maximum throughput to 20,000 RU/s, and the throughput will scale from 2000 to 20,000 RU/s. You can consume up to the supplied Tmax at any time because scaling is automatic and immediate. Cosmos DB also supports partitioning, which allows you to distribute your data across multiple servers to handle large-scale workloads efficiently.
Backups
DynamoDB
DynamoDB has 2 backup options: on-demand backups and PITR (Point-in-Time Recovery).
- On-demand backup - Allows you to create full backups of your tables for long-term storage and archiving for regulatory compliance purposes. With a simple click on the AWS management console or a single API call, you can backup and restore your table data at any time. Table performance and availability are unaffected by backup and restore operations.
- Point-in-time - PITR safeguards your DynamoDB tables against unintentional writes or deletes operations. When enabled, point-in-time backups will be created automatically, and it allows you to restore tables up to any point in the last 35 days with a precision of seconds.
Cosmos DB
Cosmos DB also has 2 backup modes,
- Continuous backup mode - The continuous backup mode can restore your data to any point within the last 30 days. It backups data in the background, consuming no additional provisioned throughput and having no effect on the performance or availability of your database.
- Periodic backup mode - The periodic backup mode is the default backup mode for Cosmos DB. Backups are taken at regular intervals, and data can be restored by submitting a request to the support team.
Pricing
DynamoDB
Similar to other Amazon services, DynamoDB core features are billed based on usage. However, an additional cost will be included if you have enabled services like backups. The pricing of your table is decided based on the capacity mode you choose.
If you choose the on-demand capacity mode, the total cost will be decided based on the application traffic. But, if you choose provisioned capacity mode, the cost will be calculated based on the number of read and write units you define.
In addition, 25 GB of data storage, 2.5 million streams read requests, and 100 GB of data transfer outs will be allocated under AWS free tier. You can find more details on AWS DynamoDB pricing in the documentation.
Cosmos DB
There are three main pricing options for Cosmos DB.
- Serverless: This is the most cost-effective solution since you only have to pay for the RUs you use. But pricing might be unpredictable, and storage capacity and performance are severely limited.
- Standard Provisioned Throughput: You select a maximum number of available RUs and will be charged hourly for that throughput even if it is not used. The billing is predictable, and you can change the number of provisioned RUs at any time.
- Autoscale Provisioned Throughput: This option is similar to the standard provisioned throughput option. But it allows you to specify a maximum RU value, and it will automatically scale down to 10% of that maximum when the database is idle.
For more information on pricing models in Cosmos DB, see the documentation.
When and Where to Pick Which Service
DynamoDB
DynamoDB is a perfect solution if you are looking for a cloud-based NoSQL database service. It is cost-effective and provides a variety of additional features.
Here are some of the most common use cases of DynamoDB:
- If your application already uses other AWS services.
- Applications that require uninterrupted performance at scaling.
- Real-time streaming applications.
- For scalable applications.
- Shopping carts and gaming platforms.
Cosmos DB
Cosmos DB is a better choice for any serverless application requiring milliseconds response times with high scalability. It natively and extensively supports multiple data models (key-value, documents, and graphs) and many APIs for data access, including Cosmos DB API for MongoDB, SQL API, Gremlin API, and Tables API.
Here are some of the most common use cases of Cosmos DB:
- E-commerce platforms.
- Simulating social interactions.
- Creating complex iOS and Android applications.
- Gaming applications.
- Internet of Things.
Conclusion
In this article, I have discussed some of the critical aspects of DynamoDB and Cosmos DB. I hope now you have a good understanding of their features and when you should choose them.
Thank you for reading!