<?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: Machine coding Master</title>
    <description>The latest articles on DEV Community by Machine coding Master (@machinecodingmaster).</description>
    <link>https://dev.to/machinecodingmaster</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%2F3894844%2F09f3cafa-c542-4beb-8efa-72045647d766.png</url>
      <title>DEV Community: Machine coding Master</title>
      <link>https://dev.to/machinecodingmaster</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/machinecodingmaster"/>
    <language>en</language>
    <item>
      <title>Surviving MatchException: The Sealed Hierarchy Landmine in Java 21+ Microservices</title>
      <dc:creator>Machine coding Master</dc:creator>
      <pubDate>Thu, 22 Oct 2026 07:56:21 +0000</pubDate>
      <link>https://dev.to/machinecodingmaster/surviving-matchexception-the-sealed-hierarchy-landmine-in-java-21-microservices-4h0k</link>
      <guid>https://dev.to/machinecodingmaster/surviving-matchexception-the-sealed-hierarchy-landmine-in-java-21-microservices-4h0k</guid>
      <description>&lt;h2&gt;
  
  
  Surviving MatchException: The Sealed Hierarchy Landmine in Java 21+ Microservices
&lt;/h2&gt;

&lt;p&gt;Teams are aggressively migrating event schemas to Java 21+ sealed interfaces and record patterns, celebrating the death of boilerplate visitor code right until their first rolling deployment triggers an outage. When a producer publishes a new permitted subtype to a shared artifact, downstream consumers running the previous client version fail at runtime with a catastrophic &lt;code&gt;java.lang.MatchException&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Heads up:&lt;/strong&gt; if you want to see these patterns applied to real interview problems, &lt;a href="https://javalld.com" rel="noopener noreferrer"&gt;javalld.com&lt;/a&gt; has full machine coding solutions with traces.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Why Most Developers Get This Wrong
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Confusing compile-time exhaustiveness with binary compatibility:&lt;/strong&gt; &lt;code&gt;javac&lt;/code&gt; validates exhaustive pattern switches against the hierarchy known &lt;em&gt;at compile time&lt;/em&gt;, omitting a fallback branch and emitting a synthetic &lt;code&gt;MatchException&lt;/code&gt; invocation if an unexpected subtype arrives at runtime.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sharing domain models over the wire:&lt;/strong&gt; Exposing internal sealed hierarchies directly in shared SDKs couples deployment lifecycles, breaking the independence of microservices during rolling upgrades or canary releases.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Assuming deserialization guarantees safety:&lt;/strong&gt; JSON parsers like Jackson might successfully deserialize a newly added permitted record if configured loosely, passing the unhandled subtype directly into your pattern switch.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Right Way
&lt;/h2&gt;

&lt;p&gt;Decouple wire-level consumption from exhaustive pattern switching by treating cross-service sealed hierarchies as open at the boundary.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sacrifice exhaustiveness at ingestion boundaries:&lt;/strong&gt; Intentionally include a fallback pattern (&lt;code&gt;case OrderEvent unknown -&amp;gt; ...&lt;/code&gt;) or a standard &lt;code&gt;default&lt;/code&gt; to swallow version skew gracefully instead of crashing the worker thread.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Model an explicit fallback subtype:&lt;/strong&gt; Introduce an &lt;code&gt;UnknownEvent(String payload) implements OrderEvent&lt;/code&gt; record into the sealed hierarchy, paired with a custom deserializer that traps unrecognized type tags.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Enforce strict consumer isolation:&lt;/strong&gt; Translate external payloads into bounded-context-specific sealed types rather than sharing the exact same Java artifact across service boundaries.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Show Me The Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Boundary consumer: Neutralize compiler-synthesized MatchException&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;handleIncomingEvent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;OrderEvent&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="k"&gt;switch&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="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;OrderCreated&lt;/span&gt; &lt;span class="n"&gt;created&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;processCreated&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;created&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;OrderCancelled&lt;/span&gt; &lt;span class="n"&gt;cancelled&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;processCancelled&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cancelled&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// CRITICAL: Prevents runtime crash when producer adds OrderRefunded &lt;/span&gt;
        &lt;span class="c1"&gt;// before this consumer updates its shared domain library&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;OrderEvent&lt;/span&gt; &lt;span class="n"&gt;unknown&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;meterRegistry&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;counter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"events.unhandled.version_skew"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;increment&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;deadLetterQueue&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;unknown&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;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Sealed pattern matching is exceptional for domain modeling within a single deployable unit, but dangerous across distributed boundaries subject to independent release cadences.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;java.lang.MatchException&lt;/code&gt; is a runtime failure mode generated by the compiler; binary compatibility does not equal semantic exhaustiveness.&lt;/li&gt;
&lt;li&gt;Always include an explicit catch-all pattern on switches operating on cross-service domain events to survive asynchronous cluster upgrades.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>systemdesign</category>
      <category>oop</category>
      <category>programming</category>
    </item>
    <item>
      <title>Stop Routing Reads to Primary: Building Causal Consistency in Java with Postgres LSN Tokens</title>
      <dc:creator>Machine coding Master</dc:creator>
      <pubDate>Mon, 19 Oct 2026 08:02:54 +0000</pubDate>
      <link>https://dev.to/machinecodingmaster/stop-routing-reads-to-primary-building-causal-consistency-in-java-with-postgres-lsn-tokens-5g98</link>
      <guid>https://dev.to/machinecodingmaster/stop-routing-reads-to-primary-building-causal-consistency-in-java-with-postgres-lsn-tokens-5g98</guid>
      <description>&lt;h2&gt;
  
  
  Stop Routing Reads to Primary: Building Causal Consistency in Java with Postgres LSN Tokens
&lt;/h2&gt;

