Sub topic 5
Replicating data across Regions
Backups are periodic copies. Replication is continuous. It keeps a second copy of your data in the recovery Region, usually seconds or minutes behind the primary. This chapter covers which replication mechanism fits which data store, how each one sets your recovery point objective (RPO), and where each one fails.
This chapter assumes you know the following:
- RTO and RPO.
- The backup and restore strategy.
- Basic S3, RDS/Aurora, and DynamoDB concepts.
- How to read JSON and AWS CLI commands.
Why replication sits beside backups
Backup and restore gives you an RPO equal to the backup interval, often hours. Pilot light, warm standby, and active/active need an RPO of seconds to minutes. Only continuous replication can deliver that.
Replication has one weakness: it copies mistakes faithfully. If a bug deletes rows or an operator drops a table, the deletion replicates to the DR Region within seconds. Replication protects against losing a Region. Backups with point-in-time recovery protect against bad data. A sound design uses both.
Replication is asynchronous
Cross-Region replication in AWS is almost always asynchronous. The primary acknowledges a write before the DR Region has it. The gap between the two is replication lag. In a Region failure, whatever has not yet crossed the gap is lost, so replication lag at the moment of failure is your actual RPO.
Two consequences follow:
- You must measure lag continuously and alarm on it. A typical lag of one second tells you nothing about the worst case during a traffic spike or a network problem.
- Your stated RPO must be at least the worst lag you observe, not the average.
Choosing a mechanism by data store
Each AWS data service has its own replication feature. The table lists the common ones.
| Data store | Mechanism | Typical lag | Replica behavior |
|---|---|---|---|
| S3 | Cross-Region Replication (CRR) | Seconds to minutes. With S3 Replication Time Control (RTC), 99.99% of objects within 15 minutes | Replica bucket is independently writable |
| DynamoDB | Global tables | Usually under a second | Every replica accepts writes (multi-active) |
| Aurora | Global Database | Typically under 1 second | Secondary cluster is read-only until promoted |
| RDS (MySQL, PostgreSQL, MariaDB, Oracle) | Cross-Region read replica | Seconds to minutes, depends on engine and load | Read-only until promoted. Promotion is one-way |
| EBS and EC2 | AWS Elastic Disaster Recovery | Seconds (continuous block-level) | Staging area, then launch recovery instances |
| EFS | EFS Replication | Typically minutes | Read-only replica file system |
| ElastiCache | Global Datastore | Typically under 1 second | Secondary is read-only until promoted |
The sections below cover the three you will use most.
Amazon S3 Cross-Region Replication
CRR copies objects from a source bucket to a bucket in another Region. Both buckets need versioning enabled. S3 needs an IAM role that can read from the source and write to the destination.
Key behaviors:
- Only objects written after you enable the rule are replicated. To copy existing objects, run S3 Batch Replication.
- Delete markers are replicated only if you turn on
DeleteMarkerReplication. Permanent deletes of a specific version are never replicated. This protects the DR copy from some mistakes. - Objects encrypted with SSE-KMS need a KMS key in the destination Region and an explicit setting for it. KMS keys are Regional, so the source key cannot decrypt the replica.
- Replication is not transitive by default. Objects that are themselves replicas are not re-replicated to a third bucket.
This replication configuration turns on RTC and replication metrics:
{
"Role": "arn:aws:iam::111122223333:role/s3-crr-role",
"Rules": [
{
"ID": "dr-all-objects",
"Status": "Enabled",
"Priority": 1,
"Filter": {},
"DeleteMarkerReplication": { "Status": "Disabled" },
"Destination": {
"Bucket": "arn:aws:s3:::app-data-usw2",
"ReplicationTime": { "Status": "Enabled", "Time": { "Minutes": 15 } },
"Metrics": { "Status": "Enabled", "EventThreshold": { "Minutes": 15 } }
}
}
]
}Apply it to the source bucket:
aws s3api put-bucket-replication \
--region us-east-1 \
--bucket app-data-use1 \
--replication-configuration file://replication.jsonWith RTC and metrics on, S3 publishes replication latency and pending-operation metrics to CloudWatch. Alarm on them.
DynamoDB global tables
A global table is a set of replica tables in different Regions that stay in sync. Every replica accepts reads and writes. DynamoDB propagates changes between them, typically within a second.
Because several Regions can write the same item, DynamoDB resolves conflicts with last-writer-wins, based on timestamps. Concurrent writes to the same item in two Regions can silently discard one of them. For DR this rarely matters if only one Region takes writes. It matters a lot for active/active, so design for it, for example by routing each customer to a home Region.
Add a replica to an existing table:
aws dynamodb update-table \
--region us-east-1 \
--table-name orders \
--replica-updates '[{"Create": {"RegionName": "us-west-2"}}]'A global table replicates deletes and bad writes too. Turn on point-in-time recovery on each replica.
Aurora Global Database
An Aurora global database has one primary cluster that accepts writes and up to several secondary clusters in other Regions. Replication happens at the storage layer, not through the database engine, so it adds little load to the primary and usually lags by under a second.
There are two ways to move the primary role:
- Switchover (planned). Use it for tests and maintenance. It waits for the secondary to catch up, so it loses no data. The
aws rds failover-global-clustercommand from the earlier code example does this. - Detach and promote (unplanned). Use it when the primary Region is down. You remove the secondary from the global cluster, which promotes it to a standalone writable cluster. It accepts whatever lag existed at that moment.
# Unplanned: detach the DR cluster so it becomes a standalone writer
aws rds remove-from-global-cluster \
--region us-west-2 \
--global-cluster-identifier app-global \
--db-cluster-identifier arn:aws:rds:us-west-2:111122223333:cluster:app-dr-clusterAfter the detach, the old primary is no longer part of the global cluster. When the original Region returns, you must rebuild it as a new secondary, because the two clusters have diverged. Plan this in your failback runbook.
Watch lag with the AuroraGlobalDBReplicationLag metric, which is reported in milliseconds:
aws cloudwatch get-metric-statistics \
--region us-west-2 \
--namespace AWS/RDS \
--metric-name AuroraGlobalDBReplicationLag \
--dimensions Name=DBClusterIdentifier,Value=app-dr-cluster \
--start-time "$(date -u -d '-1 hour' +%Y-%m-%dT%H:%M:%SZ)" \
--end-time "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
--period 60 \
--statistics MaximumUse the Maximum statistic. An average hides the spikes that define your worst-case RPO.
The date -d syntax above is for GNU date on Linux. On macOS, use date -u -v-1H.
RDS cross-Region read replicas
For RDS engines that are not Aurora, a cross-Region read replica uses the engine's native asynchronous replication. Lag depends on write volume, instance size, and network conditions, and it can grow large during bulk loads. Promoting a replica detaches it permanently from the source. A promoted replica cannot be reattached, so failback means creating a new replica in the other direction.
Common errors and gotchas
- Unencrypted or wrongly keyed replicas. Encrypted replicas need a KMS key in the destination Region. Missing key permissions are a frequent cause of failed replication.
- Assuming lag is constant. Lag grows under heavy writes. Test at peak load.
- Replicating data but not its dependencies. A database copy is useless if the DR Region lacks the secrets, parameters, AMIs, and IAM roles the application needs. Replicate or recreate those too.
- Split brain. If you promote the DR copy while the primary is still partly alive, both can take writes and diverge. Fence off the old primary before you accept writes in the new one.
- Data transfer cost. Cross-Region transfer is billed per GB. High-churn data sets can make replication a visible line item.
- Untested promotion. A replica you have never promoted is an assumption. Include promotion in DR tests.
Matching mechanism to RPO
Use these rules as a starting point:
- RPO of hours: scheduled backups with cross-Region copies are enough.
- RPO of minutes: S3 CRR with RTC, RDS read replicas, EFS replication.
- RPO of about a second or less: Aurora Global Database, DynamoDB global tables, ElastiCache Global Datastore.
- RPO of zero: asynchronous cross-Region replication cannot guarantee it. You need a synchronous design within a Region, or an application-level approach that accepts the latency of confirming writes in two places.
Exercise
Your Aurora global database shows an average replication lag of 200 ms, but the maximum over the last week was 40 seconds during a batch import. The business requires an RPO of 10 seconds. Does the design meet the requirement, and what would you do?
Show answer
It does not meet the requirement. The worst-case lag of 40 seconds exceeds the 10-second RPO, so a Region failure during a batch import could lose up to 40 seconds of writes. Options are to throttle or reschedule the batch import so lag stays under 10 seconds, to alarm on the Maximum lag statistic and pause the import when it nears the threshold, or to revisit the RPO with the business. The average value is irrelevant for this decision.
Exercise
You enable an S3 replication rule on a bucket that already holds 2 million objects. A week later you find the DR bucket holds only objects uploaded after the rule was created. Why, and how do you fix it?
Show answer
S3 replication rules apply only to objects written after the rule is enabled. Use S3 Batch Replication to copy the existing objects to the destination bucket.