API performance Level 3 This level · 7 of 10
Hard java backend

Your API p99 went from 200 ms to 2 s. What do you do?

The 30 second answer

First I ask what changed: a deploy, a traffic spike, or a data change. I check whether it is all endpoints or one, all pods or one. Then I open slow traces to see whether time is in the database, our code, or a downstream call. If a deploy caused it, I roll back first and investigate after, because users come before root cause.

Could you say this in an interview?

First, what changed? A deploy, a traffic spike, a data change. I check dashboards: is it all endpoints or one, all pods or one. Then I open slow traces to see if time is in the database, our code, or a downstream call. If it's the database, I check pg_stat_statements and EXPLAIN ANALYZE for a new slow query or a changed plan, plus locks and connection pool wait time. If a deploy caused it, I roll back first, then investigate.

Alternate interview scenarios

"Your Spring Boot API suddenly becomes slow. How would you find the bottleneck?"

This is the same regression triage, so I still start with what changed. In a Spring Boot service I would first split the http.server.requests timer from Actuator by endpoint and status to see whether one route or all of them slowed down. Then I would put it next to the JVM GC pause metrics and the Hikari pending-connection metric before I open traces. If only one endpoint is slow, the cause is usually a query or downstream call. If every endpoint is slow, I look at a shared resource such as the pool, GC or CPU.

"5,000 requests hit your API at the same time. What could become the bottleneck?"

This is a capacity question, not a regression question. I list every bounded resource on the request path, in order, with the limit it is configured to in my service. That means the server's worker threads and accept queue, the database connection pool, the HTTP client pool for each downstream service, hot rows and locks, and CPU. Once a resource is full, extra requests wait in its queue. The usual sign is that throughput flattens while latency keeps climbing. I would load-test at that concurrency, see which limit saturates first, and then cap that queue on purpose with a rate limit or load shedding. Virtual threads remove the worker-thread limit, but database concurrency still has to be capped.

"Your API is slow only during peak traffic. Which metrics would you check?"

I compare peak with off-peak on the golden signals: request rate, error rate, latency percentiles and saturation. That lets me test a few hypotheses. The first is queueing on a saturated resource, so I check busy server threads, pending connections and acquire time in the pool, GC pause time and allocation rate, and CPU throttling. The second is autoscaling lag or cold starts, where new instances are not warmed up yet. The third is a falling cache hit ratio. The fourth is a different traffic mix at peak, such as heavier endpoints or tenants. The last is downstream or database contention, which shows up as lock waits and active connections. Whichever signal rises together with the latency is the one I fix first. Profiling a single request off-peak will not reproduce the problem.

"An ECS service keeps scaling out, but throughput does not improve. How do you investigate?"

If adding tasks does not add throughput, the bottleneck is outside the tasks. So I look at what all tasks share. One candidate is the database: each new task brings its own connection pool, so 10 tasks with 20 connections each can exceed what the database handles well. Others are a downstream service with a fixed quota, a hot partition or lock, and a load balancer target or NAT limit. I also check the scaling signal itself. A target-tracking policy on CPU keeps adding tasks while tasks spin in retries or wait on I/O, and that never fixes the cause. I compare per-task throughput and the shared resource's saturation as task count rises, cap the maximum task count, and scale on a signal tied to the real constraint.

"Your API is slow but CPU usage is low. What could be the reason?"

Low CPU with high latency usually means requests are waiting, not working, so I treat it as a waiting problem. My hypotheses, in order: threads blocked on a slow downstream call or database query; requests queued for a free database connection, visible as threads awaiting a connection in Hikari metrics; lock contention, either synchronized blocks in the JVM or row locks in the database; or a full request thread pool with work waiting in the queue. To tell them apart, I take a few thread dumps a few seconds apart. If most threads sit in socket reads, it's I/O. If they wait in the pool's getConnection, the pool is too small or connections are held too long. If they're BLOCKED on the same monitor, it's a lock. The trace span breakdown confirms which call is eating the time. The fix follows the cause: timeouts and bulkheads on the dependency, shorter transactions or a right-sized pool, or removing the shared lock. Adding CPU would not help any of these.

"A new feature was deployed Friday evening. By Monday morning everything is slow, and nobody touched the code over the weekend. What happened?"

No code changed after Friday, but the Friday code has been running for two days. These are hypotheses until the metrics agree. One: something accumulates, such as a slow memory leak with GC working harder each hour, or connections or threads not returned to the pool. Two: the new feature's data grew, for example a new table or column without an index, or a query whose plan changed as the table grew and statistics went stale. Three: weekend jobs, such as backups, batch imports or vacuum, ran against the new schema. Four: Monday traffic is simply the first real peak the feature has seen. I line up the dashboards from Friday to Monday: heap after GC, pool usage, slowest queries, and latency per endpoint, then compare with the feature flag off. If everything gets slower together, it's a shared resource. If one endpoint does, it's that code path.

Sources (12)
  1. OpenTelemetry: Tracesopentelemetry.io
  2. PostgreSQL 17 docs: pg_stat_statementspostgresql.org
  3. PostgreSQL 17 docs: Using EXPLAINpostgresql.org
  4. Spring Boot: Metrics (HTTP server, JVM, Tomcat and Hikari metrics)docs.spring.io
  5. Spring Boot: Application properties (server thread and accept-queue limits)docs.spring.io
  6. HikariCP configuration (pool size and connection timeout)github.com
  7. Google SRE book: Monitoring distributed systems (four golden signals)sre.google
  8. AWS Builders' Library: Using load shedding to avoid overloadaws.amazon.com
  9. Amazon ECS: Service auto scaling (target tracking)docs.aws.amazon.com
  10. Oracle: jcmd Thread.print (thread dumps)docs.oracle.com
  11. HikariCP: MBean (JMX) monitoringgithub.com
  12. PostgreSQL docs: Routine vacuuming (updating planner statistics)postgresql.org
Previous
Next