DynamoDB Access Patterns
DynamoDB is a NoSQL key-value and document store optimized for high-throughput, low-latency access. Design access patterns around partition keys (PK) and sort keys (SK) for efficient queries. Poor design leads to hot partitions (throttling) or scan-heavy operations.
1. Keys
- Partition Key (PK): Hashes data into partitions. Choose high-cardinality (unique-ish) attributes to distribute load (e.g.,
userId + timestamp, not just userId).
- Bad: Low-cardinality PK like
status ("active/inactive") → all "active" items in one partition → hot partition.
- Good: Composite like
year#status for even spread.
- Sort Key (SK): Optional, enables range queries within a partition (e.g.,
timestamp for time-series).
- Query pattern:
PK = :pk AND SK BETWEEN :start AND :end.
- Single-table design: Common for efficiency. Use PK/SK + attributes like
PK = "USER#123", SK = "PROFILE" | "ORDER#2023-01" for multiple entity types.
2. Indexes
- Local Secondary Index (LSI): Shares PK, projects alternate SK/attributes. Limited to 10 per table, created at table creation.
- Use: Query same partition by different SK (e.g.,
PK = userId, SK = orderDate in LSI).
- Global Secondary Index (GSI): Independent PK/SK, full table scans for projection. Up to 20 GSIs, provisioned capacity.
- Use: Alternate access patterns (e.g., GSI PK =
orderId, SK = userId).
- Cost: Writes hit all GSIs; reads from index RCUs.
- Sparse indexes: Only indexed items with the projected attribute appear → efficient for optional fields.
3. Hot Partitions
- Cause: Uneven PK distribution → one partition >3000 RCUs/WCUs → throttling.
- Detect: CloudWatch metrics (Consumed RCUs/WCUs per partition),
ConsumedReadCapacityUnits spikes.
- Mitigate:
| Technique | Example |
|---|
| High-cardinality PK | userId#shardId (shard by userId % 10) |
| Random suffix | status#random(1000) |
| Write sharding | Rotate PK every N writes |
| Burst capacity/On-demand mode | Handles spikes, but costs more |
4. TTLs
- Attribute (e.g.,
ttl) as Unix epoch (seconds) → auto-delete after.
- Enable on table: No cost, eventual consistency (up to 48h delay).
- Pattern: Time-series data (
PK = metricName, SK = timestamp, ttl = timestamp + 86400 for 1-day retention).
- Monitor: TTL deletions in CloudWatch.
Best Practices: Model 80% of queries with PK/SK first. Use DAX for caching. Test with boto3 load simulators.
Bigtable Access Patterns
Bigtable is a wide-column store for massive scale (e.g., time-series, logs). Rows identified by row key, optimized for sequential access. Like DynamoDB, avoid hot spots.
1. Keys
- Row Key: Single string, hashed to tablets. Design for locality + distribution.
- Structure:
entityId#timestamp#shard (reverse timestamp for newest-first).
- Bad: Monotonic keys like
timestamp → all new writes to one tablet → hot spot.
- Good:
deviceId#reversed_timestamp (locality for device queries, even writes).
- No sort key; use column families/qualifiers for hierarchy (e.g.,
cf:sensor#2023-01-01T00:00).
2. Indexes
- No built-in secondary indexes (unlike DynamoDB). Use:
- Interleaved tables: Sub-rows under parent (e.g., user table interleaves orders).
- Separate lookup tables: Denormalize (e.g.,
orderId → userId table).
- Application-level: Bloom filters or external search (e.g., Elasticsearch).
- Column families: Group related columns (e.g.,
metrics_cf, metadata_cf). Up to 100 families/table.
3. Hot Partitions (Hot Tablets)
- Cause: Uneven row key distribution → tablet servers overload.
- Detect: Cloud Monitoring (CPU >80%, QPS imbalances),
hotTabletCount.
- Mitigate:
| Technique | Example |
|---|
| Key prefix rotation | reversed_date#deviceId |
| Sharding | deviceId#shard(0-99)#timestamp |
| Salting | salt(0-99)#key (100x spread) |
| Pre-splitting | Define split points at creation |
4. TTLs
- Per-column family:
gc_policy: max_age: 30d.
- Or
gc_policy: union(max_versions: 1, max_age: 30d).
- Auto-GC during compactions. Fine-grained control vs. DynamoDB's item-level.
Best Practices: Locality-first (group accessed-together rows). Use cbt tool for testing. Scale via nodes (1000s tablets/node).
DynamoDB vs. Bigtable Quick Compare
| Feature | DynamoDB | Bigtable |
|---|
| Keys | PK + SK | Row key |
| Indexes | LSI/GSI (managed) | None (app-managed) |
| Hot Partitions | PK cardinality | Row key prefix |
| TTLs | Item-level attribute | Column family policy |
| Scale | Provisioned/On-demand | Node-based, petabyte+ |
| Use Case | App DB (e.g., ecomm) | Analytics/logs (e.g., YouTube) |
For deeper dives, share your schema/queries—I'll optimize like a detective! 🔍