Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

Design fleet-wide observability for database access across 100 Java services so that slow queries and N+1 patterns are visible and attributable to a service and endpoint.

I would instrument JDBC in every service through the shared starter, using Micrometer Observation-based instrumentation or the OpenTelemetry agent, so each request's trace carries a span per statement, plus metrics for statement count per request and pool usage. On the database side I would make every query attributable, with a database user and application name per service and SQL comments carrying the endpoint, and collect pg_stat_statements or its equivalent centrally. Dashboards then show query count per request, slowest statements and pool usage per service and endpoint, with alerts on regressions after each deploy.

Why this question is asked

With 100 services, database problems are rarely hard to fix once found; the hard part is finding which service and which endpoint caused them. A DBA sees a slow statement but not who sent it; a team sees slow requests but not which query. Interviewers ask this design question to see whether we can connect both views and make it work for a fleet, not one service. In this answer, we will set the goals, design application-side tracing and metrics, make queries attributable on the database side, collect database statistics centrally, and then build the dashboards and alerts that turn data into action.

1What the System Must Answer

Before choosing tools, we write down the questions. Which endpoint of which service runs the most queries per request, and did that number change with the last deploy? Which statements cost the database the most time, and which service and endpoint send them? Is a service's connection pool saturated, and is it waiting for the database or holding connections idle? Every part of the design should help answer one of these.

There is also a constraint: with 100 services and many teams, the instrumentation must come for free. If each team has to add it by hand, half will not, and the gaps will be exactly where the problems are.

2Application Side: Statement Spans and Per-Request Counts

Every service gets JDBC instrumentation through the shared Spring Boot starter. Two common routes exist: Micrometer Observation-based JDBC instrumentation, for example the datasource-micrometer library, which wraps the DataSource and creates an observation per connection, query and result set; or the OpenTelemetry Java agent, which instruments JDBC without code changes. Either way, each statement becomes a span inside the request's trace, tagged with the service name and the endpoint route.

Spans show individual requests. For fleet-wide trends we also need metrics: a histogram of statements per request per endpoint, which is the N+1 detector, plus HikariCP's pool metrics, which Spring Boot already publishes through Micrometer. Statement text in spans should be the normalised SQL with placeholders, never bound values, to avoid putting personal data into the tracing system.

Java
# shared starter, applied to all 100 services
management.tracing.sampling.probability=0.1 # 10 percent of requests traced in full
management.observations.key-values.team=checkout # set per service
// metric added by the starter: statements per request, per route
jdbc.statements.per.request{service="orders", uri="/orders/recent"} p50=3 p99=4
jdbc.statements.per.request{service="orders", uri="/orders/{id}"} p50=2 p99=2

The second metric is a custom one the starter can record: count statements in a request-scoped counter and publish the total when the request ends. It is cheap and is the most useful single number for catching N+1 across a fleet, because a jump from 3 to 200 is visible on any dashboard.

3Database Side: Making Every Query Attributable

The database sees statements, not services. We give it the missing context in three ways. First, a database user per service, so database statistics that are kept per user, as pg_stat_statements is, attribute cost to a service directly. Second, an application name on every connection, set in the JDBC URL (ApplicationName for PostgreSQL), which appears in pg_stat_activity and in the server log. Third, SQL comments that carry the route and, if wanted, the trace id, following the sqlcommenter convention, so a slow statement in the database log can be tied to an endpoint and a trace.

Text
spring.datasource.url=jdbc:postgresql://db/orders?ApplicationName=orders-service
spring.datasource.username=svc_orders
-- what the database log shows with a comment added by the instrumentation
/* service='orders-service',route='/orders/recent',traceparent='00-4bf9...-01' */
select o.id, o.total from orders o where o.status = $1 order by o.created_at desc limit 100
Step throughFleet database observability
  1. Dashboards and alertsper service and endpoint, deploy markers
  2. Central storagetraces, metrics, database statistics snapshots
  3. Application instrumentationstatement spans, statements per request, pool metrics
  4. Attributionuser per service, ApplicationName, SQL comments
  5. Databasespg_stat_statements, slow query log, pg_stat_activity

5 layers, from the top down. Step through them one at a time.