&lt;p&gt;Falling back to your primary database for reads right after a mutation is a lazy architectural crutch that kills your write capacity at scale. In 2026, if you aren't using causal session tokens across your edge and read replicas, you're needlessly setting money on fire with oversized primary instances.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Want to go deeper? &lt;a href="https://javalld.com" rel="noopener noreferrer"&gt;javalld.com&lt;/a&gt; — machine coding interview problems with working Java code and full execution traces.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Why Most Developers Get This Wrong
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Arbitrary TTL pinning:&lt;/strong&gt; Routing all session reads to the primary for "5 seconds after a write" blindsides your master node during traffic spikes and wastes up to 40% of replica capacity.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ignoring database watermarks:&lt;/strong&gt; Engineers treat replication lag as an unpredictable mystery instead of querying exact write-ahead positions (&lt;code&gt;pg_current_wal_lsn()&lt;/code&gt; in Postgres or GTID in MySQL).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Over-engineering with distributed locks:&lt;/strong&gt; Reaching for heavy coordination layers like Raft or Redis locks when monotonic causal consistency (Read-Your-Own-Writes) is all the end-user requires.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Right Way
&lt;/h2&gt;

&lt;p&gt;Capture the commit log sequence number on writes, return it to the client as an HTTP response header, and enforce replica watermarks on subsequent reads.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Extract the engine's log sequence number (e.g., Postgres LSN) immediately within your mutation transaction.&lt;/li&gt;
&lt;li&gt;Echo the token back to the client via an HTTP header (e.g., &lt;code&gt;X-Causal-Token: 0/16B3748&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Pass the token back in subsequent &lt;code&gt;GET&lt;/code&gt; requests and route to a replica pool managed by a Spring &lt;code&gt;AbstractRoutingDataSource&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Check replica state against the token using a low-latency cached watermark; fall back to the primary only when lag hedging bounds (e.g., 25ms) are exceeded.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Show Me The Code
&lt;/h2&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;CausalReadDataSourceRouter&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;AbstractRoutingDataSource&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;final&lt;/span&gt; &lt;span class="nc"&gt;ReplicaWatermarkTracker&lt;/span&gt; &lt;span class="n"&gt;watermarkTracker&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;protected&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="nf"&gt;determineCurrentLookupKey&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;requiredLsn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CausalContextHolder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getRequiredLsn&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;requiredLsn&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;return&lt;/span&gt; &lt;span class="nc"&gt;DataSourceType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;REPLICA&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Cold reads bypass lag verification&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// Fast in-memory check against background-polled pg_last_wal_replay_lsn()&lt;/span&gt;
        &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;replicaCaughtUp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;watermarkTracker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isReplayed&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;requiredLsn&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;replicaCaughtUp&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;DataSourceType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;REPLICA&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;DataSourceType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;PRIMARY&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;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Replicas are for reads:&lt;/strong&gt; Pinning entire user sessions to the primary defeats the purpose of horizontal scaling.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LSN tokens enable true causal consistency:&lt;/strong&gt; The client tracks their own timeline without imposing global linearizability bottlenecks on your persistence tier.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hedging protects latency:&lt;/strong&gt; Always implement a fast fallback threshold so replication spikes degrade to primary reads rather than surfacing connection timeouts.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>systemdesign</category>
      <category>concurrency</category>
      <category>programming</category>
    </item>
    <item>
      <title>Stop Hallucinating Footnotes: Implementing Claude's Native Citations with Spring AI and Java 26</title>
      <dc:creator>Machine coding Master</dc:creator>
      <pubDate>Sun, 18 Oct 2026 07:23:52 +0000</pubDate>
      <link>https://dev.to/machinecodingmaster/stop-hallucinating-footnotes-implementing-claudes-native-citations-with-spring-ai-and-java-26-54fa</link>
      <guid>https://dev.to/machinecodingmaster/stop-hallucinating-footnotes-implementing-claudes-native-citations-with-spring-ai-and-java-26-54fa</guid>
      <description>&lt;h2&gt;
  
  
  Stop Hallucinating Footnotes: Implementing Claude's Native Citations with Spring AI and Java 26
&lt;/h2&gt;

&lt;p&gt;Enterprise legal teams in 2026 will immediately reject your RAG pipeline if your citations rely on an LLM hallucinating bracketed markdown footnotes like &lt;code&gt;[1]&lt;/code&gt;. Anthropic's native citations API solves this compliance nightmare by shifting source attribution from stochastic text generation to deterministic, byte-accurate document block references.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Most Developers Get This Wrong
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Begging Claude via system prompts to "append markdown footnotes with source URLs," which fails catastrophically under token pressure and complex multi-document context.&lt;/li&gt;
&lt;li&gt;Collapsing streaming citation events into the standard text buffer, forcing frontend teams to build fragile regex parsers to reconstruct source attributions.&lt;/li&gt;
&lt;li&gt;Ignoring Anthropic's native &lt;code&gt;char_location&lt;/code&gt; metadata, throwing away verifiable byte-offset audit trails that legal compliance engines require.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Right Way
&lt;/h2&gt;

&lt;p&gt;Feed reference context through Anthropic's native &lt;code&gt;document&lt;/code&gt; content blocks with citations enabled, streaming raw citation deltas directly into strongly typed Java 26 records.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Configure context payloads as explicit &lt;code&gt;document&lt;/code&gt; blocks with &lt;code&gt;citations: { enabled: true }&lt;/code&gt; in your Spring AI Anthropic request customizers.&lt;/li&gt;
&lt;li&gt;Intercept low-level Server-Sent Events (SSE) to isolate &lt;code&gt;citation_delta&lt;/code&gt; payloads from standard &lt;code&gt;content_block_delta&lt;/code&gt; tokens.&lt;/li&gt;
&lt;li&gt;Map exact &lt;code&gt;char_location&lt;/code&gt; spans (&lt;code&gt;start_char_index&lt;/code&gt;, &lt;code&gt;end_char_index&lt;/code&gt;) back to your primary database keys using Java 26 pattern matching.&lt;/li&gt;
&lt;li&gt;Stream citations as side-channel metadata events over your reactive SSE pipe to keep UI token rendering instant and clean.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Show Me The Code
&lt;/h2&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="n"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;DocumentRef&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;docId&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;startChar&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;endChar&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;citedText&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt;

&lt;span class="c1"&gt;// Unpack streaming citation delta events using Java 26 record deconstruction&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;DocumentRef&lt;/span&gt; &lt;span class="nf"&gt;resolveCitationDelta&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AnthropicStreamChunk&lt;/span&gt; &lt;span class="n"&gt;chunk&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="nf"&gt;switch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nf"&gt;CitationDelta&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;CharLocation&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;DocumentRef&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;ContentBlockDelta&lt;/span&gt; &lt;span class="n"&gt;ignored&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="o"&gt;-&amp;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="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Kill the footnote prompts: native citations provide mathematical source grounding that enterprise compliance teams will actually sign off on.&lt;/li&gt;
&lt;li&gt;Treat citations as first-class streaming metadata events, completely decoupled from generation text buffers.&lt;/li&gt;
&lt;li&gt;Use Java 26 record patterns to cleanly unpack nested &lt;code&gt;char_location&lt;/code&gt; boundaries into domain-safe audit trail records without boilerplate.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;I built &lt;a href="https://javalld.com" rel="noopener noreferrer"&gt;javalld.com&lt;/a&gt; while prepping for senior roles — complete LLD problems with execution traces, not just theory.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>java</category>
      <category>ai</category>
      <category>llm</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Stop AI NullPointerExceptions: Compile-Time Zero-NPE Java with JSpecify, NullAway, and Claude Code</title>
      <dc:creator>Machine coding Master</dc:creator>
      <pubDate>Sat, 17 Oct 2026 07:09:43 +0000</pubDate>
      <link>https://dev.to/machinecodingmaster/stop-ai-nullpointerexceptions-compile-time-zero-npe-java-with-jspecify-nullaway-and-claude-code-2ph3</link>
      <guid>https://dev.to/machinecodingmaster/stop-ai-nullpointerexceptions-compile-time-zero-npe-java-with-jspecify-nullaway-and-claude-code-2ph3</guid>
      <description>&lt;h2&gt;
  
  
  Stop AI NullPointerExceptions: Compile-Time Zero-NPE Java with JSpecify, NullAway, and Claude Code
&lt;/h2&gt;

&lt;p&gt;AI agents generate Java at blistering speeds, but LLMs still hallucinate null safety by littering codebases with redundant null-checks instead of enforcing structural invariants. If your agentic workflow in 2026 isn't hard-gated by strict compile-time diagnostics, you are simply automating the generation of production &lt;code&gt;NullPointerException&lt;/code&gt;s.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Most Developers Get This Wrong
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Relying on fragmented annotations:&lt;/strong&gt; LLMs mix JSR-305, SpotBugs, and JetBrains annotations indiscriminately, breaking static analysis across modular boundaries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Overusing &lt;code&gt;Optional&amp;lt;T&amp;gt;&lt;/code&gt;:&lt;/strong&gt; Models default to wrapping internal domain fields and method parameters in &lt;code&gt;Optional&lt;/code&gt;, degrading memory efficiency and polluting domain models.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Treating the LLM as a typist, not a compiler loop:&lt;/strong&gt; Developers still manually review AI-generated diffs instead of forcing the agent to resolve raw AST compiler errors autonomously.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Right Way
&lt;/h2&gt;

&lt;p&gt;Make non-null the default and wire deterministic compiler errors into Claude Code's execution loop.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Enforce standard JSpecify (&lt;code&gt;org.jspecify:jspecify:1.0.0&lt;/code&gt;) across your codebase to give LLMs a single, unambiguous annotation grammar.&lt;/li&gt;
&lt;li&gt;Mark entire packages with &lt;code&gt;@NullMarked&lt;/code&gt;, forcing the compiler to treat every unannotated type as strictly non-null.&lt;/li&gt;
&lt;li&gt;Wire Uber's NullAway into Error Prone with &lt;code&gt;TreatWarningsAsErrors&lt;/code&gt; enabled to fail builds immediately upon any contract violation.&lt;/li&gt;
&lt;li&gt;Run Claude Code CLI inside a closed feedback loop (&lt;code&gt;claude "fix NullAway errors after running ./gradlew compileJava"&lt;/code&gt;), allowing the agent to self-correct based on exact line-and-column diagnostic outputs.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;I built &lt;a href="https://javalld.com" rel="noopener noreferrer"&gt;javalld.com&lt;/a&gt; while prepping for senior roles — complete LLD problems with execution traces, not just theory.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Show Me The Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// package-info.java: @org.jspecify.annotations.NullMarked&lt;/span&gt;
&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;com.payment.core&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.jspecify.annotations.Nullable&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SettlementService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// NullAway halts compilation if order is null, or if nullable ledger is dereferenced unsafely&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Receipt&lt;/span&gt; &lt;span class="nf"&gt;settle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Order&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;@Nullable&lt;/span&gt; &lt;span class="nc"&gt;AuditLedger&lt;/span&gt; &lt;span class="n"&gt;ledger&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;txId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;transactionId&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Guaranteed non-null by @NullMarked&lt;/span&gt;
        &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;auditCode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ledger&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="n"&gt;ledger&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;record&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;txId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0L&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Receipt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;txId&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;auditCode&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;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Standardize on JSpecify:&lt;/strong&gt; Stop letting AI guess between checker frameworks; standardize on &lt;code&gt;@NullMarked&lt;/code&gt; and &lt;code&gt;@Nullable&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fast diagnostics win:&lt;/strong&gt; NullAway adds single-digit millisecond overhead to compilation, making it fast enough for real-time agent loops.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Close the agent loop:&lt;/strong&gt; Feed raw &lt;code&gt;javac&lt;/code&gt; and NullAway errors directly back into Claude Code so it fixes its own nullability violations before code ever hits git.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>productivity</category>
      <category>ai</category>
      <category>programming</category>
    </item>
    <item>
      <title>Thread-Safe Counter in Java: Fix Your First Concurrency Bug in Machine Coding</title>
      <dc:creator>Machine coding Master</dc:creator>
      <pubDate>Fri, 16 Oct 2026 07:30:09 +0000</pubDate>
      <link>https://dev.to/machinecodingmaster/thread-safe-counter-in-java-fix-your-first-concurrency-bug-in-machine-coding-gag</link>
      <guid>https://dev.to/machinecodingmaster/thread-safe-counter-in-java-fix-your-first-concurrency-bug-in-machine-coding-gag</guid>
      <description>&lt;h2&gt;
  
  
  Thread-Safe Counter in Java: Fix Your First Concurrency Bug in Machine Coding
&lt;/h2&gt;

&lt;p&gt;During high-pressure machine coding interviews, seemingly trivial building blocks are where candidates quietly introduce race conditions that fail multi-threaded verification tests. A standard counter looks deceptively simple, but failing to handle concurrent mutation immediately signals a lack of production-grade Java depth to senior interviewers.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Mistake Most Candidates Make
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Assuming &lt;code&gt;count++&lt;/code&gt; is an atomic instruction; at the bytecode level, it is a non-atomic read-modify-write sequence (&lt;code&gt;getfield&lt;/code&gt;, &lt;code&gt;iadd&lt;/code&gt;, &lt;code&gt;putfield&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Declaring &lt;code&gt;volatile int count = 0;&lt;/code&gt; under the false impression that visibility guarantees atomicity; it syncs cache lines across CPU cores but does not stop interleaved updates.&lt;/li&gt;
&lt;li&gt;Defaulting to heavy &lt;code&gt;synchronized&lt;/code&gt; blocks around simple scalar updates, introducing unnecessary lock contention and context-switching overhead.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Right Approach
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Treat state mutations as atomic transactions that guarantee both memory visibility and uninterrupted execution across threads.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;AtomicInteger&lt;/code&gt;, &lt;code&gt;LongAdder&lt;/code&gt;, &lt;code&gt;ReentrantLock&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Lock-free primitives leverage hardware-level Compare-And-Swap (CAS) instructions to eliminate race conditions without suspending threads.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Heads up:&lt;/strong&gt; if you want to see these patterns applied to real interview problems, &lt;a href="https://javalld.com" rel="noopener noreferrer"&gt;javalld.com&lt;/a&gt; has full machine coding solutions with traces.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The Key Insight (Code)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.concurrent.atomic.AtomicInteger&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ThreadSafeCounter&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;final&lt;/span&gt; &lt;span class="nc"&gt;AtomicInteger&lt;/span&gt; &lt;span class="n"&gt;count&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;AtomicInteger&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Lock-free atomic increment using hardware CAS&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;increment&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;count&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;incrementAndGet&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;get&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;count&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&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;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;count++&lt;/code&gt; is non-atomic; concurrent threads will interleave during execution, leading to lost increments and non-deterministic results.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;volatile&lt;/code&gt; fixes the cache visibility problem, but it cannot make compound read-modify-write operations thread-safe.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;AtomicInteger&lt;/code&gt; for standard thread-safe counting, and consider &lt;code&gt;LongAdder&lt;/code&gt; for high-throughput write-heavy scenarios to reduce CAS contention.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Full working implementation with execution trace available at &lt;a href="https://javalld.com/learn/thread-counter" rel="noopener noreferrer"&gt;https://javalld.com/learn/thread-counter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>concurrency</category>
      <category>interview</category>
      <category>design</category>
    </item>
    <item>
      <title>Stop Restarting Failed Loops: Resumable ReAct Agents with Spring AI State Checkpointing</title>
      <dc:creator>Machine coding Master</dc:creator>
      <pubDate>Sun, 11 Oct 2026 09:14:25 +0000</pubDate>
      <link>https://dev.to/machinecodingmaster/stop-restarting-failed-loops-resumable-react-agents-with-spring-ai-state-checkpointing-3glb</link>
      <guid>https://dev.to/machinecodingmaster/stop-restarting-failed-loops-resumable-react-agents-with-spring-ai-state-checkpointing-3glb</guid>
      <description>&lt;h2&gt;
  
  
  Stop Restarting Failed Loops: Resumable ReAct Agents with Spring AI State Checkpointing
&lt;/h2&gt;

&lt;p&gt;Your ReAct agent just burned 45,000 tokens navigating an 8-step enterprise workflow, only to blow up on a transient downstream 503 at step 7. Catching that exception and restarting the prompt chain from step zero isn't just terrible engineering—it is lighting your inference budget on fire.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Want to go deeper? &lt;a href="https://javalld.com" rel="noopener noreferrer"&gt;javalld.com&lt;/a&gt; — machine coding interview problems with working Java code and full execution traces.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Why Most Developers Get This Wrong
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Treating agents like stateless HTTP calls:&lt;/strong&gt; Wrapping &lt;code&gt;ChatClient&lt;/code&gt; inside a basic &lt;code&gt;@Retryable&lt;/code&gt; loop forces the model to regenerate every prior thought and action when a tool flakes out.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Side-effect amnesia:&lt;/strong&gt; Retrying an agent from scratch without an idempotent journal causes non-idempotent tools (like payment authorization or ticket creation) to double-execute.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;In-memory state reliance:&lt;/strong&gt; Storing the Thought-Action-Observation trace purely in JVM memory guarantees unrecoverable drops on container redeploys or out-of-memory kills.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Right Way
&lt;/h2&gt;

&lt;p&gt;Decouple reasoning from tool execution by persisting an append-only state journal at every step of the ReAct cycle.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Implement an explicit &lt;code&gt;StateCheckpointRepository&lt;/code&gt; that commits the &lt;code&gt;AgentContext&lt;/code&gt; and conversation trace immediately after every tool observation.&lt;/li&gt;
&lt;li&gt;Attach an idempotency key derived from &lt;code&gt;(agentRunId, stepIndex, toolName)&lt;/code&gt; to every external side effect.&lt;/li&gt;
&lt;li&gt;Resume failed workflows deterministically by rehydrating the checkpointed history into &lt;code&gt;ReActAgent&lt;/code&gt; rather than re-prompting the initial goal.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Show Me The Code
&lt;/h2&gt;

&lt;p&gt;Configure a durable, checkpoint-aware ReAct workflow in Spring AI:&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="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;AgentExecutor&lt;/span&gt; &lt;span class="nf"&gt;orderRecoveryAgent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ChatClient&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Builder&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;VectorCheckpointStore&lt;/span&gt; &lt;span class="n"&gt;store&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="nc"&gt;ReActAgent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;tools&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inventoryTool&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;paymentTool&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;logisticsTool&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;checkpointStore&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Persists state to PostgreSQL after every step&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;onStepFailure&lt;/span&gt;&lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;failure&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Step {} failed for run {}. Checkpointing for resume."&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; 
                &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getStepIndex&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getRunId&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;failure&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;RecoveryStrategy&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;SUSPEND&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Freezes state, releases thread&lt;/span&gt;
        &lt;span class="o"&gt;})&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;idempotencyKeyGenerator&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getRunId&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;":"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getStepIndex&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&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;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Never restart from step zero:&lt;/strong&gt; Save intermediate Thought/Action/Observation vectors so transient failures only cost you the failed step's tokens.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Enforce tool idempotency:&lt;/strong&gt; Key all side-effecting operations by agent execution ID and step index to make re-entry safe.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Treat agents as distributed state machines:&lt;/strong&gt; If your framework doesn't survive a JVM crash mid-loop, it is not production-ready.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>ai</category>
      <category>systemdesign</category>
      <category>llm</category>
    </item>
    <item>
      <title>Kill the Visitor Pattern: Refactoring Domain ASTs with Java 21 Sealed Hierarchies</title>
      <dc:creator>Machine coding Master</dc:creator>
      <pubDate>Sat, 10 Oct 2026 08:53:55 +0000</pubDate>
      <link>https://dev.to/machinecodingmaster/kill-the-visitor-pattern-refactoring-domain-asts-with-java-21-sealed-hierarchies-391f</link>
      <guid>https://dev.to/machinecodingmaster/kill-the-visitor-pattern-refactoring-domain-asts-with-java-21-sealed-hierarchies-391f</guid>
      <description>&lt;h2&gt;
  
  
  Kill the Visitor Pattern: Refactoring Domain ASTs with Java 21 Sealed Hierarchies
