🎯 Mục tiêu Task 7: Chuyển đổi từ MongoDB Atlas sang DynamoDB - HƯỚNG THỦ CÔNG ĐƠN GIẢN NHẤT
⚠️ Lưu ý quan trọng: Migration này sẽ thay đổi hoàn toàn database backend. Hãy backup dữ liệu MongoDB trước khi bắt đầu!
Tại sao chọn hướng này:
💡 Pro Tip: Nếu bạn có dữ liệu > 1 triệu records, nên sử dụng AWS Database Migration Service (DMS) thay vì cách thủ công này.
📋 Chuẩn bị trước khi export:
⚠️ Bảo mật: Không bao giờ commit connection string vào git. Sử dụng environment variables hoặc .env file.
vinashoes
products,users,carts,… → Export Collection → Export as csv💡 Optimization Tips:

Kết quả: Bạn có được những file csv:
📁 File Structure mong đợi:
exports/
├── products.csv (sản phẩm)
├── users.csv (người dùng)
├── orders.csv (đơn hàng)
├── carts.csv (giỏ hàng)
└── categories.csv (danh mục)

💰 Chi phí: Upload S3 có thể mất phí nếu file lớn. Sử dụng S3 Intelligent Tiering để tối ưu chi phí.
AWS Console → S3:

vinashoes-migration-dataproducts.csv, orders.csv, users.csv,…🚀 Speed Up Upload:
aws s3 cp *.csv s3://bucket-name/ --recursivegzip *.csv

⏱️ Thời gian import:
AWS Console → DynamoDB:

vinashoes-migration-dataproducts.csvvinashoes-products_id (String)⚠️ Partition Key Design:
_id từ MongoDB thường là ObjectId → phân phối tốt





📊 Monitoring Import Progress:
SuccessfulRequestLatencyXác nhận tất cả bảng đã import thành công:

✅ Verification Checklist:
🔧 Breaking Changes: Việc chuyển từ MongoDB sang DynamoDB sẽ thay đổi hoàn toàn cách query data. Cần test kỹ all endpoints!
npm uninstall mongoose @nestjs/mongoose
npm install @aws-sdk/client-dynamodb @aws-sdk/lib-dynamodb
📦 Package Dependencies:
@aws-sdk/client-dynamodb: Low-level DynamoDB client@aws-sdk/lib-dynamodb: High-level document client (recommended)@aws-sdk/util-dynamodb cho data transformationMongo code (cũ):
// product.service.ts
const product = await this.productModel.findById(id);
DynamoDB code (mới):
// product.repository.ts
import { Injectable } from '@nestjs/common';
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { GetCommand, ScanCommand, DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
@Injectable()
export class ProductRepository {
private client: DynamoDBDocumentClient;
constructor() {
const ddbClient = new DynamoDBClient({ region: "ap-southeast-1" });
this.client = DynamoDBDocumentClient.from(ddbClient);
}
async findById(id: string) {
const result = await this.client.send(new GetCommand({
TableName: "vinashoes-products",
Key: { _id: id }
}));
return result.Item;
}
async findAll() {
const result = await this.client.send(new ScanCommand({
TableName: "vinashoes-products"
}));
return result.Items;
}
}
⚠️ Performance Warning: ScanCommand đọc toàn bộ table → expensive! Sử dụng QueryCommand với GSI cho production.
# .env
AWS_REGION=ap-southeast-1
AWS_ACCESS_KEY_ID=your-key
AWS_SECRET_ACCESS_KEY=your-secret
🔐 Security Best Practices:
🧪 Testing Strategy:
# Test API endpoints vẫn hoạt động
curl http://localhost:3000/api/products

🔍 Advanced Testing:
# Test pagination
curl "http://localhost:3000/api/products?limit=10&lastKey=abc123"
# Test search with GSI
curl "http://localhost:3000/api/products/category/shoes"
# Test write operations
curl -X POST http://localhost:3000/api/products \
-H "Content-Type: application/json" \
-d '{"name":"Test Product","price":100}'
📋 Migration Complete - Next Steps:
Database:
Application:
Operations:
Clean-up:
💡 DynamoDB Limitations to Remember:
🚀 Performance Optimization Tips:
⚠️ Cảnh báo: Việc clean up sẽ xóa vĩnh viễn dữ liệu migration. Hãy đảm bảo migration đã thành công trước khi thực hiện!
AWS CLI Commands:
# List all objects in the migration bucket
aws s3 ls s3://vinashoes-migration-data --recursive
# Delete all objects in the bucket
aws s3 rm s3://vinashoes-migration-data --recursive
# Delete the bucket
aws s3 rb s3://vinashoes-migration-data
AWS CLI Commands:
# Delete products table
aws dynamodb delete-table --table-name vinashoes-products --region ap-southeast-1
# Delete users table
aws dynamodb delete-table --table-name vinashoes-users --region ap-southeast-1
# Delete orders table
aws dynamodb delete-table --table-name vinashoes-orders --region ap-southeast-1
# Delete carts table
aws dynamodb delete-table --table-name vinashoes-carts --region ap-southeast-1
# Delete categories table
aws dynamodb delete-table --table-name vinashoes-categories --region ap-southeast-1
⚠️ Production Warning: Chỉ xóa tables trong test environment. Production tables cần backup trước khi xóa.
AWS CLI Commands:
# Delete any DynamoDB backups created during migration
aws dynamodb list-backups --region ap-southeast-1 | jq -r '.BackupSummaries[] | select(.TableName | startswith("vinashoes-")) | .BackupArn' | xargs -I {} aws dynamodb delete-backup --backup-arn {} --region ap-southeast-1
# Delete any export files in S3 (if any)
aws s3 rm s3://vinashoes-backup-exports --recursive
Verification Commands:
# Verify S3 bucket deleted
aws s3 ls | grep vinashoes-migration-data || echo "Migration bucket deleted"
# Verify DynamoDB tables deleted
aws dynamodb list-tables --region ap-southeast-1 | grep vinashoes- || echo "All DynamoDB tables deleted"
# Check remaining resources
aws dynamodb list-backups --region ap-southeast-1 | jq '.BackupSummaries | length'
💰 Phân tích chi phí Migration từ MongoDB sang DynamoDB
| Dịch vụ | Chi phí | Mô tả |
|---|---|---|
| S3 Storage (Migration) | $0.023/GB/tháng | Lưu trữ file CSV tạm thời |
| S3 Data Transfer In | Miễn phí | Upload dữ liệu migration |
| DynamoDB Import | Miễn phí | AWS Import from S3 |
| DynamoDB Storage | $0.25/GB/tháng | Lưu trữ dữ liệu chính |
| DynamoDB Backup | $0.10/GB/tháng | Automated backup |
Chi phí migration ước tính (cho 1GB dữ liệu):
Lợi ích của Migration sang DynamoDB:
Tính toán ROI:
AWS Cost Explorer Commands:
# Get DynamoDB costs for last month
aws ce get-cost-and-usage \
--time-period Start=2024-01-01,End=2024-02-01 \
--granularity MONTHLY \
--metrics BlendedCost \
--group-by Type=DIMENSION,Key=SERVICE \
--filter '{
"Dimensions": {
"Key": "SERVICE",
"Values": ["Amazon DynamoDB"]
}
}' \
--region us-east-1
CloudWatch Monitoring:
# Monitor DynamoDB table size
aws cloudwatch get-metric-statistics \
--namespace AWS/DynamoDB \
--metric-name TableSizeBytes \
--dimensions Name=TableName,Value=vinashoes-products \
--start-time 2024-01-01T00:00:00Z \
--end-time 2024-02-01T00:00:00Z \
--period 86400 \
--statistics Maximum \
--region ap-southeast-1
DynamoDB Optimization:
Migration Cost Reduction:
Monitoring & Alerts:
| Service | MongoDB Atlas (Free Tier) | DynamoDB (On-Demand) | DynamoDB (Provisioned) | Ghi chú |
|---|---|---|---|---|
| Storage | 512MB free | $0.25/GB/tháng | $0.25/GB/tháng | DynamoDB tính theo GB |
| Data Transfer | 512MB free out | $0.09/GB | $0.09/GB | Transfer ra ngoài |
| Backup | Included | $0.10/GB/tháng | $0.10/GB/tháng | Automated backup |
| Tháng đầu | ~$0 | ~$2.5 (1GB data) | ~$2.5 (1GB data) | Ước tính cho 1GB data |
| Operation Type | MongoDB Atlas | DynamoDB On-Demand | DynamoDB Provisioned | Ghi chú |
|---|---|---|---|---|
| Reads | $0.10/1M queries | $0.25/1M RCU | $0.00013/RCU/hour | RCU = Read Capacity Unit |
| Writes | $0.10/1M operations | $1.25/1M WCU | $0.00065/RCU/hour | WCU = Write Capacity Unit |
| Queries phức tạp | Free | Thêm phí GSI | Thêm phí GSI | Global Secondary Index |
| Scenario | Estimated Monthly Cost | Optimization Tips |
|---|---|---|
| Small E-commerce (1K users) | $5-15 | Use On-Demand, monitor usage |
| Medium E-commerce (10K users) | $50-150 | Provisioned capacity, auto-scaling |
| Large E-commerce (100K+ users) | $500+ | Reserved capacity, GSI optimization |
| Development/Test | $1-5 | On-Demand with low traffic |
| Metric | MongoDB Atlas (Dedicated) | DynamoDB | Savings |
|---|---|---|---|
| Storage (1GB) | $2.50 | $0.25 | 90% |
| Compute (Basic) | $60/month | $10/month (est.) | 83% |
| Backup | $0.25/GB | $0.10/GB | 60% |
| Total Annual | $750 | $150 | 80% |
💡 Cost Optimization Tips:
Next Step: Task 8: ECR