Spring Framework Case Studies & Practical Application 2 — Questions and Answers
Question 1: A microservice needs to call three external APIs and aggregate results. Which Spring pattern best handles partial failures gracefully?
- Use @Transactional with rollback on any failure
- Use CompletableFuture with fallback methods via @CircuitBreaker (Correct answer)
- Use @Async on each call and block with get()
- Use RestTemplate with a try-catch around all three calls
Correct answer: Use CompletableFuture with fallback methods via @CircuitBreaker
Combining CompletableFuture with circuit breaker patterns (e.g., Resilience4j) allows each downstream call to fail independently while still returning partial results.
Question 2: A Spring Boot app exposes a REST endpoint that must return data within 200ms. Profiling shows JPA queries are slow. What is the first optimization step?
- Switch from JPA to plain JDBC
- Enable Hibernate second-level cache and query cache
- Add @Cacheable to the service method
- Analyze generated SQL using spring.jpa.show-sql and add missing indexes (Correct answer)
Correct answer: Analyze generated SQL using spring.jpa.show-sql and add missing indexes
Identifying slow queries with show-sql and fixing missing indexes addresses the root cause before introducing caching complexity.
Question 3: You need to process 1 million CSV rows nightly without exhausting memory. Which Spring component is most appropriate?
- Spring MVC with a MultipartFile endpoint
- Spring Batch with chunk-oriented processing (Correct answer)
- A @Scheduled method that reads all rows into a List
- Spring WebFlux with a Flux pipeline
Correct answer: Spring Batch with chunk-oriented processing
Spring Batch's chunk-oriented model reads, processes, and writes fixed-size chunks, keeping memory usage constant regardless of file size.
Question 4: A team reports that their Spring Security configuration allows unauthenticated users to access /admin/** in production but not locally. What is the most likely cause?
- The dev profile has permitAll() on /admin/** and it is active in production (Correct answer)
- HTTPS is not configured in production
- Spring Security is not on the classpath in production
- The CSRF token is missing in production requests
Correct answer: The dev profile has permitAll() on /admin/** and it is active in production
Profile-specific security configurations that use permitAll for convenience are a common cause of accidental production exposure.
Question 5: Your Spring Data JPA repository query returns stale data immediately after an update in the same transaction. What is the most likely reason?
- The EntityManager first-level cache returns the in-memory version before flushing (Correct answer)
- The database has not committed the transaction yet
- Spring Data caches all query results for 30 seconds by default
- The @Modifying query was annotated with @Transactional(readOnly=true)
Correct answer: The EntityManager first-level cache returns the in-memory version before flushing
The JPA persistence context (first-level cache) serves repeated reads from memory, so a derived finder query may return pre-update state until the context is cleared or the transaction ends.
Question 6: An e-commerce app uses Spring Events to send order confirmation emails. During a load test, emails are sent even when the database transaction rolls back. How do you fix this?
- Switch to @Async email sending
- Publish the event inside a @Transactional(propagation=REQUIRES_NEW) method
- Use @TransactionalEventListener with phase=AFTER_COMMIT (Correct answer)
- Add a try-catch around the event publication
Correct answer: Use @TransactionalEventListener with phase=AFTER_COMMIT
@TransactionalEventListener with AFTER_COMMIT phase ensures the listener only fires once the surrounding transaction has successfully committed.
Question 7: A Spring Boot service is deployed to Kubernetes and needs to report readiness separately from liveness. Which actuator endpoints should be mapped?
- /actuator/health for both probes
- /actuator/health/liveness for liveness and /actuator/health/readiness for readiness (Correct answer)
- /actuator/info for liveness and /actuator/health for readiness
- /actuator/metrics for both probes
Correct answer: /actuator/health/liveness for liveness and /actuator/health/readiness for readiness
Spring Boot Actuator exposes dedicated /health/liveness and /health/readiness sub-paths specifically designed for Kubernetes probes.
A microservice needs to call three external APIs and aggregate results.
Which Spring pattern best handles partial failures gracefully?