Scenario Based Questions

Your app fails at startup with "Failed to configure a DataSource". What do you do?

spring-boot-starter-data-jpa is on the classpath, so Boot tries to create a DataSource, but no spring.datasource.url is set and no embedded DB is present. Fix: add the URL/credentials (check the active profile is the one holding them), add H2 for local runs, or exclude DataSourceAutoConfiguration if the service truly has no database.

Your bean is not getting injected (NoSuchBeanDefinitionException). Why?

  • The class is outside the main class's package, so component scan misses it.
  • It is missing @Component/@Service, or its @Profile/@Conditional does not match.
  • You created the object with new, so Spring never manages it.
  • You inject the implementation class but only a proxy of the interface exists (JDK proxy).

A customer was charged twice for one order. How do you prevent it?

  1. Client sends an Idempotency-Key (UUID) with POST /api/payments.
  2. Server stores the key with the result in a table with a unique constraint; a repeated key returns the stored response instead of charging again.
  3. Also verify payment gateway webhooks by signature and process each event id only once.

Two users buy the last item at the same time and both succeed. How do you fix it?

It is a race condition (lost update). Options: optimistic locking with @Version on Product (one transaction fails with ObjectOptimisticLockingFailureException and you retry or return 409), an atomic SQL update UPDATE product SET stock = stock - :qty WHERE id = :id AND stock >= :qty and check the updated row count, or a pessimistic lock (SELECT ... FOR UPDATE) for very hot items.

An API that used to take 200 ms now takes 5 s in production. How do you investigate?

  1. Check metrics (Actuator/Prometheus/Grafana): latency per endpoint, DB pool usage, GC, CPU.
  2. Look at traces / logs with SQL logging: often an N+1 query or a missing index on a grown table.
  3. Check HikariCP "connection is not available" timeouts (pool exhausted by slow queries or a leak).
  4. Check slow downstream calls (payment API) and ensure timeouts and circuit breakers are set.
  5. Take a thread dump (/actuator/threaddump) to see where threads are blocked.

How do you handle a slow third-party API call in a request?

Always set connect/read timeouts on RestClient/WebClient (the default may wait for a long time). Add retries with backoff only for idempotent calls, a circuit breaker with a fallback (Resilience4j), and move non-essential calls (SMS, email) to @Async or a queue so the user does not wait.

How do you upload a large file without running out of memory?

Accept MultipartFile, set spring.servlet.multipart.max-file-size, and stream it (file.getInputStream()) directly to object storage (S3/GCS) instead of loading bytes into memory. Validate content type and size, generate your own file name, and store only the URL/key in the database.

How would you deploy a Spring Boot app with zero downtime?

Package as a Docker image (layered JAR), run at least two instances behind a load balancer, and do a rolling or blue-green deployment. Use readiness probes (/actuator/health/readiness) so traffic arrives only when the app is ready, and server.shutdown=graceful so in-flight requests finish. Database migrations (Flyway) must be backward-compatible with the old version.

How do you trace one request across logs?

Add a correlation id: a filter reads X-Request-Id (or generates one), puts it in the SLF4J MDC, and the log pattern prints it; return it in the response header. With Micrometer Tracing the trace id/span id is added automatically and propagated to downstream services.