<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kamen</title>
    <description>The latest articles on DEV Community by Kamen (@kamenivanov).</description>
    <link>https://dev.to/kamenivanov</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4043234%2Fc0c83e8a-752a-435b-b342-4a44ed85593a.png</url>
      <title>DEV Community: Kamen</title>
      <link>https://dev.to/kamenivanov</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kamenivanov"/>
    <language>en</language>
    <item>
      <title>Generated by an Application That Does Nothing (Chapter 16)</title>
      <dc:creator>Kamen</dc:creator>
      <pubDate>Mon, 23 Nov 2026 08:33:01 +0000</pubDate>
      <link>https://dev.to/kamenivanov/generated-by-an-application-that-does-nothing-chapter-16-1c05</link>
      <guid>https://dev.to/kamenivanov/generated-by-an-application-that-does-nothing-chapter-16-1c05</guid>
      <description>&lt;p&gt;The jar from the previous chapter is useless to two of the three people who need it.&lt;/p&gt;

&lt;p&gt;It describes every type this API will send and accept, it is versioned on its own, and it is published where anything can depend on it. It is also Java, and the developer building the screens writes TypeScript while the engineer writing the tests uses whatever her tooling speaks. Neither of them can read a jar, and neither should have to.&lt;/p&gt;

&lt;p&gt;So the jar was never the contract. It is one projection of the contract, the one for Java consumers, and there has to be another: a document in a format that does not belong to any language. Generating it is the obvious part. The awkward part is when. Every tool I know of reads a running application, and an application that runs is exactly what this project does not have - the service has no controllers, and will not have any for another chapter.&lt;/p&gt;

&lt;p&gt;What follows is how a complete specification comes out of a build in which nothing is implemented.&lt;/p&gt;




&lt;h2&gt;
  
  
  Where a path is written down
&lt;/h2&gt;

&lt;p&gt;A Swagger annotation describes an operation. It does not say where the operation lives. &lt;code&gt;@Operation&lt;/code&gt; has a summary and &lt;code&gt;@ApiResponse&lt;/code&gt; has a status code, and nowhere among them is a path or a verb, because those were never Swagger's job - it expects to find them in whatever framework is underneath.&lt;/p&gt;

&lt;p&gt;So something has to say it, and the choice is which file. In the previous chapter the interfaces carried documentation and the mapping lived on the controllers that implemented them, which meant a path was written in one repository and a verb in another, and the two agreed by habit. That is the arrangement this chapter ends.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Tag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Products"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Managing one's own products."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nd"&gt;@RequestMapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;RestUrl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SECURED_PRODUCTS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;produces&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MediaType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;APPLICATION_JSON_VALUE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;ProductsRestService&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;CrudRestService&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;NewProductDto&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;UpdateProductDto&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ProductDto&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The mappings are on the interface. Spring supports this, with a condition stated plainly in the &lt;code&gt;@RequestMapping&lt;/code&gt; javadoc: when using controller interfaces, put &lt;em&gt;all&lt;/em&gt; the mapping annotations on the interface rather than on the implementation. Split between the two is the documented way to break it.&lt;/p&gt;

&lt;p&gt;The part I expected to be a problem was the parameters, because Java does not inherit annotations on method parameters. It was a problem, until Framework 5.1, which enabled this - the issue asking for it was argued on the grounds of a REST interface shared between a client and a server. On anything current, &lt;code&gt;@PathVariable&lt;/code&gt; and &lt;code&gt;@RequestParam&lt;/code&gt; on an interface method are read.&lt;/p&gt;

&lt;p&gt;The paths themselves stay in &lt;code&gt;RestUrl&lt;/code&gt; as constants, so a path exists once as a string and is referenced by the interface, by the generated document, and later by nothing else, because there is nowhere else left to write it.&lt;/p&gt;

&lt;p&gt;The cost is that &lt;code&gt;spring-web&lt;/code&gt; is now a dependency of the contract. A consumer that is not a Spring application carries a jar of annotations it will not use, and a contract that declares its operations in one framework's vocabulary has married that framework. I took the trade because the alternative was the same path typed twice, and the series has spent fourteen chapters on what happens to two descriptions of one thing.&lt;/p&gt;




&lt;h2&gt;
  
  
  Four operations, three types
&lt;/h2&gt;

&lt;p&gt;The CRUD operations are declared once, for every resource that has them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;CrudRestService&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;NewDto&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;AbstractDto&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;UpdateDto&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;AbstractDto&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Dto&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;AbstractEntityDto&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three type parameters rather than one, and that is the whole point of the interface. Creating, updating and reading are three shapes: a caller supplies neither identifier nor version when creating, supplies a version but not an identifier when updating, and receives both when reading. One type across all three would need a paragraph of prose explaining which field applies when, and prose is the part of a contract a generated client cannot check.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ProductsRestService&lt;/code&gt; extends it with the three product types and adds what is specific: the owner's listing, and a status change that is an operation rather than a field, for the reason the service layer gave when it chose &lt;code&gt;changeStatus()&lt;/code&gt; over a setter.&lt;/p&gt;

&lt;p&gt;Whether a generator resolves those type variables to real schemas or gives up and writes &lt;code&gt;object&lt;/code&gt; is not something to assume. It is the first thing the test asserts.&lt;/p&gt;




&lt;h2&gt;
  
  
  Five parameters, one argument
&lt;/h2&gt;

&lt;p&gt;A listing takes a page, a size, a sort field, a direction and an optional filter. As five parameters on a method, they travel as five arguments through every layer that carries them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@GetMapping&lt;/span&gt;
&lt;span class="nc"&gt;ProductsDescriptionsDto&lt;/span&gt; &lt;span class="nf"&gt;getProducts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@ModelAttribute&lt;/span&gt; &lt;span class="nc"&gt;ProductSearchParametersDto&lt;/span&gt; &lt;span class="n"&gt;parameters&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Spring fills the object from the query string, and springdoc flattens it back into five separate query parameters in the document. The client sees exactly what it saw before, what changes is every signature between here and the database.&lt;/p&gt;

&lt;p&gt;The defaults live in the object rather than in the caller, which is a small decision with a visible consequence. A request for a listing with no arguments is reasonable, and answering it with a 400 teaches clients to send boilerplate. Because the defaults are real, they belong in the published document too, which is why each one is declared rather than merely initialised:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Parameter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;description&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"The requested page index. The first page is 1."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;schema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;@Schema&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;defaultValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The flattening is the kind of behaviour that works until a library upgrade changes its mind, and the failure is quiet: a client generator would start asking callers to build an object and send it as one value, which is not what the server reads. There is a test for it.&lt;/p&gt;




&lt;h2&gt;
  
  
  A parameter with no argument
&lt;/h2&gt;

&lt;p&gt;Two headers belong to the secured operations. One says who is asking, the other says which version of the row the caller was looking at when it decided to change it. Both were method arguments until this week, and on the four CRUD operations that meant the same two parameters, with the same two descriptions, repeated on every signature that needed them.&lt;/p&gt;

&lt;p&gt;They are gone from the signatures now. The caller's identity belongs to the request, not to the argument list, and a precondition on a request is not data the method was sent. Where exactly each one is read is the next chapter's problem, and the contract has no opinion about it. What the contract cannot do is stay silent about them, because a required header that is absent from the specification is an API that works only for people who were told. So they have to be described without a parameter to hang on, and the two of them turned out to need different answers.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;If-Match&lt;/code&gt; applies to two operations out of six, because only an update and a status change have a row whose version matters. It is declared where it applies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Parameter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;IF_MATCH&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ParameterIn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;HEADER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;description&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"The version of the entity the caller read, as returned in its 'version' field."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;schema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;@Schema&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"integer"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;format&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"int32"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The caller's identification applies to all six, which makes it a property of the secured surface rather than of any operation on it. Declaring it six times is the shape of problem this chapter already rejected once, when the price field was being repaired by hand. So it is declared nowhere and added everywhere:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Bean&lt;/span&gt;
&lt;span class="nc"&gt;OperationCustomizer&lt;/span&gt; &lt;span class="nf"&gt;requesterIdentification&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;requester&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HeaderParameter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HeaderConstants&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;REQUESTER_ID&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;required&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;description&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"The caller's identification."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StringSchema&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"uuid"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;handlerMethod&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;SecuredRestService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isAssignableFrom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;handlerMethod&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getBeanType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addParametersItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;requester&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which operations get it is decided by a marker interface the secured contracts implement, rather than by a package name. The customizer is handed the stub class, and the stub implements whatever its contract implements, so a contract that declares itself secured is one the generator can recognise without comparing strings. A package name would have worked today and broken silently the first time somebody moved a file.&lt;/p&gt;

&lt;p&gt;This is the first place in these two chapters where the document asserts something no interface declares. Everywhere else the generator reads a Spring binding and reports what it found, and a wrong answer would mean the service and its description disagree about code that exists in one file. Here the location, the requiredness and the type are literals, and nothing compiles them against the thing that will eventually read the header. I took that over the alternative, which was keeping two arguments on every method so that a documentation tool could see them. A generator is not a reason to shape a signature.&lt;/p&gt;

&lt;p&gt;One thing does survive the move. The header's name is not a string in either place, it is &lt;code&gt;HeaderConstants.REQUESTER_ID&lt;/code&gt;, which is the same constant the service will read it by, so the one piece of this that a typo could break quietly is still written down once. That class exists for exactly the reason the paths do.&lt;/p&gt;

&lt;p&gt;There is a hole left in this, and it is worth naming rather than discovering later. Nothing yet stops somebody adding a secured contract that forgets the marker. The operations would be documented, the document would look complete, and the header would be missing from exactly the operations that require it. The fix belongs next to the mapping guard a few lines below, and it is the same fix: a contract under the secured package that does not declare itself secured should fail the build rather than produce a plausible document.&lt;/p&gt;




&lt;h2&gt;
  
  
  An application with nothing in it
&lt;/h2&gt;

&lt;p&gt;What makes the rest of it possible is a Spring Boot application in the contract repository that contains no services, no database, no configuration of its own, and no controllers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@SpringBootConfiguration&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;proxyBeanMethods&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nd"&gt;@EnableAutoConfiguration&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DocumentationApplication&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;SpringApplication&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DocumentationApplication&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details in four lines. &lt;code&gt;@SpringBootConfiguration&lt;/code&gt; with &lt;code&gt;@EnableAutoConfiguration&lt;/code&gt; rather than &lt;code&gt;@SpringBootApplication&lt;/code&gt;, because the third thing that annotation brings is a component scan and there is nothing here to scan. And &lt;code&gt;main&lt;/code&gt; is not public, which Java has allowed since 25 and which this project has been doing since the services lost their public entry points.&lt;/p&gt;

&lt;p&gt;What the application needs are controllers, and controllers are what does not exist. So it makes them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;controller&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ByteBuddy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subclass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;implement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"com.pe.advanced.api.openapi.generated."&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getSimpleName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"Controller"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;annotateType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AnnotationDescription&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ofType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;RestController&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;method&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;isAbstract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;intercept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;StubMethod&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;INSTANCE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;make&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DocumentationApplication&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getClassLoader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ClassLoadingStrategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Default&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;WRAPPER&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getLoaded&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;registry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;registerBeanDefinition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getSimpleName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;RootBeanDefinition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A scanner finds every interface under &lt;code&gt;com.pe.advanced.api.rest&lt;/code&gt; carrying a &lt;code&gt;@RequestMapping&lt;/code&gt;. For each one, ByteBuddy builds a class that implements it, annotates that class &lt;code&gt;@RestController&lt;/code&gt;, and gives every abstract method a body that does nothing at all. The result is registered as a bean definition. Spring then sees what it always sees - a controller with mappings - and springdoc sees real handler methods to describe.&lt;/p&gt;

&lt;p&gt;Every one of those endpoints returns null to anybody who calls it, and nothing calls them. The application exists to be read.&lt;/p&gt;

&lt;p&gt;One line in that loop is not about generating anything:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AnnotatedElementUtils&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findMergedAnnotation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;RequestMapping&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;IllegalStateException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Missing HTTP mapping on "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A method on a contract interface without a mapping is not a documentation problem, it is an operation that no client can call and that nobody will notice is missing, because an absent operation leaves no trace in a generated file. This turns it into a failed build. The same instinct appears a few lines above it, where finding no contracts at all throws rather than producing an empty document, which is what a renamed package would otherwise do silently.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why not write the controllers by hand
&lt;/h2&gt;

&lt;p&gt;The simpler version of this is two small classes that implement the two contracts and return null, annotated &lt;code&gt;@RestController&lt;/code&gt;, committed to the repository.&lt;/p&gt;

&lt;p&gt;It works, and it has one failure mode that is enough to reject it. The classes are a parallel structure that has to be kept in step with the interfaces by whoever remembers, and the day somebody adds a third contract and forgets the third stub, nothing fails. The build succeeds, the document is produced, and it is missing an operation. A specification that is silently incomplete is worse than one that is absent, because the frontend will build against it and discover the gap at integration time.&lt;/p&gt;

&lt;p&gt;The generated version cannot drift, because there is nothing to keep in step: the stubs do not exist until the scan finds a contract, and the scan finds every contract. That is the same argument the application module made in Chapter 13 for listing beans by hand - except that there the explicit list was the safer choice and here the generated one is, and the difference is which of the two can fail loudly.&lt;/p&gt;

&lt;p&gt;ByteBuddy is a dependency most projects would rather not have, and this one does not carry it anywhere it matters. The module declares &lt;code&gt;maven.install.skip&lt;/code&gt; and &lt;code&gt;maven.deploy.skip&lt;/code&gt;, so nothing it produces reaches a repository and no consumer inherits anything from it. It is a build-time tool that happens to be written in Java.&lt;/p&gt;




&lt;h2&gt;
  
  
  A build that starts a server to read it
&lt;/h2&gt;

&lt;p&gt;The Maven side is mechanical, and two parts of it are worth copying.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;spring-boot-maven-plugin&lt;/code&gt; starts the application at &lt;code&gt;prepare-package&lt;/code&gt; and stops it at &lt;code&gt;package&lt;/code&gt;. In between, &lt;code&gt;springdoc-openapi-maven-plugin&lt;/code&gt; fetches &lt;code&gt;/v3/api-docs&lt;/code&gt; and &lt;code&gt;/v3/api-docs.yaml&lt;/code&gt; and writes both to disk. The ports come from &lt;code&gt;build-helper-maven-plugin&lt;/code&gt;, which reserves two free ones at &lt;code&gt;initialize&lt;/code&gt; - one for HTTP and one for the plugin's JMX channel - because a fixed port is a build that fails on a developer's machine for a reason that has nothing to do with the code.&lt;/p&gt;

&lt;p&gt;A random port creates a problem of its own, and this is the line that solves it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;OpenAPI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;title&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Advanced Spring API"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;version&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;servers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Server&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;url&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without it, springdoc records the server it was read from, and the published document tells every consumer that the API lives at &lt;code&gt;http://127.0.0.1:49731&lt;/code&gt;. It would be correct for about four seconds.&lt;/p&gt;

&lt;p&gt;The Swagger UI is unpacked from the webjar that springdoc already depends on, rather than pinned at a version of its own. That is one less version to keep in step, and the only file replaced is &lt;code&gt;swagger-initializer.js&lt;/code&gt;, which otherwise points at the Petstore demo. What ships is a directory containing the UI and the two documents, which opens from a file system, a static host, or a raw repository, with nothing to install.&lt;/p&gt;

&lt;p&gt;Where it goes after that is the part I would not copy without thinking. Mine lands in a Nexus raw repository, under a path and the contract's version, uploaded by &lt;code&gt;wagon-maven-plugin&lt;/code&gt; at &lt;code&gt;deploy&lt;/code&gt;. That is infrastructure I happen to have. The portable part is everything before it: a directory produced by the build, versioned with the contract that produced it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Telling the document what the mapper does
&lt;/h2&gt;

&lt;p&gt;A specification can say that a price is a string. Only a mapper makes it one, and the two are written in different places by different mechanisms, which is the condition under which they disagree.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;SpringDocUtils&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replaceWithClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BigDecimal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The previous chapter decided that money leaves this API as a string. The generator knows nothing about that decision - it reads Java types, finds &lt;code&gt;BigDecimal&lt;/code&gt;, and calls it a number. The per-field repair is &lt;code&gt;@Schema(type = "string")&lt;/code&gt; on every money field, which is an agreement maintained by whoever remembers.&lt;/p&gt;

&lt;p&gt;I know what that costs, because I have the version where it was maintained by hand, and three fields had been forgotten. The published document described them as numbers while the service sent strings, and nothing anywhere would have caught it: the schema was plausible, the serialization was correct, and the two were never compared.&lt;/p&gt;

&lt;p&gt;One line in the documentation application is the whole fix. The annotation disappears from every field, and the question of whether somebody remembered stops existing.&lt;/p&gt;




&lt;h2&gt;
  
  
  What the tests prove
&lt;/h2&gt;

&lt;p&gt;There are thirteen tests against the generated files, and they run after the server has stopped, against what is on disk rather than what an endpoint says. They exist because a generated document is the most plausible wrong answer a build can produce. It is made by a tool reading annotations, and a tool reading annotations is the thing that emits something reasonable when one is missing. Nothing about the file looks wrong when an operation is absent from it.&lt;/p&gt;

&lt;p&gt;So the tests check the things that could silently stop being true. That both projections are still two schemas rather than one, and that the public one carries no version. That &lt;code&gt;CrudRestService&lt;/code&gt; resolved to &lt;code&gt;NewProductDto&lt;/code&gt; and &lt;code&gt;UpdateProductDto&lt;/code&gt; rather than to &lt;code&gt;object&lt;/code&gt;. That every failure is a &lt;code&gt;ProblemDetail&lt;/code&gt; under &lt;code&gt;application/problem+json&lt;/code&gt;, which is what the service actually returns. That both headers are in the document as headers, which is the one pair of claims here that rests on a declaration and a customizer rather than on a binding, and that the anonymous operations carry neither. That no operation declares the same parameter twice, which is what two mechanisms writing into one list can produce. That the search parameters are five query parameters and not one schema. That no property is a number without a format, which is how a decimal that escaped the string conversion announces itself. And that &lt;code&gt;servers[0].url&lt;/code&gt; is &lt;code&gt;/&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;One assertion is deliberately absent. There is no check on the number of operations, because a count has to be updated by hand on every change to the API, and the third time somebody updates it without reading it, the test has finished teaching them to ignore it.&lt;/p&gt;




&lt;h2&gt;
  
  
  What this part settled
&lt;/h2&gt;

&lt;p&gt;The specification exists. It is produced by a build, from the same interfaces the service will implement, with its own version and no running service anywhere in the process. It can be opened in a browser, fed to a client generator, or pointed at by a mock server, and it describes every operation this API will have before a single one of them answers a request.&lt;/p&gt;

&lt;p&gt;That is the argument these two chapters have been making. The contract is a build artifact, so the only thing it can describe is what the interfaces declare, which is what separates it from a document somebody writes alongside the code and updates when they remember.&lt;/p&gt;

&lt;p&gt;What it cannot do is answer anything. Every operation in that document is backed by a class that was generated to return null, and the service in the other repository still has no controllers at all.&lt;/p&gt;




&lt;h3&gt;
  
  
  What's Next?
&lt;/h3&gt;

&lt;p&gt;Chapter 17 implements them, and most of the work is already decided. A controller implements its interface and inherits the mapping, the path and the parameter binding, so what is left in each method is the part that was always the point: calling a service and returning what it gives back.&lt;/p&gt;

&lt;p&gt;Three things that have been open for several chapters close there. The optimistic lock from Chapter 14 produces an exception the handler does not catch, and until it does, a conflict is a 500. The version has to travel from the request to the entity, which is what &lt;code&gt;If-Match&lt;/code&gt; is for. And the security rule has to deny by default and name what is public, rather than leaving a prefix to carry a meaning that nothing enforces.&lt;/p&gt;

&lt;p&gt;The first &lt;code&gt;@WebMvcTest&lt;/code&gt; also lands there, which will finally answer a question left open in Part 1 of Chapter 13: whether declaring &lt;code&gt;@ComponentScan&lt;/code&gt; by hand costs the filters that slice tests rely on.&lt;/p&gt;




&lt;h3&gt;
  
  
  Codebase &amp;amp; Architecture Blueprint
&lt;/h3&gt;

&lt;p&gt;The entire evolutionary architecture of this project is tracked using strict Git tags. There are two repositories: the service, and the API contract it implements. This chapter, like the previous one, is entirely in the second one.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;API contract (Tag: &lt;code&gt;chapter-16-openapi&lt;/code&gt;):&lt;/strong&gt; &lt;a href="https://github.com/KamenIvanov/advanced-spring-api-contract/tree/chapter-16-openapi" rel="noopener noreferrer"&gt;advanced-spring-api-contract&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Service (unchanged since Chapter 14, Tag: &lt;code&gt;chapter-14-schema&lt;/code&gt;):&lt;/strong&gt; &lt;a href="https://github.com/KamenIvanov/advanced-spring-multimodule/tree/chapter-14-schema" rel="noopener noreferrer"&gt;advanced-spring-multimodule&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Two chapters now without a commit in the service, which is the separation doing what it was supposed to do: a specification the frontend can build against was produced without touching the application at all.&lt;/p&gt;

&lt;p&gt;Note: All core modules are configured with strict compilation-level boundaries. Compile and run &lt;code&gt;mvn clean install&lt;/code&gt; to see the structure in action. Maven version 3.9.* and Java 25 are required.&lt;/p&gt;

&lt;p&gt;◀️ &lt;a href="https://dev.to/kamenivanov/the-contract-ships-before-the-server-does-chapter-15-1j4m"&gt;Read Chapter 15: The Contract Ships Before the Server Does&lt;/a&gt; &lt;br&gt;
▶️ Read Chapter 17: Implementing the Controllers (Coming soon)&lt;/p&gt;




&lt;h3&gt;
  
  
  Join the Masterclass Journey
&lt;/h3&gt;

