Quick revision
The short answer
I would make database cost visible and owned: attribute it to services and teams through query tagging and clear ownership of each database, show each team its top queries by cost and the trend after every release, and bring that cost into service reviews and capacity plans. Then I would give teams the tools and expertise to act, with DBA support and training, and set targets such as database cost per request that we review regularly, so efficiency becomes part of normal engineering work rather than an occasional clean-up.
What to remember
In this answer, we built long-term accountability for database cost. We attributed cost to services and teams with per-database ownership, per-service users and query tagging, made efficiency visible with top queries by cost and trends after each release, and brought cost into service reviews and capacity plans. We gave teams tools, DBA support and training to act, and set a small number of targets such as cost per request, reviewed regularly, so efficiency becomes a normal part of engineering.
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 make database cost visible and owned: attribute it to services and teams through query tagging and clear ownership of each database, show each team its top queries by cost and the trend after every release, and bring that cost into service reviews and capacity plans. Then I would give teams the tools and expertise to act, with DBA support and training, and set targets such as database cost per request that we review regularly, so efficiency becomes part of normal engineering work rather than an occasional clean-up.
Why this question is asked
When database spend grows faster than traffic, cost per request is rising, which means inefficiency is accumulating somewhere: queries that got slower as data grew, N+1 patterns, oversized instances bought during an incident and never resized. No single fix solves that, because the cause is spread across many teams who cannot see the cost of what they write. Interviewers ask this to see whether a senior leader can turn a bill into a feedback loop. In this answer, we will see why database cost becomes nobody's problem, then build accountability in five parts: attribution, visibility, reviews and planning, enablement, and targets, with an example of how one quarter might look.
1Why Database Cost Becomes Nobody's Problem
A team that writes a slow query sees a slightly slower endpoint. The cost lands elsewhere: on a shared database cluster whose bill goes to a platform budget, or on a database that four services share. So the people who can fix the query never see the money, and the people who see the money cannot fix the query.
There is also a growth effect. A query that scans a table costs little when the table has 100,000 rows and a lot when it has 100 million. So cost rises faster than traffic even without any new code, simply because data accumulates. Without visibility, the only response anyone has is to buy a bigger instance, which hides the problem for another year.
2Attributing Cost to Services and Teams
Accountability starts with attribution. Every database gets an owning team, recorded in the service catalogue. Shared databases are the hard case, so within them each service connects with its own database user and an application name, and its queries carry a tag such as a SQL comment with the service and endpoint. Database statistics, for example PostgreSQL's pg_stat_statements, which aggregate execution time per user and normalised statement, then let us split a shared cluster's time between the services that use it.
From there, an allocation is simple arithmetic. If service A uses 40 percent of a cluster's execution time and the cluster costs 30,000 per month, A's share is 12,000. The numbers do not need to be exact to change behaviour; they need to be roughly right, consistent and visible.
| Item | How it is attributed |
|---|---|
| Dedicated database | owning team in the service catalogue |
| Shared cluster | per-service database user, share of execution time |
| Individual query | SQL comment with service and endpoint |
| Replicas and backups | allocated in proportion to primary usage |
3Making Query Efficiency Visible
Once cost has an owner, each team needs a view it can act on. A per-service dashboard shows the top statements by total execution time and by estimated cost, statements per request for each endpoint, and the trend of cost per request. Deploys are marked on the charts, so a team can see that the release on Tuesday doubled the cost of one endpoint.
The view should point to action, not just report a number. Each top statement links to its plan, its calls per minute and the endpoint that sends it. In practice, a handful of statements usually account for most of a service's database time, so the list of things to look at is short.
4Bringing Cost Into Reviews and Capacity Plans
Visibility only works if someone looks. Database cost and cost per request become standard items in the service reviews teams already hold, for example the monthly operational review, next to latency and error rates. When a team proposes a new feature that adds a heavy read path, the design review asks what it will cost the database at expected traffic.
Capacity planning changes too. Instead of resizing clusters when they hit a limit, each plan starts from projected traffic multiplied by cost per request. If the projection says a cluster must double in six months, the first question is which queries could be made cheaper, and only then which instance to buy.
5Giving Teams the Tools and Expertise
Asking teams to fix queries they do not know how to fix produces frustration, not savings. So the platform provides tools: query tagging in the shared starter, statement counts in integration tests, a guide for reading EXPLAIN output, and checks that fail a build when a key endpoint's statement count rises. A small database group, DBAs or senior engineers with database depth, runs office hours and pairs with teams on their top queries. Short training on indexes, N+1 patterns, projections and pagination pays back quickly, because the same handful of mistakes causes most of the cost.
A concrete example: a team finds that one search query is 30 percent of their database time. With a DBA, they add a composite index and switch the list from entities to a projection. Execution time drops from 120 ms to 4 ms, and the cluster that was due for an upgrade has room for another year. Sharing that story in an engineering forum does more for the culture than any policy document.
6Setting Targets and Reviewing Them
Finally, targets turn attention into accountability. The most useful is database cost per request, or per business unit such as per order, because it stays meaningful as traffic grows: if traffic doubles and cost doubles, that is fine; if cost triples, something regressed. Each team with significant database spend sets a target for the next quarter, such as reducing cost per request by 15 percent or keeping it flat while traffic grows.
Targets are reviewed regularly, quarterly for most teams, with the data from the dashboards. We celebrate savings and treat regressions as engineering work, not blame. And we keep the targets few: a single cost-per-request number per service is better than ten metrics nobody remembers.
- Attribute costowner per database, tagging per service
- Show top queries and trendsper service, with deploy markers
- Review in service and capacity reviewscost per request next to latency
- Teams act with supportDBA office hours, tooling, training
- Measure against targetscost per request, reviewed quarterly
5 steps in order. Play them, or step through them one at a time.
Each turn of the loop should show cost per request falling or flat while traffic grows.
7Follow-up questions
What if a shared database cannot be split cleanly between teams?
Approximate allocation by execution time share is good enough to start. The goal is that each team sees its trend and its top queries. Over time, heavily used shared databases are often split along service boundaries, which also makes ownership clearer.
How do you stop teams from gaming a cost-per-request target?
Pair it with reliability and latency targets, so cost cannot be saved by dropping features or caching stale data. Reviews look at the actual changes behind the numbers, not only at the numbers.
Is buying a bigger instance ever the right answer?
Yes, when the workload is efficient and the growth is real. The point of accountability is to make that a deliberate choice, made after checking that the top queries are reasonable, instead of the default response to every alert.
8Conclusion
In this answer, we built long-term accountability for database cost. We attributed cost to services and teams with per-database ownership, per-service users and query tagging, made efficiency visible with top queries by cost and trends after each release, and brought cost into service reviews and capacity plans. We gave teams tools, DBA support and training to act, and set a small number of targets such as cost per request, reviewed regularly, so efficiency becomes a normal part of engineering.
Open with
Cost growing faster than traffic means cost per request is rising, and usually the cause is spread across many teams who never see the bill. So I would build a feedback loop.
Then, if they want more
First attribution: every database gets an owning team, and on shared clusters each service uses its own database user and tags its queries with a SQL comment, so pg_stat_statements can split execution time per service and we can allocate cost roughly.
Second visibility: each team gets a dashboard of its top statements by total time and estimated cost, statements per request per endpoint, and cost per request over time with deploy markers, so they can see what a release did.
Third, I would put database cost into the service reviews and capacity plans we already run, next to latency and errors, and make capacity plans start from cost per request times projected traffic rather than from resizing when a limit is hit.
Fourth, enablement: tooling in the shared starter, statement-count checks in tests, DBA office hours and short training on indexes, N+1 and projections, because a few common mistakes cause most of the cost. Fifth, targets: a cost per request target per significant service, reviewed quarterly, paired with latency and reliability so nobody games it.
Sharing concrete wins, like a query going from 120 milliseconds to 4, helps the culture more than a policy.
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 "Database cost is now one of your largest cloud bills, and it grows faster than traffic....": https://scrivio-interview-library.netlify.app/databases/jpa-performance/1522e53711/