A global secondary index in DynamoDB is an index with a partition and sort key that can differ from the base table, allowing efficient querying on non-primary key attributes across all partitions.
Key takeaways:
DynamoDB global tables replicate multi-region data, improving read/write performance for global applications.
They enhance resilience and availability by automatically replicating data across multiple AWS regions.
Data synchronization is managed through DynamoDB Streams, with time stamp-based conflict resolution for consistency.
Setting up a global table involves creating a source table, enabling streams, and adding replicas in other regions.
Updates in one region are automatically propagated across all regions, maintaining real-time data consistency.
DynamoDB is a NoSQL database provided by AWS. Like other NoSQL databases, it stores data in tables. DynamoDB is a regional service, so it is available only in the region specified during its creation. As a result, the response of DynamoDB reads/writes gets slower as the requesting entity gets further away from the specified region. This difference in response time is negligible, but for applications that require very fast reads/writes, this might affect the user experience.
To tackle this issue, we can use DynamoDB global tables.
DynamoDB global tables are multi-regional tables that enable us to localize read and write performance for global applications. These tables ensure that our data is automatically replicated across multiple AWS regions, providing better performance for globally scaled applications.
Another advantage of using global tables is that they make our infrastructure more resilient and highly available.
Creating DynamoDB global tables involves a series of processes. Here’s a detailed look at how this works:
We create a DynamoDB table in our primary AWS region, specifying the desired table schema and other configurations. This table is referred to as a source table.
After we’ve created the source table, we specify the additional AWS regions where we want to replicate the table. DynamoDB automatically creates tables with the same schema in the selected regions. The data within the source table is also replicated in its replicas. These tables are interconnected to form the global table.
Here’s how data synchronization within these tables works:
When a change in an item is made to the table in one region, this change is recorded by the stream associated with that table.
DynamoDB uses the information in the streams to propagate changes to corresponding tables in other regions.
In case updates are made to the same item simultaneously across multiple regions, DynamoDB relies on timestamps associated with these changes to resolve conflicts. The system considers the update with the latest timestamp as the authoritative change, ensuring consistency across regions.
Let’s now implement a DynamoDB global table. Follow the steps below:
We’ll create our source DynamoDB table using the create-table
command. This command with all the required arguments is given below:
Enter your access_key_id
and secret_access_key
in the widget below before executing the command. If you don’t have the access keys, follow the steps in this documentation to generate the keys.
Note: The IAM user whose credentials are being used must have permission to perform all the required actions.
aws dynamodb create-table \--table-name Books \--attribute-definitions \AttributeName=Author,AttributeType=S \AttributeName=Book,AttributeType=S \--key-schema \AttributeName=Author,KeyType=HASH \AttributeName=Book,KeyType=RANGE \--billing-mode PAY_PER_REQUEST \--region us-east-1
This command will create a table titled Books
in the us-east-1
region.
Now that we have the source table, let’s create its replica in another region using the update-table
command. With all the required arguments, this command is given in the playground below.
aws dynamodb update-table --table-name Books --cli-input-json \'{"ReplicaUpdates":[{"Create": {"RegionName": "us-east-2"}}]}' \--region=us-east-1
This command will create a replica of the Books
table in the us-east-2
region.
This completes our DynamoDB global table setup. Our global table consists of one source and replica located in the us-east-1
and us-east-2
regions, respectively.
Let’s test our global table by adding an item to the source table and then reading the replica table to see if the added item was replicated. To do that, we’ll use the put-item
command. This command with all the required arguments is given in the playground below:
aws dynamodb put-item \--table-name Books \--item '{"Author": {"S":"item_1"},"Book": {"S":"Value 1"}}' \--return-consumed-capacity 'TOTAL' \--region us-east-1
An item will be added to our source table.
This added item should be replicated in our replica table. Let’s look at the items in the replica to see if they exist. Execute this command to look for the added item in the replica table.
aws dynamodb get-item \--table-name Books \--key '{"Author": {"S":"item_1"},"Book": {"S":"Value 1"}}' \--region us-east-1
You’ll get an output similar to the provided key indicating that this item is present in the replica table in the us-east-2
region. Similarly, any other action performed on the items in this table will be replicated on all other tables of this global table and vice versa.
Haven’t found what you were looking for? Contact Us
Free Resources