&lt;p&gt;This article is part of an ongoing weekly series. If you want to receive every upcoming chapter directly in your inbox, alongside deep-dives, infrastructure architecture diagrams, and premium insights before anyone else, &lt;strong&gt;&lt;a href="https://kamenivanov.substack.com/subscribe" rel="noopener noreferrer"&gt;subscribe to my Substack Newsletter here&lt;/a&gt;&lt;/strong&gt;!&lt;/p&gt;




&lt;h3&gt;
  
  
  Recommended Resources for Java &amp;amp; Spring Boot Engineers
&lt;/h3&gt;

&lt;p&gt;Disclosure: I earn a commission if you purchase through the links below, at no additional cost to you.&lt;/p&gt;

&lt;p&gt;If you are preparing for Senior/Lead Java interviews or looking to solidify your Spring Boot &amp;amp; Architecture skills, check out these highly-rated resources from the &lt;strong&gt;Javarevisited&lt;/strong&gt; publication &lt;em&gt;(Use promo code&lt;/em&gt; &lt;strong&gt;&lt;em&gt;friends20&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;for an exclusive&lt;/em&gt; &lt;strong&gt;&lt;em&gt;20% discount&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;automatically applied at checkout)&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the Java Interview&lt;/strong&gt;
Prepare for core Java, concurrency, JVM internals, and design pattern questions.
&lt;a href="https://gumroad.com/a/501547619/QqjGH" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/HMOAv" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the Spring Boot Interview&lt;/strong&gt;
Master Spring Core, Auto-configuration, Spring Data JPA, Security, and Microservices.
&lt;a href="https://gumroad.com/a/501547619/hrUXKY" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/pfolo" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the SQL Interview&lt;/strong&gt;
Deep-dive into query optimization, indexing, joins, and complex SQL window functions.
&lt;a href="https://gumroad.com/a/501547619/fhmehw" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/brruh" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Complete Java + Spring + SQL Interview Bundle&lt;/strong&gt;
Get all three interview guides in a single heavily discounted package.
&lt;a href="https://gumroad.com/a/501547619/gvqgjk" rel="noopener noreferrer"&gt;Get the Ultimate Interview Bundle&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spring Professional Certification Practice Questions (250+ Questions)&lt;/strong&gt;
Validating your skills? Practice with real exam-style questions before taking the Spring Professional certification.
&lt;a href="https://gumroad.com/a/501547619/sygyq" rel="noopener noreferrer"&gt;Get the Spring Professional Questions&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/qelhye" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>api</category>
      <category>architecture</category>
      <category>java</category>
      <category>typescript</category>
    </item>
    <item>
      <title>@Data on a JPA Entity Gave Us a False `equals()` and 18,000 Duplicate Rows</title>
      <dc:creator>Kamen</dc:creator>
      <pubDate>Fri, 20 Nov 2026 07:32:10 +0000</pubDate>
      <link>https://dev.to/kamenivanov/data-on-a-jpa-entity-gave-us-a-false-equals-and-18000-duplicate-rows-18nm</link>
      <guid>https://dev.to/kamenivanov/data-on-a-jpa-entity-gave-us-a-false-equals-and-18000-duplicate-rows-18nm</guid>
      <description>&lt;p&gt;The nightly import had been running for months without a complaint. Then a report came back with customers listed twice, and a count of the staging table showed roughly 18,000 rows more than the source file contained. The import code had a deduplication step, it was three lines long, and it looked impossible to get wrong.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Customer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;toSave&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashSet&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;existingCustomers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;toSave&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;importedCustomers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;customerDao&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;saveAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toSave&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A &lt;code&gt;HashSet&lt;/code&gt; deduplicates by &lt;code&gt;equals&lt;/code&gt; and &lt;code&gt;hashCode&lt;/code&gt;, and the entity had both, because it was annotated with Lombok's &lt;code&gt;@Data&lt;/code&gt;. That is the part everyone trusted and nobody read.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@Data&lt;/code&gt; generates &lt;code&gt;equals&lt;/code&gt; and &lt;code&gt;hashCode&lt;/code&gt; from every field of the class, and on a JPA entity every field includes the primary key. A customer loaded from the database carried an id. The same customer parsed from the import file had not been persisted yet, so its id was &lt;code&gt;null&lt;/code&gt;. Two objects describing the same person, with the same external reference and the same email, were not equal to each other, because one of them had an id and the other did not. The set kept both, &lt;code&gt;saveAll&lt;/code&gt; inserted the second one, and the deduplication step had been quietly doing nothing for as long as the id had been part of the comparison.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0q0v51rhjj7sg85oebzk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0q0v51rhjj7sg85oebzk.png" alt="Query latency vs table size" width="799" height="416"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The same generated method has a second failure mode that is easier to hit and harder to see. Because the id participates in &lt;code&gt;hashCode&lt;/code&gt;, an entity's hash changes the moment it is persisted and the database assigns a value. An object placed in a &lt;code&gt;HashSet&lt;/code&gt; before the flush lands in one bucket, and after the flush the set looks for it in another. The object is in the collection and the collection says it is not there. Any code that holds entities in a set or uses them as map keys across a flush boundary is exposed to this, and it fails intermittently, which is how it survives review.&lt;/p&gt;

&lt;p&gt;The fix was to stop deriving identity from the database. The import records had a natural key already, a source system and an external id, unique by contract and never changed after creation, so &lt;code&gt;equals&lt;/code&gt; and &lt;code&gt;hashCode&lt;/code&gt; were written by hand against those two fields and nothing else. Identity became a property of the record rather than of its row, which means it is the same before the insert, after the insert and after a detached object is merged back.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Override&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nc"&gt;Customer&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sourceSystem&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sourceSystem&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;externalId&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;externalId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@Override&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;hashCode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sourceSystem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;externalId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhtuxa7yldg6e8xb34unp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhtuxa7yldg6e8xb34unp.png" alt="FK index missing vs present" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When there is no natural key to lean on, the usual alternative is a UUID assigned in the constructor rather than by the database, which gives the same stability without requiring the business to supply one.&lt;/p&gt;

&lt;p&gt;There is a smaller reason to keep &lt;code&gt;@Data&lt;/code&gt; away from entities, and it shows up in logs rather than in data. The annotation also generates &lt;code&gt;toString&lt;/code&gt; across all fields, including lazily loaded associations, so printing an entity can trigger a query per association, or a &lt;code&gt;LazyInitializationException&lt;/code&gt; outside a session. The usual compromise, if Lombok is going to stay on the class, is &lt;code&gt;@Getter&lt;/code&gt; and &lt;code&gt;@Setter&lt;/code&gt; rather than &lt;code&gt;@Data&lt;/code&gt;, with &lt;code&gt;equals&lt;/code&gt;, &lt;code&gt;hashCode&lt;/code&gt; and &lt;code&gt;toString&lt;/code&gt; written.&lt;/p&gt;

&lt;p&gt;The broader point is that an entity has two identities, the row it maps to and the thing it represents, and they are not available at the same moments in its lifecycle. A generated method cannot know which one you meant. Ours picked the row, in a set that was asking about the thing.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;More like this?&lt;/strong&gt; I write short production war stories like this one, plus a deep-dive architecture series. &lt;a href="https://kamenivanov.substack.com/subscribe" rel="noopener noreferrer"&gt;Subscribe here&lt;/a&gt; if you want both in your inbox.&lt;/p&gt;




&lt;h3&gt;
  
  
  Recommended Resources for Java &amp;amp; Spring Boot Engineers
&lt;/h3&gt;

&lt;p&gt;If you are preparing for Senior/Lead Java interviews or looking to solidify your Spring Boot &amp;amp; Architecture skills, check out these highly-rated resources from the &lt;strong&gt;Javarevisited&lt;/strong&gt; publication &lt;em&gt;(Use promo code&lt;/em&gt; &lt;strong&gt;&lt;em&gt;friends20&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;for an exclusive&lt;/em&gt; &lt;strong&gt;&lt;em&gt;20% discount&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;automatically applied at checkout)&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the Java Interview&lt;/strong&gt;
Prepare for core Java, concurrency, JVM internals, and design pattern questions.
&lt;a href="https://gumroad.com/a/501547619/QqjGH" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/HMOAv" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the Spring Boot Interview&lt;/strong&gt;
Master Spring Core, Auto-configuration, Spring Data JPA, Security, and Microservices.
&lt;a href="https://gumroad.com/a/501547619/hrUXKY" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/pfolo" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the SQL Interview&lt;/strong&gt;
Deep-dive into query optimization, indexing, joins, and complex SQL window functions.
&lt;a href="https://gumroad.com/a/501547619/fhmehw" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/brruh" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Complete Java + Spring + SQL Interview Bundle&lt;/strong&gt;
Get all three interview guides in a single heavily discounted package.
&lt;a href="https://gumroad.com/a/501547619/gvqgjk" rel="noopener noreferrer"&gt;Get the Ultimate Interview Bundle&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spring Professional Certification Practice Questions (250+ Questions)&lt;/strong&gt;
Validating your skills? Practice with real exam-style questions before taking the Spring Professional certification.
&lt;a href="https://gumroad.com/a/501547619/sygyq" rel="noopener noreferrer"&gt;Get the Spring Professional Questions&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/qelhye" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>backend</category>
      <category>database</category>
      <category>debugging</category>
      <category>java</category>
    </item>
    <item>
      <title>A 5-Person Team, 14 Microservices. How We Got There and Why We Walked It Back</title>
      <dc:creator>Kamen</dc:creator>
      <pubDate>Mon, 16 Nov 2026 09:30:53 +0000</pubDate>
      <link>https://dev.to/kamenivanov/a-5-person-team-14-microservices-how-we-got-there-and-why-we-walked-it-back-1on7</link>
      <guid>https://dev.to/kamenivanov/a-5-person-team-14-microservices-how-we-got-there-and-why-we-walked-it-back-1on7</guid>
      <description>&lt;p&gt;The first line of our local setup guide was a warning that you needed 24 GB of RAM. Running the whole system on a laptop meant starting fourteen services, three databases, a message broker and a gateway, and a new engineer typically spent their first two days getting it to boot before touching a line of business code.&lt;/p&gt;

&lt;p&gt;We had arrived at fourteen, one reasonable decision at a time. The system started as three services, split along lines that made sense. After that, every new capability that sounded like a separate concern became a separate service, because that was how the architecture diagrams we admired were drawn, and because splitting felt like the responsible, forward-looking choice. Nobody ever proposed having fourteen. Each service was individually defensible, and nobody was in a position to say that the sum of them no longer was.&lt;/p&gt;

&lt;p&gt;The cost was not in the code. It was in everything around the code. Each service had its own pipeline, its own configuration, its own dashboards, its own on-call runbook and its own contract with its neighbors. With five engineers, that meant everyone owned roughly three services outright and had a shallow, partial understanding of the other eleven. A change that crossed a boundary, which was most of the interesting changes, meant touching three repositories, agreeing on a contract version, and deploying in a specific order so that a consumer never met a producer it did not yet understand. A feature that took a day to write took most of a week to ship.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fabn14hqnwtqdbkkmm1fu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fabn14hqnwtqdbkkmm1fu.png" alt="Before and After" width="800" height="365"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The turning point was a small bug: a rounding rule in a pricing calculation that existed, slightly differently, in two services. Fixing it correctly required changes in both, a coordinated release, and a day spent proving that no consumer in between depended on the old behavior. In a single codebase that would have been one pull request and one test run.&lt;/p&gt;

&lt;p&gt;We did not abandon services entirely, which is the part that gets lost in "we went back to a monolith" stories. We asked a narrower question of each of the fourteen: does this piece have a different scaling profile, a different release cadence, or a different owner from everything around it? Two did, one for heavy document processing and one that integrated with an external provider with its own failure behavior. Those stayed separate. The other twelve became modules in a single deployable, with the same boundaries enforced by the build instead of by the network.&lt;/p&gt;

&lt;p&gt;Over the following two quarters, the median time from starting a feature to having it in production fell from about nine days to about three. The local environment now starts three processes and fits in 8 GB, and nobody misses the other eleven pipelines.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fksthmdubffuqmdptmq4o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fksthmdubffuqmdptmq4o.png" alt="Lead time" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Microservices were never the mistake. The mistake was treating the number of services as a measure of architectural maturity, when the constraint that actually mattered was how much operational surface five people could hold in their heads. A service boundary is a cost you pay every day, in deployment, observability and coordination, so it needs a reason that pays you back every day too.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;More like this?&lt;/strong&gt;&lt;br&gt;
I write short production war stories like this one, plus a deep-dive architecture series. &lt;a href="https://kamenivanov.substack.com/subscribe" rel="noopener noreferrer"&gt;Subscribe here&lt;/a&gt; if you want both in your inbox.&lt;/p&gt;




&lt;h3&gt;
  
  
  Recommended Resources for Java &amp;amp; Spring Boot Engineers
&lt;/h3&gt;

&lt;p&gt;If you are preparing for Senior/Lead Java interviews or looking to solidify your Spring Boot &amp;amp; Architecture skills, check out these highly-rated resources from the &lt;strong&gt;Javarevisited&lt;/strong&gt; publication &lt;em&gt;(Use promo code&lt;/em&gt; &lt;strong&gt;&lt;em&gt;friends20&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;for an exclusive&lt;/em&gt; &lt;strong&gt;&lt;em&gt;20% discount&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;automatically applied at checkout)&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the Java Interview&lt;/strong&gt;
Prepare for core Java, concurrency, JVM internals, and design pattern questions.
&lt;a href="https://gumroad.com/a/501547619/QqjGH" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/HMOAv" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the Spring Boot Interview&lt;/strong&gt;
Master Spring Core, Auto-configuration, Spring Data JPA, Security, and Microservices.
&lt;a href="https://gumroad.com/a/501547619/hrUXKY" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/pfolo" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the SQL Interview&lt;/strong&gt;
Deep-dive into query optimization, indexing, joins, and complex SQL window functions.
&lt;a href="https://gumroad.com/a/501547619/fhmehw" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/brruh" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Complete Java + Spring + SQL Interview Bundle&lt;/strong&gt;
Get all three interview guides in a single heavily discounted package.
&lt;a href="https://gumroad.com/a/501547619/gvqgjk" rel="noopener noreferrer"&gt;Get the Ultimate Interview Bundle&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spring Professional Certification Practice Questions (250+ Questions)&lt;/strong&gt;
Validating your skills? Practice with real exam-style questions before taking the Spring Professional certification.
&lt;a href="https://gumroad.com/a/501547619/sygyq" rel="noopener noreferrer"&gt;Get the Spring Professional Questions&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/qelhye" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>architecture</category>
      <category>microservices</category>
      <category>softwareengineering</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>We Debugged "Random" 502 Errors for 2 Days. The Answer Was in One HTTP Header Nobody Looked At</title>
      <dc:creator>Kamen</dc:creator>
      <pubDate>Thu, 12 Nov 2026 13:56:41 +0000</pubDate>
      <link>https://dev.to/kamenivanov/we-debugged-random-502-errors-for-3-days-the-answer-was-in-one-http-header-nobody-looked-at-27od</link>
      <guid>https://dev.to/kamenivanov/we-debugged-random-502-errors-for-3-days-the-answer-was-in-one-http-header-nobody-looked-at-27od</guid>
      <description>&lt;p&gt;The 502s hit about one login in fifty, and they looked completely random, which is the worst possible bug. Our application logs showed a clean 200 for every one of those requests. Our monitoring showed nothing unusual in CPU, garbage collection or database latency. The only evidence that anything had gone wrong was a user telling us they had been sent to an error page.&lt;/p&gt;

&lt;p&gt;For three days we chased the usual suspects, because the usual suspects are usually right. We checked connection pool exhaustion, a long garbage collection pause, a node being recycled during a deployment, and the keep-alive timeout between the proxy and the application. Each one was a reasonable theory and each one fell apart against the same fact: the application believed it had answered successfully. A server that logs a 200 and a client that receives a 502 are not describing the same request, so the failure had to be happening somewhere between them.&lt;/p&gt;

&lt;p&gt;The break came from the proxy's own error log, which we had not opened until then because the application logs had been so convincing. It contained one line, repeated at the same rate as the complaints: the upstream had sent too big a header while the proxy was reading the response header.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frmhrgx21trsygv16ryun.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frmhrgx21trsygv16ryun.png" alt="Header Buffer" width="799" height="416"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The header in question was a custom &lt;code&gt;X-User-Permissions&lt;/code&gt; that the login response used to hand the single-page frontend a list of the user's permissions, so it could hide the buttons they could not use. The header grew with the number of roles a user had, and the proxy buffers response headers in a fixed-size area, 8 KB in our configuration. A user with five roles produced a header of about 3 KB. A user with sixty produced one of about 12 KB. The proxy could not fit it, gave up on the upstream response and returned a 502 to the client, while the application, which had finished its work correctly, logged a 200 and moved on.&lt;/p&gt;

&lt;p&gt;The pattern had been there all along, hidden by how we counted. The failures were not random across requests. They were concentrated in logins by long-tenured administrators with many roles. Plotted against role count instead of time, the errors began at around 35 roles, exactly where the header crossed the buffer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnjjg0rqbpns0h0wzahgq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnjjg0rqbpns0h0wzahgq.png" alt="Header size by role count" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Raising the proxy buffer made the errors disappear within minutes, but that only moved the cliff further out. The real fix was to stop sending data that grows with the user in a response header. The permissions now travel in the response body, which is not subject to the small header buffers on every hop.&lt;/p&gt;

&lt;p&gt;When a client sees an error that the server never recorded, the interesting component is the one in between, and the evidence is usually in the headers it was handed. We spent three days looking inside a healthy application to explain a failure that happened outside it.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;More like this?&lt;/strong&gt;&lt;br&gt;
I write short production war stories like this one, plus a deep-dive architecture series. &lt;a href="https://kamenivanov.substack.com/subscribe" rel="noopener noreferrer"&gt;Subscribe here&lt;/a&gt; if you want both in your inbox.&lt;/p&gt;




&lt;h3&gt;
  
  
  Recommended Resources for Java &amp;amp; Spring Boot Engineers
&lt;/h3&gt;

&lt;p&gt;If you are preparing for Senior/Lead Java interviews or looking to solidify your Spring Boot &amp;amp; Architecture skills, check out these highly-rated resources from the &lt;strong&gt;Javarevisited&lt;/strong&gt; publication &lt;em&gt;(Use promo code&lt;/em&gt; &lt;strong&gt;&lt;em&gt;friends20&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;for an exclusive&lt;/em&gt; &lt;strong&gt;&lt;em&gt;20% discount&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;automatically applied at checkout)&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the Java Interview&lt;/strong&gt;
Prepare for core Java, concurrency, JVM internals, and design pattern questions.
&lt;a href="https://gumroad.com/a/501547619/QqjGH" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/HMOAv" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the Spring Boot Interview&lt;/strong&gt;
Master Spring Core, Auto-configuration, Spring Data JPA, Security, and Microservices.
&lt;a href="https://gumroad.com/a/501547619/hrUXKY" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/pfolo" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the SQL Interview&lt;/strong&gt;
Deep-dive into query optimization, indexing, joins, and complex SQL window functions.
&lt;a href="https://gumroad.com/a/501547619/fhmehw" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/brruh" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Complete Java + Spring + SQL Interview Bundle&lt;/strong&gt;
Get all three interview guides in a single heavily discounted package.
&lt;a href="https://gumroad.com/a/501547619/gvqgjk" rel="noopener noreferrer"&gt;Get the Ultimate Interview Bundle&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spring Professional Certification Practice Questions (250+ Questions)&lt;/strong&gt;
Validating your skills? Practice with real exam-style questions before taking the Spring Professional certification.
&lt;a href="https://gumroad.com/a/501547619/sygyq" rel="noopener noreferrer"&gt;Get the Spring Professional Questions&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/qelhye" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>The Contract Ships Before the Server Does (Chapter 15)</title>
      <dc:creator>Kamen</dc:creator>
      <pubDate>Mon, 09 Nov 2026 07:44:40 +0000</pubDate>
      <link>https://dev.to/kamenivanov/the-contract-ships-before-the-server-does-chapter-15-1j4m</link>
      <guid>https://dev.to/kamenivanov/the-contract-ships-before-the-server-does-chapter-15-1j4m</guid>
      <description>&lt;p&gt;Three groups of people need this API, one of them is me, and I have fourteen chapters of code. The other two are the developer building the screens and the engineer writing the tests, and what they have is a conversation in which I describe what the responses will look like. That is the normal flow and it is worse than it sounds. It is not that they are blocked, it is that they are not: they start, against what they remember of what I said, and the disagreement surfaces weeks later as a bug report about a field that was always called something else. By then there are three versions of the truth, and mine is the only one that compiles.&lt;/p&gt;

&lt;p&gt;What follows is the artifact that replaces the conversation. It is not documentation, and it is not generated from the running service, because a specification that requires a working server cannot exist before the server works. It is a repository of its own, versioned on its own, published to a registry, and declared as a dependency by everything that needs it - including the service that implements it.&lt;/p&gt;




&lt;h2&gt;
  
  
  A repository of its own
&lt;/h2&gt;

&lt;p&gt;The first decision is the one that looks like overhead, the types and the operation signatures could live in the service, in a module beside the others, and the frontend could read them on GitHub. Instead they live in &lt;code&gt;advanced-spring-api-contract&lt;/code&gt;, which is a separate repository with a separate build and a separate version number.&lt;/p&gt;

&lt;p&gt;The reason is the version number, and it is easier to see from the other end. Last week's work on this project produced a migration and a mapped column: real changes to the service, worth a release. Nothing in them changed what a caller sends or receives. If the contract were a module of the service, it would have been released too, with a new version, and the two teams downstream would have had to look at it to find out that nothing concerns them. Multiply that by every bug fix for a year and the version number stops carrying information - which is the point at which people stop reading it.&lt;/p&gt;

&lt;p&gt;Kept separate, the arithmetic reverses, the contract's version changes when the contract changes, and only then. A bug fixed in the service ships as a new service with the same contract, and the signal downstream is that there is no signal. The repository holds three modules. &lt;code&gt;dto&lt;/code&gt; is the types on the wire, &lt;code&gt;rest-api&lt;/code&gt; is the operations, and for now the configuration that decides how those types are rendered. &lt;code&gt;events&lt;/code&gt; is empty, and exists anyway: a consumer of the HTTP API and a consumer of a topic are two different audiences with two different promises, and neither should have to depend on the other's. An empty module is cheap, discovering later that both promises are in one jar is not.&lt;/p&gt;




