Quick revision
The short answer
I would offer one supported routing mechanism, a shared Spring starter or a proxy layer, with lag-aware routing and automatic fallback to the primary, and require every service to declare which consistency each kind of read needs, defaulting to the primary when unsure. Replicas are the right tool for read-heavy work that tolerates some lag; they do not help write throughput or heavy analytical queries, and caching, fixing queries or a separate read model are often cheaper. Lag and routing metrics become part of the standard dashboards.
What to remember
In this answer, we saw that home-made replica routing fails in the same ways across teams, so the platform offers one supported mechanism with lag-aware routing and primary fallback. Teams classify their reads by required consistency, with the primary as the default when unsure. We say no to replicas for write throughput and heavy analytics, prefer caching, query fixes or read models when they are cheaper, and make lag and routing metrics part of the standard dashboards.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would offer one supported routing mechanism, a shared Spring starter or a proxy layer, with lag-aware routing and automatic fallback to the primary, and require every service to declare which consistency each kind of read needs, defaulting to the primary when unsure. Replicas are the right tool for read-heavy work that tolerates some lag; they do not help write throughput or heavy analytical queries, and caching, fixing queries or a separate read model are often cheaper. Lag and routing metrics become part of the standard dashboards.
Why this question is asked
When several teams solve the same problem independently, the organisation pays several times: in duplicated code, in different bugs, and in incidents that the platform team cannot help with because every setup is different. Replica routing is a classic case, because it looks simple and hides real traps such as early connection binding, lag and fallback. Interviewers ask this at a senior level to see whether we can set a standard that people actually adopt, and whether we know when to say no to a popular tool. In this answer, we will look at what goes wrong with home-made routing, define the standard, set consistency defaults, decide when replicas are the wrong tool, and make the results visible.
1What Goes Wrong With Home-Made Routing
Each team's version usually works in the demo and fails in one of the same few ways. Routing by the read-only flag silently sends everything to the primary because the connection is fetched before the flag is set, unless the data source is wrapped in a LazyConnectionDataSourceProxy. Reads go to a replica that is minutes behind because nobody checks lag. A replica outage takes the service down because there is no fallback. And when something breaks, nobody outside the team can tell where queries were going, because there are no metrics.
A platform standard is valuable precisely because these traps are the same for every team. Solving them once, correctly, and making the solution easy to adopt is cheaper than reviewing ten different implementations.
2One Supported Routing Mechanism
There are two reasonable shapes. A shared Spring Boot starter puts the routing inside each service: a routing data source behind a lazy connection proxy, routing by the transaction's read-only flag or an explicit annotation, a health check per replica with lag measurement, and fallback to the primary. A proxy layer, such as Pgpool-II or a cloud provider's reader endpoint, routes outside the application, which helps when services use different languages, but it knows less about which reads need fresh data.
For a fleet of Spring services on PostgreSQL, I would usually start with the starter, because routing decisions depend on application knowledge, and keep the configuration small: replica URLs, a maximum acceptable lag and a switch to disable replicas entirely during an incident.
- Service code
@Transactional(readOnly = true)or@ReadFrom(PRIMARY) - Platform starterrouting, consistency defaults, metrics
- Lazy connection proxyroute after the transaction flags are set
- Replica health and lag checksdrop replicas over the lag limit
- Primary and replicasPostgreSQL streaming replication
5 layers, from the top down. Step through them one at a time.
3Consistency Defaults
The hardest part of replica routing is not technical; it is knowing which reads can tolerate stale data. So the standard asks teams to classify reads. A read that must reflect the latest committed state, such as a balance or a permission, is primary-only. A read that must reflect the user's own writes uses read-your-writes routing, for example a short primary window after a write. A read that tolerates a few seconds of lag, such as a catalogue or history page, can use any healthy replica.
And the default is the primary. A read only goes to a replica when someone has decided that it may. That way a team that adopts the starter without thinking gets correctness first and the performance benefit when they opt in, rather than subtle bugs.
4When Replicas Are the Wrong Tool
Part of the standard is telling teams when not to use it. Replicas copy every write, so they do nothing for write throughput: a primary that is saturated by writes needs batching, partitioning or sharding, not readers. Replicas also do not suit heavy analytical queries. A 20-minute report on a hot standby can conflict with replication, so either it gets cancelled or, with hot_standby_feedback, it holds back cleanup on the primary and causes bloat; such work belongs in a warehouse or a separate analytical store fed by change data capture.
And sometimes there is a cheaper fix. A read path that hits the database 50 times per request needs a better query, not a replica. A small set of very hot keys needs a cache. A complex search page may need a dedicated read model, a projection built for that query, rather than more copies of the transactional schema.
| Situation | Better answer |
|---|---|
| Many lag-tolerant reads, primary CPU high | Replicas through the standard starter |
| Primary saturated by writes | Batching, partitioning, sharding |
| Long analytical reports | Warehouse or analytical store via CDC |
| A few very hot rows | Cache with clear invalidation |
| N+1 queries or missing index | Fix the query first |
| Complex search screen | Dedicated read model or search index |
5Making Routing Visible
The starter publishes the same metrics in every service: queries per data source, routing decisions and fallbacks, and lag per replica as the service sees it. These go into the standard dashboards next to the database's own metrics. With that in place, an incident review can answer basic questions in minutes, such as whether reads moved to the primary when a replica lagged, and the platform team can see which services benefit from replicas and which do not.
Finally, I would set a migration path rather than a deadline. Teams with home-made routing move to the starter when they next touch that code or when their setup causes an incident, and the platform team helps with the first few migrations so the standard proves itself.
6Follow-up questions
Would you ever allow a team to keep its own routing?
Yes, if it has a real need the standard does not cover and it publishes the same metrics. The goal is fewer unknown setups, not uniformity for its own sake. If several teams have the same unmet need, that is a signal to extend the standard.
How do you stop teams from routing everything to replicas to save cost?
The default is the primary, and moving a read to a replica requires a declared consistency class that reviewers can see. Dashboards also show stale-read incidents by service, which makes the trade-off visible.
How many replicas should a database have?
Enough to carry the lag-tolerant read load with one replica down, plus headroom. More replicas add WAL sender work on the primary and cost money, so we size from measured read load, not habit.
7Conclusion
In this answer, we saw that home-made replica routing fails in the same ways across teams, so the platform offers one supported mechanism with lag-aware routing and primary fallback. Teams classify their reads by required consistency, with the primary as the default when unsure. We say no to replicas for write throughput and heavy analytics, prefer caching, query fixes or read models when they are cheaper, and make lag and routing metrics part of the standard dashboards.
Open with
I would replace the different implementations with one supported mechanism, most likely a shared Spring Boot starter, because routing needs application knowledge. It would contain a routing data source behind a LazyConnectionDataSourceProxy so the read-only flag is actually seen, replica health and lag checks, automatic fallback to the primary, and a switch to turn replicas off in an incident.
Then, if they want more
The standard would require each read path to declare its consistency need: primary-only for things like balances and permissions, read-your-writes for a user's own edits, and lag-tolerant for catalogue or history pages. The default is the primary, so a team that does nothing gets correct results. I would also be explicit about when replicas are the wrong tool.
They do not help write throughput, they are a poor home for long analytical queries, which belong in a separate store, and often a query fix, a cache or a read model is cheaper than another replica. Every service using the starter publishes queries per data source, fallbacks and lag in the standard dashboards.
Teams migrate when they next touch that code, with help from the platform team, and I would extend the standard when several teams show the same unmet need.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Your organisation runs many Spring services on PostgreSQL. Several teams have built...": https://scrivio-interview-library.netlify.app/databases/database-scaling/6b2b460b81/