Skip to content

fix(db-postgres): stabilize read replicas support#16083

Merged
r1tsuu merged 6 commits intomainfrom
fix/read-replicas-pg
Mar 30, 2026
Merged

fix(db-postgres): stabilize read replicas support#16083
r1tsuu merged 6 commits intomainfrom
fix/read-replicas-pg

Conversation

@r1tsuu
Copy link
Copy Markdown
Member

@r1tsuu r1tsuu commented Mar 28, 2026

Problem

When readReplicas is configured, reads that happen as part of writes (e.g. reading back the row after an INSERT) were incorrectly routed to the replica instead of the primary, causing NotFound errors due to replication lag.

Solution

  • getPrimaryDb - used in upsertRow, updateOne, updateMany to always use the primary DB for internal reads in write operations.
  • readReplicasAfterWriteInterval - time-based read-after-write consistency. Before, if you did for example payload.create and immediately payload.find after - the new document would not show due to replication lag. Defaults to 2000 (ms) but can be configured.

@r1tsuu r1tsuu requested a review from denolfe as a code owner March 28, 2026 10:06
@r1tsuu r1tsuu linked an issue Mar 28, 2026 that may be closed by this pull request
@r1tsuu r1tsuu mentioned this pull request Mar 28, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 28, 2026

📦 esbuild Bundle Analysis for payload

This analysis was generated by esbuild-bundle-analyzer. 🤖
This PR introduced no changes to the esbuild bundle! 🙌

Copy link
Copy Markdown
Contributor

@JarrodMFlesch JarrodMFlesch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sasha and I chatted about the confusing db variables being created and Sasha landed on passing the result of getTransaction directly into getPrimaryDB. This change made it easier to read and I think less error prone going forward. Now there is only 1 db var in each file.

@r1tsuu r1tsuu merged commit 46ddf8d into main Mar 30, 2026
155 checks passed
@r1tsuu r1tsuu deleted the fix/read-replicas-pg branch March 30, 2026 17:45
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 1, 2026

🚀 This is included in version v3.81.0

r1tsuu added a commit that referenced this pull request Apr 10, 2026
…16240)

PR #16083 added `getPrimaryDb`
logic to fix the broken behaviour with the `readReplica` config option
but didn't include it to some other operations like with versions or
jobs. This PR ensures that all the write operations use it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

404 Error - Media Upload + Read Replica Using read replicas: Error: Cannot use 'in' operator to search for '_rels' in undefined

2 participants