&lt;h2&gt;
  
  
  Two types for one product
&lt;/h2&gt;

&lt;p&gt;A product has two audiences, somebody browsing the catalogue wants a name, a price and a picture of what they might buy. The seller who owns it wants everything, and will change some of it. Those are not the same representation, and the API does not pretend they are. The obvious alternative is one path whose response depends on who asked. It is worse in three ways that are easy to miss until they happen. A shared cache cannot tell the two apart without a correctly set &lt;code&gt;Vary&lt;/code&gt;, and the day it gets that wrong it serves the full representation to anonymous callers. A specification cannot describe one path that returns two schemas, so the published contract becomes vaguer exactly where it matters. And a client written against the anonymous response has no way to know that the field it is missing exists for somebody else.&lt;/p&gt;

&lt;p&gt;So there are two types, &lt;code&gt;ProductDescriptionDto&lt;/code&gt; and &lt;code&gt;ProductDto&lt;/code&gt;, and they are siblings rather than one extending the other:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ProductDescriptionDto&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;AbstractCreatableEntityDto&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;sku&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;BigDecimal&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;ProductStatusDto&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What they share is a base holding an identifier and a creation time. What the full one adds is an update time and a version, and the reason the public projection does not inherit them is the argument this project has now made three times in three places. The version is a concurrency token: a caller that cannot write has nothing to do with one. The schema put the column on the tables that can be updated, the entity put the field on the class that introduces updating, and the contract hands the number only to the audience that can use it. The same rule, applied at a third boundary.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;CategoryAssignmentDto&lt;/code&gt; is the other end of that rule, it extends the creatable base and stops there, because an assignment is never updated - it exists or it does not and giving it a version would be giving it a capability it does not have.&lt;/p&gt;




&lt;h2&gt;
  
  
  What a caller may send
&lt;/h2&gt;

&lt;p&gt;There are two more types per entity, and they are the ones that look redundant in a review.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NewProductDto&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;AbstractDto&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UpdateProductDto&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;AbstractDto&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Neither extends the read type, and neither carries an identifier, a timestamp or a version, because none of those belong to a caller. The alternative - one type for all three directions, with documentation explaining which fields apply when - puts the rules in prose, and prose is the one part of a contract that a generated client cannot check. A field that is read-only is a comment, a field that is absent is a compiler error. Two fields are missing on purpose, and both absences carry an argument from earlier chapters.&lt;/p&gt;

&lt;p&gt;There is no &lt;code&gt;status&lt;/code&gt; on either write type. A status change is a transition with rules: a draft may become active, an archived product may become nothing at all, and the service holds the table of what may follow what. A settable field would describe the column. The operation that replaces it is a separate one, and it is the same reasoning the service layer used when it chose &lt;code&gt;changeStatus()&lt;/code&gt; over a setter.&lt;/p&gt;

&lt;p&gt;And the measurements are flat on creation - a &lt;code&gt;dimensions&lt;/code&gt; string and a &lt;code&gt;weight&lt;/code&gt; - rather than a nested specification object. The specification is a thing with its own identity once it exists, and it has its own table and its own version to prove it. But at the moment of creation it does not exist, and asking a caller to post an object with an empty identifier so that the server can fill it in would be describing the storage rather than the request. On the way back it is a nested object, because by then it is one.&lt;/p&gt;




&lt;h2&gt;
  
  
  No Java in the payload
&lt;/h2&gt;

&lt;p&gt;There is one annotation this base class deliberately does not carry.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@JsonIgnoreProperties&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ignoreUnknown&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AbstractDto&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The missing one is &lt;code&gt;@JsonTypeInfo&lt;/code&gt;, and with it the &lt;code&gt;@class&lt;/code&gt; property that writes a Java class name into every payload. It is a useful thing in the right place and the wrong thing here, for a reason that is mechanical rather than aesthetic.&lt;/p&gt;

&lt;p&gt;A polymorphic type annotation is not a property of the class. It is an instruction to the mapper, which is why a tool that builds a specification from the Java types cannot see it - not through an oversight, and not in some versions and not others, but by construction. The result is a field that exists in every response and in none of the schemas. A generated client does not have it and works anyway, because generators tolerate what they do not expect. A strict schema validator does not, which is how the test engineer finds out. And the package structure of the service travels to the browser, unofficially, which means somebody will eventually depend on it and a refactoring will break them.&lt;/p&gt;

&lt;p&gt;The annotation that remains points the other way, and it is the reason adding a field to this API is not a negotiation. &lt;code&gt;ignoreUnknown&lt;/code&gt; means a client compiled against version 1.0 of this jar keeps working when 1.1 adds a property. Without it, every addition is a breaking change and the version number starts to mean something nobody wants it to mean.&lt;/p&gt;

&lt;p&gt;Removing the property has a consequence that has to be met somewhere, and it is met in the signature. A deserializer that discovers the type from the payload needs the type to be in the payload, one that is told the type does not. So the reader here takes the target as an argument - &lt;code&gt;process(payload, ProductDto.class)&lt;/code&gt; and that is not a workaround. Over HTTP the caller always knows what it asked for, and on the server the framework deserializes into the parameter type the handler declares.&lt;/p&gt;

&lt;p&gt;There is one place where a type marker earns its keep, and it is the module that is still empty. A consumer reading from a topic genuinely does not know what it is holding. When &lt;code&gt;events&lt;/code&gt; has something in it, it will carry a discriminator - a logical event name, not a class name, because &lt;code&gt;ProductCreated&lt;/code&gt; is a contract and a fully qualified Java type is an internal detail that changes meaning during a rename.&lt;/p&gt;




&lt;h2&gt;
  
  
  A price is not a number
&lt;/h2&gt;

&lt;p&gt;The contract says a product has a price. What it does not say, in any schema language, is what that price looks like when it arrives.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addSerializer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BigDecimal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ToStringSerializer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A price goes out as a JSON string. The reason is the client, not the server: a JSON number parsed by a browser becomes a double, and a double is the wrong container for money twice over. The first loss is the scale. &lt;code&gt;19.90&lt;/code&gt; arrives and becomes &lt;code&gt;19.9&lt;/code&gt; - the same amount, displayed as something no price list has ever shown and the repair is a formatter on the client that now holds its own opinion about how many decimals a currency has. The second loss is real, add that same &lt;code&gt;19.90&lt;/code&gt; a hundred times in a double and the total is &lt;code&gt;1990.0000000000036&lt;/code&gt;, and a client that rounds its way out of that will find that &lt;code&gt;round(2.675, 2)&lt;/code&gt; is &lt;code&gt;2.67&lt;/code&gt;. Writing the price as a string keeps the scale the server decided on, and makes arithmetic on money something a client has to opt into with a type that can do it rather than something it falls into by parsing.&lt;/p&gt;

&lt;p&gt;Which raises the question of where that decision belongs, and the answer is why &lt;code&gt;rest-api&lt;/code&gt; depends on databind rather than only on annotations. The mapper lives in the contract, a specification can say that a price is a string - only a configuration makes it one, and if that configuration lives in the service then the contract describes an intention and the service decides the reality. Publishing them together means the artifact that declares the operations also declares how their values are rendered, and the tests that exercise it are testing the published contract rather than a local approximation of it.&lt;/p&gt;

&lt;p&gt;That holds on one condition, and it is worth stating now because it is the first thing chapter seventeen has to get right. A service that lets the framework configure its own mapper has two configurations. The tests prove one of them, the requests are served by the other, and they agree until somebody changes one - at which point the tests still pass. The application module will take its mapper from here rather than from autoconfiguration, and that line is what makes everything in this section true in production rather than only in a test.&lt;/p&gt;




&lt;h2&gt;
  
  
  One shape for an instant
&lt;/h2&gt;

&lt;p&gt;The same question, asked about time, has a less obvious answer. Jackson's handling of &lt;code&gt;java.time&lt;/code&gt; is correct ISO-8601, and correct ISO-8601 is not one format. The fractional part is dropped when it is zero and printed to three, six or nine digits otherwise, depending on the value. Every one of those strings is valid and they are not interchangeable to a consumer that compares them, slices them by position, or uses one as a cache key. The bug that produces is the worst kind: it appears only for the request that happened to land on a whole second.&lt;/p&gt;

&lt;p&gt;So the format is pinned, in one place, because the same string is needed by the serializer, by the examples in the specification and by the tests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="no"&gt;PATTERN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Always UTC, always three digits, always a trailing &lt;code&gt;Z&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The deserializer is deliberately more forgiving than the serializer, and the asymmetry is the point. What this API writes is one exact shape. What it accepts is any ISO-8601 instant: with a fractional part or without one, and with an offset other than UTC. A client that sends &lt;code&gt;2026-11-03T16:15:00+02:00&lt;/code&gt; has named a moment in time correctly, and rejecting it would be pedantry dressed up as rigour. Strict on the way out, so that nobody downstream has to handle variation, tolerant on the way in, so that nobody upstream has to guess.&lt;/p&gt;




&lt;h2&gt;
  
  
  What the mapper does not do
&lt;/h2&gt;

&lt;p&gt;The mapper is built once, in a static initialiser, and handed out as a single instance. That is not thrift for its own sake: building one is expensive and a built one is immutable and safe to share, so the alternative is paying for the same object on every request and giving each copy its own chance to be configured slightly differently. One instance is also what makes the configuration a fact about the contract rather than a habit of whoever constructed it last.&lt;/p&gt;

&lt;p&gt;There is no &lt;code&gt;activateDefaultTyping&lt;/code&gt;. It is the switch that puts the &lt;code&gt;@class&lt;/code&gt; property back, and a mapper is where somebody would reasonably turn it on to solve a different problem - deserializing a field declared as &lt;code&gt;Object&lt;/code&gt;, for example. Leaving it off is the same decision as leaving &lt;code&gt;@JsonTypeInfo&lt;/code&gt; off the base class, made in the second place it can be made.&lt;/p&gt;

&lt;p&gt;There is no &lt;code&gt;INDENT_OUTPUT&lt;/code&gt; either, pretty-printed JSON is a convenience for whoever is reading a response by eye, which is nobody in production, and the whitespace is paid for on every byte of every response.&lt;/p&gt;

&lt;p&gt;The one addition is stranger than either, and it is a character escape:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="no"&gt;CHARS_TO_ESCAPE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s1"&gt;'\u2028'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'\u2029'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Those two are a line separator and a paragraph separator. They are legal inside a JSON string, and in JavaScript they used to be line terminators even inside a string literal, which meant a product name containing one produced valid JSON that became a syntax error the moment a page embedded it in a script. ES2019 removed them by allowing them in string literals, so the classic version of this bug is now historical, but it's not gone, though. The two characters are still line terminators elsewhere in the JavaScript grammar, so a payload that lands somewhere other than a string literal - inside a comment, for instance, still behaves differently from what it looks like. Escaping them costs one class and removes a whole category of question about where a consumer is allowed to put the bytes. That is the kind of trade I will take without thinking about it, and the kind that is impossible to retrofit once somebody is parsing responses in anger.&lt;/p&gt;




&lt;h2&gt;
  
  
  What the tests prove
&lt;/h2&gt;

&lt;p&gt;Fifteen tests is not much of a test suite, and they are the most valuable thing in this repository. They exist because everything in the two sections above is a claim. The serializer writes money as a string: that is a claim until something reads the output and checks. Instants carry three digits even when the fourth would be a zero: a claim, and the one most likely to be true on a developer's machine and false at the moment it matters. No Java class name travels to a client: a claim, and an easy one to break by adding an annotation to a base class in a hurry.&lt;/p&gt;

&lt;p&gt;So there is a test for a price of &lt;code&gt;20.00&lt;/code&gt;, which exists to prove the trailing zero survives. One for &lt;code&gt;0.0000000001&lt;/code&gt;, which exists because a small enough number tempts a mapper into scientific notation. One for an instant on a whole second, which is the case the default format gets wrong and the case nobody writes a test for. One for nine digits of precision, to establish that they are truncated rather than rounded. One asserting that neither &lt;code&gt;@class&lt;/code&gt; nor &lt;code&gt;com.pe.advanced&lt;/code&gt; appears in the output, which is how a future change to &lt;code&gt;AbstractDto&lt;/code&gt; fails loudly rather than quietly. And one for a payload containing a field this version has never heard of, because forward compatibility is a promise and an unverified promise is a hope.&lt;/p&gt;

&lt;p&gt;That is what makes this a contract rather than a document. A document describes, this compiles, runs, and fails the build when the description and the behaviour disagree.&lt;/p&gt;




&lt;h2&gt;
  
  
  What this part settled
&lt;/h2&gt;

&lt;p&gt;There is a jar now, it has a version number that moves when the API moves and stays still when the service merely improves. It holds twenty-six types that say what a product, a category and an assignment look like in both directions, with the audiences separated rather than merged. It renders money and time in exactly one way, and proves it.&lt;/p&gt;

&lt;p&gt;What it cannot do is tell anybody which URL to call. There is not a path, not a verb, not an operation anywhere in it. The types describe what a response contains - nothing yet describes what to ask for, and a frontend developer holding this jar still cannot write a request.&lt;/p&gt;




&lt;h3&gt;
  
  
  What's Next?
&lt;/h3&gt;

&lt;p&gt;Chapter 16 adds the operations, and the interesting part is not the interfaces - it is that declaring the path and the verb in exactly one place is what makes a specification generable at all. Swagger annotations describe an operation, they do not say where it lives. Something has to, and whichever file it is becomes the one that cannot disagree with anything. Then the module that turns all of it into an OpenAPI document at build time, without a running service and without a single implemented endpoint, so that the file reaches the registry before the first controller exists. That is the whole reason for the separation this chapter argued for, and it is the point at which the frontend and the tests stop waiting.&lt;/p&gt;

&lt;p&gt;Two small disagreements surfaced while writing these types and belong to that chapter or the one after it. A product's weight is a &lt;code&gt;double&lt;/code&gt; in the domain and an integer in the schema, and grams in floating point mean nothing. A category has a description column and no such field in the domain type. Both are the same shape of bug this project keeps finding: two descriptions of one thing, each defensible alone.&lt;/p&gt;




&lt;h3&gt;
  
  
  Codebase &amp;amp; Architecture Blueprint
&lt;/h3&gt;

&lt;p&gt;The entire evolutionary architecture of this project is tracked using strict Git tags. From this chapter on there are two repositories: the service, and the API contract it implements. This chapter is entirely in the second one.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;API contract (Tag: &lt;code&gt;chapter-15-api-contract&lt;/code&gt;):&lt;/strong&gt; &lt;a href="https://github.com/KamenIvanov/advanced-spring-api-contract/tree/chapter-15-api-contract" rel="noopener noreferrer"&gt;advanced-spring-api-contract&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Service (unchanged since Chapter 14, Tag: &lt;code&gt;chapter-14-schema&lt;/code&gt;):&lt;/strong&gt; &lt;a href="https://github.com/KamenIvanov/advanced-spring-multimodule/tree/chapter-14-schema" rel="noopener noreferrer"&gt;advanced-spring-multimodule&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That second line is the chapter's argument rather than a formality: the service has not moved, so its tag has not moved either.&lt;/p&gt;

&lt;p&gt;Note: All core modules are configured with strict compilation-level boundaries. Compile and run &lt;code&gt;mvn clean install&lt;/code&gt; to see the structure in action. Maven version 3.9.* and Java 25 are required.&lt;/p&gt;

&lt;p&gt;◀️ &lt;a href="https://dev.to/kamenivanov/the-schema-three-chapters-were-already-relying-on-chapter-14-21j"&gt;Read Chapter 14: The Schema Three Chapters Were Already Relying On&lt;/a&gt;&lt;br&gt;
▶️ &lt;a href="https://dev.to/kamenivanov/generated-by-an-application-that-does-nothing-chapter-16-1c05"&gt;Read Chapter 16: Generating the Specification&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Join the Masterclass Journey
&lt;/h3&gt;

&lt;p&gt;This article is part of an ongoing weekly series. If you want to receive every upcoming chapter directly in your inbox, alongside deep-dives, infrastructure architecture diagrams, and premium insights before anyone else, &lt;strong&gt;&lt;a href="https://kamenivanov.substack.com/subscribe" rel="noopener noreferrer"&gt;subscribe to my Substack Newsletter here&lt;/a&gt;&lt;/strong&gt;!&lt;/p&gt;




&lt;h3&gt;
  
  
  Recommended Resources for Java &amp;amp; Spring Boot Engineers
&lt;/h3&gt;

&lt;p&gt;Disclosure: I earn a commission if you purchase through the links below, at no additional cost to you.&lt;/p&gt;

&lt;p&gt;If you are preparing for Senior/Lead Java interviews or looking to solidify your Spring Boot &amp;amp; Architecture skills, check out these highly-rated resources from the &lt;strong&gt;Javarevisited&lt;/strong&gt; publication &lt;em&gt;(Use promo code&lt;/em&gt; &lt;strong&gt;&lt;em&gt;friends20&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;for an exclusive&lt;/em&gt; &lt;strong&gt;&lt;em&gt;20% discount&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;automatically applied at checkout)&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the Java Interview&lt;/strong&gt;
Prepare for core Java, concurrency, JVM internals, and design pattern questions.
&lt;a href="https://gumroad.com/a/501547619/QqjGH" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/HMOAv" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the Spring Boot Interview&lt;/strong&gt;
Master Spring Core, Auto-configuration, Spring Data JPA, Security, and Microservices.
&lt;a href="https://gumroad.com/a/501547619/hrUXKY" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/pfolo" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the SQL Interview&lt;/strong&gt;
Deep-dive into query optimization, indexing, joins, and complex SQL window functions.
&lt;a href="https://gumroad.com/a/501547619/fhmehw" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/brruh" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Complete Java + Spring + SQL Interview Bundle&lt;/strong&gt;
Get all three interview guides in a single heavily discounted package.
&lt;a href="https://gumroad.com/a/501547619/gvqgjk" rel="noopener noreferrer"&gt;Get the Ultimate Interview Bundle&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spring Professional Certification Practice Questions (250+ Questions)&lt;/strong&gt;
Validating your skills? Practice with real exam-style questions before taking the Spring Professional certification.
&lt;a href="https://gumroad.com/a/501547619/sygyq" rel="noopener noreferrer"&gt;Get the Spring Professional Questions&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/qelhye" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>api</category>
      <category>architecture</category>
      <category>softwaredevelopment</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Our Async Event Tests Passed 9 Times Out of 10. The Flakiness Was Hiding a Real Race Condition</title>
      <dc:creator>Kamen</dc:creator>
      <pubDate>Fri, 06 Nov 2026 10:55:23 +0000</pubDate>
      <link>https://dev.to/kamenivanov/our-async-event-tests-passed-9-times-out-of-10-the-flakiness-was-hiding-a-real-race-condition-3773</link>
      <guid>https://dev.to/kamenivanov/our-async-event-tests-passed-9-times-out-of-10-the-flakiness-was-hiding-a-real-race-condition-3773</guid>
      <description>&lt;p&gt;The test failed about once in every ten CI runs, which is rare enough to ignore and frequent enough to hate. It covered a simple flow: an order was saved, an event was published, and an asynchronous listener sent a confirmation. The assertion at the end checked that the notification had gone out, and it sat behind a &lt;code&gt;Thread.sleep(100)&lt;/code&gt; that had been there since the first version of the test.&lt;/p&gt;

&lt;p&gt;Everyone had a theory, and everyone's theory was that the CI machines were slow. So the sleep went from 100 milliseconds to 500, the failure rate dropped, and the ticket was closed. Nobody asked the more useful question, which was why a listener that did almost no work needed any waiting at all.&lt;/p&gt;

&lt;p&gt;The answer was in production, where the same flow produced a handful of "order not found" warnings per day under load. The listener was annotated with a plain &lt;code&gt;@EventListener&lt;/code&gt; plus &lt;code&gt;@Async&lt;/code&gt;, which means the event was handed to another thread the moment it was published, while the publishing transaction was still open. Most of the time the transaction committed before the listener thread got around to querying the order. Occasionally it did not, and the listener looked for a row that did not exist yet from any other connection's point of view. The test was flaky for the same reason production was occasionally wrong: a race between a commit on one thread and a read on another. Longer sleeps only gave the commit more time to win.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F83lfju92anplspinpdq4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F83lfju92anplspinpdq4.png" alt="Event Listener" width="799" height="416"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The fix had two parts. On the production side, the listener became a &lt;code&gt;@TransactionalEventListener&lt;/code&gt; bound to the &lt;code&gt;AFTER_COMMIT&lt;/code&gt; phase, so it does not start until the data it depends on is visible to everyone.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Async&lt;/span&gt;
&lt;span class="nd"&gt;@TransactionalEventListener&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;phase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;TransactionPhase&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;AFTER_COMMIT&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;OrderPlaced&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;notifier&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;send&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;orderId&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the test side, the sleep was replaced with Awaitility, which polls for the condition and fails with a clear message if it is not met within a limit.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;await&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;atMost&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Duration&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ofSeconds&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
       &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;untilAsserted&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;verify&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;notifier&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;send&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One detail cost us an hour. A test method annotated with &lt;code&gt;@Transactional&lt;/code&gt; rolls back instead of committing, so an &lt;code&gt;AFTER_COMMIT&lt;/code&gt; listener never fires inside it. The test that exercises this flow has to commit for real, which means it cannot lean on the usual rollback cleanup and needs to remove its own data afterwards.&lt;/p&gt;

&lt;p&gt;Across 200 CI runs, the original test failed 22 times, the 500 millisecond version failed 6 times, and the version with the corrected listener and Awaitility failed none.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Foljvgyddfazcx9pttown.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Foljvgyddfazcx9pttown.png" alt="Failed runs" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The lesson is not about Awaitility, а sleep in a test is a guess about timing, and when the guess is right only most of the time, the failures are often reporting something real about the code. Raising the timeout makes the report quieter without making it wrong.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;More like this?&lt;/strong&gt;&lt;br&gt;
I write short production war stories like this one, plus a deep-dive architecture series. &lt;a href="https://kamenivanov.substack.com/subscribe" rel="noopener noreferrer"&gt;Subscribe here&lt;/a&gt; if you want both in your inbox.&lt;/p&gt;




