Replicating DynamoDB streams feature in RDS

This is probably the most common migration path DynamodDB users have. Almost no one understands DynamoDBs data modelling requirments and query ‘restrictions’ / capabilities before jumping into an all serverless architecture for the sake of it.

Don’t use triggers. Converting a row to json is very slow and expensive in postgres. Your throughput for writes will drastically slow down. We use two replication slots, one that listens to all table in certain schemas changes and writes to an audit table which is in the same postgres table. It basically becomes our audit history for every change. The audit table lives in a second schema so we don’t accidentally have a recursive loop. LSN + ordinal is guaranteed to be unique in postgres. Then the second slot only. We set replica identity to full which gives old and new records. We have a unit test that ensures all tables in those schemas have that enabled.

The second slot only listens to that audit table and publishes those changes to SNS. Postgres also guarantees ordering but SNS doesn’t in the normal.

We added a gin index on old and new state to our audit table which slowed our throughput quite a bit. We’re migrating to three slots so that we have an ingestion table which has one slot. And then two slots off ingestion. One that publishes to sns and another that publishes to the audit table with gin indexes which can lag much further behind without much harm.

RDS we have an alarm on the replication slot lag to ensure no slot is broken and growing in size to where it’s a problem with postgres.

Bonus:
We also added a trigger to ensure no commit changes more than 30k rows as you need consume that many changes. Right now we buffer on disk space but as precaution we limited. Probably could be removed but with the two slot gin design the throughput slowed down. The sns design is like our webhook publisher. We also have each table have a non null changed reason on each table that’s written via a trigger only with a session variable. This way if we forget to set the session variable then the mutation would fail. Without the trigger, an update could forget to set the changed_reason and keep the previous reasons which is wrong. A reason could be like entity created, entity.updated, entity.deleted. You wouldn’t want created sent out twice.

Hey! Saw your thread on the DynamoDB to RDS migration.

The short answer is that RDS for PostgreSQL does have a CDC equivalent, just different plumbing. DynamoDB Streams gives you item-level changes almost magically, but on RDS Postgres the same thing is done via logical replication slots (WAL-based). You enable the rds.logical_replication parameter, create a slot, and then consume the change stream. From there you can fan it out to Lambda via DMS, or use wal2json to get a JSON payload that looks fairly similar to what Streams gives you.

The tricky part is usually deciding what consumes those changes downstream. If you were triggering Lambda off DynamoDB Streams, DMS can replicate that pattern reasonably well. If it was something more custom, a direct logical replication consumer on the WAL slot is more flexible but needs a bit more ops care around slot lag.

Quick question that would change my thinking here: are the downstream consumers mostly event-driven (Lambda, EventBridge) or more like a second database being kept in sync? That shapes whether DMS or a native Postgres pub/sub setup makes more sense for your case.

I do AWS architecture and migration advisory work, so happy to think through the specific pattern with you.

CDC / dms