I'm designing an API (a WCF nettcp intranet webservice, not a public web api) that will target "AlwaysOn Availability Groups" database. The AlwaysOn is set in Synchronous-commit mode. It was suggested to me that I should use secondary replica for performance purposes. There is a way to target read-only db directly (client's connection string must specify the application intent as "read-only.") to offload workloads from your primary replica. So I was thinking... is there really a design pattern handle this situation?
We're talking about two connection strings (read-write and read-only ) for theoretically one database. Simple rule is: API should targtet primary for writes and secondary for reads, but life is not that simple.
For example: If a client calls DeleteItem
and immediately after GetAllItems
, even in sync-mode, it might get all items including the deleted one because the read-only db didn't have enough time to update (sometimes data is not yet available for read, we're talking about milliseconds. I tested it). Because API is the one who decides which connection string to use, it will use read-write for DeleteItem
operation, and then read-only for GetAllItems
operation.
So my question is how should I handle active secondary database?
Should I let API to decide whether to use read-only db to offload worload?
Should API's clietns have an option to decide explicitly? Like, "Hi API, I want all items, but use read-only connection string!"?
Is there a desing pattern, how to handle this situation?
Thanks for all the help,