There is a catch worth knowing. pg_stat_statements groups statements by a fingerprint of the parsed query, which ignores comments, so comments do not split its statistics by route. That is why we need the per-service user for aggregate statistics, and comments for logs, slow query entries and live sessions. Comments with a trace id also make every statement text unique, which can hurt drivers or tools that cache by statement text, so many teams put only the service and route in the comment.

4Collecting Database Statistics Centrally

pg_stat_statements counters are cumulative since the last reset, so a collector takes a snapshot every minute or few minutes and stores the differences in the metrics system, labelled by database, user (that is, service) and query id. Managed databases often provide this through their own performance tools, and the same idea applies to MySQL's performance_schema statement digests. With those snapshots, we can chart database time per service and see the top statements by total time for any period, which links directly to the trace view through the normalised statement text.

5Dashboards and Alerts on Regressions

The dashboards follow the questions. Per service, we show statements per request by endpoint, the slowest statements by mean and total time, and pool usage: active, pending and usage time. Across the fleet, a table ranks services by database time and by change since last week, which is where we spot the next problem early.

Alerts focus on change after deploys, because most regressions arrive with a release. Deploy events are recorded as markers, and an alert fires when an endpoint's p99 statements per request, or a statement's mean time, rises by more than a set ratio within an hour of a deploy, for example 2 times. The alert names the service, the endpoint and the deploy, so the owning team gets a precise lead rather than a vague database is slow message.

Signals and what they catch
SignalSourceCatches
Statements per request by endpointapplication metricN+1 after a release
Statement spans in tracesMicrometer or OpenTelemetrywhich call in a request is slow
Top statements by total time per userpg_stat_statements snapshotsexpensive queries per service
Pool active, pending, usage timeHikariCP metricssaturation, connections held idle
Slow query log with commentsdatabase logsingle slow statements tied to a route

6Follow-up questions

Does tracing every statement cost too much?

Spans per statement add overhead and storage, so we sample traces, for example 10 percent, while metrics such as statements per request are recorded for every request. Metrics are cheap and catch trends; sampled traces explain individual cases.

Why not rely only on the database statistics?

They show which statements are expensive, but not which endpoint sends them or how many times per request. An N+1 pattern of 200 fast statements may not appear near the top of pg_stat_statements by mean time at all, while the per-request count makes it obvious.

How do you roll this out to 100 services?

Through the shared starter and platform defaults, so services get it with a dependency upgrade. The platform team tracks adoption per service, and the database users per service are created by the provisioning tooling.

7Conclusion

In this answer, we designed fleet-wide database observability for 100 services. A shared starter adds JDBC instrumentation with statement spans and a statements-per-request metric, plus pool metrics. A database user per service, an application name and SQL comments make queries attributable on the database side, and central snapshots of pg_stat_statements show cost per service and statement. Dashboards for query count per request, slowest statements and pool usage, and alerts on regressions after deploys, send precise leads to the owning team.

What to say in the interviewAbout 80 seconds aloud

Open with

I would design it from both ends and connect them. In the applications, the shared starter adds JDBC instrumentation for every service, either Micrometer Observation-based, such as datasource-micrometer, or the OpenTelemetry agent, so every request's trace has a span per statement with normalised SQL and no bound values.

Then, if they want more

The starter also records statements per request per endpoint as a metric, which is the best fleet-wide N+1 detector, alongside the Hikari pool metrics Boot already publishes. Traces are sampled; metrics are recorded for every request.

On the database side, I make queries attributable: a database user per service, so pg_stat_statements, which aggregates per user and query, attributes cost to services; ApplicationName in the JDBC URL for live sessions; and SQL comments with service and route for slow query logs. One detail is that pg_stat_statements ignores comments when grouping, which is why the per-service user matters.

A collector snapshots pg_stat_statements centrally every minute. Then dashboards per service show statements per request by endpoint, top statements by total time, and pool usage, plus a fleet ranking by database time. Alerts compare these after each deploy, using deploy markers, and name the service, endpoint and release when something regresses.

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.
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 "Design fleet-wide observability for database access across 100 Java services so that...": https://scrivio-interview-library.netlify.app/databases/jpa-performance/6efe15e176/