&lt;/h2&gt;

&lt;p&gt;For decades, the Visitor pattern was our only escape hatch for adding operations to object structures without mutating them. In modern backend engineering, clinging to double-dispatch boilerplate across your domain evaluators and AST engines is pure technical debt—sealed hierarchies and record patterns have rendered it completely obsolete.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Heads up:&lt;/strong&gt; if you want to see these patterns applied to real interview problems, &lt;a href="https://javalld.com" rel="noopener noreferrer"&gt;javalld.com&lt;/a&gt; has full machine coding solutions with traces.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Why Most Developers Get This Wrong
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Treating the Gang of Four Visitor pattern as an untouchable standard, forcing every new domain node to implement &lt;code&gt;accept(NodeVisitor v)&lt;/code&gt; and fragmenting traversal logic across dozens of files.&lt;/li&gt;
&lt;li&gt;Reverting to manual &lt;code&gt;instanceof&lt;/code&gt; checks with ugly &lt;code&gt;if/else&lt;/code&gt; ladders instead of trusting the compiler to guarantee total coverage.&lt;/li&gt;
&lt;li&gt;Leaving intermediate record extractors in place rather than leveraging nested pattern deconstruction to unwrap complex payloads in a single expression.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Right Way
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Model your domain engine as an algebraic data type with sealed interfaces, then evaluate nodes using exhaustive &lt;code&gt;switch&lt;/code&gt; expressions and nested deconstruction.&lt;/li&gt;
&lt;li&gt;Declare the root node as &lt;code&gt;sealed interface Node permits ...&lt;/code&gt; to turn your hierarchy into a closed, compiler-enforced contract.&lt;/li&gt;
&lt;li&gt;Delete all &lt;code&gt;accept()&lt;/code&gt;, &lt;code&gt;visit()&lt;/code&gt;, and &lt;code&gt;Visitor&lt;/code&gt; classes across the entire codebase—zero double-dispatch overhead.&lt;/li&gt;
&lt;li&gt;Match on structural shapes directly in switch arms, extracting nested values without intermediate local variables.&lt;/li&gt;
&lt;li&gt;Eliminate runtime &lt;code&gt;default&lt;/code&gt; branches: adding a new record type to the sealed hierarchy breaks the build at compile time wherever an evaluator exists.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Show Me The Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;sealed&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Expr&lt;/span&gt; &lt;span class="n"&gt;permits&lt;/span&gt; &lt;span class="nc"&gt;Const&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Neg&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt;
&lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;Const&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Expr&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt;
&lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Expr&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Expr&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Expr&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt;
&lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;Neg&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Expr&lt;/span&gt; &lt;span class="n"&gt;expr&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Expr&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt;