&lt;h3&gt;
  
  
  Recommended Resources for Java &amp;amp; Spring Boot Engineers
&lt;/h3&gt;

&lt;p&gt;If you are preparing for Senior/Lead Java interviews or looking to solidify your Spring Boot &amp;amp; Architecture skills, check out these highly-rated resources from the &lt;strong&gt;Javarevisited&lt;/strong&gt; publication &lt;em&gt;(Use promo code&lt;/em&gt; &lt;strong&gt;&lt;em&gt;friends20&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;for an exclusive&lt;/em&gt; &lt;strong&gt;&lt;em&gt;20% discount&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;automatically applied at checkout)&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the Java Interview&lt;/strong&gt;
Prepare for core Java, concurrency, JVM internals, and design pattern questions.
&lt;a href="https://gumroad.com/a/501547619/QqjGH" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/HMOAv" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the Spring Boot Interview&lt;/strong&gt;
Master Spring Core, Auto-configuration, Spring Data JPA, Security, and Microservices.
&lt;a href="https://gumroad.com/a/501547619/hrUXKY" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/pfolo" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the SQL Interview&lt;/strong&gt;
Deep-dive into query optimization, indexing, joins, and complex SQL window functions.
&lt;a href="https://gumroad.com/a/501547619/fhmehw" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/brruh" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Complete Java + Spring + SQL Interview Bundle&lt;/strong&gt;
Get all three interview guides in a single heavily discounted package.
&lt;a href="https://gumroad.com/a/501547619/gvqgjk" rel="noopener noreferrer"&gt;Get the Ultimate Interview Bundle&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spring Professional Certification Practice Questions (250+ Questions)&lt;/strong&gt;
Validating your skills? Practice with real exam-style questions before taking the Spring Professional certification.
&lt;a href="https://gumroad.com/a/501547619/sygyq" rel="noopener noreferrer"&gt;Get the Spring Professional Questions&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/qelhye" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>debugging</category>
      <category>java</category>
      <category>softwareengineering</category>
      <category>testing</category>
    </item>
    <item>
      <title>The Schema Three Chapters Were Already Relying On (Chapter 14)</title>
      <dc:creator>Kamen</dc:creator>
      <pubDate>Mon, 02 Nov 2026 09:28:02 +0000</pubDate>
      <link>https://dev.to/kamenivanov/the-schema-three-chapters-were-already-relying-on-chapter-14-21j</link>
      <guid>https://dev.to/kamenivanov/the-schema-three-chapters-were-already-relying-on-chapter-14-21j</guid>
      <description>&lt;p&gt;Chapter 12 spent several pages arguing that the only guard against assigning the same product to the same category twice is a unique constraint on &lt;code&gt;(category_id, product_id)&lt;/code&gt;. It rejected a pessimistic read lock on the grounds that a lock delays a conflicting change rather than preventing it, settled on reading current state to classify a batch, and said plainly that the database is the only real arbiter of the race that remains.&lt;/p&gt;

&lt;p&gt;That constraint did not exist. Neither did the foreign key with &lt;code&gt;ON DELETE CASCADE&lt;/code&gt; that the same chapter called a backstop, nor the &lt;code&gt;BINARY(16)&lt;/code&gt; column that the version 7 UUID decision quietly depends on. Three chapters of reasoning had been written against a schema that was still a sentence in a paragraph, and the previous chapter configured Flyway to apply migrations without there being a migration to apply.&lt;/p&gt;

&lt;p&gt;Writing it is where several earlier decisions stop being arguments and become something a database will enforce or refuse. A few of them also turn out to have been wrong, or at least incomplete, in ways only visible once the columns have types.&lt;/p&gt;




&lt;h2&gt;
  
  
  One file, or one per table
&lt;/h2&gt;

&lt;p&gt;Before any of that configuration does anything, there has to be something in &lt;code&gt;classpath:db/migrations&lt;/code&gt;, and there are two questions before the SQL: where the file lives and how many files there are.&lt;/p&gt;

&lt;p&gt;It lives in &lt;code&gt;dao-impl&lt;/code&gt;, with the entities, rather than in the application module that configured Flyway. The application module owns the wiring and says where to look, the module that owns persistence owns the shape of it. A schema in &lt;code&gt;application&lt;/code&gt; would mean that the module which knows what a &lt;code&gt;ProductEntity&lt;/code&gt; is and the module which knows what the &lt;code&gt;products&lt;/code&gt; table is are two different modules, and every column change would be a change in both. &lt;code&gt;classpath:&lt;/code&gt; is what makes this work without anyone declaring it: Flyway looks on the classpath and &lt;code&gt;dao-impl&lt;/code&gt; is on it, so the application never names the module that holds the migration.&lt;/p&gt;

&lt;p&gt;The second question is how many files it should be. One per table is the tempting answer, because it mirrors the code, where each entity has its own class. I went with one, and the reason is that a migration is not a unit of structure, it is a unit of change. These four tables are not four changes. &lt;code&gt;products&lt;/code&gt; has a foreign key to &lt;code&gt;product_specifications&lt;/code&gt;, &lt;code&gt;category_assignments&lt;/code&gt; has foreign keys to two of the others, and that fixes the only order they can be created in. Split into four files, two of them cannot be applied on their own at all and a third can only follow them. Flyway would run the four in sequence and arrive at the same schema, so nothing would break, but the history would now record four events where one happened, and a developer reading a &lt;code&gt;create_products&lt;/code&gt; migration in isolation would be looking at a fragment of a decision rather than a decision.&lt;/p&gt;

&lt;p&gt;The rule that follows is short enough to state once and then follow for the rest of the project. One file per change, named after the change. The baseline is a single file because the baseline is one change: the schema this application starts life with, which is why it is called &lt;code&gt;V01.00.00__init.sql&lt;/code&gt; rather than named after a table. The three-part version is a release number rather than a counter, so a migration's name says which version of the product introduced it, and ordering by filename still gives Flyway the sequence it needs. Everything after it gets its own file, whether it touches one table or four.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="nv"&gt;`categories`&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nv"&gt;`id`&lt;/span&gt;            &lt;span class="nb"&gt;BINARY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                                &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;`created_at`&lt;/span&gt;    &lt;span class="nb"&gt;DATETIME&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                               &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="k"&gt;CURRENT_TIMESTAMP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nv"&gt;`updated_at`&lt;/span&gt;    &lt;span class="nb"&gt;DATETIME&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                               &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="k"&gt;CURRENT_TIMESTAMP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="k"&gt;CURRENT_TIMESTAMP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nv"&gt;`created_by_id`&lt;/span&gt; &lt;span class="nb"&gt;BINARY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                                &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;`updated_by_id`&lt;/span&gt; &lt;span class="nb"&gt;BINARY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                                &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;`version`&lt;/span&gt;       &lt;span class="nb"&gt;INTEGER&lt;/span&gt;                                   &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;`name`&lt;/span&gt;          &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                              &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;`description`&lt;/span&gt;   &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                                      &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;`status`&lt;/span&gt;        &lt;span class="nb"&gt;ENUM&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'INACTIVE'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'ACTIVE'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'ARCHIVED'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;`id`&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="nv"&gt;`uk_categories_name`&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;`name`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;ENGINE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;InnoDB&lt;/span&gt;
  &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="n"&gt;CHARSET&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;utf8mb4&lt;/span&gt;
  &lt;span class="k"&gt;COLLATE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;utf8mb4_0900_ai_ci&lt;/span&gt;
  &lt;span class="n"&gt;ROW_FORMAT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;DYNAMIC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is &lt;code&gt;categories&lt;/code&gt;, and its first six columns are the audit base classes rendered into SQL. They repeat verbatim in &lt;code&gt;products&lt;/code&gt;, which adds a name, a SKU, a price, a status and a nullable &lt;code&gt;specification_id&lt;/code&gt;, and in &lt;code&gt;product_specifications&lt;/code&gt;, which adds a dimension and a weight. &lt;code&gt;category_assignments&lt;/code&gt; is the exception and carries only three of them, having dropped &lt;code&gt;updated_at&lt;/code&gt;, &lt;code&gt;updated_by_id&lt;/code&gt; and &lt;code&gt;version&lt;/code&gt; along with the base class that declares them, and added the two columns it exists for. The whole file is in the repository at this chapter's tag, and the part of it worth reading inline is the bottom of that last table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="no"&gt;PRIMARY&lt;/span&gt; &lt;span class="nf"&gt;KEY&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
    &lt;span class="no"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="n"&gt;uk_assignments_category_product&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt; &lt;span class="no"&gt;UNIQUE&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="n"&gt;category_id&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
    &lt;span class="no"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="n"&gt;fk_assignments_category&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt; &lt;span class="no"&gt;FOREIGN&lt;/span&gt; &lt;span class="nf"&gt;KEY&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="n"&gt;category_id&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="no"&gt;REFERENCES&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="n"&gt;categories&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
    &lt;span class="no"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="n"&gt;fk_assignments_product&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt; &lt;span class="no"&gt;FOREIGN&lt;/span&gt; &lt;span class="nf"&gt;KEY&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="no"&gt;REFERENCES&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="n"&gt;products&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="no"&gt;ON&lt;/span&gt; &lt;span class="no"&gt;DELETE&lt;/span&gt; &lt;span class="no"&gt;CASCADE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Sixteen bytes, in the right order
&lt;/h2&gt;

&lt;p&gt;The identifier column is &lt;code&gt;BINARY(16)&lt;/code&gt;, which is sixteen bytes rather than the thirty-six a &lt;code&gt;CHAR(36)&lt;/code&gt; would take, and this is where the &lt;code&gt;@UuidGenerator(style = VERSION_7)&lt;/code&gt; decision from Chapter 12 collects the rest of its payment. In InnoDB the primary key is the clustered index, so rows are physically stored in primary key order and every insert goes wherever its key sorts. A version 4 UUID is random, which means inserts land uniformly across the whole index, and a table larger than the buffer pool starts reading pages from disk in order to write to them. A version 7 UUID carries a millisecond timestamp in its leading bits, so in the byte ordering that &lt;code&gt;BINARY(16)&lt;/code&gt; preserves, successive inserts sort next to each other and the writes concentrate on the right-hand edge of the index. Chapter 12 wanted version 7 because in-memory id assignment is what lets Hibernate batch inserts at all. This is the second half of the same decision, and it only holds if the column preserves the ordering, which the hyphenated text form in a &lt;code&gt;CHAR(36)&lt;/code&gt; does not.&lt;/p&gt;




&lt;h2&gt;
  
  
  A specification with a life of its own
&lt;/h2&gt;

&lt;p&gt;The specification lives in its own table rather than as two more columns on &lt;code&gt;products&lt;/code&gt;, and that is a modelling decision rather than a normalisation reflex. A specification is a thing with an identity and a life of its own: it is created, it is amended, and it is audited, which is why it carries the same audit columns as everything else here. Flattening it into &lt;code&gt;products&lt;/code&gt; would mean a product row that cannot say who last changed its dimensions as distinct from who last changed its price. The cost is a join on every read that needs it and a nullable foreign key, since a product can exist before anyone has measured it.&lt;/p&gt;

&lt;p&gt;The uniqueness on &lt;code&gt;specification_id&lt;/code&gt; is what makes the relationship actually one to one. A foreign key on its own permits two products to point at the same specification row, and the day that happens, editing one product's weight silently edits another's. The unique constraint forbids it at the only level that can be trusted, and it conveniently supplies the index InnoDB requires on the referencing side of the foreign key, so the constraint earns its keep twice. It also does not get in the way of the nullable column, because MySQL treats each &lt;code&gt;NULL&lt;/code&gt; as distinct for uniqueness purposes, so any number of products can be waiting to be measured while no two can share a measurement.&lt;/p&gt;

&lt;p&gt;What the schema cannot express is the other half of that relationship. Deleting a product leaves its specification behind as an orphan, because the foreign key points from product to specification and a cascade in that direction would be backwards. There is no &lt;code&gt;ON DELETE&lt;/code&gt; clause that fixes this, which is why the association on &lt;code&gt;ProductEntity&lt;/code&gt; carries &lt;code&gt;orphanRemoval = true&lt;/code&gt; and &lt;code&gt;CascadeType.ALL&lt;/code&gt;, leaving Hibernate to issue the second delete. Worth being explicit that this is now a rule living in the mapping rather than in the database, which means a row deleted by hand in a SQL client will leave the orphan the application would have cleaned up. That is an acceptable asymmetry as long as it is a known one.&lt;/p&gt;




&lt;h2&gt;
  
  
  The column that changes behaviour
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;version&lt;/code&gt; column is the one addition here that changes behaviour rather than describing it. Paired with a &lt;code&gt;@Version private Integer version&lt;/code&gt; on &lt;code&gt;AbstractUpdatableEntity&lt;/code&gt;, it gives Hibernate an optimistic lock: every update carries the version it read in its &lt;code&gt;WHERE&lt;/code&gt; clause and increments it, so an update that matches no rows means someone else got there first. This is the problem Chapter 12 looked at and declined to solve with a pessimistic read lock, on the grounds that holding a lock only delays a conflicting change rather than preventing it. Optimistic locking makes a narrower and more useful promise. It does not order concurrent operations, it detects a lost update: two requests both read a product at version 3, both compute a new state from it, and without this column the second write silently discards the first. With it, the second write fails and the caller finds out.&lt;/p&gt;

&lt;p&gt;That failure needs somewhere to go, and as the exception handler stands at the end of Chapter 11, it does not have one. Spring translates the Hibernate failure into &lt;code&gt;ObjectOptimisticLockingFailureException&lt;/code&gt;, which descends from &lt;code&gt;TransientDataAccessException&lt;/code&gt; rather than from &lt;code&gt;DataIntegrityViolationException&lt;/code&gt;, so the integrity-violation handler that catches the unique-constraint race does not catch this one. It falls through to the &lt;code&gt;Throwable&lt;/code&gt; mapping and becomes a 500, which is wrong in the way that matters: the request was not malformed and the server is not broken, the state moved underneath it, and retrying is a reasonable thing for the caller to do. It belongs with the other conflicts, as a 409, and adding it is a two-line change to a class that already has the shape for it. Which base class gets the field is worth deciding on purpose rather than by instinct, and the instinct is to put it next to the identifier so everything in the system has one. A version number is a guard on update, so the concept arrives with the class that introduces updating, exactly as Chapter 10 argued for &lt;code&gt;changeStatus()&lt;/code&gt;. One level higher and every creation-only table carries a column that can never hold anything but zero. &lt;code&gt;category_assignments&lt;/code&gt; is such a table, which is why it is the only one here without the column, and that absence is the schema agreeing with the code rather than an omission.&lt;/p&gt;

&lt;p&gt;There is a consequence of that choice sitting directly on top of Chapter 12's batching. Spring Data decides between &lt;code&gt;persist&lt;/code&gt; and &lt;code&gt;merge&lt;/code&gt; by looking at the version property first when that property is of a non-primitive type, treating &lt;code&gt;null&lt;/code&gt; as new, and falling back to inspecting the identifier when there is no such property. So products and categories are classified by their version, while &lt;code&gt;CategoryAssignmentEntity&lt;/code&gt;, having none, is classified by its id. Both land on &lt;code&gt;persist&lt;/code&gt;, and that is precisely what keeps &lt;code&gt;saveAllAndFlush&lt;/code&gt; batching: a &lt;code&gt;merge&lt;/code&gt; would issue a select before every insert, turning a two-hundred-item assignment from four round trips into two hundred selects and two hundred inserts. Nothing would break, no test would fail, and the performance decision from the previous chapter would have quietly gone. Giving the assignment entity a version it does not need, or assigning its id in the transformer before persistence, are both ways to cause that. The field has to be a wrapper rather than a primitive for the same reason. &lt;code&gt;int&lt;/code&gt; cannot be null, so Spring Data stops asking the version and falls back to the identifier, which here still happens to answer correctly because Hibernate assigns the id during persistence. The behaviour would be right by accident rather than by design, and the next change to how ids are assigned would quietly take it away.&lt;/p&gt;

&lt;p&gt;It is also the only field in these classes with no getter and no setter, and that is intentional rather than an oversight. The annotations in this project sit on fields, so Hibernate reads and writes through reflection and needs neither. What a setter would add is the ability for something in the application to assign a version, and assigning a version is the only way to defeat the lock from the inside: a transformer that mechanically copies every property would eventually copy a number read minutes earlier and turn the check into a comparison with itself. A version is established by persistence, not set by callers, which is the same argument Chapter 10 made for &lt;code&gt;changeStatus()&lt;/code&gt; over a status setter. A short comment saying the field exists for Hibernate is worth adding, because a field with no accessors looks unused to every static analyser and to the next person tidying up.&lt;/p&gt;

&lt;p&gt;Two things changed behaviour the moment the column was mapped, and neither is in the paragraph above. The first is deletion. The DAO deletes by loading the row and handing the entity to the repository, and once there is a version Hibernate puts it in the &lt;code&gt;DELETE&lt;/code&gt; statement's &lt;code&gt;WHERE&lt;/code&gt; clause, so a row that changed between the load and the delete now refuses to be deleted. That is the correct answer - you are deleting something that is no longer what you read, but it's a second path to the same unhandled exception, which makes the missing handler slightly more than a convenience.&lt;/p&gt;

&lt;p&gt;The second was a bug the column exposed rather than caused. Updating a product used to replace its specification with a freshly constructed entity built from the domain object. With &lt;code&gt;orphanRemoval&lt;/code&gt; that already meant a delete and an insert where an update was intended, which was wasteful and worked. With a version it stops working: the new entity carries an identifier that already exists and a version that is null, so it is classified as new, and &lt;code&gt;persist&lt;/code&gt; with an occupied identifier is not a quiet failure. A persistence test caught it within minutes of the field being added, which is the first time in this chapter that something failed loudly rather than being found by reading two files side by side. The test was &lt;code&gt;AbstractCrudTestCase.testUpdate&lt;/code&gt;, the shared base case from Chapter 7 that every entity inherits, so nothing had been written to look for this.&lt;/p&gt;

&lt;p&gt;What it reported was this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ObjectOptimisticLockingFailureException: Row was already updated or deleted by another
transaction for entity [ProductSpecificationEntity with id '01a19b12-...']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nobody had updated or deleted anything. There was one thread and one transaction. The replacement entity carried an identifier that already existed and a version of null, so the statement Hibernate issued could not match any row, and zero rows updated is indistinguishable, at the layer that counts them, from somebody else having got there first. The message is not wrong so much as it is reporting the only conclusion available to it, which is worth remembering the next time one of these appears in a log: it says a row moved, and it means a statement matched nothing.&lt;/p&gt;

&lt;p&gt;The fix is to copy into the specification that was loaded rather than to replace it. The reason it is worth telling is that the schema change did not break the code - it revealed that the code had been describing the wrong operation all along, and that a test had been passing because nothing was checking. What this version protects is narrower than it first appears, the lock travels with the entity inside a single unit of work, so it covers the DAO's own load-then-write and not the wider window between the service reading a product and the service writing it back. Closing that one means carrying the version out through the domain type and into the API, which is a conversation about request headers and belongs to the chapter that builds the REST layer. The column is a prerequisite for it rather than a substitute.&lt;/p&gt;




&lt;h2&gt;
  
  
  Enums and timestamps, stored on purpose
&lt;/h2&gt;

&lt;p&gt;Both status columns are a MySQL &lt;code&gt;ENUM&lt;/code&gt; holding the names from the Java enum, and neither is a &lt;code&gt;VARCHAR&lt;/code&gt; nor an ordinal. The ordinal is the easy one to reject, storing &lt;code&gt;ProductStatus.ACTIVE&lt;/code&gt; as &lt;code&gt;2&lt;/code&gt; means the database's meaning of &lt;code&gt;2&lt;/code&gt; is defined by the declaration order of a Java enum, and inserting a new constant in the middle of that enum silently reinterprets every existing row. Nothing in the database changed and every row now means something else.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;VARCHAR(32)&lt;/code&gt; is where I started, and it is the defensible default: it costs a few bytes a row, it is readable in a query result, and it leaves the set of legal values entirely to the application. What it does not do is refuse anything. A column that accepts any thirty-two characters will accept &lt;code&gt;'ACTIVEE'&lt;/code&gt;, and the row that results is one no code path can produce and no code path can handle, which makes it the kind of data that is discovered months later by a &lt;code&gt;switch&lt;/code&gt; that throws.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;ENUM&lt;/code&gt; refuses it. In strict mode the insert is an error rather than a row, and that is worth more here than it would be in most places, because these two enums are not open sets. &lt;code&gt;ProductStatus&lt;/code&gt; and &lt;code&gt;CategoryStatus&lt;/code&gt; declare transitions in code - &lt;code&gt;canTransitionTo&lt;/code&gt; on each constant, which means the legal values are not a list somebody appends to as the business grows but a lifecycle with rules attached. A new status is never only a new string: it is a new set of transitions, a decision about what may become it and what it may become, and work in the service layer. Something that can never be added casually is exactly the kind of thing that can afford to be schema.&lt;/p&gt;

&lt;p&gt;The usual objection is the second source of truth, and it holds: the set now exists in the Java enum and in the column definition, and the migration that adds a value has to be deployed before the code that writes it. For a list that genuinely grows that is a tax with nothing to show for it. For a lifecycle it is closer to a feature, because the deployment order it forces is the correct one anyway - the database should know about a state before a request can put a row into it.&lt;/p&gt;

&lt;p&gt;The cost is elsewhere, in the comparison semantics. An &lt;code&gt;ENUM&lt;/code&gt; column is an index internally, so &lt;code&gt;WHERE status = 2&lt;/code&gt; quietly matches the second member of the list and &lt;code&gt;ORDER BY status&lt;/code&gt; sorts by declaration order rather than alphabetically. That is the ordinal hazard reappearing in queries after being rejected in storage, and the only mitigation is the one taken here: declare the values in the same order as the Java enum, so the index order is the lifecycle order and a sort by status at least means something. Anyone writing SQL against these columns by hand should compare against the string.&lt;/p&gt;

