These are three popular databases used to store and manage data. Understanding their differences helps you pick the right one for your needs.
DynamoDB vs MongoDB vs Cassandra
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
DynamoDB
No single syntax applies as these are different database systems with their own query languages and APIs.
DynamoDB uses a key-value and document data model with AWS SDKs.
MongoDB uses a JSON-like query language for documents.
Examples
DynamoDB
DynamoDB: GetItem operation to fetch an item by primary key
aws dynamodb get-item --table-name Music --key '{"Artist": {"S": "No One You Know"}, "SongTitle": {"S": "Call Me Today"}}'DynamoDB
MongoDB: Find documents where age is greater than 25 db.users.find({ age: { $gt: 25 } })
DynamoDB
Cassandra: Select all rows from users table
SELECT * FROM users;Sample Program
This creates a users table in Cassandra, inserts two users, and selects all users.
DynamoDB
-- Sample CQL query for Cassandra CREATE TABLE users ( id UUID PRIMARY KEY, name text, age int ); INSERT INTO users (id, name, age) VALUES (uuid(), 'Alice', 30); INSERT INTO users (id, name, age) VALUES (uuid(), 'Bob', 25); SELECT * FROM users;
Important Notes
DynamoDB is fully managed by AWS and scales automatically.
MongoDB is document-oriented and good for flexible schemas.
Cassandra is designed for high availability and large scale across many servers.
Summary
DynamoDB is best for simple key-value access with automatic scaling.
MongoDB offers flexible document storage and rich queries.
Cassandra excels at handling huge data with high availability.
Practice
1. Which database is best known for automatic scaling and simple key-value access?
easy
Solution
Step 1: Understand DynamoDB's core feature
DynamoDB is designed for simple key-value access and automatic scaling.Step 2: Compare with other databases
MongoDB focuses on flexible document storage, Cassandra on high availability for huge data.Final Answer:
DynamoDB -> Option BQuick Check:
Automatic scaling + key-value = DynamoDB [OK]
Hint: Automatic scaling with key-value means DynamoDB [OK]
Common Mistakes:
- Confusing MongoDB's flexible documents with key-value simplicity
- Thinking Cassandra automatically scales like DynamoDB
- Choosing MySQL which is relational, not key-value
2. Which of the following is the correct way to describe MongoDB's data model?
easy
Solution
Step 1: Identify MongoDB's data model
MongoDB stores data as flexible JSON-like documents allowing rich queries.Step 2: Eliminate other options
Column-family store describes Cassandra, key-value with fixed schema fits DynamoDB less, relational tables fit SQL databases.Final Answer:
Flexible document storage with rich queries -> Option CQuick Check:
MongoDB = flexible documents + rich queries [OK]
Hint: MongoDB = flexible JSON documents + rich queries [OK]
Common Mistakes:
- Confusing MongoDB with Cassandra's column-family model
- Thinking MongoDB uses fixed schema like relational DB
- Mixing key-value with document storage
3. Given a large dataset requiring high availability and fast writes across multiple data centers, which database is most suitable?
medium
Solution
Step 1: Analyze requirements for high availability and multi-datacenter writes
Cassandra is designed for huge data with high availability and multi-region replication.Step 2: Compare other options
MongoDB supports replication but less optimized for huge scale multi-datacenter writes; DynamoDB is scalable but less focused on multi-datacenter writes; SQLite is local and not distributed.Final Answer:
Cassandra -> Option DQuick Check:
High availability + multi-datacenter = Cassandra [OK]
Hint: Huge data + multi-region writes = Cassandra [OK]
Common Mistakes:
- Choosing DynamoDB for multi-datacenter writes
- Confusing SQLite as distributed database
- Assuming MongoDB handles huge multi-region writes best
4. You try to use MongoDB's flexible document queries on DynamoDB but get errors. What is the likely cause?
medium
Solution
Step 1: Understand query capabilities of DynamoDB
DynamoDB supports key-value and simple queries but not rich flexible document queries like MongoDB.Step 2: Eliminate incorrect causes
DynamoDB does not use SQL syntax, is not column-family, and is not relational.Final Answer:
DynamoDB does not support flexible document queries like MongoDB -> Option AQuick Check:
DynamoDB lacks MongoDB's flexible queries [OK]
Hint: DynamoDB lacks MongoDB's rich document query support [OK]
Common Mistakes:
- Assuming DynamoDB uses SQL syntax
- Confusing data models between MongoDB and Cassandra
- Thinking DynamoDB supports relational tables
5. You need a database for an app that requires flexible JSON documents, automatic scaling, and global availability. Which approach best fits this need?
hard
Solution
Step 1: Identify features needed
The app needs flexible JSON documents, automatic scaling, and global availability.Step 2: Match features to databases
DynamoDB supports JSON documents, automatic scaling, and global tables for availability. MongoDB supports JSON but global availability requires extra setup and scaling is manual. Cassandra lacks native JSON support and SQLite is local only.Final Answer:
Use DynamoDB with JSON support and global tables -> Option AQuick Check:
JSON + auto scale + global = DynamoDB [OK]
Hint: DynamoDB global tables + JSON = best for scaling + availability [OK]
Common Mistakes:
- Choosing MongoDB without considering scaling complexity
- Ignoring Cassandra's lack of JSON support
- Selecting SQLite which is not distributed