&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;eval&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Expr&lt;/span&gt; &lt;span class="n"&gt;expr&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="nf"&gt;switch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;expr&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nf"&gt;Const&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;val&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;val&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Const&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="nc"&gt;Const&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;r&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;l&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// nested deconstruction&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;r&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;eval&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;eval&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nf"&gt;Neg&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;eval&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&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;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Double-dispatch is dead: pattern matching gives you clean separation of data and behavior without recursive indirection.&lt;/li&gt;
&lt;li&gt;Exhaustiveness is safer than visitor contracts: &lt;code&gt;javac&lt;/code&gt; catches missing edge cases at compile time without relying on runtime fallbacks.&lt;/li&gt;
&lt;li&gt;Nested record patterns collapse tree transformations: complex AST evaluation logic drops from hundreds of boilerplate lines down to declarative switch blocks.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>design</category>
      <category>programming</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Java Machine Coding: Print Sequence with Multiple Threads</title>
      <dc:creator>Machine coding Master</dc:creator>
      <pubDate>Fri, 09 Oct 2026 09:31:03 +0000</pubDate>
      <link>https://dev.to/machinecodingmaster/java-machine-coding-print-sequence-with-multiple-threads-322i</link>
      <guid>https://dev.to/machinecodingmaster/java-machine-coding-print-sequence-with-multiple-threads-322i</guid>
      <description>&lt;h2&gt;
  
  
  Java Machine Coding: Print Sequence with Multiple Threads
