0
0
AwsHow-ToBeginner · 4 min read

How to Use Global Tables in DynamoDB for Multi-Region Replication

Use DynamoDB Global Tables to automatically replicate your tables across multiple AWS regions for high availability and low latency. Create a global table by specifying existing tables in different regions or creating new ones, and DynamoDB handles data replication transparently.
📐

Syntax

To create a DynamoDB Global Table, you specify the table name and the AWS regions where replicas will exist. AWS CLI or SDK commands include the CreateGlobalTable API or table creation with Replicas property. Each replica is a full copy of the table in a different region.

  • TableName: The name of the global table.
  • Replicas: List of regions where the table replicas will be created.
bash
aws dynamodb create-global-table \
    --global-table-name MyGlobalTable \
    --replication-group RegionName=us-east-1 RegionName=eu-west-1
💻

Example

This example shows how to create a DynamoDB Global Table with replicas in two regions using AWS CLI. It demonstrates automatic replication of data between us-east-1 and eu-west-1.

bash
aws dynamodb create-global-table \
    --global-table-name MyGlobalTable \
    --replication-group RegionName=us-east-1 RegionName=eu-west-1

# After creation, put an item in one region
aws dynamodb put-item \
    --table-name MyGlobalTable \
    --item '{"Id": {"S": "123"}, "Name": {"S": "Test Item"}}' \
    --region us-east-1

# Get the item from the other region
aws dynamodb get-item \
    --table-name MyGlobalTable \
    --key '{"Id": {"S": "123"}}' \
    --region eu-west-1
Output
PutItem succeeded. { "Item": { "Id": {"S": "123"}, "Name": {"S": "Test Item"} } }
⚠️

Common Pitfalls

Common mistakes when using DynamoDB Global Tables include:

  • Trying to create global tables with different key schemas in replicas, which is not allowed.
  • Not enabling streams on tables before creating global tables; streams must be enabled for replication.
  • Ignoring eventual consistency: writes replicate asynchronously, so immediate reads in another region might not reflect the latest write.
  • Using legacy global tables (version 2017) instead of the current version (version 2019) which supports more regions and features.
bash
## Wrong: Creating global table without streams enabled
aws dynamodb create-table \
    --table-name MyTable \
    --attribute-definitions AttributeName=Id,AttributeType=S \
    --key-schema AttributeName=Id,KeyType=HASH \
    --billing-mode PAY_PER_REQUEST \
    --stream-specification StreamEnabled=false

# This will fail when creating global table because streams are not enabled.

## Right: Enable streams before creating global table
aws dynamodb update-table \
    --table-name MyTable \
    --stream-specification StreamEnabled=true,StreamViewType=NEW_AND_OLD_IMAGES
📊

Quick Reference

  • Create Global Table: Use CreateGlobalTable API or AWS CLI with --replication-group.
  • Enable Streams: Streams must be enabled with NEW_AND_OLD_IMAGES view.
  • Consistent Key Schema: All replicas must have the same primary key.
  • Replication Delay: Replication is asynchronous; expect slight delays.
  • Version: Use the latest global tables version (2019) for best features.

Key Takeaways

DynamoDB Global Tables replicate your data automatically across multiple AWS regions for high availability.
Enable DynamoDB Streams with the correct view type before creating global tables.
All replicas must share the same primary key schema to work correctly.
Replication is asynchronous; expect slight delays in data consistency across regions.
Use the latest global tables version (2019) for improved features and wider region support.