The user wants to know about access patterns for DynamoDB and Bigtable, specifically focusing on:
- Keys: Primary keys, composite keys, how they dictate access.
- Indexes: Secondary indexes (LSI/GSI for DynamoDB, similar concepts for Bigtable).
- Hot Partitions: What they are, why they are bad, how to avoid them.
- TTLs: Time-to-Live attributes for automatic expiration.
The user didn't ask for a comparison specifically, but since they mentioned both DynamoDB (AWS) and Bigtable (GCP), I should explain the concepts in a way that applies to both, while highlighting specific terminology or implementation differences where relevant. NoSQL access patterns are fundamentally different from SQL, so emphasizing "query-driven design" is crucial.
1. Keys (The Foundation)
- Concept: In NoSQL, the key is the index. You generally cannot query by non-key attributes efficiently.
- DynamoDB:
- Partition Key (PK) - Hash.
- Composite Key (PK + Sort Key) - Hash + Range.
- Bigtable:
- Row Key. It's essentially one big string. The design pattern is usually prefix (bucket) + delimiter + identifier.
- Access Pattern Implication: You must know the key to get an item (Get) or query a range within a single partition (Query). Scans are expensive and avoided.
2. Indexes (Secondary Access)
- Why: You can't query by non-key attributes without them.
- DynamoDB:
- LSI (Local Secondary Index): Same partition, different sort key. Limited size (10GB).
- GSI (Global Secondary Index): Different partition and/or sort key. Eventually consistent (mostly). Allows "sparse indexes."
- Bigtable:
- Doesn't have "native" secondary indexes in the same automatic way DynamoDB does.
- Strategy: Create a separate table with the inverted relationship or use " interleaved" tables (parent-child).
- Access Pattern Implication: Indexes allow "views" of the data. If I want to find users by email, but my PK is UserID, I need an index on Email.
3. Hot Partitions (The Performance Killer)
- Concept: Uneven distribution of read/write traffic.
- Cause: Monotonic keys (timestamps, auto-incrementing IDs), or "celebrity" status (one user gets all the traffic).
- DynamoDB: Adaptive Capacity helps, but design matters.
- Bigtable: Tablets split automatically, but if writes are sequential, they all go to the last tablet.
- Mitigation: Hashing prefixes, Salting, sharding keys (e.g.,
userid#orderid vs just orderid).
4. TTLs (Data Lifecycle)
- Concept: Auto-delete old data to save money and effort.
- DynamoDB: Enable TTL attribute on a table. Set the timestamp.
- Bigtable: Garbage Collection (GC) policies configured at the column family level (e.g., max versions, max age).
- Access Pattern Implication: Great for session stores, logs, temporary events.
Structure of the Response:
I will structure this by concept (Keys, Indexes, Hot Partitions, TTLs) rather than by database, but I will call out specific behaviors for each within those sections. I'll start with the "Golden Rule" of NoSQL design.
- Introduction: Query-first design.
- Section 1: Primary Keys: Structure, Hash vs. Range, Bigtable Row Key design.
- Section 2: Indexes: GSI/LSI (DynamoDB) vs. Materialized Views/Duplication (Bigtable).
- Section 3: Hot Partitions: The problem (monotonic writes), the solution (salting/hashing).
- Section 4: TTLs & Lifecycle: Usage cases, differences in implementation.
- Summary/Best Practices Table: Quick reference.
Refining specific details:
- Bigtable Indexes: I need to be careful here. Bigtable strictly allows only one index: the Primary Key (Row Key). If you need another index, you manually build a second table. This is a key difference from DynamoDB which has built-in GSIs. I must highlight this.
- Hot Partitions: Use the term "Write Amplification" or "Hotspotting."
- Keys: Emphasize that the Sort Key allows complex query patterns (begins_with, between).
Let's draft the content.
- Keys: DynamoDB = Partition Key (+ Sort Key). Bigtable = Row Key (lexicographical sort).
- Hot: DynamoDB specific term is "Hot Partition." Bigtable is "Hotspotting."
- Mitigation: Adding random suffixes/prefixes.
Final check: Does this cover everything the user asked? Yes.
Keys, Indexes, Hot Partitions, TTLs.
Is it specific to DynamoDB/Bigtable? Yes.
I will format with code blocks for examples and bold text for key terms. I'll include a specific "Access Pattern" example (e.g., an e-commerce order system) to make it concrete.
Self-Correction during drafting: I should mention that DynamoDB is a "document store" (sort of) while Bigtable is a "wide-column store," but for access patterns, the concept of "Key-Value" dominates both. The distinction matters more for how data is stored inside the row, but the access via the key is similar.
Let's write it out.
Designing for DynamoDB and Bigtable requires a fundamental shift from relational database thinking. In SQL, you design the schema first and write queries later. In NoSQL (DynamoDB/Bigtable), you must know your queries (access patterns) before you design the schema.
Here is a breakdown of how to handle Keys, Indexes, Hot Partitions, and TTLs to optimize access patterns.
1. Keys: The Foundation of Access
In both systems, the Primary Key is the only efficient way to retrieve a single item. You cannot efficiently query for "All items where color='red'" unless that attribute is part of a key or index.
DynamoDB
- Simple Key (Partition Key only): Good for simple lookups (e.g., User Profile by
UserID).
- Composite Key (Partition + Sort Key): The most powerful pattern. You group items by the Partition Key and query/range them by the Sort Key.
- Example: Partition:
UserID, Sort: Timestamp.
- Access Pattern: "Get all activity for User X in the last 24 hours."
Bigtable
- Row Key: Bigtable only has one key (the Row Key). It is physically stored in lexicographical (alphabetical) order.
- Design Pattern: You often have to "materialize" relationships into the key.
- Example:
UserID#Timestamp.
- Access Pattern: Scanning a specific user's history is fast because the row keys are sorted together.
💡 Pro Tip: Interleaving (Bigtable) / Composite Keys (DynamoDB).
If you need to fetch a parent and all its children (e.g., a Customer and all their Orders) in one request:
- Bigtable: Use a prefix like
CustomerA#Order1. This keeps rows physically adjacent.
- DynamoDB: Use a GSI or a "Sparse Index" strategy where the Partition Key is the
EntityID and the Sort Key defines the type (e.g., PK: CUST_1, SK: PROFILE; PK: CUST_1, SK: ORDER_2023).
2. Indexes: Handling Secondary Access Patterns
If you need to query by an attribute that isn't part of your Primary Key, you need an Index.
DynamoDB
- Local Secondary Index (LSI):
- Constraint: Must be created at table creation time. Shares the partition capacity with the base table.
- Use Case: Querying a different sort key on the same partition (e.g., Query
User_1 but sort by ZipCode instead of Timestamp).
- Warning: If the item collection grows beyond 10GB, it fails. Usually avoided in favor of GSIs.
- Global Secondary Index (GSI):
- Flexibility: Can use any attribute as the Partition or Sort key. It is essentially a separate table that is automatically synchronized.
- Cost: Consumes its own Write/Read Capacity Units (RCU/WCU).
- Sparse Indexes: A powerful pattern. Only items that possess the index key attributes are projected into the index. This saves money and space.
Bigtable
- No Native Secondary Indexes: Bigtable does not support secondary indexes automatically.
- The Workaround: You must denormalize your data.
- Create a separate table (or column family) that inverts the mapping (e.g., a
EmailToUserID table).
- Write to both the main table and the mapping table in your application logic.
3. Hot Partitions: Performance & Sharding
"Hot partitioning" occurs when too much traffic (read or write) targets a single key range, overwhelming the physical hardware responsible for that slice of data.
The "Hot Key" Problem
If your Partition Key is Status and you update status "Active" 10,000 times per second, that partition will throttle, while the "Inactive" partition sits idle.
Mitigation Strategies
- High-Cardinality Partition Keys: Ensure your partition keys have many distinct values (e.g., use
UserID instead of Country).
- Write Sharding (Salting):
- Add a random suffix (or calculated bucket number) to the key.
- Instead of:
Date: 2023-10-27
- Use:
Date: 2023-10-27#1, Date: 2023-10-27#2 ... Date: 2023-10-27#N.
- This distributes the write load across N physical partitions.
- Cost: To read, you must query all N suffixes and aggregate the results in your application.
- Hotspotting in Bigtable (Monotonic Values):
- Bigtable Row Keys are sorted. If you use a timestamp as the key (e.g.,
2023-10-27 09:00:01), all new writes go to the end of the table. This creates a "hot" tablet server at the tail.
- Fix: Reverse the timestamp (
UnixMaxTime - Timestamp) or prefix with a non-sequential ID (e.g., SensorID#Timestamp) to spread the writes across different sensor buckets.
4. TTLs (Time to Live): Automated Expiration
TTLs allow you to set a specific attribute on an item; after a defined number of seconds, the database deletes the item automatically.
Access Pattern Implications
- Session Stores: Perfect for keeping user sessions. If the user doesn't log in for 30 days, the row disappears.
- Event Logs / Analytics: Store logs for 30 days, then let TTL delete them to save storage costs.
- GDPR / Data Retention: Automate compliance without running a nightly "Delete" job (which consumes capacity and can throttle the table).
Differences
- DynamoDB: TTL is a background process. Deletion is "eventually consistent" (usually within 48 hours, often much faster). It does not consume Write Capacity Units (WCU) for the deletion itself.
- Bigtable: Managed via Garbage Collection (GC) policies. You define rules at the Column Family level (e.g., "Keep only the last 3 versions" or "Discard cells older than 30 days").
Summary: The Cheat Sheet
| Feature | DynamoDB | Bigtable |
|---|
| Key Structure | Partition Key (+ optional Sort Key). | Single Row Key (sorted string). |
| Secondary Index | GSI (Global) & LSI (Local). Built-in and managed. | None. Must manually create secondary tables (denormalization). |
| Hot Key Fix | Add random suffix (Sharding). | Use reversed timestamps or specific prefixes to spread keys. |
| TTL | Enable on attribute. Expires whole Item. | GC Policy on Column Family. Expires specific Cells/Versions. |
| Best For | General purpose, JSON-like documents, variable schemas. | High-throughput time-series, analytical heavy workloads, flat data. |