&lt;/h2&gt;

&lt;p&gt;Printing a strictly ordered sequence using multiple threads is a classic FAANG concurrency screen because it instantly exposes whether you grasp inter-thread coordination. Candidates who struggle typically fall back on brittle busy-waiting loops or create deadlocks under high thread contention.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Mistake Most Candidates Make
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Spinning on &lt;code&gt;volatile&lt;/code&gt; variables:&lt;/strong&gt; Relying on shared flags inside tight &lt;code&gt;while&lt;/code&gt; loops, burning 100% CPU core capacity through busy-waiting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Overusing &lt;code&gt;notifyAll()&lt;/code&gt; on single intrinsic locks:&lt;/strong&gt; Waking up every dormant thread simultaneously, causing a thundering herd problem where $N-1$ threads immediately go back to sleep.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Missing spurious wakeup guards:&lt;/strong&gt; Using &lt;code&gt;if&lt;/code&gt; instead of &lt;code&gt;while&lt;/code&gt; around wait checks, leading to corrupt execution order when the JVM awakens a thread unpredictably.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Right Approach
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Core mental model:&lt;/strong&gt; Model execution as an explicit turn-based state machine where each worker blocks on its own condition variable until specifically signaled by its predecessor.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Key entities/classes:&lt;/strong&gt; &lt;code&gt;ReentrantLock&lt;/code&gt;, &lt;code&gt;Condition[]&lt;/code&gt;, and a shared &lt;code&gt;currentTurn&lt;/code&gt; state integer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Why it beats the naive approach:&lt;/strong&gt; Target-signaling dedicated condition queues yields $O(1)$ wakeups and completely prevents CPU burn and contention overhead.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Key Insight (Code)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;lock&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;lock&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="c1"&gt;// 1. Guard against spurious wakeups&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;currentTurn&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;threadId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;conditions&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;threadId&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="na"&gt;await&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// 2. Perform work and advance state&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;currentTurn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;currentTurn&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="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;totalThreads&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// 3. Wake ONLY the next thread in sequence&lt;/span&gt;
    &lt;span class="n"&gt;conditions&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;currentTurn&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="na"&gt;signal&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;lock&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;unlock&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;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Always wrap &lt;code&gt;.await()&lt;/code&gt; calls in a &lt;code&gt;while&lt;/code&gt; loop check to preserve invariants against JVM spurious wakeups.&lt;/li&gt;
