Okay, the user is asking about access patterns for DynamoDB and Bigtable, specifically covering keys, indexes, hot partitions, and TTLs. This seems like a technical query from someone designing or optimizing a database schema, likely a developer or architect. They might be comparing these two NoSQL databases or planning a migration.
I should structure the response to address each of their points clearly, contrasting DynamoDB and Bigtable where relevant. For keys, I need to explain partition keys and sort keys in DynamoDB versus row keys in Bigtable. Indexes are critical—DynamoDB has LSIs and GSIs, while Bigtable uses secondary indexes via Chromium or custom solutions. Hot partitions are a common pain point, so I should highlight how each system handles uneven data distribution. TTLs are simpler but worth mentioning for both.
The user probably wants practical advice, not just definitions. I’ll include anti-patterns and best practices, like avoiding monotonic keys in DynamoDB and using salt prefixes for Bigtable. They might also care about scalability trade-offs, so I’ll touch on how DynamoDB scales partitions automatically versus Bigtable’s manual splitting.
I should keep it concise but thorough, avoiding fluff. No need for introductions—just dive into the mechanics. If they’re asking about these four topics, they’re likely deep into implementation and need actionable insights. I’ll wrap up with a summary table for quick reference, since they might be cross-referencing these databases side-by-side.
Excellent question. These are the core practical considerations when designing for high-scale NoSQL databases. Here’s a detailed breakdown for Amazon DynamoDB and Google Cloud Bigtable, highlighting their differences and common patterns.
1. Primary Keys & Data Modeling
Key Difference: DynamoDB's sort key provides powerful intra-partition sorting/querying. Bigtable requires you to model relationships via row key structure or column design.
2. Secondary Indexes
-
DynamoDB:
- Local Secondary Index (LSI): Same PK, different SK. Must be defined at table creation. Shares partition with base table. Limited to 5 per table.
- Global Secondary Index (GSI): Different PK and/or SK. Physically separate table. Can be added/removed later. Each GSI has its own provisioned throughput (or On-Demand). Primary tool for alternative query patterns. Consumes extra storage and write capacity (writes are duplicated to all GSIs).
-
Bigtable:
- No native secondary indexes. Queries can only be done by:
- Full row key lookup.
- Row key prefix scan (e.g.,
get all rows starting with "user#").
- Filtering on column values (expensive, requires full row scan of the relevant row range).
- Solutions:
- Denormalization: Duplicate data into multiple tables with different row keys for each query pattern (most common).
- Materialized Views (Application-Managed): Your app writes to multiple tables.
- Third-party Indexing: Use Elasticsearch or Google Cloud Datastore/Firestore as a secondary index layer.
- Google's "Secondary Index" Sample: A common pattern is to create a separate "index table" where the row key is the indexed attribute and the value is the original row key.
Key Difference: DynamoDB has first-class, managed secondary indexes (with cost). Bigtable forces you to handle indexing in your application logic or via separate systems.
3. Hot Partitions / Uneven Data Distribution
This occurs when a single partition key value receives a disproportionate share of traffic, throttling that partition while others sit idle.
-
DynamoDB:
- Cause: Monotonic or low-cardinality partition keys (e.g.,
timestamp, status='active').
- Mitigation:
- Add a "salt" prefix (random shard number) to the partition key. e.g.,
shard_#_user_id. Requires application logic to scatter/gather.
- Use high-cardinality attributes as PK.
- Use Write Sharding pattern for time-series: PK =
device_id#YYYY-MM-DD, SK = timestamp. Distributes daily writes across time, but yesterday's partition can still be hot. Better: device_id#HH (hourly).
- Adaptive Capacity: DynamoDB can temporarily borrow throughput from other partitions, but sustained hot partitions will hit limits.
-
Bigtable:
- Cause: Row keys with sequential or monotonic prefixes (e.g.,
timestamp, user_id if all load is on one user).
- Mitigation:
- Reverse/Invert components of the row key. e.g., for time-series, use
timestamp_reversed (salt with max_timestamp - timestamp) so newest data spreads across nodes.
- Salt/Prepend random hash (like DynamoDB). e.g.,
MD5(user_id) % 1024 | user_id.
- Hotspotting Detection: Use Bigtable's
hotspotting metrics in monitoring. Tablets (split units) will be uneven.
- Manual Splitting: You can manually pre-split a table at key boundaries to distribute load proactively.
Key Difference: Both suffer from the same root cause (poor row/partition key design). Bigtable gives you more low-level control (manual splits) but requires more foresight. DynamoDB's adaptive capacity offers a slight buffer.
4. Time-to-Live (TTL)
-
DynamoDB:
- Native, server-side TTL. Set a
ttl attribute (epoch time in seconds) on an item. A background process automatically expires and deletes items within 48 hours of expiration.
- Use Case: Session data, transient logs, temporary locks.
- Caveats: No precise deletion timing. Deletes consume write capacity. Expired items might still appear in
Query/Scan until removed. Declining ttl attribute value is not a delete.
-
Bigtable:
- No native TTL feature. Deletions must be performed by:
- Application Logic: Your app issues
Delete mutations for old rows.
- Cron/Dataflow Jobs: Scheduled pipelines that scan and delete.
- Garbage Collection with Column Families: You can set a
GC rule on a column family to automatically delete cells (not entire rows) after a certain max age or max versions. This is per-cell, not per-row.
- Use Case: Cell-level TTL for caching (e.g.,
SET cache_value with ttl=1h). Row-level TTL requires custom logic.
Key Difference: DynamoDB offers a simple, table-level TTL attribute. Bigtable's GC rules are more granular (cell-level) but cannot automatically expire entire rows based on a timestamp attribute without custom processing.
Summary Table
| Feature | DynamoDB | Bigtable |
|---|
| Primary Key | Partition Key + Optional Sort Key | Single Row Key (byte string) |
| Secondary Indexes | Native LSIs & GSIs (managed, costly) | None. Requires denormalization or external indexes (Elasticsearch, etc.) |
| Hot Partition Mitigation | Salt/shard PK, high cardinality, adaptive capacity | Reverse/invert key, salt prefix, manual tablet splitting |
| TTL | Native, attribute-based. Server-side, ~48hr delay. | None for rows. Cell-level GC rules only. Row deletion is app-managed. |
| Query Power | GetItem (PK), Query (PK+SK range), Scan (expensive). GSIs add Query paths. | Get (row key), prefix Scan (row key range). Column filtering is full scan. |
| Schema Philosophy | Single-table, flexible attributes. Access patterns dictate PK/SK structure. | Wide-column, sparse rows. Row key must encode all primary query prefixes. |
Core Design Mantras
- For DynamoDB: "Design your data model around your queries, not your entities." Know your
Query patterns first. Use GSIs sparingly.
- For Bigtable: "The row key is your everything." Everything you want to query by must be in the row key prefix. There is no planner; you are the query optimizer.
When to choose which?
- Choose DynamoDB for applications with multiple, evolving access patterns that benefit from managed secondary indexes and a flexible single-table model. Common in web-scale apps (user profiles, shopping carts, game state).
- Choose Bigtable for massive, sequential access patterns with a single, predictable query path (e.g., time-series IoT data, user activity streams, web analytics). It excels when your primary access is "give me all rows for a key prefix between A and Z."