
Cloud
Learning Level
// All entities in one table with polymorphic keys
// Partition Key: entityId
// Sort Key: entityType#timestamp
// User entity
{
PK: 'USER#123',
SK: 'PROFILE#2024-01-01',
email: 'user@example.com',
name: 'John'
}
// Order entity for same user
{
PK: 'USER#123',
SK: 'ORDER#2024-01-15',
orderId: 'ORD-456',
total: 99.99
}
// Query all data for user
const params = {
KeyConditionExpression: 'PK = :pk',
ExpressionAttributeValues: { ':pk': 'USER#123' }
};
// Query specific type
const params = {
KeyConditionExpression: 'PK = :pk AND begins_with(SK, :prefix)',
ExpressionAttributeValues: {
':pk': 'USER#123',
':prefix': 'ORDER#'
}
};// Subcollections for related data
/users/{userId}
/posts/{postId}
/comments/{commentId}
// Access pattern
db.collection('users').doc('user123')
.collection('posts').doc('post456')
.collection('comments').get()
// Denormalization for performance
{
userId: 'user123',
userName: 'John', // Denormalized
post: {
content: 'Hello',
likes: 42,
// Comments stored in subcollection, not here
}
}Primary Region (US)
↓ (Async replication)
Secondary Region (EU)
Secondary Region (APAC)
Eventual consistency between regions
<1 second replicationAutomatic replication across regions
No configuration needed
Read from nearest regionconst CosmosClient = require("@azure/cosmos").CosmosClient;
const client = new CosmosClient({
endpoint: "https://account.documents.azure.com:443/",
key: "key",
connectionPolicy: {
preferredLocations: ["East US", "West Europe"]
}
});
// Reads from preferred location
// Writes to primary region
// Multi-master optionalDynamoDB:
Avoid scans (sequential read)
Use queries with GSI (index)
Projection expressions (read only needed fields)Firestore:
Index frequently queried fields
Use composite indexes for complex queries
Cache hot data in Realtime DB// DynamoDB batch writes
const dynamodb = new AWS.DynamoDB.DocumentClient();
await dynamodb.batchWrite({
RequestItems: {
'Users': [
{ PutRequest: { Item: { userId: '1', name: 'Alice' } } },
{ PutRequest: { Item: { userId: '2', name: 'Bob' } } },
{ PutRequest: { Item: { userId: '3', name: 'Charlie' } } }
]
}
}).promise();
// Firestore batch
const batch = db.batch();
batch.set(db.collection('users').doc('user1'), { name: 'Alice' });
batch.set(db.collection('users').doc('user2'), { name: 'Bob' });
batch.set(db.collection('users').doc('user3'), { name: 'Charlie' });
await batch.commit();Provisioned Mode:
Read: $1.25 per 100 RCU/month
Write: $6.25 per 100 WCU/month
Predictable workloads
Pay-per-Request:
Read: $1.25 per 1M reads
Write: $6.25 per 1M writes
Unpredictable workloads (spiky)
Auto-scaling:
Automatically adjusts capacity
Prevents throttling
Good middle groundDelete unused data
Unused collections = costs
Batch operations
25 operations = 1 operation
Caching
Cache frequently read data
Reduce database readsProvisioned throughput (RUs)
1 RU = 1KB read or write
Minimum 400 RU/s = ~$24/month
Serverless (pay per usage)
$0.25 per 1M RUs
Better for unpredictable
Scaling strategies:
Autoscale: 4-100x throughput
Manual: Fixed RU/s// Limited transaction support (max 25 items)
const dynamodb = new AWS.DynamoDB.DocumentClient();
await dynamodb.transactWrite({
TransactItems: [
{
Put: {
TableName: 'Accounts',
Item: { id: '1', balance: 900 }
}
},
{
Put: {
TableName: 'Accounts',
Item: { id: '2', balance: 1100 }
}
}
]
}).promise();// Full ACID transactions
db.runTransaction(async (transaction) => {
const user1Ref = db.collection('users').doc('user1');
const user2Ref = db.collection('users').doc('user2');
const user1 = await transaction.get(user1Ref);
const user2 = await transaction.get(user2Ref);
transaction.update(user1Ref, { balance: user1.get('balance') - 100 });
transaction.update(user2Ref, { balance: user2.get('balance') + 100 });
});Metrics to track:
- ConsumedReadCapacityUnits: Alert > 80% provisioned
- ConsumedWriteCapacityUnits: Alert > 80% provisioned
- UserErrors: Alert on any
- SystemErrors: Alert on any
- ThrottledRequests: Alert > 0Metrics:
- Read operations: Track growth
- Write operations: Track growth
- Delete operations: Optimize
- Bytes read: Monitor costs| Use Case | Winner | Reason |
|---|---|---|
| Global Apps | Cosmos DB | Best multi-region support |
| Cost Sensitive | Firestore | Generous free tier + lowest prices |
| Infinite Scale | DynamoDB | Proven at massive scale |
| Real-time Apps | Firebase | Native real-time + offline |
| Complex Queries | Firestore | Best query language |
| Latency Critical | DynamoDB/Cosmos | Sub-millisecond |
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward