[PR #5887] fix(adpaters/drizzle-adapter) use primary db connection for transactions #14544

Closed
opened 2026-04-13 09:31:30 -05:00 by GiteaMirror · 0 comments
Owner

Original Pull Request: https://github.com/better-auth/better-auth/pull/5887

State: closed
Merged: No


When using drizzle with read replicas, transactions that have both read and write operations seem to use both connections (primary and read-only), resulting in the writes not being reflected on to the read-only instances (before the transaction is committed).

For instance, when using the emailOTP plugin, and users signed up using this method the following would happen within the same drizzle transaction:

  1. The user would be created (primary/writer instance)
  2. Read the newly created user (read replica) > TypeError: null is not an object (evaluating 'user.banned')

I did a workaround in my application using better-auth and the drizzle-adapter, by passing the primary db instance to the drizzleAdapter drizzleAdapter(db.$primary, ...), which fixed the issue, but completely disables using read replicas with better-auth.

This PR should solve the issue, while still allowing for the use of read replicas (when transactions are not used, internally)


Summary by cubic

Run transactions on the primary DB connection in the Drizzle adapter (falling back to the default when no primary exists). This avoids mixed read/write transactions hitting read replicas and missing uncommitted writes (e.g., email OTP user reads returning null).

Written for commit 6146dbda00. Summary will update automatically on new commits.

**Original Pull Request:** https://github.com/better-auth/better-auth/pull/5887 **State:** closed **Merged:** No --- When using drizzle with [read replicas](https://orm.drizzle.team/docs/read-replicas), transactions that have both read and write operations seem to use both connections (primary and read-only), resulting in the writes not being reflected on to the read-only instances (before the transaction is committed). For instance, when using the emailOTP plugin, and users signed up using this method the following would happen within the same drizzle transaction: 1. The user would be created (primary/writer instance) 2. Read the newly created user (read replica) > `TypeError: null is not an object (evaluating 'user.banned')` I did a workaround in my application using better-auth and the drizzle-adapter, by passing the primary db instance to the drizzleAdapter `drizzleAdapter(db.$primary, ...)`, which fixed the issue, but completely disables using read replicas with better-auth. This PR should solve the issue, while still allowing for the use of read replicas (when transactions are not used, internally) <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Run transactions on the primary DB connection in the Drizzle adapter (falling back to the default when no primary exists). This avoids mixed read/write transactions hitting read replicas and missing uncommitted writes (e.g., email OTP user reads returning null). <sup>Written for commit 6146dbda001744ceac88ee5297bc905c5ded8f68. Summary will update automatically on new commits.</sup> <!-- End of auto-generated description by cubic. -->
GiteaMirror added the pull-request label 2026-04-13 09:31:30 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#14544