&lt;p&gt;One asymmetry to know about rather than fix. The persistence tests from Chapter 7 run on H2 with the schema generated from the entities, so the column there is whatever &lt;code&gt;@Enumerated(STRING)&lt;/code&gt; produces and not an &lt;code&gt;ENUM&lt;/code&gt; at all. The guard this section is about exists in production and nowhere else, and no test touches it. That is acceptable because the application is not the thing the guard protects against - it already writes only valid names, but it does mean the protection is the one decision in this file that the test suite cannot confirm.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;DATETIME(3)&lt;/code&gt; rather than &lt;code&gt;DATETIME&lt;/code&gt; is not cosmetic. MySQL's default temporal precision is whole seconds, so a plain &lt;code&gt;DATETIME&lt;/code&gt; column truncates everything below the second without complaint, and &lt;code&gt;Instant&lt;/code&gt; carries far more than that. The failure that produces is one of the quieter ones: two rows written a few milliseconds apart come back with identical &lt;code&gt;created_at&lt;/code&gt; values, an ordering by creation time becomes non-deterministic, and a test that sorts by timestamp passes or fails depending on how fast the machine is. Three digits is a deliberate stopping point rather than the maximum, because milliseconds are the finest granularity anything in this system actually reasons about, and it is worth knowing that the H2 database behind the persistence tests from Chapter 7 does not truncate the same way, so a timestamp compared across a round trip behaves slightly differently in a test than in production.&lt;/p&gt;

&lt;p&gt;The database-side defaults on those columns deserve a note precisely because they almost never fire. Hibernate includes every mapped column in both its inserts and its updates, so the timestamps always arrive with a value, and an explicitly supplied value is also the condition under which MySQL skips &lt;code&gt;ON UPDATE CURRENT_TIMESTAMP(3)&lt;/code&gt;. They are not redundancy for its own sake: they are what makes a row written outside the application, by a later migration or by someone at a SQL prompt, come out with sane audit timestamps instead of zeroes.&lt;/p&gt;




&lt;h2&gt;
  
  
  Constraints as the only real arbiter
&lt;/h2&gt;

&lt;p&gt;The unique constraint on &lt;code&gt;(category_id, product_id)&lt;/code&gt; is the one Chapter 12 leaned on. That chapter walked through several ways of preventing the same product from being assigned to the same category twice, rejected a pessimistic read lock, and settled on reading current state to classify the batch while letting the database be the only real arbiter of the race that remains. This line is that arbiter, two concurrent requests assigning the same product both read an empty assignment table, both decide the insert is needed, and one of them receives a &lt;code&gt;DataIntegrityViolationException&lt;/code&gt; that the handler turns into a 409.&lt;/p&gt;

&lt;p&gt;The two foreign keys out of &lt;code&gt;category_assignments&lt;/code&gt; are asymmetric. Deleting a product cascades to its assignments, because an assignment to a product that no longer exists is not data, it is a dangling row, and Chapter 12 described this as a backstop rather than a mechanism: the service deletes assignments explicitly, and the cascade covers the paths the service does not own. The category side has no cascade, which means deleting a category with products still assigned to it fails. That is intentional, categories are platform-owned taxonomy and archiving is the operation that retires one, so a delete that quietly took assignments with it would be silent data loss dressed up as convenience. There is one index declared outright, on &lt;code&gt;products.created_at&lt;/code&gt;, and it is there because creation time is how a product list is ordered and paged, which is a read pattern rather than a constraint and so has no other line in the file to arrive on.&lt;/p&gt;

&lt;p&gt;There is also one index nobody declared and the system depends on anyway. The unassign path queries assignments by &lt;code&gt;product_id&lt;/code&gt; alone, which the unique constraint on &lt;code&gt;(category_id, product_id)&lt;/code&gt; cannot serve, since a composite index is only usable from its leftmost column. InnoDB requires an index on the referencing columns of every foreign key and creates one silently when the schema does not provide it, so &lt;code&gt;fk_assignments_product&lt;/code&gt; leaves a usable &lt;code&gt;product_id&lt;/code&gt; index behind as a side effect. This is a MySQL behaviour rather than a general one: PostgreSQL does not index foreign key columns, and the same schema ported there would quietly turn that query into a sequential scan. Depending on it is fine as long as the dependency is known rather than accidental, which is why it is written down here.&lt;/p&gt;




&lt;h2&gt;
  
  
  A migration that cannot fail is a suggestion
&lt;/h2&gt;

&lt;p&gt;One thing the file does not say is &lt;code&gt;IF NOT EXISTS&lt;/code&gt;. It reads as harmless defensiveness, and in a hand-run script it would be. Inside a Flyway migration it undoes work the rest of this chapter paid for. Flyway already guarantees that each migration runs exactly once by recording it in &lt;code&gt;flyway_schema_history&lt;/code&gt;, so the only situation in which &lt;code&gt;IF NOT EXISTS&lt;/code&gt; has anything to do is one where the table is already there and Flyway does not know about it, which is exactly the situation that should stop the application rather than be smoothed over. The migration would report success, the table would keep whatever shape it already had, and &lt;code&gt;setValidateOnMigrate(true)&lt;/code&gt; would have been satisfied by a history entry describing a schema change that never happened. A migration that cannot fail is not a migration, it is a suggestion.&lt;/p&gt;




&lt;h2&gt;
  
  
  What the schema enforces
&lt;/h2&gt;

&lt;p&gt;Reading the file back, the migration turns out to be where four earlier chapters get audited. The UUID version from Chapter 12 only pays off because the column preserves byte ordering. The batching from the same chapter only survives because one table intentionally lacks a column. The &lt;code&gt;DataIntegrityViolationException&lt;/code&gt; handler from Chapter 11 only has something to catch because of one &lt;code&gt;UNIQUE&lt;/code&gt; line, and it turns out to be one handler short, because the optimistic lock this migration introduces produces a different exception family that currently becomes a 500.&lt;/p&gt;

&lt;p&gt;That last one is the pattern worth taking away. Most of these are not bugs in the sense of something failing: the application starts and a reviewer reading either the Java or the SQL on its own would find nothing wrong. The specification update is the exception, and it is instructive for being one - it had been wrong for several chapters and only became detectable once a column existed that could notice. They are disagreements between two files that are only visible when you put them side by side and ask what each one assumes about the other. A schema is where those assumptions become checkable, which is a good argument for writing it deliberately rather than letting a tool generate it from the entities.&lt;/p&gt;




&lt;h3&gt;
  
  
  What's Next?
&lt;/h3&gt;

&lt;p&gt;The application starts, the schema exists, and fourteen chapters of code still cannot answer a single request. There is no controller, no mapping, no request body, and the handler from Chapter 11 has nothing to hand a response back to.&lt;/p&gt;

&lt;p&gt;Chapter 15 builds the REST layer, and it inherits two unfinished things from this one. The optimistic lock introduced here produces an exception family the handler does not catch, which is a two-line change and a 409. And the version column only protects a single unit of work, so making it protect the window between a client reading a product and writing it back means carrying the version out through the domain type and into the API — which is a conversation about request headers, conditional requests and what a client is expected to send back.&lt;/p&gt;

&lt;p&gt;The module boundary gets tested for the first time too. The REST module holds the interfaces and the DTOs and depends on Jackson and the Swagger annotations rather than on &lt;code&gt;spring-boot-starter-web&lt;/code&gt;, which is a claim the previous chapter made and this one has no way to check. The first &lt;code&gt;@WebMvcTest&lt;/code&gt; is also what finally settles whether declaring &lt;code&gt;@ComponentScan&lt;/code&gt; by hand costs the filters that slice tests rely on, a question Part 1 of Chapter 13 reasoned about and left open.&lt;/p&gt;




&lt;h3&gt;
  
  
  Codebase &amp;amp; Architecture Blueprint
&lt;/h3&gt;

&lt;p&gt;The entire evolutionary architecture of this project is tracked using strict Git tags. To clone the repository and switch exactly to the state established in Chapter 14, use the following link:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GitHub Repository (Tag: &lt;code&gt;chapter-14-schema&lt;/code&gt;):&lt;/strong&gt; &lt;a href="https://github.com/KamenIvanov/advanced-spring-multimodule/tree/chapter-14-schema" rel="noopener noreferrer"&gt;advanced-spring-multimodule&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note: All core modules are configured with strict compilation-level boundaries. Compile and run &lt;code&gt;mvn clean install&lt;/code&gt; to see the structure in action. Maven version 3.9.* and Java 25 are required.&lt;/p&gt;

&lt;p&gt;◀️ &lt;a href="https://dev.to/kamenivanov/bootstrapping-the-system-part-3-making-it-run-chapter-13-eig"&gt;Read Chapter 13 Part 3: Bootstrapping the System, Making It Run&lt;/a&gt;&lt;br&gt;
▶️ &lt;a href="https://dev.to/kamenivanov/the-contract-ships-before-the-server-does-chapter-15-1j4m"&gt;Read Chapter 15: The API Contract&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Join the Masterclass Journey
&lt;/h3&gt;

&lt;p&gt;This article is part of an ongoing weekly series. If you want to receive every upcoming chapter directly in your inbox, alongside deep-dives, infrastructure architecture diagrams, and premium insights before anyone else, &lt;strong&gt;&lt;a href="https://kamenivanov.substack.com/subscribe" rel="noopener noreferrer"&gt;subscribe to my Substack Newsletter here&lt;/a&gt;&lt;/strong&gt;!&lt;/p&gt;




&lt;h3&gt;
  
  
  Recommended Resources for Java &amp;amp; Spring Boot Engineers
&lt;/h3&gt;

&lt;p&gt;Disclosure: I earn a commission if you purchase through the links below, at no additional cost to you.&lt;/p&gt;

&lt;p&gt;If you are preparing for Senior/Lead Java interviews or looking to solidify your Spring Boot &amp;amp; Architecture skills, check out these highly-rated resources from the &lt;strong&gt;Javarevisited&lt;/strong&gt; publication &lt;em&gt;(Use promo code&lt;/em&gt; &lt;strong&gt;&lt;em&gt;friends20&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;for an exclusive&lt;/em&gt; &lt;strong&gt;&lt;em&gt;20% discount&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;automatically applied at checkout)&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the Java Interview&lt;/strong&gt;
Prepare for core Java, concurrency, JVM internals, and design pattern questions.
&lt;a href="https://gumroad.com/a/501547619/QqjGH" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/HMOAv" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the Spring Boot Interview&lt;/strong&gt;
Master Spring Core, Auto-configuration, Spring Data JPA, Security, and Microservices.
&lt;a href="https://gumroad.com/a/501547619/hrUXKY" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/pfolo" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the SQL Interview&lt;/strong&gt;
Deep-dive into query optimization, indexing, joins, and complex SQL window functions.
&lt;a href="https://gumroad.com/a/501547619/fhmehw" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/brruh" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Complete Java + Spring + SQL Interview Bundle&lt;/strong&gt;
Get all three interview guides in a single heavily discounted package.
&lt;a href="https://gumroad.com/a/501547619/gvqgjk" rel="noopener noreferrer"&gt;Get the Ultimate Interview Bundle&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spring Professional Certification Practice Questions (250+ Questions)&lt;/strong&gt;
Validating your skills? Practice with real exam-style questions before taking the Spring Professional certification.
&lt;a href="https://gumroad.com/a/501547619/sygyq" rel="noopener noreferrer"&gt;Get the Spring Professional Questions&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/qelhye" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>architecture</category>
      <category>backend</category>
      <category>database</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Our Redis cache was serving prices from 3 Hours ago. The invalidation strategy that looked correct on paper</title>
      <dc:creator>Kamen</dc:creator>
      <pubDate>Thu, 29 Oct 2026 11:25:51 +0000</pubDate>
      <link>https://dev.to/kamenivanov/our-redis-cache-was-serving-prices-from-3-hours-ago-the-invalidation-strategy-that-looked-correct-8ak</link>
      <guid>https://dev.to/kamenivanov/our-redis-cache-was-serving-prices-from-3-hours-ago-the-invalidation-strategy-that-looked-correct-8ak</guid>
      <description>&lt;p&gt;A support ticket arrived with a screenshot of a product page showing one price and a checkout page showing another. The price had been changed that morning, and the cache had been serving the old value for nearly three hours.&lt;/p&gt;

&lt;p&gt;On paper, our invalidation was textbook, prices were read through a cache-aside pattern: check Redis, fall back to the database on a miss, store the result with a three-hour TTL. When a price changed, the service updated the database and then evicted the key. Update, then evict, it is the sequence most articles recommend, and the code reviewers had approved it for exactly that reason.&lt;/p&gt;

&lt;p&gt;The flaw was where the eviction ran. It sat inside the same transaction as the update, which meant it executed before the commit. For a short window, Redis was empty while the database still held the old price. If a reader arrived in that window, it missed the cache, read the old price from the database, and wrote it back to Redis with a fresh three-hour TTL. Then the writer committed. The database now held the new price, the cache held the old one, and nothing was scheduled to correct it until the TTL expired.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftznj1v62vxsv5fjeja6j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftznj1v62vxsv5fjeja6j.png" alt="Eviction flow" width="800" height="501"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The window was only a few milliseconds wide, which is why it took a busy morning and a bulk price change on popular products to expose it. A product page that receives dozens of requests a second will put a reader into a few-millisecond window surprisingly often, and we ship price changes in bulk.&lt;/p&gt;

&lt;p&gt;Two changes closed it. The eviction now runs after the transaction commits, so a reader that misses the cache can only ever load a value that is already final. And the TTL dropped from three hours to five minutes as a backstop. The after-commit eviction handles the normal case, and the TTL bounds the damage when something bypasses it, such as a direct database update during a data fix, or an eviction that fails because Redis was briefly unreachable.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fi19vpdlpvxnz0ni3dni3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fi19vpdlpvxnz0ni3dni3.png" alt="Cache TTL" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The order "update, then evict" is only half of a correct strategy. The other half is when the eviction becomes visible relative to the commit, and a long TTL turns any mistake in that timing into a long-lived one. Treat the TTL as the safety net for your invalidation logic, not as a freshness policy, and size it by how long you can tolerate being wrong.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;More like this?&lt;/strong&gt;&lt;br&gt;
I write short production war stories like this one, plus a deep-dive architecture series. &lt;a href="https://kamenivanov.substack.com/subscribe" rel="noopener noreferrer"&gt;Subscribe here&lt;/a&gt; if you want both in your inbox.&lt;/p&gt;




&lt;h3&gt;
  
  
  Recommended Resources for Java &amp;amp; Spring Boot Engineers
&lt;/h3&gt;

&lt;p&gt;If you are preparing for Senior/Lead Java interviews or looking to solidify your Spring Boot &amp;amp; Architecture skills, check out these highly-rated resources from the &lt;strong&gt;Javarevisited&lt;/strong&gt; publication &lt;em&gt;(Use promo code&lt;/em&gt; &lt;strong&gt;&lt;em&gt;friends20&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;for an exclusive&lt;/em&gt; &lt;strong&gt;&lt;em&gt;20% discount&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;automatically applied at checkout)&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the Java Interview&lt;/strong&gt;
Prepare for core Java, concurrency, JVM internals, and design pattern questions.
&lt;a href="https://gumroad.com/a/501547619/QqjGH" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/HMOAv" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the Spring Boot Interview&lt;/strong&gt;
Master Spring Core, Auto-configuration, Spring Data JPA, Security, and Microservices.
&lt;a href="https://gumroad.com/a/501547619/hrUXKY" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/pfolo" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the SQL Interview&lt;/strong&gt;
Deep-dive into query optimization, indexing, joins, and complex SQL window functions.
&lt;a href="https://gumroad.com/a/501547619/fhmehw" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/brruh" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Complete Java + Spring + SQL Interview Bundle&lt;/strong&gt;
Get all three interview guides in a single heavily discounted package.
&lt;a href="https://gumroad.com/a/501547619/gvqgjk" rel="noopener noreferrer"&gt;Get the Ultimate Interview Bundle&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spring Professional Certification Practice Questions (250+ Questions)&lt;/strong&gt;
Validating your skills? Practice with real exam-style questions before taking the Spring Professional certification.
&lt;a href="https://gumroad.com/a/501547619/sygyq" rel="noopener noreferrer"&gt;Get the Spring Professional Questions&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/qelhye" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>softwareengineering</category>
      <category>debugging</category>
      <category>backend</category>
      <category>database</category>
    </item>
    <item>
      <title>Bootstrapping the System, Part 3: Making It Run (Chapter 13)</title>
      <dc:creator>Kamen</dc:creator>
      <pubDate>Mon, 26 Oct 2026 08:13:18 +0000</pubDate>
      <link>https://dev.to/kamenivanov/bootstrapping-the-system-part-3-making-it-run-chapter-13-eig</link>
      <guid>https://dev.to/kamenivanov/bootstrapping-the-system-part-3-making-it-run-chapter-13-eig</guid>
      <description>&lt;p&gt;Parts 1 and 2 left the application with every bean it needs and a correct home for every setting it reads. What neither of them touched is the process.&lt;/p&gt;

&lt;p&gt;A process has properties that no properties file decides. How many requests it is allowed to hold at once, which is a concurrency model rather than a number. How a container's environment becomes something a JVM will accept, given that nothing hands a properties file to a container. What a log line looks like and where it lands, which no amount of &lt;code&gt;logging.level&lt;/code&gt; configuration answers. And what the thing can tell you about itself at three in the morning, when the only question that matters is which version is running.&lt;/p&gt;

&lt;p&gt;Those four are this part, and it ends with an image and a compose file, so that from here on the whole project starts with one command. The first of them is a single line that reaches further than anything else in the file.&lt;/p&gt;




&lt;h2&gt;
  
  
  Virtual threads change what the pool means
&lt;/h2&gt;

&lt;p&gt;It is this one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight properties"&gt;&lt;code&gt;&lt;span class="py"&gt;spring.threads.virtual.enabled&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Boot responds by running the servlet container's request handling on virtual threads, so every inbound request gets its own, and the asynchronous machinery inside Spring follows. The change is small to write and large in what it removes, because the thread pool that used to sit in front of everything was doing a job nobody had asked it to do.&lt;/p&gt;

&lt;p&gt;A classic Tomcat serves requests from a fixed pool, two hundred threads by default. A request that blocks on a database call holds one of those two hundred for the duration, which means that two hundred was never a thread count so much as a concurrency limit, and the limit was enforced in the one place in the stack that knew nothing about what the requests were doing. Exceed it and requests queue in the connector, which is admission control by accident: nobody chose two hundred, nobody tuned it against the database, and the number is in a different file from everything it affects.&lt;/p&gt;

&lt;p&gt;With virtual threads there is effectively no such ceiling, and the consequence is that the first real limit a request meets is the one in Part 2's datasource block. Ten thousand concurrent requests become ten thousand virtual threads, cheaply, and all ten thousand of them arrive at Hikari asking for one of twenty-five connections. Twenty-five proceed and the rest wait. This is the same backpressure as before, relocated to somewhere that actually understands the constraint, which is the right place for it, and it makes &lt;code&gt;connection-timeout=5000&lt;/code&gt; a far more consequential line than it looks. Five seconds is now the application's admission control: a request that cannot get a connection within it fails, visibly, with something the handler from Chapter 11 can turn into a response, rather than sitting in a queue nobody is measuring.&lt;/p&gt;

&lt;p&gt;Enabling this two years ago would have been a worse idea than it is now, and the reason is worth knowing because it is the sort of thing that silently undoes the whole feature. A virtual thread that blocked inside a &lt;code&gt;synchronized&lt;/code&gt; block used to pin itself to its carrier platform thread, so an application with a synchronized hot path quietly reverted to a small thread pool under load, which is the opposite of what the configuration appeared to request. JDK 24 removed that behaviour, and on the JDK 25 this project targets, a virtual thread blocking on a monitor unmounts like any other blocking operation. What remains is narrow: native frames can still pin, through a native method or the Foreign Function and Memory API calling back into blocking Java. Nothing in this codebase does either.&lt;/p&gt;

&lt;p&gt;What does not change is anything about the transaction model, which is worth stating because it is the first thing people assume breaks. A virtual thread is a thread. &lt;code&gt;@Transactional&lt;/code&gt; binds its entity manager to the current thread exactly as before, the &lt;code&gt;ThreadLocal&lt;/code&gt; machinery underneath it works unchanged, and the service layer cannot tell the difference. The one thing to be careful with is a &lt;code&gt;ThreadLocal&lt;/code&gt; used as a cache rather than as context: one that held two hundred copies of something under the old model will hold as many copies as there are concurrent requests under this one. There is no such cache here, which is the only reason this is a footnote rather than a section of its own.&lt;/p&gt;




&lt;h2&gt;
  
  
  From compose to the JVM
&lt;/h2&gt;

&lt;p&gt;In a container, nothing passes a properties file. The entrypoint script does the translation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/sh&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;var &lt;span class="k"&gt;in &lt;/span&gt;DB_URL DB_USER DB_PASSWORD ACTIVE_PROFILE&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
    &lt;/span&gt;&lt;span class="nb"&gt;eval&lt;/span&gt; &lt;span class="s2"&gt;"value=&lt;/span&gt;&lt;span class="se"&gt;\$&lt;/span&gt;&lt;span class="nv"&gt;$var&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nt"&gt;-z&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$value&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
        &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; +&lt;span class="s2"&gt;"%T"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;: FATAL: &lt;/span&gt;&lt;span class="nv"&gt;$var&lt;/span&gt;&lt;span class="s2"&gt; is not set"&lt;/span&gt;
        &lt;span class="nb"&gt;exit &lt;/span&gt;1
    &lt;span class="k"&gt;fi
done

&lt;/span&gt;&lt;span class="nv"&gt;DEBUG_OPTS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;DEBUG&lt;/span&gt;&lt;span class="k"&gt;:-&lt;/span&gt;&lt;span class="nv"&gt;false&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"true"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
    &lt;/span&gt;&lt;span class="nv"&gt;DEBUG_OPTS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8787"&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;