&lt;li&gt;Array-based &lt;code&gt;Condition&lt;/code&gt; references allow deterministic, point-to-point signaling between arbitrary thread topologies.&lt;/li&gt;
&lt;li&gt;Update sequencing state strictly within the critical section prior to firing &lt;code&gt;.signal()&lt;/code&gt; to ensure atomicity.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;If you're prepping for interviews, I've been building &lt;a href="https://javalld.com" rel="noopener noreferrer"&gt;javalld.com&lt;/a&gt; — real machine coding problems with full execution traces.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Full working implementation with execution trace available at &lt;a href="https://javalld.com/learn/print-sequence" rel="noopener noreferrer"&gt;https://javalld.com/learn/print-sequence&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>concurrency</category>
      <category>interview</category>
      <category>programming</category>
    </item>
    <item>
      <title>Java &amp; AI: What Developers Need to Know</title>
      <dc:creator>Machine coding Master</dc:creator>
      <pubDate>Sun, 04 Oct 2026 08:41:57 +0000</pubDate>
      <link>https://dev.to/machinecodingmaster/java-ai-what-developers-need-to-know-3fej</link>
      <guid>https://dev.to/machinecodingmaster/java-ai-what-developers-need-to-know-3fej</guid>
      <description>&lt;h2&gt;
  
  
  Stop Context Starvation: Implementing Parent Document Retrieval in Spring AI
&lt;/h2&gt;

&lt;p&gt;Uniform 512-token chunking in production RAG pipelines is dead; it either dilutes vector search precision or starves your LLM of surrounding context. By adopting parent-child retrieval in Spring AI with pgvector, you decouple needle-in-a-haystack search granularity from generation context.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Most Developers Get This Wrong
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Embedding massive chunks:&lt;/strong&gt; Stuffing 1,000-token blocks into &lt;code&gt;PgVectorStore&lt;/code&gt; hoping embeddings retain fine-grained facts—cosine similarity inevitably washes out.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Feeding micro-chunks directly to the LLM:&lt;/strong&gt; Ingesting 128-token snippets yields high search precision, but passing isolated sentence fragments causes the LLM to hallucinate from missing context.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Over-engineering with extra infrastructure:&lt;/strong&gt; Spinning up separate graph databases or external key-value caches instead of leveraging relational metadata linking already available in PostgreSQL.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Right Way
&lt;/h2&gt;

