A Clean Read Replica Routing Pattern for ASP.NET Core
Every backend I have worked on eventually hits the same problem. There is one endpoint, usually a listing or a report, that reads a lot and returns a lot, and it slowly becomes the reason the database has a bad day. Someone opens the catalog page, the query goes through a few million rows, and every write happening at that moment waits in line behind it.
The textbook answer is boring and correct: send those reads to a replica. You probably already have one, or you can create one in a few minutes on any managed database. The primary handles writes, the replica handles the heavy reads.
The part the textbook skips is the annoying one: how do you route a specific request to the replica without passing a connection object through every method that might touch the database?
I got this wrong a few times before I got it...
Copyright of this story solely belongs to hackernoon.com. To see the full text click HERE