Skip to content

Drizzle re-select on read only replica #1475

@hayes

Description

@hayes

Hey all!

Was going to post an issue but remembered this was here! I've found a solution to my problem, but wanted to post here as info for someone that may stumble upon this as well, and also to get it across the eyes of Hayes if you think something needs to be changed or if you think its good as is!


I encountered an error after performing an insert, which I discovered was related to a read-after-write inconsistency wherein drizzle inserted the record into the primary (write) database instance, and, immediately after, performed a read to return the newly created record from the read replica instance. Because of the replication lag between our (aws rds) instances in the cluster (~10-100ms), the read couldn't find the fresh record as it was not yet in the read replica.This resulted in the following error:

Model foo(foo's id) not found

Screenshot 2025-06-24 at 2 36 28 PM

My solution was just to override the specific drizzle query to use the primary db for both the read and write like so:

await db.$primary // $primary is the only addition
.insert(foo)
.values({
    ...values
})
.returning();

After looking into this some more, we've realized that it's actually likely drizzleWithFieldInput that's causing this extra select, as we have an update mutation that's doing very similar logic, but just with fieldWithInput (which does not make an extra select)...

Originally posted by @MMShep97 in #1439 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions