← Back to Home

Module 3: DynamoDB Table Design

Learning Objectives

Introduction to DynamoDB

Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. DynamoDB lets you offload the administrative burdens of operating and scaling a distributed database so that you don't have to worry about hardware provisioning, setup and configuration, replication, software patching, or cluster scaling.

With DynamoDB, you can create database tables that can store and retrieve any amount of data and serve any level of request traffic. You can scale up or scale down your tables' throughput capacity without downtime or performance degradation.

Key Differences from Relational Databases

If you're familiar with relational databases like MySQL or PostgreSQL, you'll need to adjust your thinking when working with DynamoDB:

DynamoDB Core Concepts

Tables, Items, and Attributes

The basic DynamoDB components are:

Primary Keys

When you create a table, you must specify its primary key. The primary key uniquely identifies each item in the table, so that no two items can have the same key. DynamoDB supports two types of primary keys:

Understanding Partitions

DynamoDB stores data in partitions. A partition is an allocation of storage for a table, backed by solid-state drives (SSDs) and automatically replicated across multiple Availability Zones within an AWS Region. Partition management is handled entirely by DynamoDB—you never have to manage partitions yourself.

When you create a table, DynamoDB allocates enough partitions to handle your provisioned throughput requirements. As your application data and throughput requirements change, DynamoDB automatically adjusts by splitting busy partitions and distributing the data over a larger number of partitions.

// Example CloudFormation template for a DynamoDB table
{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "UsersTable": {
      "Type": "AWS::DynamoDB::Table",
      "Properties": {
        "TableName": "Users",
        "BillingMode": "PAY_PER_REQUEST",
        "AttributeDefinitions": [
          {
            "AttributeName": "userId",
            "AttributeType": "S"
          },
          {
            "AttributeName": "email",
            "AttributeType": "S"
          }
        ],
        "KeySchema": [
          {
            "AttributeName": "userId",
            "KeyType": "HASH"
          }
        ],
        "GlobalSecondaryIndexes": [
          {
            "IndexName": "EmailIndex",
            "KeySchema": [
              {
                "AttributeName": "email",
                "KeyType": "HASH"
              }
            ],
            "Projection": {
              "ProjectionType": "ALL"
            }
          }
        ]
      }
    }
  }
}

DynamoDB Table Design Best Practices

Effective Partition Key Design

The partition key is used to determine the physical partition where the item will be stored. A well-designed partition key will:

Good candidates for partition keys include:

Poor candidates for partition keys include:

Using Composite Keys Effectively

A composite key (partition key + sort key) allows you to:

Common patterns include:

Design for Access Patterns

Unlike relational databases where you can add indexes later, with DynamoDB you need to identify your access patterns upfront and design your tables accordingly:

// Example of DynamoDB table design for a social media app
// Primary table: UserId (partition) and ItemType#Timestamp (sort)

// Example items in the table:
// User profile:
{
  "UserId": "U123",
  "ItemType": "PROFILE",
  "Name": "John Doe",
  "Email": "john@example.com"
}

// User post:
{
  "UserId": "U123",
  "ItemType": "POST#2023-06-15T14:30:00Z",
  "Content": "Hello world!",
  "Likes": 42
}

// User follower:
{
  "UserId": "U123",
  "ItemType": "FOLLOWER#U456",
  "FollowerName": "Jane Smith"
}

Key Topics

DynamoDB Partitions

Learn about how DynamoDB stores and manages data across partitions.

  • Partition fundamentals
  • How partitioning affects performance
  • Designing keys for optimal partition usage

DynamoDB Design Best Practices

Learn best practices for designing DynamoDB tables to optimize for your access patterns.

  • Single-table design principles
  • Handling relationships in NoSQL
  • Avoiding common design pitfalls

DynamoDB Table Design and Creation

Learn how to create and configure DynamoDB tables using AWS CloudFormation.

  • CloudFormation templates for DynamoDB
  • Setting up tables with Java
  • Provisioning capacity

Guided Project

In the guided project for this module, you'll apply what you've learned about DynamoDB table design to create the database tables for your Learn and Be Curious project.

Project: Creating DynamoDB Tables for Your Application

This project will guide you through designing and creating DynamoDB tables based on the API you designed in Module 2.

Design Document - DynamoDB Tables

Work on the tables section of your design document, defining the DynamoDB tables needed for your project.

Additional Resources

AWS DynamoDB Documentation

Official AWS documentation for Amazon DynamoDB.

DynamoDB Best Practices

AWS guide for DynamoDB best practices.

CloudFormation DynamoDB Examples

Examples of creating DynamoDB tables with CloudFormation.