DynamoDB vs MongoDB vs Cassandra - Performance Comparison
When working with databases like DynamoDB, MongoDB, and Cassandra, it's important to understand how their operations scale as data grows.
We want to know how the time to perform common tasks changes when the amount of data increases.
Analyze the time complexity of a simple read operation in DynamoDB.
const params = {
TableName: "Users",
Key: { "UserId": "123" }
};
const result = await dynamodb.get(params).promise();
This code fetches a single item by its primary key from a DynamoDB table.
Look for loops or repeated work inside the operation.
- Primary operation: Single key lookup in a distributed hash table.
- How many times: Exactly once per request, no loops over data.
As the number of items in the table grows, the time to get one item stays about the same.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 lookup |
| 100 | 1 lookup |
| 1000 | 1 lookup |
Pattern observation: The time does not increase with more data because the lookup uses a key directly.
Time Complexity: O(1)
This means the time to get an item by key stays constant no matter how big the table is.
[X] Wrong: "Getting an item by key takes longer as the table grows because there are more items to search."
[OK] Correct: DynamoDB uses a hash-based index, so it goes directly to the item without scanning the whole table.
Understanding how different databases handle data lookup helps you choose the right tool and explain your choices clearly in conversations.
What if we changed the query to scan the entire table instead of using a key? How would the time complexity change?