Spring Boot services · Chapter 41 of 41
Interview: data and web
By the end of this chapter you can find and fix the data and web bugs that cost real money: N+1 queries, lazy loading errors, lost audit entries, double orders and wasted sessions. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The interview
- 5 tasks
- Spring Boot
- In the all labs plan
The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.
Why you need this
The last round. "Spring core you know," says Dev. "Now the questions that cost money." He opens the orders API with five bug reports. Meera: the station report takes a minute at 10,000 orders. Anjali: GET /orders/7 answers 500. Ravi: the audit log never shows a rejected order, and when the network drops, the counter's retry charges the customer twice. Kabir: the server holds thousands of HTTP sessions nobody uses.
Each works in a demo with three orders and one counter, and fails with real data, real networks and real load.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1 · interview question
Interview: fix the N+1 queries in the station report
Make GET /reports/stations run at most two SQL statements however many orders there are, and still answer the orders and the cups (the sum of the line quantities) per station, by station code: with 4 orders at SBC and 2 at NDLS, each of 1 Masala chai and 2 Cutting chai, it answers NDLS (2 orders, 6 cups) then SBC (4 orders, 12 cups).
Be ready to explain where the extra statements came from, and why FetchType.EAGER is not a fix.
- Task 2 · interview question
Interview: fix the LazyInitializationException on GET /orders/{id}
Make GET /orders/{id} answer 200 with the order and its lines instead of a 500 with a LazyInitializationException: CSMT with 2 Masala chai and 1 Cutting chai answers station CSMT, total 105, and two lines, Masala chai first (quantity 2), then Cutting chai (quantity 1). An id that does not exist still answers 404.
Open-in-view is off in this lab. Be ready to explain why reading the lines failed.
- Task 3 · interview question
Interview: keep the audit entry of a rejected order with the right propagation
Make a rejected order roll back whole while its audit entry is kept: SBC with 2 Masala chai and 5 Bun maska (3 left) answers 409, leaves Masala chai at 10 and no order, and the audit log has an entry starting "REJECTED SBC". A placed order still gets its entry starting "PLACED NDLS".
Be ready to explain why the entry was rolled back, and what decides which transaction a method joins.
- Task 4 · interview question
Interview: make POST /orders idempotent with an Idempotency-Key
Make OrderService.place keep the Idempotency-Key with the order. A new key places the order as now, stored with its key and the request, and returns Placed(view, false). The same key with the same request places nothing and takes no stock: it returns the first order as Placed(view, true), so POST /orders answers 201 with the same id, total and Location and the header Idempotent-Replayed: true (the first answer has no such header). The same key with a different request throws IdempotencyConflictException: 422.
A different key places a new order, and a request with no key answers 400. Be ready to explain what makes a retry safe.
- Task 5 · interview question
Interview: make the API stateless
Make the API create no HTTP session at all: not for GET /reports/stations with good HTTP Basic credentials (200), not for one with none (401) or with a wrong password (401), and no JSESSIONID cookie.
Be ready to explain why a refused request created a session, and why sessions hurt behind a load balancer.
What you will learn
- N+1 and lazy loading: one query per order, and a lazy list read after its transaction closed.
- Propagation: a write that must survive its caller's rollback.
- Idempotency keys: a retried POST that places nothing new.
- Stateless APIs: no session for a client that sends credentials every time.
More in The interview
Build "Interview: data and web" yourself
Tapri Cloud: the counters’ Java grows into a Spring Boot service with a database. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.