LS·198
Phase 2 — Java and Backend Foundation
The second phase: the Java language, object-oriented design, collections, generics, exceptions, the JVM, concurrency, build tools and HTTP, ending with a REST service built without a framework; why this order, an entry check, step-by-step exit checks mapped to Volume 3, the project with milestones and definitions of done, traps, a time budget, and a lab that serves the same API with three executors.
Start here: Phase 2 in one page
Phase 2 turns general programming skill into professional backend skill in one language. The roadmap uses Java because it dominates enterprise backends and has a rich, well-documented runtime; the habits transfer to Kotlin, C#, Go and others. The phase ends with a deliberately framework-free project: a REST service without Spring. Building routing, JSON handling, validation and concurrency by hand once is the fastest way to understand what a framework does for you, and what it hides.
The sequence
Java → OOP → Collections → Generics → Exceptions → JVM → Concurrency → Build tools → HTTP
Language basics come first; then the modelling tools (classes, interfaces, collections, generics, error handling); then the runtime that executes them (JVM, memory, garbage collection); then concurrency, which needs both; then the tools that build and ship code; and finally HTTP, the protocol your service speaks.
A taste of the project
The lab served the same small /skips API, built on the JDK's own HttpServer, with each request waiting 20 ms (like a database call), under 100 concurrent connections:
| Executor | Requests/s | Average latency |
|---|---|---|
| the default (one thread) | 48 | 697 ms |
| a pool of 16 threads | 780 | 127 ms |
| virtual threads (Java 21) | 1,534 | 65 ms |
One line of configuration changed throughput 32-fold. The arithmetic is Phase 2 material: one thread × (1 / 0.02 s) = 50 requests per second; 16 threads = 800. Knowing that arithmetic is what lets you size a thread pool, a connection pool or a server fleet later.
How deep to go in Phase 2
Volume 17 defines six mastery levels; Phase 2's exit targets are:
| Topic | Exit level | What that means here |
|---|---|---|
| Java language and OOP | L2–L3 | write idiomatic, tested Java; explain dispatch, records, sealed types |
| collections and generics | L3 | choose collections by cost; explain erasure, bounds, variance |
| exceptions | L2 | design error handling and propagation |
| JVM | L2–L3 | explain class loading, heap, GC basics, JIT at a high level |
| concurrency | L2–L3 | use executors, locks, atomics, virtual threads; explain races |
| build tools | L2 | build, test and package with Maven or Gradle |
| HTTP | L3 | methods, status codes, headers, caching, idempotency |
The sequence, step by step
| Step | Learn | Read | Exit check |
|---|---|---|---|
| 1 | syntax, types, records, control flow, testing with JUnit | Vol. 3, ch. 33 | solve 20 small exercises with tests |
| 2 | classes, interfaces, composition, sealed types, alternatives to OOP | Vol. 3, ch. 34, 37 | model a domain (orders, skips, plans) with tests |
| 3 | lists, maps, sets, queues; costs; equals/hashCode | Vol. 3, ch. 35 | pick and justify collections for five scenarios; benchmark two |
| 4 | generics: bounds, wildcards, erasure | Vol. 3, ch. 36 | write a small generic repository interface |
| 5 | exceptions and error design | Vol. 3, ch. 33–34 | an error-handling policy for your service |
| 6 | JVM internals, memory and performance | Vol. 3, ch. 40–41 | read a GC log and explain a pause |
| 7 | threads, executors, locks, atomics, virtual threads | Vol. 3, ch. 38–39 | reproduce and fix a race condition |
| 8 | Maven or Gradle, modules, dependency conflicts | Vol. 3, ch. 42–43 | build a multi-module project with tests in CI |
| 9 | HTTP and API design | Vol. 3, ch. 44–45 | design the project's API (resources, codes, errors) on paper |
Entry check
Phase 1's exit criteria, or equivalent: comfortable programming in some language, data-structure costs, processes and threads, sockets and HTTP basics.
The phase project: a REST service without Spring
Goal. A small but complete HTTP JSON API (e.g. TiffinBox skips and subscriptions) using only the JDK (com.sun.net.httpserver or raw sockets), a JSON library if you wish, and JDBC for storage.
Milestones. Routing by method and path → JSON parsing and serialisation → validation with clear 400 errors → resource design (201 + Location, 404, 409) → storage behind a repository interface (in-memory first, then PostgreSQL via JDBC) → an executor choice backed by a load test → graceful shutdown → tests at unit and HTTP level → build and run with one command.
Done when. The API passes an HTTP-level test suite, including malformed bodies, unknown routes and concurrent creates; a load test shows the throughput predicted by your executor arithmetic; shutdown finishes in-flight requests; and a README explains each design choice in a sentence.
Stretch. Add ETags and conditional requests, or rewrite the server on an NIO selector.
Common traps
- Starting with Spring. You'll configure what you don't understand; do this project first.
- One thread for everything. The default executor serialises all requests (the lab: 48 req/s).
- Swallowing exceptions or returning
500for client mistakes. - Mutable shared state without synchronisation, which tests rarely catch.
- Over-designing class hierarchies; prefer composition, records and small interfaces.
Exit criteria
- [ ] The project runs, passes its tests and has a measured load test.
- [ ] I can explain
equals/hashCode, erasure, and the difference between a platform and a virtual thread. - [ ] I can read a simple GC log and a thread dump.
- [ ] I can predict a service's throughput from handler time and concurrency.
- [ ] I scored at least 80% on 40 mixed questions from Volume 3's chapters 33–45 quizzes, twice.
Time budget
Volume 3's chapters 33–45 are roughly 95,000 words (about 7 hours of reading); at four hours of study per reading hour that's about 28 hours, plus 2–3 weeks for the project: about 2 months at 8 hours a week.
Lab: one API, three executors
Setup. OpenJDK 21 on the lab machine (2 CPUs); a single-file service on the JDK's com.sun.net.httpserver.HttpServer with a /skips resource: GET returns a count, POST validates a body of the form {"day": "YYYY-MM-DD"} and returns 201 Created with a Location header, or 400 with a JSON error. Every request sleeps 20 ms to stand in for a database call. wrk with 100 connections for 6 s against GET /skips.
Experiment 1: the executor decides throughput
setExecutor(…) |
Requests/s | Avg latency | Max latency | Predicted ceiling |
|---|---|---|---|---|
| not set (the server's own single thread) | 47.9 | 697 ms | 4.83 s | 1 / 0.02 = 50 |
newFixedThreadPool(16) |
779.6 | 127 ms | 189 ms | 16 / 0.02 = 800 |
newVirtualThreadPerTaskExecutor() |
1,534.0 | 65 ms | 97 ms | 100 / 0.02 = 5,000 |
The first two matched their predictions within 5%. Virtual threads removed the thread limit, so the next bottleneck took over: the server's single dispatcher thread accepting and parsing connections, and the 2 CPUs shared with wrk. Finding "what limits throughput now?" after each fix is the habit this lab is meant to build. Little's law checks the latencies: 100 connections / 780 req/s ≈ 128 ms, as measured.
Experiment 2: validation at the edge
POST {"day": "2026-11-10"} -> HTTP 201 {"id":1} Location: /skips/1
POST {"day": "tuesday"} -> HTTP 400 {"error":"body must be {\"day\": \"YYYY-MM-DD\"}"}
POST not json -> HTTP 400 (same message)
The service rejects bad input before doing any work, with a message a client developer can act on, and never a stack trace. (The lab validates with a regular expression to stay dependency-free; a real project should parse JSON with a library and validate the parsed object, since regexes can't handle general JSON.)
Try it yourself
- Predict, then measure, a 32-thread pool.
- Replace
Thread.sleep(20)with a real JDBC query against PostgreSQL and a 10-connection pool; which limit applies now? - Send 1,000 concurrent
POSTs and check that every ID is unique (what makesAtomicIntegersafe here?). - Add graceful shutdown: stop accepting, finish in-flight requests, then exit; prove it with a load test running during shutdown.
Revision sheet: Phase 2, Java and backend foundation
Key ideas
- Sequence: Java → OOP → collections → generics → exceptions → JVM → concurrency → build tools → HTTP.
- The project is a REST service without Spring, to learn what frameworks do.
- Throughput ≈ concurrency ÷ time per request (until the next bottleneck); Little's law checks latency.
- Validate at the edge; map errors to correct HTTP status codes with safe messages.
- Exit at L2–L3: build, test and explain; deep JVM debugging comes later.
Numbers to remember
- Lab: 20 ms handler: 1 thread 48 req/s; 16 threads 780; virtual threads 1,534 (next bottleneck: dispatcher and CPU).
- Budget: ~28 study hours + 2–3 project weeks ≈ 2 months at 8 h/week.
Pitfalls
- Learning Spring before HTTP and threads.
- The default single-thread executor in production.
500for client errors; stack traces in responses.- Shared mutable state without synchronisation.
Glossary
- Executor: a service that runs tasks on threads.
- Virtual thread: a lightweight JVM-scheduled thread.
- Type erasure: generics checked at compile time and removed at run time.
- Idempotent method: one whose repeated calls have the same effect (GET, PUT, DELETE).
- Location header: the URI of a newly created resource.