armantik.com / lab / java

Senior Java Backend Guide

243 questions a Java backend engineer gets asked, from language fundamentals and concurrency to Spring, databases, Kafka, Kubernetes and production troubleshooting. Topics run in learning order, and inside each topic the Basic questions come first, then the Advanced ones. Each question has a short answer, the full explanation, code, follow-up questions and the signs of a weak answer.

Basic and Advanced say how deep the topic goes, not how long the answer is: every answer is written for a senior interview. In your first years as a developer? Read the short answer first, and open the rest when you need it.

Four answers are easier to draw than to say, so they are drawn by hand at the end: thread pools, Spring proxies, garbage collection and connection pools.

243 questions22 topics139 basic, 104 advanced

  1. Loading questions…

Four answers worth drawing

Sketched the way you would draw them in an interview. Each board goes with one question above, and its numbers are the answer's own.

1. How a thread pool grows #60

Say: core threads first, then the queue, then extra threads up to max, then reject.

ThreadPoolExecutor: core 4, max 8, a queue of 10tasks1core 42queue of 10if all 4 are busy3up to max 8if the queue is full4reject: 503or CallerRunsan unbounded LinkedBlockingQueue never fills:steps 3 and 4 never happen, max 8 is ignoredunder a spike, latency grows first, then the heapa bounded queue and a chosen rejection policy = backpressure
  1. With fewer than 4 threads running, a new task gets a new core thread.
  2. When all 4 are busy, the task waits in the queue of 10.
  3. Only when the queue is full does the pool start extra threads, up to max 8.
  4. When max is reached too, the task is rejected: map it to a 503, or let CallerRunsPolicy slow the producer.

The catch: an unbounded LinkedBlockingQueue, as in newFixedThreadPool, never fills, so max is ignored and overload shows up as latency and then heap. Spring's default task executor has an unbounded queue too.

2. Why this.saveAudit() skips the transaction #102

Say: Spring injects a proxy, and only calls that come in through it get the transaction.

callerproxybegin atransaction,call, commitAuditServicesave()@Transactional saveAudit()12this.saveAudit(): a plain Java call,it never goes through the proxyonly calls that come in through the proxy are advisedsaveAudit runs without a new transaction, and no error says sofix: move saveAudit to its own bean, and call that bean@Async has the same trap: it runs on the caller’s thread
  1. The caller holds the proxy, not the real AuditService.
  2. The proxy begins a transaction, calls the real save(), then commits.
  3. Inside, this.saveAudit() is a plain Java call on the real object. It never passes the proxy, so its @Transactional does nothing.

The catch: there is no error, only wrong behavior, and @Async fails the same way by running on the caller's thread. Move the method to its own bean.

3. Generational garbage collection #73

Say: most objects die young, so collect the young space often and cheaply.

the heap, split by ageeden: every new objectS0S1oldminor GC: copy the 2 survivors,free all of eden at oncesurvived many: promotedmost objects die young: request DTOs, JSON buffersParallel: best throughput, longer stop the world pausesG1: regions, aims at a 200 ms pause goal by defaultZGC: works alongside your threads, pauses under 1 ms
  1. Every new object goes into eden. Most are request DTOs and buffers that are dead a moment later.
  2. When eden fills, a minor GC copies the few survivors to a survivor space and frees all of eden at once, so its cost depends on what lives, not on the garbage.
  3. Objects that survive many minor collections are promoted to the old generation, which is collected less often.

The catch: pick the collector for the goal. Parallel gives throughput with longer pauses, G1 aims at a pause goal (200 ms by default), and ZGC keeps pauses under 1 ms at the cost of CPU and memory headroom.

4. Size the connection pool from the database #78

Say: the database's useful parallelism sets the budget, not the number of web threads.

200 threadspool 10200 threadspool 10200 threadspool 10200 threadspool 10200 threadspool 10200 threadspool 106 replicasPostgreSQLmax_connections 1006 × 10 = 60 connections1,000 tx/s × 10 ms= about 10 busyLittle’s lawnot 6 × 200 = 1,200threads wait a moment in the pool, not inside the database
  1. By Little's law, 1,000 transactions a second at 10 ms each keep about 10 connections busy.
  2. Six replicas with a pool of 10 each open 60 connections, under PostgreSQL's default of 100.
  3. A pool sized like the threads, 200 each, would ask for 1,200. The extra threads should wait briefly in the pool, not inside the database.

The catch: hold time is the whole transaction. An HTTP call inside @Transactional multiplies the pool you need, and a rolling deploy briefly adds a seventh replica's connections.