简体   繁体   English

如何为PostgreSQL设置AWS RDS的跨区域副本

[英]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. 我在亚洲有一个用于PostgreSQL设置的RDS,并希望在美国有一个读取副本。

But unfortunately just found from the official site that only RDS for MySQL has cross-region replica but not for PostgreSQL. 但遗憾的是,从官方网站上发现,只有RDS for MySQL具有跨区域副本但不适用于PostgreSQL。

And I saw this page introduced other ways to migrate data in to and out of RDS for PostgreSQL. 我看到这个页面介绍了将数据迁移到RDS for PostgreSQL和从RDS迁移出来的其他方法。

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? 如果不是自己在美国购买EC2来安装PostgreSQL,有没有办法将ASIA RDS的数据同步到US RDS?

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 自定义EC2实例

You can create your own EC2 instances and install PostgreSQL so you can customize replication behavior. 您可以创建自己的EC2实例并安装PostgreSQL,以便自定义复制行为。

I've documented configuring master-slave replication with PostgreSQL on my blog: http://thedulinreport.com/2015/01/31/configuring-master-slave-replication-with-postgresql/ 我已经记录了在我的博客上使用PostgreSQL配置主从复制: 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. 当然,您失去了AWS RDS的一些优势,即自动多可用区冗余等,现在突然之间您必须负责维护您的配置。 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 如果您使用的是Java,那么这里介绍一些想法: JDBC - 连接多个数据库

Use SQS to uncouple database writes 使用SQS解除数据库写入的连接

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. 对于所有数据库写入,您应该使用SQS并具有将消息从队列中取出的后台编写器进程。

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 要跨区域在SQS上发布,您可以利用将消息发布到多个队列的SNS配置: 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. 您需要实施一个对帐过程 - 例如,一个简单的过程可以是来自亚洲的pg_dump和每周进入美国的pg_restore,以重新同步它。 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. 另一种方法可以做类似Cassandra读取修复的事情:每10次从美国数据库读取,启动后台进程以对亚洲数据库运行相同的查询,如果它们返回不同的结果,您可以启动进程来重放某些消息。

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. 所以,选择你的战斗:要么你创建自己的EC2实例并获得配置和devops(yuck)的所有权,实现两阶段提交以保证一致性,或放宽一致性要求并使用SQS和异步编写器。

This is now directly supported by RDS . 现在,RDS直接支持此功能。

Example of creating a cross region replica using the CLI: 使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM