PostgreSQL CDC to a Redis-Compatible Cache: 4.8 ms From Commit to Readable
Murtaza Shajapurwala ·
Caching rows from PostgreSQL in a Redis-compatible store usually means one of two compromises:
- Write to both. Every code path that changes a row must also update the cache, and any path that forgets leaves a stale entry.
- Expire and refetch. The cache is wrong for up to one TTL after a change.
Change data capture (CDC) avoids both. PostgreSQL already records every committed change for replication. A pipeline reads that stream and applies each change to the cache, in commit order, without the application doing anything.
KiviDB's Data integration does this for PostgreSQL (logical replication) and MySQL (binary log). We wanted to know how well it works, so we pointed it at a live table and timed it. The study also exposed a slow spot in our own pipeline. We fixed it, redeployed and measured again. Both sets of numbers are below.
The full case study, with the method and every figure, is at kividb.io/case-studies/postgres-cdc.
The setup
- Source: PostgreSQL 16.4 in AWS eu-central-1.
- Logical replication on, TLS required, reachable only from KiviDB Cloud's addresses.
- An
orderstable of 50,000 rows (id, customer, status, total, item count, timestamp), 4.5 MB.
- Target: a free-tier KiviDB Cloud database (100 MB of memory) in the same region.
- Pipeline: one pipeline mapping
public.ordersto one hash per row underorder:{id}. It copies the table first, then streams changes from its own replication slot. - Clock: a single task in the same region, connected to both databases, so source and target are timed by the same clock.
PUT /data-integration/orders
{ "instance_id": "<database>",
"source_type": "postgres",
"source_host": "db.example.com",
"source_database": "shop",
"source_user": "replicator",
"source_tls": "require",
"tables": [{ "table": "public.orders",
"key": "order:{id}", "type": "hash" }] }
After that, HGETALL order:1042 returns the row. The application only ever writes to PostgreSQL.
Test 1: how long one change takes to arrive
We updated one row's status 300 times. Each time, the clock started when COMMIT returned. From then on we read the key in KiviDB once a millisecond until the new value appeared.
| Median | 95th percentile | 99th percentile | |
|---|---|---|---|
| Commit to readable | 4.8 ms | 5.2 ms | 5.8 ms |
That time includes everything:
- PostgreSQL decoding the change;
- the pipeline receiving it and writing it;
- the polling read that saw it.
One KiviDB round trip from the clock task took about 1.6 ms, so the polling itself adds a little to each figure.
Test 2: bursts, where the first run was slow
Single changes were fast from the start. Bursts were not. In the first run, 10,000 inserts moved at about 610 rows a second. That rate was the same whether the inserts were sent as 100 commits of 100 rows or as one transaction of 10,000.
The cause was in our pipeline. Changes have to reach the target in commit order, and the first version kept that order by waiting for each write's reply before sending the next. That is correct, but it costs a full round trip per row.
Waiting is not what keeps the order, though. Writes sent on one connection already arrive in order. So the pipeline now sends all of a transaction's writes together. Before it saves its position in the stream, it waits until every one of them is confirmed. If the pipeline stops partway through, it still resumes from a position whose writes are all known to be applied.
We deployed the change and ran the same tests again:

| Burst of 10,000 inserts | Before | After |
|---|---|---|
| As 100 commits of 100 rows | 606 rows/s | 2,594 rows/s |
| As one transaction | 611 rows/s | 2,674 rows/s |
Each figure is end to end, from the first write in PostgreSQL to the last row readable in KiviDB. That makes bursts about 4.3 times faster. A single change still takes 4.8 ms at the median, and its 95th percentile went from 6.5 ms to 5.2 ms.
Test 3: deletes and the initial copy
The same change sped up the other two slow operations.

| Before | After | |
|---|---|---|
One DELETE of 10,000 rows, until the last key is gone | 14.3 s | 0.77 s |
| Initial copy of the 50,000-row table | ~95 s | ~14 s |
The pipeline reports its progress every few seconds, and we read the copy times from those reports, so they are approximate. The delete times were measured directly.
Test 4: does the copy match?
After all the inserts, updates and deletes, we checked agreement in two ways:
- Counts: both sides held 60,000 rows.
- Fields: 1,000 random rows compared field by field all matched, 1,000 of 1,000.
What happened when the target ran out of memory
During the first run, the free-tier target ran out of memory partway through a burst. That was not planned, but it was useful:
- KiviDB refused the writes, and the pipeline stopped with an error.
- We freed space, and the pipeline carried on from its last saved position.
- The final comparison was an exact match.
The error was the weak part. It reported that a transaction had been discarded, which is accurate but does not tell you what to do. It now says the target database is full and that the pipeline will resume once there is room. That change shipped together with the batching fix.
What this means if you cache PostgreSQL rows
- No dual writes. The application writes only to PostgreSQL. Inserts, updates and deletes reach KiviDB on their own, in order, in a few milliseconds.
- Short staleness. A row in the cache lags the database by milliseconds, not by a TTL.
- Bursts are fine. A bulk update or a large delete arrives at thousands of rows a second, not one round trip per row.
- Failures are recoverable. A stopped pipeline keeps its slot and position and resumes where it stopped. Deleting a pipeline drops its slot, so PostgreSQL stops keeping WAL for it.
Caveats
- Small target. The target was a free-tier database on a shared host. A dedicated database has more memory and its own CPU, so the numbers would differ. The method would not.
- One region. The source, the target and the clock were all in one region. Across regions, each change also pays the network distance between them.
- One table shape. The test used one table with six columns. Wide rows or large JSON columns move more bytes per change.
Pricing
A pipeline costs $0.04 an hour while it runs. The first 10 GB moved each month are included, then each further GB costs $0.10. A pipeline needs credit to start, and it stops when the organization runs out of credit.
Read the full method and every figure at /case-studies/postgres-cdc. Or create a free database, add a pipeline from PostgreSQL or MySQL, and read your rows as keys a few milliseconds after they commit.