A Clean Read Replica Routing Pattern for ASP.NET Core

https://hackernoon.imgix.net/images/2jqChkrv03exBUgkLrDzIbfM99q2-oj0221r.jpeg

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