dynamo-db
  1. dynamo-db-multi-region-replication-for-high-availability

Multi-region replication for high availability - DynamoDB Global Tables

DynamoDB Global Tables is a feature introduced by AWS which allows you to replicate the data across multiple AWS regions simultaneously. This feature ensures that your data is available and consistent across all regions, even if a region goes down. In this tutorial, we will learn how to create a Global Table in DynamoDB and how it works.

Syntax

The syntax to create a Global Table in DynamoDB is as follows:

{
    "TableName": "table_name",
  
    "AttributeDefinitions": [
        {
            "AttributeName": "attribute_name",
            "AttributeType": "S"|"N"|"B"
        }
        ...
    ],
  
    "KeySchema": [
        {
            "AttributeName": "attribute_name",
            "KeyType": "HASH"|"RANGE"
        }
        ...
    ],
  
    "GlobalSecondaryIndexes": [
        {
            "IndexName": "index_name",
            "KeySchema": [
                {
                    "AttributeName": "attribute_name",
                    "KeyType": "HASH"|"RANGE"
                }
                ...
            ],
              
            "Projection": {
                "ProjectionType": "ALL"|"KEYS_ONLY"|"INCLUDE",
                "NonKeyAttributes": [
                    "attribute_name", ...
                ]
            },
              
            "ProvisionedThroughput": {
                "ReadCapacityUnits": number,
                "WriteCapacityUnits": number
            }
        }
        ...
    ],
    
    "ReplicationGroup": {
        "RegionName": [
            "region_name_1",
            "region_name_2",
            ...
        ]
    },
    
    "BillingMode": "PROVISIONED"|"PAY_PER_REQUEST",
    
    "ProvisionedThroughput": {
        "ReadCapacityUnits": number,
        "WriteCapacityUnits": number
    },
    
    "StreamSpecification": {
        "StreamEnabled": boolean,
        "StreamViewType": "NEW_IMAGE"|"OLD_IMAGE"|"NEW_AND_OLD_IMAGES"|"KEYS_ONLY"
    }    
}

Example

In the following example, we will create a Global Table named testTable with two regions us-east-1 and us-west-2.

{
    "TableName": "testTable",
  
    "AttributeDefinitions": [
        {
            "AttributeName": "id",
            "AttributeType": "S"
        }
    ],
  
    "KeySchema": [
        {
            "AttributeName": "id",
            "KeyType": "HASH"
        }
    ],
  
    "GlobalSecondaryIndexes": [
        {
            "IndexName": "nameIndex",
            "KeySchema": [
                {
                    "AttributeName": "name",
                    "KeyType": "HASH"
                }
            ],
              
            "Projection": {
                "ProjectionType": "ALL"
            },
              
            "ProvisionedThroughput": {
                "ReadCapacityUnits": 5,
                "WriteCapacityUnits": 5
            }
        }
    ],
    
    "ReplicationGroup": {
        "RegionName": [
            "us-east-1",
            "us-west-2"
        ]
    },
    
    "BillingMode": "PAY_PER_REQUEST"
}

Output

After executing the above code, you will see that a Global Table named testTable is created with two regions us-east-1 and us-west-2.

Explanation

In the above syntax, ReplicationGroup is a mandatory attribute that specifies the list of AWS regions where the data will be replicated. DynamoDB handles data replication and propagation across these regions.

Use

DynamoDB Global Tables is mainly used to achieve high availability for applications running around the world. With Global Tables, your application can handle requests from any region, and if one region goes down, your other regions will continue to provide data and services. Global Tables can also be used to reduce latency and improve the performance of your application.

Important Points

  • DynamoDB Global Tables is currently limited to tables with partition keys.
  • Global Secondary Indexes can be created on Global Tables but must be created individually from each region.
  • Data is eventually Consistent across all regions.
  • AWS charges for replicated data and replication traffic.

Summary

In this tutorial, we learned about Multi-region replication for high availability using DynamoDB Global Tables. Global Tables is a feature introduced by AWS that allows you to replicate data across multiple AWS regions simultaneously, ensuring that your data is available and consistent across all regions, even if a region goes down. We have seen the syntax, use-case, and some of the important points associated with Global Tables in DynamoDB.

Published on: