简体   繁体   English

将 PostgreSQL 通知放入 AWS SQS 队列

[英]Getting PostgreSQL notifications into an AWS SQS Queue

An application that I'm working on uses AFTER CREATE/UPDATE/DELETE triggers to create pg_notify notifications when certain actions occur within the system.我正在处理的应用程序使用AFTER CREATE/UPDATE/DELETE触发器在系统内发生某些操作时创建pg_notify通知。 Currently, we have a small Node.JS application that LISTEN s for the events and then immediately turns around and posts them to an AWS SNS topic, which gets forwarded to our SQS event queue.目前,我们有一个小的Node.js应用LISTEN S为事件,然后马上转身,并将它们发布到AWS SNS的主题,被转发到我们的SQS事件队列。 From that queue, we trigger all sorts of things based on the event (emails, SMSs, lambdas, long running jobs, etc).从该队列中,我们根据事件(电子邮件、短信、lambda、长时间运行的作业等)触发各种事情。

This architecture works well, but the Node.JS application that sits in between the PostgreSQL instance and the SNS topic seems a bit fragile.这种架构运行良好,但位于 PostgreSQL 实例和 SNS 主题之间的 Node.JS 应用程序似乎有点脆弱。 I can't really run two copies in two availability zones, because messages will be duplicated.我真的不能在两个可用区中运行两个副本,因为消息会被复制。

I'm looking for a better way to get these Postgres notifications into SQS.我正在寻找一种更好的方法将这些 Postgres 通知放入 SQS。 Are there any options out there for this?有什么选择吗? If Postgres Aurora has something, we might consider that.如果 Postgres Aurora 有什么东西,我们可能会考虑。

Use your current strategy of a small application that LISTEN s for events.使用LISTEN事件的小型应用程序的当前策略。 Just introduce a deduplication step between that app and your event subscribers.只需在该应用程序和您的事件订阅者之间引入重复数据删除步骤即可。 This will allow you to run several instances of your app.这将允许您运行应用程序的多个实例。

For example, you could use a FIFO SQS queue.例如,您可以使用FIFO SQS 队列。 These automatically drop duplicate messages.这些会自动删除重复的消息。 Since FIFO queues cannot subscribe to SNS, you'd need to put messages directly to the queue instead of through SNS.由于 FIFO 队列无法订阅 SNS,因此您需要将消息直接放入队列而不是通过 SNS。

Alternatively, you could use DynamoDB to store checksums of your recent messages and if your app encounters a duplicate, drop it manually (make sure to use conditional writes to prevent race conditions).或者,您可以使用 DynamoDB 来存储最近消息的校验和,如果您的应用遇到重复消息,请手动删除它(确保使用条件写入以防止竞争条件)。

Some options I've found:我发现的一些选项:

Continue with the current method继续当前方法

I could keep the current small application that's redirecting events from my PostgreSQL RDS and dumping them into SNS->SQS.我可以保留当前从我的 PostgreSQL RDS 重定向事件并将它们转储到 SNS->SQS 的小应用程序。 I can deploy it in a 1 region/max 1/min 1 auto-scaling group to make sure there is not more than copy running at a time.我可以将它部署在 1 个区域/最多 1 个/分钟 1 个自动缩放组中,以确保一次只运行一个副本。

Ditch my RDS and use a self hosted database抛弃我的 RDS 并使用自托管数据库

I could ditch RDS and run PostgreSQL on an EC2 instance and then use PL/Python along with the AWS-SDK to make calls to SNS instead of using pg_notify .我可以放弃 RDS 并在 EC2 实例上运行 PostgreSQL,然后使用 PL/Python 和 AWS-SDK 来调用 SNS 而不是使用pg_notify I don't like this idea, because I lose the ease of use that comes with RDS.我不喜欢这个想法,因为我失去了 RDS 带来的易用性。

For now, I'll be sticking with the current method, unless someone has some other ideas that I could explore.现在,我将坚持使用当前的方法,除非有人有其他一些我可以探索的想法。 I'm sure there will be more options in the future (like when Aurora PostgreSQL adds support for calling Lambdas, like the Aurora MySQL has).我相信未来会有更多选择(比如当 Aurora PostgreSQL 添加对调用 Lambdas 的支持时,就像 Aurora MySQL 那样)。

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

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