Azure Cosmos DB là globally distributed, multi-model database với guarantees về latency, throughput, consistency và availability. Đây là lựa chọn hàng đầu cho workloads cần low latency và global scale.
Key Characteristics
Feature
Value
Latency
< 10ms read, < 15ms write (P99)
Availability
99.999% (multi-region)
Throughput
Provisioned RU/s hoặc Serverless
Distribution
Multi-region, multi-write
APIs
NoSQL, MongoDB, Cassandra, Gremlin, Table
Data Modeling - Embedding vs Referencing
Nguyên tắc quan trọng nhất
Model data theo cách app đọc, không phải theo cách relational database chuẩn hóa.
Khi nào Embed vs Reference?
Criteria
Embed (Denormalize)
Reference (Normalize)
Đọc cùng nhau?
Luôn đọc chung
Đọc riêng biệt
Tỷ lệ update
Ít update
Update thường xuyên
Cardinality
1:few (< 100)
1:many (unbounded)
Document size
< 2 MB sau embed
Gần giới hạn 2 MB
Consistency
Atomic (cùng document)
Eventually consistent
Ví dụ: Order Document
json
// EMBEDDED - Order + OrderItems (1:few, always read together){"id":"order-123","customerId":"cust-456","customerName":"Nguyen Van A","orderDate":"2024-01-15T10:30:00Z","status":"confirmed","items":[{"productId":"prod-789","productName":"Laptop Dell XPS","quantity":1,"unitPrice":25000000,"totalPrice":25000000},{"productId":"prod-012","productName":"USB-C Hub","quantity":2,"unitPrice":500000,"totalPrice":1000000}],"totalAmount":26000000,"partitionKey":"cust-456"}
Partition Key Strategy
Chọn Partition Key - Decision Framework
Ví dụ Partition Key theo use case
Use Case
Partition Key
Lý do
E-commerce orders
customerId
Query by customer phổ biến nhất
IoT telemetry
deviceId
Mỗi device ghi độc lập
Multi-tenant SaaS
tenantId
Data isolation per tenant
Chat messages
conversationId
Messages luôn query theo conversation
Logging
date + serviceId
Tránh hot partition trên date alone
Consistency Levels
Level
Guarantee
Latency
RU Cost
Use Case
Strong
Linearizable reads
Cao nhất
2x RU
Financial transactions
Bounded Staleness
Reads lag ≤ K versions hoặc T time
Cao
2x RU
Leaderboards, inventory
Session
Read-your-writes trong session
Trung bình
1x RU
Default - hầu hết apps
Consistent Prefix
Reads không bao giờ out of order
Thấp
1x RU
Social media feeds
Eventual
Không guarantee order
Thấp nhất
1x RU
Analytics, non-critical reads
Recommendation: Bắt đầu với Session consistency - đủ mạnh cho hầu hết scenarios, cost hợp lý.