&lt;span class="c"&gt;# Strip carriage returns: .env files edited on Windows carry them,&lt;/span&gt;
&lt;span class="c"&gt;# and a trailing \r makes the JVM reject the last option.&lt;/span&gt;
&lt;span class="nv"&gt;JAVA_OPTS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;printf&lt;/span&gt; &lt;span class="s1"&gt;'%s'&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;JAVA_OPTS&lt;/span&gt;&lt;span class="k"&gt;:-}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; | &lt;span class="nb"&gt;tr&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s1"&gt;'\r'&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; +&lt;span class="s2"&gt;"%T"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;: ===== Environment ====="&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; +&lt;span class="s2"&gt;"%T"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;: ACTIVE_PROFILE=&lt;/span&gt;&lt;span class="nv"&gt;$ACTIVE_PROFILE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; +&lt;span class="s2"&gt;"%T"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;: DB_URL=&lt;/span&gt;&lt;span class="nv"&gt;$DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; +&lt;span class="s2"&gt;"%T"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;: DB_USER=&lt;/span&gt;&lt;span class="nv"&gt;$DB_USER&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; +&lt;span class="s2"&gt;"%T"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;: ======================="&lt;/span&gt;

&lt;span class="nb"&gt;cd&lt;/span&gt; /runtime

&lt;span class="c"&gt;# $DEBUG_OPTS and $JAVA_OPTS are unquoted: word splitting is how&lt;/span&gt;
&lt;span class="c"&gt;# whitespace-separated JVM options arrive at java as separate arguments.&lt;/span&gt;
&lt;span class="c"&gt;# Options whose values contain spaces belong in JAVA_TOOL_OPTIONS instead.&lt;/span&gt;
&lt;span class="c"&gt;# exec allows Java to receive container shutdown signals directly.&lt;/span&gt;
&lt;span class="nb"&gt;exec &lt;/span&gt;java &lt;span class="nv"&gt;$DEBUG_OPTS&lt;/span&gt; &lt;span class="nv"&gt;$JAVA_OPTS&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="s2"&gt;"-Dspring.profiles.active=&lt;/span&gt;&lt;span class="nv"&gt;$ACTIVE_PROFILE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="s2"&gt;"-Dspring.datasource.url=&lt;/span&gt;&lt;span class="nv"&gt;$DB_URL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="s2"&gt;"-Dspring.datasource.username=&lt;/span&gt;&lt;span class="nv"&gt;$DB_USER&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="s2"&gt;"-Dspring.datasource.password=&lt;/span&gt;&lt;span class="nv"&gt;$DB_PASSWORD&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="nt"&gt;-Ddebug&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;false&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="nt"&gt;-jar&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;JAR_LOCATION&lt;/span&gt;:?JAR_LOCATION&lt;span class="p"&gt; must be set&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Spring Boot would have accepted &lt;code&gt;SPRING_DATASOURCE_URL&lt;/code&gt; directly, through relaxed binding, and this script could have been one line. Translating by hand buys two things.&lt;/p&gt;

&lt;p&gt;The first is the echo block. When a container will not start and the question is whether the variable arrived at all, this is the first thing in the log. With relaxed binding there is no such point: either Spring complains about something downstream, or you go digging through &lt;code&gt;docker inspect&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The second is that &lt;code&gt;DB_URL&lt;/code&gt; is a name this project owns. Compose files, deployment manifests and whatever CI does all refer to it, and none of them know that there is a Spring application on the other side. &lt;code&gt;SPRING_DATASOURCE_URL&lt;/code&gt; leaks the framework into the orchestration, and the day the framework changes, every one of those files changes with it.&lt;/p&gt;

&lt;p&gt;The cost is that the script needs maintaining, and a new setting has to be added to it rather than working automatically. That is fine while the list is short, and the list staying short is itself useful pressure.&lt;/p&gt;

&lt;p&gt;The explicit check at the top is there because an unset variable produces &lt;code&gt;-Dspring.datasource.password=&lt;/code&gt; and an attempt to connect with an empty password, which fails much further along and much less clearly. &lt;code&gt;exec&lt;/code&gt; makes the JVM process 1 in the container, so signals from Docker reach it directly rather than being swallowed by a shell that would have stayed between them.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;-Ddebug=false&lt;/code&gt; is not redundant either. Spring reads the &lt;code&gt;debug&lt;/code&gt; property, and if it is true, every autoconfiguration logger goes to DEBUG regardless of what &lt;code&gt;logback.xml&lt;/code&gt; says. Setting it explicitly means nothing in the environment can turn the log into a wall of Spring internals by accident.&lt;/p&gt;

&lt;p&gt;Three smaller things in that script are each a bug I would otherwise have hit twice. The debug agent is attached only when &lt;code&gt;DEBUG&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt;, which keeps the decision in the environment rather than in the image, and makes a remote debugger something you turn on for a container instead of something you rebuild for. &lt;code&gt;JAVA_OPTS&lt;/code&gt; is piped through &lt;code&gt;tr -d '\r'&lt;/code&gt; because a &lt;code&gt;.env&lt;/code&gt; file edited on Windows carries carriage returns and a trailing one makes the JVM reject the last option with a message about a flag that looks perfectly spelled. And &lt;code&gt;$DEBUG_OPTS $JAVA_OPTS&lt;/code&gt; are the only unquoted expansions in the file: word splitting is how a whitespace-separated list of JVM options arrives at &lt;code&gt;java&lt;/code&gt; as separate arguments, which is also why an option whose value contains spaces belongs in &lt;code&gt;JAVA_TOOL_OPTIONS&lt;/code&gt; rather than here. The comments saying so are in the script because this is exactly the kind of line somebody tidies up by adding quotes.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;${JAR_LOCATION:?JAR_LOCATION must be set}&lt;/code&gt; is the same instinct as the loop above it, applied to the one variable the loop cannot check, because it comes from the image rather than from the environment.&lt;/p&gt;




&lt;h2&gt;
  
  
  Appenders in XML, levels in properties
&lt;/h2&gt;

&lt;p&gt;Logging is configured in two files, and the division between them is not arbitrary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;appender&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"console"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"ch.qos.logback.core.ConsoleAppender"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;encoder&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"ch.qos.logback.classic.encoder.PatternLayoutEncoder"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;pattern&amp;gt;&lt;/span&gt;%d{HH:mm:ss.SSS} [%thread] %highlight(%-5level) %cyan(%logger{36}) - %msg%n&lt;span class="nt"&gt;&amp;lt;/pattern&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/encoder&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/appender&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;appender&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"file"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"ch.qos.logback.core.rolling.RollingFileAppender"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;file&amp;gt;&lt;/span&gt;${LOG_DIR:-.}/advanced.log&lt;span class="nt"&gt;&amp;lt;/file&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;append&amp;gt;&lt;/span&gt;true&lt;span class="nt"&gt;&amp;lt;/append&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;encoder&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"ch.qos.logback.classic.encoder.PatternLayoutEncoder"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;pattern&amp;gt;&lt;/span&gt;%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n&lt;span class="nt"&gt;&amp;lt;/pattern&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/encoder&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;rollingPolicy&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"ch.qos.logback.core.rolling.TimeBasedRollingPolicy"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;fileNamePattern&amp;gt;&lt;/span&gt;${LOG_DIR:-.}/%d{yyyy-MM, aux}/advanced-%d{yyyy-MM-dd}.log.gz&lt;span class="nt"&gt;&amp;lt;/fileNamePattern&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/rollingPolicy&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/appender&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;root&lt;/span&gt; &lt;span class="na"&gt;level=&lt;/span&gt;&lt;span class="s"&gt;"INFO"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;appender-ref&lt;/span&gt; &lt;span class="na"&gt;ref=&lt;/span&gt;&lt;span class="s"&gt;"console"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;appender-ref&lt;/span&gt; &lt;span class="na"&gt;ref=&lt;/span&gt;&lt;span class="s"&gt;"file"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/root&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;logback.xml&lt;/code&gt; holds structure: which appenders exist, what a line looks like, where the file goes and when it rolls. None of that differs between a laptop and production, and all of it is awkward to express as flat properties, which is exactly the case where a nested format earns its keep. The levels are not here. Every &lt;code&gt;logging.level.*&lt;/code&gt; is a property, because a level is a statement about an environment, and an environment is what profiles are for.&lt;/p&gt;

&lt;p&gt;That boundary is easy to state and easy to violate, and the reference project I lifted this file from violated it: a committed &lt;code&gt;&amp;lt;logger name="..." level="DEBUG" /&amp;gt;&lt;/code&gt; for the application's own package. It works on a laptop, it is invisible in review, and it ships DEBUG to production while &lt;code&gt;application-dev.properties&lt;/code&gt; sits there looking authoritative about log levels it no longer controls. Two files disagreeing about the same setting is worse than either of them being wrong, because the losing one still reads as true.&lt;/p&gt;

&lt;p&gt;The console pattern is coloured and the file pattern is not. Highlight escape codes are useful in a terminal and are literal bytes in a file, where they turn every &lt;code&gt;grep&lt;/code&gt; into a question about whether the match failed or the colour code got in the way. Same pattern otherwise, so a line copied out of one reads identically in the other.&lt;/p&gt;

&lt;p&gt;The log file's directory comes from &lt;code&gt;LOG_DIR&lt;/code&gt; with the working directory as its fallback, and that indirection exists because of a bug in the file I started from. That project wrote to &lt;code&gt;./app.log&lt;/code&gt; from logback and told the actuator its log file was at &lt;code&gt;/runtime/logs/app.log&lt;/code&gt;, so the logfile endpoint returned a 404 for a file that existed somewhere else entirely, and had done for long enough that nobody expected the endpoint to work. One variable, read by both, is the whole fix: logback writes there and &lt;code&gt;management.endpoint.logfile.external-file&lt;/code&gt; points at the same place.&lt;/p&gt;




&lt;h2&gt;
  
  
  An application that knows which commit it is
&lt;/h2&gt;

&lt;p&gt;The first question anyone asks a service that is behaving strangely is which version of itself it is running, and the answer is usually a guess based on when it was last deployed. Making the application answer it costs one resource file and one bean.&lt;/p&gt;

&lt;p&gt;Maven writes the file at build time by filtering a template:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight properties"&gt;&lt;code&gt;&lt;span class="py"&gt;buildJdk&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;${java.version}  &lt;/span&gt;
&lt;span class="py"&gt;buildJvm&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;${java.vm.vendor} ${java.vm.name} ${java.vm.version}  &lt;/span&gt;
&lt;span class="py"&gt;buildOs&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;${os.name} ${os.version} (${os.arch})  &lt;/span&gt;
&lt;span class="py"&gt;buildTime&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;${buildTime}  &lt;/span&gt;
&lt;span class="py"&gt;builtBy&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;${user.name}  &lt;/span&gt;
&lt;span class="py"&gt;implementationBranch&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;${scmBranch}  &lt;/span&gt;
&lt;span class="py"&gt;implementationRevision&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;${scmRevision}  &lt;/span&gt;
&lt;span class="py"&gt;implementationTitle&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;${project.artifactId}  &lt;/span&gt;
&lt;span class="py"&gt;implementationVendor&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;${project.organization.name}  &lt;/span&gt;
&lt;span class="py"&gt;implementationVendorId&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;${project.groupId}  &lt;/span&gt;
&lt;span class="py"&gt;implementationVersion&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;${project.version}  &lt;/span&gt;
&lt;span class="py"&gt;specificationTitle&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;${project.name}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Those placeholders are three different kinds of thing, which matters when one of them silently stays a placeholder. &lt;code&gt;project.*&lt;/code&gt; comes from the POM. &lt;code&gt;java.version&lt;/code&gt; and &lt;code&gt;user.name&lt;/code&gt; are JVM system properties that the resources plugin resolves while it runs, so they describe the machine that built the jar rather than the machine running it, which is the point. &lt;code&gt;scmBranch&lt;/code&gt;, &lt;code&gt;scmRevision&lt;/code&gt; and &lt;code&gt;buildTime&lt;/code&gt; all come from &lt;code&gt;buildnumber-maven-plugin&lt;/code&gt;, under names its configuration chooses rather than names Maven provides, which is why &lt;code&gt;timestampPropertyName&lt;/code&gt; is set to &lt;code&gt;buildTime&lt;/code&gt; and &lt;code&gt;buildNumberPropertyName&lt;/code&gt; to &lt;code&gt;scmRevision&lt;/code&gt;. None of the three exists unless the plugin's &lt;code&gt;create&lt;/code&gt; goal has already run, so it is bound to &lt;code&gt;generate-resources&lt;/code&gt;, one phase before the resources plugin reads the template. Get that wrong and the file ships with &lt;code&gt;${scmRevision}&lt;/code&gt; in it as a literal string, which is the worst outcome available: an endpoint that answers confidently with nothing.&lt;/p&gt;

&lt;p&gt;There is one hole in that - &lt;code&gt;revisionOnScmFailure&lt;/code&gt; is set to &lt;code&gt;0&lt;/code&gt;, so a build from an exported copy with no git metadata reports a revision of zero instead of failing. That is the same failure the paragraph above warns about, chosen rather than stumbled into: a build that is not from a checkout has no answer to give, and a conspicuous zero is better than a hash that happens to parse.&lt;/p&gt;

&lt;p&gt;Exposing it is a &lt;code&gt;MapInfoContributor&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BuildInfoContributor&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;MapInfoContributor&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="no"&gt;BUILD_PROPERTIES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"/build.properties"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;BuildInfoContributor&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;readBuildProperties&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;readBuildProperties&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;InputStream&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BuildInfoContributor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getResourceAsStream&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;BUILD_PROPERTIES&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IllegalStateException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;BUILD_PROPERTIES&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" is missing from the jar"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;

            &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;productInfoProperties&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Properties&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;productInfoProperties&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;load&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;is&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

            &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;buildProperties&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TreeMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;();&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Entry&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;buildProperty&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;productInfoProperties&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;entrySet&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;buildProperties&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buildProperty&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getKey&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;buildProperty&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getValue&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;buildProperties&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The null check is there because the version I started from did not have one, and the failure it produces is instructive. &lt;code&gt;getResourceAsStream&lt;/code&gt; returns null for a resource that is not on the classpath rather than throwing, &lt;code&gt;Properties.load(null)&lt;/code&gt; throws a &lt;code&gt;NullPointerException&lt;/code&gt;, and because this runs in a constructor, Spring reports it as a bean creation failure with an NPE several frames down in a class that has nothing to do with the cause. The actual cause is a resources-plugin configuration that stopped filtering, which is a one-line fix behind twenty lines of stack trace. Replacing it with a thrown &lt;code&gt;IllegalStateException&lt;/code&gt; naming the file turns a debugging session into reading the first line of the log.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;TreeMap&lt;/code&gt; rather than a &lt;code&gt;HashMap&lt;/code&gt; is a smaller decision in the same spirit. The endpoint renders the map in iteration order, and sorted output means two deployments can be compared by eye rather than by reading both twice.&lt;/p&gt;




&lt;h2&gt;
  
  
  Enough actuator to be useful
&lt;/h2&gt;

&lt;p&gt;The actuator goes on a separate port, bound to loopback:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight properties"&gt;&lt;code&gt;&lt;span class="py"&gt;management.server.port&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;8081&lt;/span&gt;
&lt;span class="py"&gt;management.server.address&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;127.0.0.1&lt;/span&gt;
&lt;span class="py"&gt;management.endpoints.web.base-path&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;/&lt;/span&gt;
&lt;span class="py"&gt;management.endpoints.web.exposure.include&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;health,info,logfile&lt;/span&gt;

&lt;span class="py"&gt;management.endpoint.health.show-components&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;always&lt;/span&gt;
&lt;span class="py"&gt;management.endpoint.health.show-details&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;always&lt;/span&gt;
&lt;span class="py"&gt;management.endpoint.health.group.liveness.include&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;ping&lt;/span&gt;
&lt;span class="py"&gt;management.endpoint.health.group.readiness.include&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;*&lt;/span&gt;

&lt;span class="py"&gt;management.endpoint.logfile.external-file&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;${LOG_DIR:.}/advanced.log&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The separate port is the part worth copying. On 8080 the health endpoint is reachable by anyone who can reach the application, and &lt;code&gt;show-details=always&lt;/code&gt; means it reports which components are up, including the database. On 8081 bound to &lt;code&gt;127.0.0.1&lt;/code&gt; it is reachable from inside the container and nowhere else, which is exactly who needs it: the Docker healthcheck in the next section, and a developer with a shell.&lt;/p&gt;

&lt;p&gt;Liveness and readiness are separated because they answer different questions. Liveness is whether the process should be restarted, and &lt;code&gt;ping&lt;/code&gt; is enough: the JVM is up and answering. Readiness is whether it should receive traffic, which includes the database, because an application that cannot reach its database should not be in the load balancer. Conflating them produces a restart loop whenever the database is briefly unavailable.&lt;/p&gt;

&lt;p&gt;The logfile endpoint reads the same &lt;code&gt;LOG_DIR&lt;/code&gt; the appender writes to, which is the whole reason that variable exists rather than two hard-coded paths that agree until one of them is edited.&lt;/p&gt;

&lt;p&gt;Three endpoints is a short list - Prometheus, loggers, heapdump, mappings and the rest are all useful, and every one of them is easier to argue about once there is something running to observe.&lt;/p&gt;




&lt;h2&gt;
  
  
  The first image, and three things wrong with it
&lt;/h2&gt;

&lt;p&gt;Everything above converges here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; bellsoft/liberica-openjdk-alpine:${liberica.version}&lt;/span&gt;

&lt;span class="k"&gt;ENV&lt;/span&gt;&lt;span class="s"&gt; LOG_DIR=/runtime/logs&lt;/span&gt;
&lt;span class="k"&gt;ENV&lt;/span&gt;&lt;span class="s"&gt; JAR_LOCATION=/data/advanced-service.jar&lt;/span&gt;

&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /runtime&lt;/span&gt;

&lt;span class="k"&gt;RUN &lt;/span&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$LOG_DIR&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;chmod &lt;/span&gt;a+rwx &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$LOG_DIR&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; scripts /scripts&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; maven/application-${project.version}-exec.jar ${JAR_LOCATION}&lt;/span&gt;

&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt; 8080 8787&lt;/span&gt;

&lt;span class="k"&gt;HEALTHCHECK&lt;/span&gt;&lt;span class="s"&gt; --interval=5s --timeout=3s --retries=20 \&lt;/span&gt;
    CMD wget -q -O - -S http://127.0.0.1:8081/health || exit 1

