KatInterview

Spring Boot 3 and Java 21 Drill-Down on a Small REST Service

System DesignModel answer
MediumModel answerSystem Designspring-bootjava-21virtual-threads

Last updated

Defend a small Spring Boot 3 / Java 21 REST service: explain startup, auto-configuration, proxies, modern Java features, and design and concurrency trade-offs.

Imagine you have just finished building a compact REST service with Spring Boot 3 on Java 21. It has a few endpoints, a service layer that holds the business rules, a repository (in memory or JPA), and some unit tests. A reviewer now asks you to explain how it works and why you built it that way. Answer with concrete references to your code, and use short snippets where they help.

You may assume a servlet-based Spring MVC stack. State any other assumption you make.

Part 1: Framework mechanics. Describe what happens between main and the first served request. Cover how the application context is built, how auto-configuration decides which beans exist, how dependency injection wires controller, service and repository, and what proxies and bean scopes mean for annotations such as @Transactional. Explain how you would override or exclude an auto-configured bean.

Part 2: Java 21 in the solution. Choose the Java 21 features that would make the service cleaner or more scalable. Show where each fits and what it replaces. State clearly which features are final in 21, which were finalized earlier, and which are still preview.

Part 3: Defending the design. Justify each layer, describe your test strategy, and explain how the service behaves under concurrent requests. Say what you would change before production, including validation and error handling at the API edge.

Hints

Hint 1

For Part 1, trace the path: `@SpringBootApplication` combines configuration, component scanning and auto-configuration import; then conditions such as `@ConditionalOnMissingBean` filter candidate beans. Then ask what object a caller really holds when a bean carries `@Transactional`.

Hint 2

For Part 2, sort features by release: some were finalized before 21 (records, sealed types, `instanceof` patterns), some are final in 21 (virtual threads, switch patterns, record patterns, sequenced collections), and some are still preview in 21 (string templates, unnamed patterns).

Hint 3

For Part 3, ask what is shared and mutable inside singleton beans, what each test type proves that the others cannot, and what changes when request threads become cheap but the database connection pool stays small.