&lt;p&gt;Decouple retrieval units from synthesis units by indexing micro-chunks containing a &lt;code&gt;parent_id&lt;/code&gt; metadata pointer, then hydrating the full parent document before building your prompt.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Split hierarchically:&lt;/strong&gt; Generate large parent sections (1,024 tokens) for synthesis and split them into child micro-chunks (128 tokens) for vector indexing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Link via metadata:&lt;/strong&gt; Inject the parent's primary key into each child &lt;code&gt;Document&lt;/code&gt; using Spring AI's &lt;code&gt;doc.getMetadata().put("parent_id", parentId)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Query pgvector with children:&lt;/strong&gt; Execute vector similarity searches strictly against the child embeddings for maximum retrieval sensitivity.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Expand context before generation:&lt;/strong&gt; Map child hits back to unique parent IDs and fetch the full parents via standard relational queries before calling &lt;code&gt;ChatClient&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Show Me The Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
java
public List&amp;lt;Document&amp;gt; retrieveWithParentContext(String query) {
    // 1. High-precision similarity search on 128-token child embeddings
    List&amp;lt;Document&amp;gt; childHits = vectorStore.similaritySearch(
        SearchRequest.builder().query(query).topK(5).similarityThreshold(0.78).build()
    );

    // 2. Extract unique parent UUIDs from Spring AI Document metadata
    Set&amp;lt;UUID&amp;gt; parentIds = childHits.stream()
        .map(doc -&amp;gt; UUID.fromString(doc.getMetadata().get("parent_id").toString()))
        .collect(Collectors.toSet());

    //
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>ai</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>The Shallow Immutability Trap: Hardening Java 21 Records for Virtual Threads</title>
      <dc:creator>Machine coding Master</dc:creator>
      <pubDate>Sat, 03 Oct 2026 08:35:33 +0000</pubDate>
      <link>https://dev.to/machinecodingmaster/the-shallow-immutability-trap-hardening-java-21-records-for-virtual-threads-3pgf</link>
      <guid>https://dev.to/machinecodingmaster/the-shallow-immutability-trap-hardening-java-21-records-for-virtual-threads-3pgf</guid>
      <description>&lt;h2&gt;
  
  
  The Shallow Immutability Trap: Hardening Java 21 Records for Virtual Threads
&lt;/h2&gt;

&lt;p&gt;Virtual threads allow us to spawn millions of concurrent tasks with trivial overhead, but teams are quietly sabotaging their services by assuming records provide deep immutability. Java records only make component references &lt;code&gt;final&lt;/code&gt;; passing mutable collections through them into massive concurrent fan-outs guarantees silent data corruption.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Most Developers Get This Wrong
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Confusing shallow field immutability with deep thread safety&lt;/strong&gt;: A &lt;code&gt;record UserPayload(List&amp;lt;String&amp;gt; roles)&lt;/code&gt; prevents reassignment of &lt;code&gt;roles&lt;/code&gt;, but any virtual thread can still call &lt;code&gt;roles.add()&lt;/code&gt; concurrently without triggering a compile-time warning.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Blinded by structured concurrency&lt;/strong&gt;: Launching thousands of subtasks via &lt;code&gt;StructuredTaskScope&lt;/code&gt; while passing around unhardened records leads to unsynchronized, cross-thread mutations on shared heap structures.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Trusting generated accessors&lt;/strong&gt;: Standard record accessors return direct object references, completely bypassing encapsulation and leaking internal mutable state to unpredictable caller threads.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Right Way
&lt;/h2&gt;

&lt;p&gt;Enforce strict, deep immutability at construction time so records remain genuinely read-only across arbitrary virtual thread boundaries.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use compact canonical constructors to replace incoming collection references with frozen copies via &lt;code&gt;List.copyOf()&lt;/code&gt;, &lt;code&gt;Set.copyOf()&lt;/code&gt;, or &lt;code&gt;Map.copyOf()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Reject or defensively copy mutable components (such as &lt;code&gt;Date&lt;/code&gt;, arrays, or custom domain POJOs) before the instance finishes instantiation.&lt;/li&gt;
&lt;li&gt;Avoid exposing raw, mutable third-party objects inside records—wrap them in unmodifiable adapters or domain primitives.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Want to go deeper? &lt;a href="https://javalld.com" rel="noopener noreferrer"&gt;javalld.com&lt;/a&gt; — machine coding interview problems with working Java code and full execution traces.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Show Me The Code
&lt;/h2&gt;

&lt;p&gt;Here is how to properly harden a record against concurrent mutation across millions of virtual threads:&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="n"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;OrderBatch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;batchId&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;UUID&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;orderIds&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Instant&lt;/span&gt; &lt;span class="n"&gt;createdAt&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Compact canonical constructor enforces deep immutability&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;OrderBatch&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Objects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;requireNonNull&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;batchId&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"batchId cannot be null"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;Objects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;requireNonNull&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;createdAt&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"createdAt cannot be null"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// List.copyOf handles null-checking of elements and guarantees an unmodifiable list&lt;/span&gt;
        &lt;span class="n"&gt;orderIds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;copyOf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderIds&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;If an external virtual thread attempts &lt;code&gt;orderBatch.orderIds().add(newId)&lt;/code&gt;, it immediately fails fast with an &lt;code&gt;UnsupportedOperationException&lt;/code&gt; instead of corrupting shared memory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Records guarantee shallow immutability only; &lt;code&gt;final&lt;/code&gt; references to mutable objects are not thread-safe.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;List.copyOf()&lt;/code&gt; inside a compact constructor provides instant, fail-fast protection against concurrent modification bugs.&lt;/li&gt;
&lt;li&gt;As virtual thread counts scale into the millions, defensive copying at record boundaries is mandatory for state integrity.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>concurrency</category>
      <category>programming</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Reader-Writer Lock in Java: Solving the Library Problem in LLD</title>
      <dc:creator>Machine coding Master</dc:creator>
      <pubDate>Mon, 28 Sep 2026 08:54:57 +0000</pubDate>
      <link>https://dev.to/machinecodingmaster/reader-writer-lock-in-java-solving-the-library-problem-in-lld-omj</link>
      <guid>https://dev.to/machinecodingmaster/reader-writer-lock-in-java-solving-the-library-problem-in-lld-omj</guid>
      <description>&lt;h2&gt;
  
  
  Reader-Writer Lock in Java: Solving the Library Problem in LLD
&lt;/h2&gt;

&lt;p&gt;In Java concurrency and machine coding rounds, synchronizing read-heavy systems with coarse-grained locks is an immediate red flag. The classic "Library Problem"—where thousands browse books while librarians infrequently update the catalog—tests whether you understand lock contention and read-write access asymmetry.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Mistake Most Candidates Make
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Slapping &lt;code&gt;synchronized&lt;/code&gt; or a plain &lt;code&gt;ReentrantLock&lt;/code&gt; across all operations, serializing every reader and collapsing system throughput to a single thread.&lt;/li&gt;
&lt;li&gt;Ignoring writer starvation by defaulting to unfair lock policies where an endless stream of readers completely starves pending writes.&lt;/li&gt;
&lt;li&gt;Omitting proper resource cleanup by failing to place &lt;code&gt;unlock()&lt;/code&gt; inside &lt;code&gt;finally&lt;/code&gt; blocks, leading to permanent lock leaks when runtime exceptions occur.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Right Approach
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Core mental model:&lt;/strong&gt; Multiple readers can safely access shared state concurrently, but a writer demands absolute, exclusive ownership before mutating data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Key entities/classes:&lt;/strong&gt; &lt;code&gt;ReadWriteLock&lt;/code&gt;, &lt;code&gt;ReentrantReadWriteLock&lt;/code&gt;, &lt;code&gt;Lock&lt;/code&gt;, &lt;code&gt;CatalogManager&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Why it beats the naive approach:&lt;/strong&gt; It eliminates reader-on-reader lock contention entirely while maintaining strict consistency boundaries during updates.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Shameless plug: &lt;a href="https://javalld.com" rel="noopener noreferrer"&gt;javalld.com&lt;/a&gt; has full LLD implementations with step-by-step execution traces — free to use while prepping.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The Key Insight (Code)
&lt;/h2&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;BookCatalog&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;final&lt;/span&gt; &lt;span class="nc"&gt;ReentrantReadWriteLock&lt;/span&gt; &lt;span class="n"&gt;rwLock&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;ReentrantReadWriteLock&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&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;final&lt;/span&gt; &lt;span class="nc"&gt;Lock&lt;/span&gt; &lt;span class="n"&gt;readLock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rwLock&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;readLock&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;writeLock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rwLock&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;writeLock&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;final&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;Book&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;books&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;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Book&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;readLock&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;lock&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;books&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="n"&gt;readLock&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;unlock&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="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Book&lt;/span&gt; &lt;span class="n"&gt;book&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;writeLock&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;lock&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="n"&gt;books&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;book&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;book&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="n"&gt;writeLock&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;unlock&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;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;ReentrantReadWriteLock&lt;/code&gt; maximizes read-heavy performance by splitting synchronization into shared read locks and exclusive write locks.&lt;/li&gt;
&lt;li&gt;Enabling fairness (&lt;code&gt;fair = true&lt;/code&gt;) prevents writer starvation by prioritizing waiting writer threads over newly arriving reader threads in the acquisition queue.&lt;/li&gt;
&lt;li&gt;Lock downgrading (acquiring read lock while holding write lock, then releasing write lock) is fully supported, whereas lock upgrading is impossible and triggers deadlocks.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Full working implementation with execution trace available at &lt;a href="https://javalld.com/learn/reader-writer" rel="noopener noreferrer"&gt;https://javalld.com/learn/reader-writer&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>concurrency</category>
      <category>interview</category>
      <category>oop</category>
    </item>
    <item>
      <title>Stop Bloating Vector Indexes: Slicing Embeddings with Spring AI and Pgvector</title>
      <dc:creator>Machine coding Master</dc:creator>
      <pubDate>Sun, 27 Sep 2026 08:38:38 +0000</pubDate>
      <link>https://dev.to/machinecodingmaster/stop-bloating-vector-indexes-slicing-embeddings-with-spring-ai-and-pgvector-4fn8</link>
      <guid>https://dev.to/machinecodingmaster/stop-bloating-vector-indexes-slicing-embeddings-with-spring-ai-and-pgvector-4fn8</guid>
      <description>&lt;h2&gt;
  
  
  Stop Bloating Vector Indexes: Slicing Embeddings with Spring AI and Pgvector
&lt;/h2&gt;

&lt;p&gt;Your Postgres instance is quietly running out of RAM because you are indexing default 1536-dimensional vectors for chunks that require a fraction of that depth. Matryoshka Representation Learning (MRL) lets you safely prune up to 75% of those dimensions without sacrificing retrieval recall, slashing your &lt;code&gt;pgvector&lt;/code&gt; index footprint overnight.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Most Developers Get This Wrong
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Assuming embedding dimensions are immutable:&lt;/strong&gt; Blindly persisting 1536d or 3072d vectors directly into &lt;code&gt;pgvector&lt;/code&gt; and wondering why the HNSW graph spills out of &lt;code&gt;shared_buffers&lt;/code&gt; directly to disk.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Truncating arbitrary embeddings:&lt;/strong&gt; Slicing vectors generated by standard non-MRL models (like legacy Ada models), which completely breaks vector space geometry and destroys cosine similarity.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Over-engineering with client-side PCA:&lt;/strong&gt; Adding complex dimensionality reduction pipelines in Java services instead of using models specifically pre-trained to nest semantic information in prefix sub-vectors.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Right Way
&lt;/h2&gt;

&lt;p&gt;Let MRL models pack the highest-variance features into the first $N$ dimensions, then slice the output directly through Spring AI during ingestion and query execution.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Deploy MRL-native models such as &lt;code&gt;text-embedding-3-small&lt;/code&gt; or &lt;code&gt;nomic-embed-text&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Enforce truncated dimensions at the application layer using Spring AI's native client configuration.&lt;/li&gt;
&lt;li&gt;Re-index &lt;code&gt;pgvector&lt;/code&gt; storage from &lt;code&gt;vector(1536)&lt;/code&gt; down to &lt;code&gt;vector(512)&lt;/code&gt; or &lt;code&gt;vector(256)&lt;/code&gt;, reducing index build times and query latencies exponentially.&lt;/li&gt;
&lt;li&gt;Implement an optional two-stage retrieval: perform fast approximate nearest neighbors (ANN) on 256d vectors, then re-rank candidate documents if your domain requires razor-thin precision.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Heads up:&lt;/strong&gt; if you want to see these patterns applied to real interview problems, &lt;a href="https://javalld.com" rel="noopener noreferrer"&gt;javalld.com&lt;/a&gt; has full machine coding solutions with traces.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Show Me The Code
&lt;/h2&gt;

&lt;p&gt;Configure your Spring AI bean to slice dimensions at the API boundary, keeping Postgres lean:&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;@Configuration&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;VectorStoreConfig&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Bean&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;EmbeddingModel&lt;/span&gt; &lt;span class="nf"&gt;embeddingModel&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;OpenAiConnectionProperties&lt;/span&gt; &lt;span class="n"&gt;props&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OpenAiEmbeddingOptions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"text-embedding-3-small"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;dimensions&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;512&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// MRL slices 1536 -&amp;gt; 512 natively&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;OpenAiEmbeddingModel&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;OpenAiApi&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;props&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getApiKey&lt;/span&gt;&lt;span class="o"&gt;()),&lt;/span&gt; &lt;span class="nc"&gt;MetadataMode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;EMBED&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;options&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="c1"&gt;// Postgres schema:&lt;/span&gt;
&lt;span class="c1"&gt;// ALTER TABLE vector_store ALTER COLUMN embedding TYPE vector(512);&lt;/span&gt;
&lt;span class="c1"&gt;// CREATE INDEX ON vector_store USING hnsw (embedding vector_cosine_ops);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;70%+ RAM reduction:&lt;/strong&gt; Moving from 1536 to 512 dimensions drops Postgres vector memory pressure by roughly 66% while retaining over 98% of retrieval recall.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Zero client-side math:&lt;/strong&gt; Spring AI handles the dimension constraint natively through &lt;code&gt;OpenAiEmbeddingOptions&lt;/code&gt;, offloading dimensionality management to the model provider.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HNSW stays in memory:&lt;/strong&gt; Smaller vectors keep your graph indices inside Postgres &lt;code&gt;shared_buffers&lt;/code&gt;, eliminating costly NVMe disk paging during similarity scans.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>ai</category>
      <category>systemdesign</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
