Amazon DynamoDB Table Design Patterns - Single-Table Design and GSI Strategies
Learn about DynamoDB partition key design, single-table design, and how to implement diverse access patterns using GSIs.
Overview of DynamoDB
DynamoDB is a fully managed NoSQL database that delivers single-digit millisecond latency. Unlike RDS, it is schemaless and accesses data through a combination of partition keys and sort keys. As the standard data store for serverless architectures, it is commonly used alongside Lambda and API Gateway. It offers two capacity modes: on-demand and provisioned.
Table Design
The partition key directly affects data distribution, so choose attributes with high cardinality (such as user IDs or order IDs). In single-table design, the PK is prefixed like "USER#123" or "ORDER#456", and the SK represents entity types and relationships. A Query specifying the PK can retrieve both user information and order history in a single call. GSIs support different access patterns (e.g., looking up users by email address or searching orders by date range), and you can create up to 20 per table.
GSI and Single-Table Design
Global Secondary Indexes (GSIs) allow queries using a different partition key and sort key from the main table. In single-table design, multiple entities (users, orders, products) are stored in a single table, and GSI overloading handles diverse access patterns. Prefixes like 'USER#123' for PK and 'ORDER#456' for SK are used, and entities are filtered with the begins_with condition. Sparse indexes include only items with specific attributes in the index, reducing index size and cost. For a deeper understanding of NoSQL design patterns, specialized books (Amazon) are a great resource.
DynamoDB Cost Optimization
DynamoDB's on-demand mode charges per read (approximately $0.25 per million RRUs) and per write (approximately $1.25 per million WRUs). Provisioned mode charges hourly for WCU/RCU, and when combined with Auto Scaling, it improves cost efficiency for stable workloads. Reserved capacity offers discounts of up to 77%. Since GSIs consume their own capacity, remove unnecessary GSIs and minimize projection attributes to reduce storage costs. Use TTL to automatically delete expired data and keep storage costs down.
Summary
DynamoDB is a NoSQL database that achieves single-digit millisecond latency through partition key and sort key design. GSI overloading and single-table design support diverse access patterns, while sparse indexes optimize index costs. By choosing between on-demand and provisioned modes and using TTL for automatic data deletion, you can manage costs efficiently.