&lt;span class="k"&gt;ENTRYPOINT&lt;/span&gt;&lt;span class="s"&gt; ["/bin/sh", "/scripts/run.sh"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The base image is pinned to &lt;code&gt;25.0.4-9&lt;/code&gt;, a Java version and a build number rather than the &lt;code&gt;25&lt;/code&gt; that floats, and the value is a property in the parent POM so that the JDK is declared next to every other version this build pins. Filtering puts it in the &lt;code&gt;FROM&lt;/code&gt; line, the same mechanism that puts &lt;code&gt;project.version&lt;/code&gt; in the &lt;code&gt;COPY&lt;/code&gt;. &lt;code&gt;JAR_LOCATION&lt;/code&gt; is the &lt;code&gt;-exec&lt;/code&gt; jar from Part 1, which exists as a separate artifact precisely so that the image can name it without ambiguity, and it is copied from &lt;code&gt;maven/&lt;/code&gt; rather than from &lt;code&gt;target/&lt;/code&gt; because that is where the Maven plugin that builds this image places the assembly inside the build context. &lt;code&gt;LOG_DIR&lt;/code&gt; is the variable the logback appender and the actuator's logfile endpoint both read, which is why they cannot disagree about where the log is. And the healthcheck reaches &lt;code&gt;127.0.0.1:8081&lt;/code&gt;, which works only because the actuator is bound to loopback and the healthcheck runs inside the container: the one caller that needs it is the one caller that can reach it.&lt;/p&gt;

&lt;p&gt;Getting the healthcheck destination right matters more than it looks. The version I started from used &lt;code&gt;0.0.0.0&lt;/code&gt;, which is an address to bind to rather than an address to connect to. On Linux it usually resolves to the local host anyway, which is the worst kind of bug: correct by accident, and silently broken the day something changes underneath it.&lt;/p&gt;

&lt;p&gt;This image works. It also has three problems I can name, and naming them now is the right choice than presenting it as finished.&lt;/p&gt;

&lt;p&gt;It runs as root, and the &lt;code&gt;chmod a+rwx&lt;/code&gt; on the log directory is the tell. A world-writable directory is what you write when the question of which user owns the process has not been asked.&lt;/p&gt;

&lt;p&gt;It copies the fat jar as a single layer, so every change to a single service class produces a new layer containing every dependency the application has. Spring Boot can take a jar apart into layers that change at different rates, and this image does not.&lt;/p&gt;

&lt;p&gt;And the choice between a JDK, a JRE and a purpose-built runtime has not been made. This image carries a full JDK, which includes a compiler the service never calls, and the obvious correction is the JRE image from the same publisher. It is not quite that simple, because two things I use live in the JDK rather than in the JRE: the JDWP agent the script attaches when &lt;code&gt;DEBUG&lt;/code&gt; is true comes from &lt;code&gt;jdk.jdwp.agent&lt;/code&gt;, and &lt;code&gt;jcmd&lt;/code&gt;, &lt;code&gt;jstack&lt;/code&gt; and &lt;code&gt;jfr&lt;/code&gt; are what you reach for the first time a production container misbehaves. An image that cannot take a thread dump is a false economy. The version is a &lt;code&gt;jlink&lt;/code&gt; runtime with those modules included, which is smaller than either and needs a module list this project has not worked out yet. Until then the JDK pays for itself, and the only reason this is on the list is size, which starts to matter the day the image is pulled rather than built in place.&lt;/p&gt;

&lt;p&gt;None of the three is an oversight, and each one needs something this project does not have yet. Layering matters once the build runs often enough for the cache to pay, the runtime image is a module list rather than a one-line change, and the user question is properly answered alongside everything else about running this in an orchestrator. They are a chapter of their own, later, and the shape of it will be the same as every other chapter here: the first version was minimal, and the next one extends it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Running it, two ways
&lt;/h2&gt;

&lt;p&gt;A fresh clone should reach a running application without a document explaining how. One compose file, and an &lt;code&gt;.env&lt;/code&gt; beside it, do it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;mysql&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mysql:9.6.0&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;MYSQL_ROOT_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;root&lt;/span&gt;
      &lt;span class="na"&gt;MYSQL_DATABASE&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${DB_NAME}&lt;/span&gt;
      &lt;span class="na"&gt;MYSQL_USER&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${DB_USERNAME}&lt;/span&gt;
      &lt;span class="na"&gt;MYSQL_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${DB_PASSWORD}&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;3306:3306"&lt;/span&gt;
    &lt;span class="na"&gt;healthcheck&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;CMD"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;mysqladmin"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ping"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-h"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;localhost"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-uroot"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-proot"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
      &lt;span class="na"&gt;interval&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;5s&lt;/span&gt;
      &lt;span class="na"&gt;timeout&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;3s&lt;/span&gt;
      &lt;span class="na"&gt;retries&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20&lt;/span&gt;

  &lt;span class="na"&gt;advanced-service&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;container_name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;advanced-service&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;advanced-service:${TAG}&lt;/span&gt;
    &lt;span class="na"&gt;depends_on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;mysql&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;condition&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;service_healthy&lt;/span&gt;
    &lt;span class="na"&gt;deploy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;resources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;limits&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;memory&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;512M&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;DB_POOL_SIZE&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;
      &lt;span class="na"&gt;JAVA_OPTS&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;&amp;gt;-&lt;/span&gt;
        &lt;span class="s"&gt;-XX:MaxRAMPercentage=55&lt;/span&gt;
        &lt;span class="s"&gt;-XX:MaxMetaspaceSize=128m&lt;/span&gt;
        &lt;span class="s"&gt;-XX:ReservedCodeCacheSize=64m&lt;/span&gt;
        &lt;span class="s"&gt;-XX:MaxDirectMemorySize=32m&lt;/span&gt;
        &lt;span class="s"&gt;-XX:+UseSerialGC&lt;/span&gt;
        &lt;span class="s"&gt;-Xss512k&lt;/span&gt;
        &lt;span class="s"&gt;-XX:TieredStopAtLevel=1&lt;/span&gt;
      &lt;span class="na"&gt;DEBUG&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${DEBUG}&lt;/span&gt;
      &lt;span class="na"&gt;ACTIVE_PROFILE&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;dev&lt;/span&gt;
      &lt;span class="na"&gt;DB_URL&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;jdbc:mysql://mysql:3306/${DB_NAME}?sslMode=DISABLED&amp;amp;allowPublicKeyRetrieval=true&lt;/span&gt;
      &lt;span class="na"&gt;DB_USER&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${DB_USERNAME}&lt;/span&gt;
      &lt;span class="na"&gt;DB_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${DB_PASSWORD}&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;8787:8787"&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;8080:8080"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Four of the variables under &lt;code&gt;environment&lt;/code&gt; are the ones the script insists on, spelled exactly as it reads them, and that is the whole point of owning the names: this block is readable without knowing that there is a Spring application behind it. Their values are not here, though. They come from an &lt;code&gt;.env&lt;/code&gt; file sitting next to the compose file, which holds the database name, the credentials, the image tag and the debug switch, so the compose file describes the shape of the deployment and the &lt;code&gt;.env&lt;/code&gt; holds the things that differ between one machine and another. The debug default there is &lt;code&gt;false&lt;/code&gt;, because a file that is convenient to commit should not also be the one that attaches a debugger.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;JAVA_OPTS&lt;/code&gt; is the longest entry and the one that matters most. The memory limit is declared once, under &lt;code&gt;deploy.resources.limits&lt;/code&gt;, and the JVM is told to size itself as a percentage of it rather than given a fixed &lt;code&gt;-Xmx&lt;/code&gt;, which is the difference between a container you can resize and a container you have to rebuild. The rest of that block is what a small limit actually requires: a metaspace ceiling, a bounded code cache, the serial collector rather than G1's several heap regions and threads, and a smaller stack. &lt;code&gt;DB_POOL_SIZE&lt;/code&gt; is set to five in the same spirit, because the twenty-five from Part 2 is a production number and twenty-five connections are not free on either side of the socket.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;depends_on&lt;/code&gt; with &lt;code&gt;condition: service_healthy&lt;/code&gt; is worth the extra two lines over the bare form, which only waits for the container to be created and not for the database inside it to accept connections. With the condition, compose waits for the healthcheck. Flyway's retry settings from Part 1 then cover what remains, because a database that passes &lt;code&gt;mysqladmin ping&lt;/code&gt; is not necessarily a database that has finished initialising, and the two mechanisms protect against different parts of the same gap.&lt;/p&gt;

&lt;p&gt;The database port is still published even though the application now reaches MySQL over the compose network, and that is deliberate: it is what lets a client on the host inspect what the schema actually looks like once there is one.&lt;/p&gt;

&lt;p&gt;The second way is the one to use while writing code, and the reason is the cycle rather than the debugger. Attaching to the container works - that is what &lt;code&gt;DEBUG&lt;/code&gt; and 8787 are for, but every change still costs a rebuild of the jar, a rebuild of the image and a restart, where the same change from an IDE costs a recompiled class and often not even that. Bring up the database alone, copy the committed example file, and run the application from there:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker compose &lt;span class="nt"&gt;-f&lt;/span&gt; docs/docker/compose.yml up mysql
&lt;span class="nb"&gt;cp &lt;/span&gt;local.properties.example local.properties
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The run configuration passes the file in, along with the profile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight properties"&gt;&lt;code&gt;&lt;span class="py"&gt;--spring.config.additional-location&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;file:./local.properties --spring.profiles.active=dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Either way the log shows the same two things: Flyway connecting, finding no migrations to apply yet, and a context with ten beans that came from four configuration classes. &lt;code&gt;curl localhost:8081/health&lt;/code&gt; answers &lt;code&gt;UP&lt;/code&gt; from inside the container, or from the host when the application runs on it.&lt;/p&gt;

&lt;p&gt;That empty migrations directory is the last loose end. Part 1 configured Flyway without showing what it would run, and the schema it is waiting for turns out to carry decisions that three earlier chapters have been quietly depending on, which is why it gets a chapter of its own rather than a paragraph here.&lt;/p&gt;




&lt;h2&gt;
  
  
  What the three parts add up to
&lt;/h2&gt;

&lt;p&gt;Thirteen chapters of code that could not start now starts, and the shape of how it starts is visible in four places rather than inferred from annotations. The &lt;code&gt;@Import&lt;/code&gt; list is the inventory. The four configuration levels each answer one question. The entrypoint script states its requirements and refuses to run without them. The actuator says whether the process is alive, whether it should receive traffic, which commit it was built from, and where to find the log.&lt;/p&gt;

&lt;p&gt;Several of the decisions in these three parts cost something, and it is worth naming them. Disabling the component scan means a line of typing per configuration class. Declaring beans by hand means three lines per service instead of one annotation. Translating environment variables by hand means the script needs maintaining and a new setting does not work automatically. In exchange, every one of those things is a place where a reader can see what the system is made of without running it, and &lt;code&gt;ProductsServiceImpl&lt;/code&gt; is still a class that a test can instantiate with &lt;code&gt;new&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;What the application cannot do is answer a single request. There is no controller, no mapping, no request body, and the exception handler from Chapter 11 has nothing to handle, because nothing can be asked of it yet.&lt;/p&gt;




&lt;h3&gt;
  
  
  What's Next?
&lt;/h3&gt;

&lt;p&gt;The application starts, and the first thing it does is look for migrations and find none. Chapter 12 argued at length that the only guard against assigning the same product to a category twice is a unique constraint, and that constraint has been a sentence in a paragraph ever since.&lt;/p&gt;

&lt;p&gt;Chapter 14 is that file. Why four tables are one migration rather than four, why the identifier column is sixteen bytes in a particular order, what a &lt;code&gt;version&lt;/code&gt; column changes about the service layer, and which of the handler mappings from Chapter 11 turns out to be missing once optimistic locking exists.&lt;/p&gt;

&lt;p&gt;The three problems with the image stay open until there is a reason to solve them, which is the chapter on deploying this rather than running it.&lt;/p&gt;




&lt;h3&gt;
  
  
  Codebase &amp;amp; Architecture Blueprint
&lt;/h3&gt;

&lt;p&gt;The entire evolutionary architecture of this project is tracked using strict Git tags. To clone the repository and switch exactly to the state established in Chapter 13, Part 3, use the following link:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GitHub Repository (Tag: &lt;code&gt;chapter-13-spring-p3&lt;/code&gt;):&lt;/strong&gt; &lt;a href="https://github.com/KamenIvanov/advanced-spring-multimodule/tree/chapter-13-spring-p3" rel="noopener noreferrer"&gt;advanced-spring-multimodule&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note: All core modules are configured with strict compilation-level boundaries. Compile and run &lt;code&gt;mvn clean install&lt;/code&gt; to see the structure in action. Maven version 3.9.* and Java 25 are required.&lt;/p&gt;

&lt;p&gt;◀️ &lt;a href="https://dev.to/kamenivanov/bootstrapping-the-system-part-2-where-the-settings-come-from-chapter-13-11ic"&gt;Read Chapter 13 Part 2: Bootstrapping the System, Where the Settings Come From&lt;/a&gt;&lt;br&gt;
▶️ &lt;a href="https://dev.to/kamenivanov/the-schema-three-chapters-were-already-relying-on-chapter-14-21j"&gt;Read Chapter 14: The Schema Three Chapters Were Already Relying On&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Join the Masterclass Journey
&lt;/h3&gt;

&lt;p&gt;This article is part of an ongoing weekly series. If you want to receive every upcoming chapter directly in your inbox, alongside deep-dives, infrastructure architecture diagrams, and premium insights before anyone else, &lt;strong&gt;&lt;a href="https://kamenivanov.substack.com/subscribe" rel="noopener noreferrer"&gt;subscribe to my Substack Newsletter here&lt;/a&gt;&lt;/strong&gt;!&lt;/p&gt;




&lt;h3&gt;
  
  
  Recommended Resources for Java &amp;amp; Spring Boot Engineers
&lt;/h3&gt;

&lt;p&gt;Disclosure: I earn a commission if you purchase through the links below, at no additional cost to you.&lt;/p&gt;

&lt;p&gt;If you are preparing for Senior/Lead Java interviews or looking to solidify your Spring Boot &amp;amp; Architecture skills, check out these highly-rated resources from the &lt;strong&gt;Javarevisited&lt;/strong&gt; publication &lt;em&gt;(Use promo code&lt;/em&gt; &lt;strong&gt;&lt;em&gt;friends20&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;for an exclusive&lt;/em&gt; &lt;strong&gt;&lt;em&gt;20% discount&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;automatically applied at checkout)&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the Java Interview&lt;/strong&gt;
Prepare for core Java, concurrency, JVM internals, and design pattern questions.
&lt;a href="https://gumroad.com/a/501547619/QqjGH" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/HMOAv" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the Spring Boot Interview&lt;/strong&gt;
Master Spring Core, Auto-configuration, Spring Data JPA, Security, and Microservices.
&lt;a href="https://gumroad.com/a/501547619/hrUXKY" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/pfolo" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the SQL Interview&lt;/strong&gt;
Deep-dive into query optimization, indexing, joins, and complex SQL window functions.
&lt;a href="https://gumroad.com/a/501547619/fhmehw" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/brruh" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Complete Java + Spring + SQL Interview Bundle&lt;/strong&gt;
Get all three interview guides in a single heavily discounted package.
&lt;a href="https://gumroad.com/a/501547619/gvqgjk" rel="noopener noreferrer"&gt;Get the Ultimate Interview Bundle&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spring Professional Certification Practice Questions (250+ Questions)&lt;/strong&gt;
Validating your skills? Practice with real exam-style questions before taking the Spring Professional certification.
&lt;a href="https://gumroad.com/a/501547619/sygyq" rel="noopener noreferrer"&gt;Get the Spring Professional Questions&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/qelhye" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>backend</category>
      <category>devops</category>
      <category>docker</category>
      <category>java</category>
    </item>
    <item>
      <title>The generic CRUD repository didn't cost us a sprint. What we built around it did.</title>
      <dc:creator>Kamen</dc:creator>
      <pubDate>Fri, 23 Oct 2026 10:44:45 +0000</pubDate>
      <link>https://dev.to/kamenivanov/the-generic-crud-repository-didnt-cost-us-a-sprint-what-we-built-around-it-did-2fic</link>
      <guid>https://dev.to/kamenivanov/the-generic-crud-repository-didnt-cost-us-a-sprint-what-we-built-around-it-did-2fic</guid>
      <description>&lt;p&gt;The generic repository had been sold internally as a productivity win, and for a while it was. One base interface with &lt;code&gt;save&lt;/code&gt;, &lt;code&gt;findById&lt;/code&gt;, &lt;code&gt;findAll&lt;/code&gt;, &lt;code&gt;delete&lt;/code&gt;, parameterized over any domain object, meant a new service could be wired up to persistence in an afternoon instead of a day. Every team adopted it and nobody had to write the same boilerplate CRUD logic twice, and for the first year, that felt like exactly the kind of reuse the architecture was supposed to reward.&lt;/p&gt;

&lt;p&gt;The cracks showed up gradually, in the shape of business rules that didn't fit the generic contract and ended up scattered around it instead. A cancellation didn't just need &lt;code&gt;save&lt;/code&gt;, it needed to check three preconditions, emit a domain event, and update two related domain objects atomically. That logic didn't belong in a generic repository, so it got bolted onto the service layer as a special case, then another special case for refunds, then another for partial fulfillment, each one technically using the generic repository underneath while wrapping it in enough surrounding logic that the "generic" part stopped being where the real complexity lived. The repository stayed clean. Everything around it didn't.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkd19e4euh7f9rfxrblhc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkd19e4euh7f9rfxrblhc.png" alt="Generic repository VS Use case boundaries" width="799" height="376"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The actual cost showed up during a sprint meant to add a straightforward feature: partial refunds needed to trigger a different notification path than full refunds. Finding where refund logic actually lived took most of the first two days, because it wasn't in one place, it was distributed across four service classes, each calling the same generic &lt;code&gt;save&lt;/code&gt; method with different surrounding conditionals that had accumulated independently over a year of separate contributors solving separate tickets. There was no single use case class to open and read top to bottom. There was a repository interface that told you nothing about what a refund actually was as an operation.&lt;/p&gt;

&lt;p&gt;We spent the rest of that sprint doing something other than the feature: extracting explicit use case classes, &lt;code&gt;ProcessFullRefund&lt;/code&gt;, &lt;code&gt;ProcessPartialRefund&lt;/code&gt;, each owning its own preconditions and side effects, with the generic repository pushed back down to where it belonged, a plain persistence detail behind the use case instead of the abstraction the business logic organized itself around. The feature that started the sprint shipped in the following one, in a day, because by then there was finally a single class whose job was to answer the question "what does a partial refund actually do." Finding the same logic afterward meant opening one class and reading it top to bottom, about half an hour.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ft6dyx52sejk6qvqj50o9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ft6dyx52sejk6qvqj50o9.png" alt="Time to locate BL" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A generic CRUD repository isn't wrong for what it actually is, a persistence detail. It becomes expensive the moment a team starts treating "wire it to the generic repository" as equivalent to "the business logic is handled," because those are not the same claim, and the gap between them doesn't show up on a velocity chart until the day someone needs to change behavior that was never given a home of its own.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;More like this?&lt;/strong&gt; I write short production war stories like this one, plus a deep-dive architecture series. &lt;a href="https://kamenivanov.substack.com/subscribe" rel="noopener noreferrer"&gt;Subscribe here&lt;/a&gt; if you want both in your inbox.&lt;/p&gt;




&lt;h3&gt;
  
  
  Recommended Resources for Java &amp;amp; Spring Boot Engineers
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Disclosure: the links below are affiliate links from the Javarevisited publication. If you buy through them, I may earn a commission at no extra cost to you.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you are preparing for Senior/Lead Java interviews or looking to solidify your Spring Boot &amp;amp; Architecture skills, check out these highly-rated resources from the &lt;strong&gt;Javarevisited&lt;/strong&gt; publication. The &lt;strong&gt;20% discount&lt;/strong&gt; is applied automatically at checkout through these links; if it doesn't show up, use promo code &lt;strong&gt;friends20&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the Java Interview&lt;/strong&gt;
Prepare for core Java, concurrency, JVM internals, and design pattern questions.
&lt;a href="https://gumroad.com/a/501547619/QqjGH" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/HMOAv" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the Spring Boot Interview&lt;/strong&gt;
Master Spring Core, Auto-configuration, Spring Data JPA, Security, and Microservices.
&lt;a href="https://gumroad.com/a/501547619/hrUXKY" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/pfolo" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the SQL Interview&lt;/strong&gt;
Deep-dive into query optimization, indexing, joins, and complex SQL window functions.
&lt;a href="https://gumroad.com/a/501547619/fhmehw" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/brruh" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Complete Java + Spring + SQL Interview Bundle&lt;/strong&gt;
Get all three interview guides in a single heavily discounted package.
&lt;a href="https://gumroad.com/a/501547619/gvqgjk" rel="noopener noreferrer"&gt;Get the Ultimate Interview Bundle&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spring Professional Certification Practice Questions (250+ Questions)&lt;/strong&gt;
Validating your skills? Practice with real exam-style questions before taking the Spring Professional certification.
&lt;a href="https://gumroad.com/a/501547619/sygyq" rel="noopener noreferrer"&gt;Get the Spring Professional Questions&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/qelhye" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>We Stopped Importing Spring Into the Domain Module. The Compile Errors Exposed 30 Boundary Violations.</title>
      <dc:creator>Kamen</dc:creator>
      <pubDate>Wed, 21 Oct 2026 13:07:10 +0000</pubDate>
      <link>https://dev.to/kamenivanov/we-stopped-importing-spring-into-the-domain-module-the-compile-errors-exposed-30-boundary-4jfm</link>
      <guid>https://dev.to/kamenivanov/we-stopped-importing-spring-into-the-domain-module-the-compile-errors-exposed-30-boundary-4jfm</guid>
      <description>&lt;p&gt;The domain module was supposed to be framework-free. That was the whole premise of the module split back when the project moved to a multi-module Maven layout: domain objects and use cases in one module, persistence and web concerns in others, a dependency arrow pointing one direction only. Nobody was supposed to be able to import Spring into domain code, because domain code wasn't supposed to know Spring existed.&lt;/p&gt;

&lt;p&gt;Nobody enforced that on purpose. It was a convention, documented once in an architecture decision record nobody had reread in over a year, and conventions without enforcement decay exactly as fast as you'd expect. A new engineer needing a bean somewhere reached for &lt;code&gt;@Component&lt;/code&gt; because that's what got autowired correctly, and it worked, because the domain module still had &lt;code&gt;spring-context&lt;/code&gt; on its classpath as a transitive dependency from something else. Nothing broke, nothing failed review, because nobody was specifically looking for it, and the annotation looked completely at home next to everything else in the class.&lt;/p&gt;

&lt;p&gt;The point where this stopped being theoretical was a routine cleanup: pulling &lt;code&gt;spring-context&lt;/code&gt; off the domain module's dependency list entirely, on the assumption that removing an unused transitive dependency was a safe, boring change. The build didn't just fail, it failed in 30 places, scattered across use case classes, domain services, even two places inside what were supposed to be pure value objects.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fz36d46h02vhb5nusuap5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fz36d46h02vhb5nusuap5.png" alt="Dependency leakage" width="800" height="353"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each of those 30 was a small decision that had made sense in isolation. A &lt;code&gt;@Value&lt;/code&gt; here to inject a configuration property directly into a domain service instead of passing it through a constructor from the application layer. A &lt;code&gt;@Component&lt;/code&gt; there because Spring's classpath scanning was easier than manually wiring a dependency. None of them were dramatic, and none of them showed up in code review as anything worth blocking, because reviewers were reading for correctness, not for which module a symbol was imported from.&lt;/p&gt;

&lt;p&gt;The cost wasn't abstract. Domain logic that imports Spring can't be unit tested without a Spring context spinning up somewhere in the test, even for a pure business rule that has nothing to do with dependency injection. It can't be reused in a different delivery mechanism without dragging a framework along with it. And it quietly breaks the one guarantee the module boundary was supposed to provide, that the rules governing a claim, a payout, or an account state don't depend on which web framework happens to be serving the request.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fuzr4m38lb8ubg06rz8n7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fuzr4m38lb8ubg06rz8n7.png" alt="Boundary Violations by Type" width="800" height="320"&gt;&lt;/a&gt;&lt;br&gt;
Fixing the 30 was a day of mechanical refactoring. Making sure it stayed fixed was the part that mattered: an ArchUnit rule added to the build that fails any test run where a class in the domain package imports anything from &lt;code&gt;org.springframework&lt;/code&gt;, with the dependency itself removed from the module's POM so it can't come back in silently through someone else's transitive import. The convention that used to live in a document nobody read now lives in the build, which is the only place a convention survives contact with a deadline.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;More like this?&lt;/strong&gt;&lt;br&gt;
I write short production war stories like this one, plus a deep-dive architecture series. &lt;a href="https://kamenivanov.substack.com/subscribe" rel="noopener noreferrer"&gt;Subscribe here&lt;/a&gt; if you want both in your inbox.&lt;/p&gt;




&lt;h3&gt;
  
  
  Recommended Resources for Java &amp;amp; Spring Boot Engineers
&lt;/h3&gt;

&lt;p&gt;If you are preparing for Senior/Lead Java interviews or looking to solidify your Spring Boot &amp;amp; Architecture skills, check out these highly-rated resources from the &lt;strong&gt;Javarevisited&lt;/strong&gt; publication &lt;em&gt;(Use promo code&lt;/em&gt; &lt;strong&gt;&lt;em&gt;friends20&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;for an exclusive&lt;/em&gt; &lt;strong&gt;&lt;em&gt;20% discount&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;automatically applied at checkout)&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the Java Interview&lt;/strong&gt;
Prepare for core Java, concurrency, JVM internals, and design pattern questions.
&lt;a href="https://gumroad.com/a/501547619/QqjGH" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/HMOAv" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the Spring Boot Interview&lt;/strong&gt;
Master Spring Core, Auto-configuration, Spring Data JPA, Security, and Microservices.
&lt;a href="https://gumroad.com/a/501547619/hrUXKY" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/pfolo" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the SQL Interview&lt;/strong&gt;
Deep-dive into query optimization, indexing, joins, and complex SQL window functions.
&lt;a href="https://gumroad.com/a/501547619/fhmehw" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/brruh" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Complete Java + Spring + SQL Interview Bundle&lt;/strong&gt;
Get all three interview guides in a single heavily discounted package.
&lt;a href="https://gumroad.com/a/501547619/gvqgjk" rel="noopener noreferrer"&gt;Get the Ultimate Interview Bundle&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spring Professional Certification Practice Questions (250+ Questions)&lt;/strong&gt;
Validating your skills? Practice with real exam-style questions before taking the Spring Professional certification.
&lt;a href="https://gumroad.com/a/501547619/sygyq" rel="noopener noreferrer"&gt;Get the Spring Professional Questions&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/qelhye" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>architecture</category>
      <category>backend</category>
      <category>java</category>
      <category>refactoring</category>
    </item>
    <item>
      <title>Bootstrapping the System, Part 2: Where the Settings Come From (Chapter 13)</title>
      <dc:creator>Kamen</dc:creator>
      <pubDate>Mon, 19 Oct 2026 07:33:39 +0000</pubDate>
      <link>https://dev.to/kamenivanov/bootstrapping-the-system-part-2-where-the-settings-come-from-chapter-13-11ic</link>
      <guid>https://dev.to/kamenivanov/bootstrapping-the-system-part-2-where-the-settings-come-from-chapter-13-11ic</guid>
      <description>&lt;p&gt;Part 1 left the application with a complete set of beans and no way to start. Both &lt;code&gt;PersistenceConfiguration&lt;/code&gt; and &lt;code&gt;FlywayConfiguration&lt;/code&gt; take a &lt;code&gt;HikariDataSource&lt;/code&gt; as a parameter, nothing declares one, and a datasource needs three things that cannot be committed to a repository: a URL, a username and a password.&lt;/p&gt;

&lt;p&gt;The lazy fix is a &lt;code&gt;local.properties&lt;/code&gt; with all three in it, gitignored, and a note in the README. That works on a laptop and nowhere else. The application also has to start in a container, where there is no file to read, and in whatever CI eventually runs it, where neither the file nor the container's environment looks like either of the first two. One of those three contexts will be wrong whichever one gets to be the default.&lt;/p&gt;

&lt;p&gt;So the question is not where the settings live. It is how many different questions a setting can be answering, and giving each of those questions its own place.&lt;/p&gt;




&lt;h2&gt;
  
  
  Properties, and the four places they come from
&lt;/h2&gt;

&lt;p&gt;This project uses &lt;code&gt;.properties&lt;/code&gt; rather than &lt;code&gt;.yml&lt;/code&gt;, and the reason is unglamorous: one setting per line, greppable, and the key is visible in full at every line. In yml, finding out whether &lt;code&gt;spring.datasource.hikari.maximum-pool-size&lt;/code&gt; is set means reading an indented tree and assembling the path in your head. With properties, searching for the string finds it. Blocks of related settings sit next to each other and read like a table. Yml wins on nesting and on lists, and if this project grows a configuration section that is genuinely a list of objects, that section will be worth moving. Until then the flat file is easier to scan. Configuration arrives at the running application from four places, and each one answers a different question.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;application.properties&lt;/code&gt; holds what is true everywhere. The context path, the server port, the connection pool sizing, the JPA settings, the actuator exposure. If a setting would be identical on a laptop, in staging and in production, it lives here and nowhere else.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;application-dev.properties&lt;/code&gt; holds what is true of an environment. Log levels verbose enough to be useful while developing, and whatever else differs between dev, staging and production as a matter of what the environment is for. This is what a profile is: a description of an environment, shared by everyone who deploys to it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;local.properties&lt;/code&gt; holds what is true of one machine. The database URL, the username, the password. It is in &lt;code&gt;.gitignore&lt;/code&gt;, it is passed as an additional config location when running the application, and it never appears in the repository. A profile would be the wrong tool here, because a profile named after a developer is in git by definition, and the moment two people have one, &lt;code&gt;application-dev.properties&lt;/code&gt; describes nobody.&lt;/p&gt;

&lt;p&gt;What the repository does carry is &lt;code&gt;local.properties.example&lt;/code&gt;, with the three keys and values that match the compose file from Part 3, and a first line saying to copy it. That distinction is worth making explicitly, because the tempting shortcut is to commit the real file on the grounds that &lt;code&gt;root&lt;/code&gt; against &lt;code&gt;localhost&lt;/code&gt; is not a secret. It is not a secret, and committing it is still wrong, for a duller reason than security: the moment the file is in git, every developer's machine-specific values are in everyone's working copy, and the first person who changes a port has changed it for everybody. The example file is the shape of the thing, the thing itself belongs to whoever is running it.&lt;/p&gt;

&lt;p&gt;Environment variables hold what is true of a container, and this is the place that gets conflated with the other three most often. A container has no file to read. Whatever is true of a particular deployment arrives through its environment, which makes the name of the variable part of the contract between this application and whatever starts it: a compose file, a deployment manifest, a CI job. Part 3 is where that contract gets written, in a script that translates those names into something a JVM will accept and refuses to start when one of them is missing.&lt;/p&gt;




&lt;h2&gt;
  
  
  The datasource block, line by line
&lt;/h2&gt;

&lt;p&gt;The largest block in &lt;code&gt;application.properties&lt;/code&gt; is the datasource, and it is worth reading one line at a time, because very little of it is boilerplate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight properties"&gt;&lt;code&gt;&lt;span class="py"&gt;spring.jpa.open-in-view&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;false&lt;/span&gt;

&lt;span class="py"&gt;spring.datasource.driver-class-name&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;com.mysql.cj.jdbc.Driver&lt;/span&gt;
&lt;span class="py"&gt;spring.datasource.hikari.pool-name&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;advanced-connection-pool&lt;/span&gt;
&lt;span class="py"&gt;spring.datasource.hikari.initialization-fail-timeout&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;-1&lt;/span&gt;

&lt;span class="py"&gt;spring.datasource.hikari.data-source-properties.useServerPrepStmts&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;true&lt;/span&gt;
&lt;span class="py"&gt;spring.datasource.hikari.data-source-properties.cachePrepStmts&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;true&lt;/span&gt;
&lt;span class="py"&gt;spring.datasource.hikari.data-source-properties.prepStmtCacheSize&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;250&lt;/span&gt;
&lt;span class="py"&gt;spring.datasource.hikari.data-source-properties.prepStmtCacheSqlLimit&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;2048&lt;/span&gt;

&lt;span class="py"&gt;spring.datasource.hikari.maximum-pool-size&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;${DB_POOL_SIZE:25}&lt;/span&gt;
&lt;span class="py"&gt;spring.datasource.hikari.minimum-idle&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;${DB_POOL_SIZE:25}&lt;/span&gt;

&lt;span class="py"&gt;spring.datasource.hikari.max-lifetime&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;1800000&lt;/span&gt;
&lt;span class="py"&gt;spring.datasource.hikari.connection-timeout&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;5000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;open-in-view=false&lt;/code&gt; disables the filter that holds a Hibernate session open for the duration of the whole HTTP request. Boot enables it by default and logs a warning about it on every single start, which is an unusually direct admission that the default is wrong. With the filter on, a lazy association can be loaded during JSON serialization, long after the service method has returned, which means a query firing from the web layer that nothing in the service layer ever asked for. This project is partly protected from that by its own structure, since the domain module contains no entities and the transformers in &lt;code&gt;dao-impl&lt;/code&gt; copy what they need while the session is still open. Turning the filter off converts the remaining risk from a silent extra query into a visible failure, which is the version I would rather debug.&lt;/p&gt;

&lt;p&gt;The driver class is redundant, strictly speaking, because Boot infers it from the JDBC URL. I keep it because the URL is not in this file at all: it arrives from &lt;code&gt;local.properties&lt;/code&gt; on a laptop and from an environment variable in a container, which leaves the driver line as the only place in the committed configuration that states which database this application talks to. &lt;code&gt;pool-name&lt;/code&gt; earns its line the same way, by being visible later: it appears in HikariCP's log output and in the names of its threads, so a thread dump taken during an incident says which pool a stuck thread was waiting on.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;initialization-fail-timeout=-1&lt;/code&gt; tells Hikari not to fail pool creation when the database is unreachable at startup. It belongs to the same decision as Flyway's infinite connect retries from earlier in this chapter: in a container the application and the database start together, the database is slower, and waiting is better than crash-looping while the orchestrator restarts a process that was never broken. It is worth being precise about what this does not do. It does not hide a failure, the application starts, the readiness probe reports it as not ready because the database health indicator is failing, and the orchestrator keeps traffic away from it. The failure is still visible, just in the place designed to report it.&lt;/p&gt;

&lt;p&gt;The four prepared statement properties are a single setting wearing four names, and the ordering matters. MySQL's client-side statement cache does nothing on its own - &lt;code&gt;cachePrepStmts&lt;/code&gt; without &lt;code&gt;useServerPrepStmts&lt;/code&gt; caches objects that still send the full SQL text on every execution. The pair together moves parsing to the server once per statement shape and then reuses it, which is worth having in a system where the same handful of queries run constantly. The two numbers come from HikariCP's own MySQL guidance rather than from a measurement of this project, and I am stating that rather than implying otherwise.&lt;/p&gt;

&lt;p&gt;The pool is deliberately fixed, with maximum and minimum set to the same value from the same variable. Pool resizing is cheap until the moment it is not. A spike arrives, the pool grows, and every new connection costs a TCP connect, a MySQL handshake and an authentication round trip, all of it paid inside the request unlucky enough to have triggered the growth. Fixing the size pays that cost once at startup instead. The trade-off is real and in the other direction: twenty-five connections are held open by an application doing nothing, and on a database shared by several services, connections are the scarce resource rather than memory. Pulling both numbers from one variable means an environment can scale the pool down without accidentally making it dynamic again.&lt;/p&gt;

&lt;p&gt;Strictly speaking the second line restates a default. Leaving &lt;code&gt;minimumIdle&lt;/code&gt; unset is what HikariCP's own documentation recommends, and an unset minimum already equals the maximum. I set it anyway, for two reasons that are about reading rather than behaviour: one variable visibly driving both numbers is what makes the fixed pool obvious to someone scanning the file, and an environment that wants a smaller pool should not have to know which of the two settings is the one that matters. That is a judgement call rather than a rule, and the next paragraph is about a line where the same judgement goes the other way.&lt;/p&gt;

&lt;p&gt;What is not in that block is &lt;code&gt;idle-timeout&lt;/code&gt;, and the reason is not that obvious - Hikari only retires idle connections down to &lt;code&gt;minimumIdle&lt;/code&gt;, so when minimum and maximum are equal there is nothing for an idle timeout to act on and the setting is ignored entirely. It had been sitting in this file looking exactly like tuning, left over from a time when the pool was allowed to shrink. A configuration file nobody reads end to end accumulates these: lines that were correct under an earlier decision and became decoration under a later one, while still reading as deliberate to the next person who opens the file.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;max-lifetime&lt;/code&gt; is the one that does the work the idle timeout appeared to be doing. Thirty minutes retires every connection regardless of whether it was busy, which keeps the pool ahead of MySQL's own &lt;code&gt;wait_timeout&lt;/code&gt; and ahead of the load balancers and firewalls that silently drop long-lived TCP connections. A connection the pool believes is alive and the network has already discarded is the source of the stale-connection errors that only ever show up after a quiet period.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;connection-timeout=5000&lt;/code&gt; is a decision about failure shape. The default is thirty seconds, which means that under contention a thread waits half a minute for a connection before giving up, and the symptom a user sees is a request that eventually succeeds very slowly. Five seconds turns the same condition into errors, which are visible in a way that latency is not. This is also the setting that would have made the &lt;code&gt;REQUIRES_NEW&lt;/code&gt; approach from Chapter 12 fail loudly rather than quietly: a per-item transaction across a two-hundred-item batch exhausts a twenty-five connection pool immediately, and with this timeout it says so in five seconds instead of appearing as a request that takes ten minutes.&lt;/p&gt;

&lt;p&gt;Two lines came out of this block that are worth mentioning for their absence. &lt;code&gt;spring.jpa.database&lt;/code&gt; and &lt;code&gt;spring.jpa.database-platform&lt;/code&gt; both declare the Hibernate dialect, Hibernate resolves it from the connection metadata without either of them, and keeping both means two places to edit when the database changes and two chances to edit only one.&lt;/p&gt;

&lt;p&gt;Within a single file, defaults go inline, as the pool size above does. The value after the colon is what applies when the variable is absent, which makes the file self-documenting about what can be overridden and what it falls back to. It also marks the setting as optional, and that is the distinction the entrypoint script in Part 3 makes from the other direction: a property with no inline default is one the container must supply, and that is precisely the list the script refuses to start without.&lt;/p&gt;




&lt;h2&gt;
  
  
  Typed configuration
&lt;/h2&gt;

&lt;p&gt;Flat properties are untyped strings and are only checked when something reads them. &lt;code&gt;@ConfigurationProperties&lt;/code&gt; turns a group of them into an object that is validated at startup, and wiring one in is the first change this part makes to the entry point from Part 1: the class list grows by an &lt;code&gt;@EnableConfigurationProperties&lt;/code&gt; block alongside the &lt;code&gt;@Import&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@ConfigurationProperties&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prefix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"app.assignments"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nd"&gt;@Validated&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;AssignmentProperties&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
        &lt;span class="nd"&gt;@NotNull&lt;/span&gt; &lt;span class="nd"&gt;@Min&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nd"&gt;@Max&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;maxBatchSize&lt;/span&gt;
&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bound from:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight properties"&gt;&lt;code&gt;&lt;span class="py"&gt;app.assignments.max-batch-size&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;200&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the size cap Chapter 12 said the bulk assignment endpoint would need. The chapter argued that a single transaction and an &lt;code&gt;IN&lt;/code&gt; clause are fine at a few hundred ids and a problem at a hundred thousand, and left the bound unspecified. Here it is a number with a name, validated into a range.&lt;/p&gt;

&lt;p&gt;Where it gets read is a decision in its own right, and the obvious answer is wrong. &lt;code&gt;CategoryAssignmentsServiceImpl&lt;/code&gt; is the class that loops over the ids, so injecting the properties record there looks natural. It would also be the first time in thirteen chapters that a class in &lt;code&gt;business-logic&lt;/code&gt; took a Spring configuration object as a constructor argument, which is precisely the boundary the previous twelve were spent defending. A maximum batch size is a statement about what the API accepts, not about what the business rule allows: a request carrying ten thousand ids should be refused before anything reads a database, and refusing it is the job of the layer that parses the request. So the record is read by the controller, in the module that is allowed to know about Spring, and the service keeps a constructor that a test can satisfy with three mocks.&lt;/p&gt;

&lt;p&gt;That leaves this chapter introducing a configuration value before the thing that consumes it exists, which is a slightly odd order to publish in and an honest reflection of how it happened. The alternative was to hold the property back and reopen the entry point two chapters later, which buys tidiness in exchange for pretending the decision was made at a different time than it was.&lt;/p&gt;

&lt;p&gt;The record is registered through &lt;code&gt;@EnableConfigurationProperties&lt;/code&gt; on the application class rather than by annotating it with &lt;code&gt;@Component&lt;/code&gt;, which keeps it consistent with everything else: the application decides what exists.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@Validated&lt;/code&gt; is what makes the constraints run. Without it the annotations are documentation. With it, &lt;code&gt;max-batch-size=0&lt;/code&gt; fails the context startup with a message naming the property and the constraint, which is a far better outcome than a bulk endpoint that silently accepts batches of zero.&lt;/p&gt;




&lt;h2&gt;
  
  
  What this part settled
&lt;/h2&gt;

&lt;p&gt;A setting now has exactly one correct home, chosen by what it is true of rather than by what is convenient. Identical everywhere goes in &lt;code&gt;application.properties&lt;/code&gt;. True of an environment goes in a profile. True of one machine goes in a gitignored file nobody else sees. True of a container arrives as an environment variable, and what happens to it on the way into the JVM is the subject of Part 3.&lt;/p&gt;

&lt;p&gt;The datasource block turned out to be the densest file, and two of its lines were doing nothing: an idle timeout that cannot fire against a fixed pool, and a pair of dialect declarations saying the same thing twice. Both had been correct under an earlier decision and had quietly become decoration under a later one, which is what a configuration file does when nobody reads it end to end.&lt;/p&gt;

&lt;p&gt;Reading a file like that end to end is most of the work, and it is work that only pays once. Every setting here is now either a decision somebody can disagree with or a default nobody has to think about, and the difference between the two is written down rather than inferred.&lt;/p&gt;




&lt;h3&gt;
  
  
  What's Next?
&lt;/h3&gt;

&lt;p&gt;Every setting now has a correct home, and the process that reads them is still unconfigured in every respect that is not a property. Nothing has decided how many requests it may hold at once. Nothing has decided what a log line looks like or where it is written. Nothing turns a container's environment into something a JVM will accept, given that nothing hands a properties file to a container. And the application cannot yet answer the first question anyone asks of a running service, which is which version of itself it happens to be.&lt;/p&gt;

&lt;p&gt;Part 3 is those four, and it ends with an image and a compose file, so that from here on the whole thing starts with one command.&lt;/p&gt;




&lt;h3&gt;
  
  
  Codebase &amp;amp; Architecture Blueprint
&lt;/h3&gt;

&lt;p&gt;The entire evolutionary architecture of this project is tracked using strict Git tags. To clone the repository and switch exactly to the state established in Chapter 13, Part 2, use the following link:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GitHub Repository (Tag: &lt;code&gt;chapter-13-spring-p2&lt;/code&gt;):&lt;/strong&gt; &lt;a href="https://github.com/KamenIvanov/advanced-spring-multimodule/tree/chapter-13-spring-p2" rel="noopener noreferrer"&gt;advanced-spring-multimodule&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note: All core modules are configured with strict compilation-level boundaries. Compile and run &lt;code&gt;mvn clean install&lt;/code&gt; to see the structure in action. Maven version 3.9.* and Java 25 are required.&lt;/p&gt;

&lt;p&gt;◀️ &lt;a href="https://dev.to/kamenivanov/bootstrapping-the-system-part-1-nothing-is-a-bean-yet-chapter-13-2l4o"&gt;Read Chapter 13 Part 1: Bootstrapping the System, Nothing Is a Bean Yet&lt;/a&gt; &lt;br&gt;
▶️ &lt;a href="https://dev.to/kamenivanov/bootstrapping-the-system-part-3-making-it-run-chapter-13-eig"&gt;Read Chapter 13 Part 3: Bootstrapping the System, Making It Run&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Join the Masterclass Journey
&lt;/h3&gt;

&lt;p&gt;This article is part of an ongoing weekly series. If you want to receive every upcoming chapter directly in your inbox, alongside deep-dives, infrastructure architecture diagrams, and premium insights before anyone else, &lt;strong&gt;&lt;a href="https://kamenivanov.substack.com/subscribe" rel="noopener noreferrer"&gt;subscribe to my Substack Newsletter here&lt;/a&gt;&lt;/strong&gt;!&lt;/p&gt;




&lt;h3&gt;
  
  
  Recommended Resources for Java &amp;amp; Spring Boot Engineers
&lt;/h3&gt;

&lt;p&gt;Disclosure: I earn a commission if you purchase through the links below, at no additional cost to you.&lt;/p&gt;

&lt;p&gt;If you are preparing for Senior/Lead Java interviews or looking to solidify your Spring Boot &amp;amp; Architecture skills, check out these highly-rated resources from the &lt;strong&gt;Javarevisited&lt;/strong&gt; publication &lt;em&gt;(Use promo code&lt;/em&gt; &lt;strong&gt;&lt;em&gt;friends20&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;for an exclusive&lt;/em&gt; &lt;strong&gt;&lt;em&gt;20% discount&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;automatically applied at checkout)&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the Java Interview&lt;/strong&gt;
Prepare for core Java, concurrency, JVM internals, and design pattern questions.
&lt;a href="https://gumroad.com/a/501547619/QqjGH" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/HMOAv" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the Spring Boot Interview&lt;/strong&gt;
Master Spring Core, Auto-configuration, Spring Data JPA, Security, and Microservices.
&lt;a href="https://gumroad.com/a/501547619/hrUXKY" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/pfolo" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grokking the SQL Interview&lt;/strong&gt;
Deep-dive into query optimization, indexing, joins, and complex SQL window functions.
&lt;a href="https://gumroad.com/a/501547619/fhmehw" rel="noopener noreferrer"&gt;Get Full Book (Paid)&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/brruh" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Complete Java + Spring + SQL Interview Bundle&lt;/strong&gt;
Get all three interview guides in a single heavily discounted package.
&lt;a href="https://gumroad.com/a/501547619/gvqgjk" rel="noopener noreferrer"&gt;Get the Ultimate Interview Bundle&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spring Professional Certification Practice Questions (250+ Questions)&lt;/strong&gt;
Validating your skills? Practice with real exam-style questions before taking the Spring Professional certification.
&lt;a href="https://gumroad.com/a/501547619/sygyq" rel="noopener noreferrer"&gt;Get the Spring Professional Questions&lt;/a&gt; | &lt;a href="https://gumroad.com/a/501547619/qelhye" rel="noopener noreferrer"&gt;Download Free Sample Copy&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>architecture</category>
      <category>backend</category>
      <category>java</category>
      <category>softwareengineering</category>
    </item>
  </channel>
</rss>
