Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup cross region replica of AWS RDS for PostgreSQL

I have a RDS for PostgreSQL setup in ASIA and would like to have a read copy in US.

But unfortunately just found from the official site that only RDS for MySQL has cross-region replica but not for PostgreSQL.

And I saw this page introduced other ways to migrate data in to and out of RDS for PostgreSQL.

If not buy an EC2 to install a PostgreSQL by myself in US, is there any way the synchronize data from ASIA RDS to US RDS?

like image 939
Randy Huang Avatar asked Sep 09 '15 10:09

Randy Huang


People also ask

Can AWS RDS Read replica cross-region?

You can now create cross-region read replicas for Amazon RDS database instances! This feature builds upon our existing support for read replicas that reside within the same region as the source database instance.

Can AWS RDS be multi region?

AWS provides you with the flexibility to place instances and store data within multiple geographic regions and across multiple Availability Zones (AZ) within each AWS Region. Amazon RDS for SQL Server provides a Multi-AZ deployment, which replicates data synchronously across AZs.

How many Read replicas can be created from an RDS PostgreSQL primary database?

Amazon RDS for MySQL, MariaDB, PostgreSQL, Oracle, and SQL Server allow you to add up to 5 read replicas to each DB Instance.


2 Answers

It all depends on the purpose of your replication. Is it to provide a local data source and avoid network latencies ?

Assuming that your goal is to have cross-region replication, you have a couple of options.

Custom EC2 Instances

You can create your own EC2 instances and install PostgreSQL so you can customize replication behavior.

I've documented configuring master-slave replication with PostgreSQL on my blog: http://thedulinreport.com/2015/01/31/configuring-master-slave-replication-with-postgresql/

Of course, you lose some of the benefits of AWS RDS, namely automated multi-AZ redundancy, etc., and now all of a sudden you have to be responsible for maintaining your configuration. This is far from perfect.

Two-Phase Commit

Alternate option is to build replication into your application. One approach is to use a database driver that can do this, or to do your own two-phase commit. If you are using Java, some ideas are described here: JDBC - Connect Multiple Databases

Use SQS to uncouple database writes

Ok, so this one is the one I would personally prefer. For all of your database writes you should use SQS and have background writer processes that take messages off the queue.

You will need to have a writer in Asia and a writer in the US regions. To publish on SQS across regions you can utilize SNS configuration that publishes messages onto multiple queues: http://docs.aws.amazon.com/sns/latest/dg/SendMessageToSQS.html

Of course, unlike a two phase commit, this approach is subject to bugs and it is possible for your US database to get out of sync. You will need to implement a reconciliation process -- a simple one can be a pg_dump from Asian and pg_restore into US on a weekly basis to re-sync it, for instance. Another approach can do something like a Cassandra read-repair: every 10 reads out of your US database, spin up a background process to run the same query against Asian database and if they return different results you can kick off a process to replay some messages.

This approach is common, actually, and I've seen it used on Wall St.


So, pick your battle: either you create your own EC2 instances and take ownership of configuration and devops (yuck), implement a two-phase commit that guarantees consistency, or relax consistency requirements and use SQS and asynchronous writers.

like image 152
Oleg Dulin Avatar answered Oct 08 '22 06:10

Oleg Dulin


This is now directly supported by RDS.

Example of creating a cross region replica using the CLI:

aws rds create-db-instance-read-replica \
    --db-instance-identifier DBInstanceIdentifier \
    --region us-west-2 \
    --source-db-instance-identifier arn:aws:rds:us-east-1:123456789012:db:my-postgres-instance
like image 22
ydaetskcoR Avatar answered Oct 08 '22 08:10

ydaetskcoR