<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Gradient</title>
	<atom:link href="https://www.gradientco.net/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.gradientco.net</link>
	<description>All that matters, in one place.</description>
	<lastBuildDate>Tue, 10 Nov 2026 02:24:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://www.gradientco.net/wp-content/uploads/2020/07/cropped-gradientlogo-01-32x32.png</url>
	<title>Gradient</title>
	<link>https://www.gradientco.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>The rollback that never ran: transactional self invocation in Spring Boot</title>
		<link>https://www.gradientco.net/engineering/transactional-self-invocation/</link>
		
		<dc:creator><![CDATA[Nabeel Ahmed]]></dc:creator>
		<pubDate>Tue, 10 Nov 2026 02:24:26 +0000</pubDate>
				<category><![CDATA[Engineering]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<guid isPermaLink="false">https://www.gradientco.net/?p=10244</guid>

					<description><![CDATA[A rejected order leaves its rows behind and a REQUIRES_NEW audit row disappears, because a call through this never reaches Spring's transactional proxy. A test that records every transaction, and four ways out.]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">A puzzle in three numbers: a nightly import reports <code>imported=2, rejected=[ACME-1002]</code>, yet the orders table holds 3 rows. The rejected order still has 2 order lines, saved by a <code>@Transactional</code> method that threw on its third line. We set this up on purpose in our own test project to reproduce transactional self invocation, and nothing logged a warning.</p>



<p class="wp-block-paragraph">So which part of Spring decided not to roll back?</p>



<p class="wp-block-paragraph"><strong>Short answer:</strong> none of it decided anything. <code>@Transactional</code> works through a proxy that wraps the bean, and a call written as <code>this.placeOne()</code> goes straight to the object, past the proxy, so no transaction starts. Move the transactional method to another bean, or write the boundary out with <code>TransactionTemplate</code>.</p>



<h2 class="wp-block-heading">What could be wrong with this import service?</h2>



<p class="wp-block-paragraph">The project runs Java 25, Spring Boot 4.1.1 and H2 as the test database. A partner sends a file of orders, and each order must be saved whole, with all its lines, or not at all:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-start:36;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2f363c;color:#d3d7dd">OrderService.java (buggy)</span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">ImportReport</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">placeAll</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">List</span><span style="color: #F97583">&lt;</span><span style="color: #B392F0">OrderDraft</span><span style="color: #F97583">&gt;</span><span style="color: #E1E4E8"> drafts) {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">int</span><span style="color: #E1E4E8"> imported </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #79B8FF">0</span><span style="color: #E1E4E8">;</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">List</span><span style="color: #F97583">&lt;</span><span style="color: #B392F0">String</span><span style="color: #F97583">&gt;</span><span style="color: #E1E4E8"> rejected </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">ArrayList</span><span style="color: #F97583">&lt;&gt;</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">for</span><span style="color: #E1E4E8"> (</span><span style="color: #B392F0">OrderDraft</span><span style="color: #E1E4E8"> draft </span><span style="color: #F97583">:</span><span style="color: #E1E4E8"> drafts) {</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #F97583">try</span><span style="color: #E1E4E8"> {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">                </span><span style="color: #B392F0">placeOne</span><span style="color: #E1E4E8">(draft);</span></span>
<span class="line"><span style="color: #E1E4E8">                imported</span><span style="color: #F97583">++</span><span style="color: #E1E4E8">;</span></span>
<span class="line"><span style="color: #E1E4E8">            } </span><span style="color: #F97583">catch</span><span style="color: #E1E4E8"> (</span><span style="color: #B392F0">DiscontinuedProductException</span><span style="color: #E1E4E8"> e) {</span></span>
<span class="line"><span style="color: #E1E4E8">                rejected.</span><span style="color: #B392F0">add</span><span style="color: #E1E4E8">(draft.</span><span style="color: #B392F0">customerRef</span><span style="color: #E1E4E8">());</span></span>
<span class="line"><span style="color: #E1E4E8">            }</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">ImportReport</span><span style="color: #E1E4E8">(imported, rejected);</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">    </span><span style="color: #F97583">@Transactional</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">PurchaseOrder</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">placeOne</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">OrderDraft</span><span style="color: #E1E4E8"> draft) {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">PurchaseOrder</span><span style="color: #E1E4E8"> order </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> orders.</span><span style="color: #B392F0">save</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">PurchaseOrder</span><span style="color: #E1E4E8">(draft.</span><span style="color: #B392F0">customerRef</span><span style="color: #E1E4E8">()));</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">for</span><span style="color: #E1E4E8"> (</span><span style="color: #B392F0">OrderDraft</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">LineDraft</span><span style="color: #E1E4E8"> line </span><span style="color: #F97583">:</span><span style="color: #E1E4E8"> draft.</span><span style="color: #B392F0">lines</span><span style="color: #E1E4E8">()) {</span></span>
<span class="line"><span style="color: #E1E4E8">            catalog.</span><span style="color: #B392F0">requireSellable</span><span style="color: #E1E4E8">(line.</span><span style="color: #B392F0">sku</span><span style="color: #E1E4E8">());</span></span>
<span class="line"><span style="color: #E1E4E8">            lines.</span><span style="color: #B392F0">save</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">OrderLine</span><span style="color: #E1E4E8">(order, line.</span><span style="color: #B392F0">sku</span><span style="color: #E1E4E8">(), line.</span><span style="color: #B392F0">quantity</span><span style="color: #E1E4E8">()));</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> order;</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span></code></pre></div>



<p class="wp-block-paragraph">Line 50 puts the transaction on the unit of work, one order. Line 41 calls it from a loop that keeps going when one order is bad. Splitting a batch into one transaction per item is good design, and a reviewer would likely approve it as written.</p>



<p class="wp-block-paragraph">The second half of the class has the same shape. <code>cancel</code> writes an audit row through <code>writeAudit</code>, marked <code>REQUIRES_NEW</code> so the audit survives even when the cancellation fails:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-start:60;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2f363c;color:#d3d7dd">OrderService.java (buggy, continued)</span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">@Transactional</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">void</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">cancel</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">long</span><span style="color: #E1E4E8"> orderId, </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8"> requestedBy) {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">writeAudit</span><span style="color: #E1E4E8">(orderId, </span><span style="color: #9ECBFF">&quot;CANCEL_REQUESTED by &quot;</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">+</span><span style="color: #E1E4E8"> requestedBy);</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">PurchaseOrder</span><span style="color: #E1E4E8"> order </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> orders.</span><span style="color: #B392F0">findById</span><span style="color: #E1E4E8">(orderId).</span><span style="color: #B392F0">orElseThrow</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">if</span><span style="color: #E1E4E8"> (order.</span><span style="color: #B392F0">isShipped</span><span style="color: #E1E4E8">()) {</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #F97583">throw</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">OrderAlreadyShippedException</span><span style="color: #E1E4E8">(orderId);</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span>
<span class="line"><span style="color: #E1E4E8">        order.</span><span style="color: #B392F0">markCancelled</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">    </span><span style="color: #F97583">@Transactional</span><span style="color: #E1E4E8">(propagation </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">Propagation</span><span style="color: #E1E4E8">.</span><span style="color: #79B8FF">REQUIRES_NEW</span><span style="color: #E1E4E8">)</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">void</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">writeAudit</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">long</span><span style="color: #E1E4E8"> orderId, </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8"> action) {</span></span>
<span class="line"><span style="color: #E1E4E8">        audit.</span><span style="color: #B392F0">save</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">AuditEntry</span><span style="color: #E1E4E8">(orderId, action));</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span></code></pre></div>



<p class="wp-block-paragraph">Both pairs fail the same way, for the same reason.</p>



<h2 class="wp-block-heading">Who actually starts a transaction in Spring?</h2>



<p class="wp-block-paragraph">Not your method, and not the annotation. An annotation is only a label; something has to read it and act on it.</p>



<p class="wp-block-paragraph">Think of a hotel switchboard. Calls from outside go through the operator, who logs them and connects you. A guest who walks down the corridor and knocks on the next door never meets the operator.</p>



<p class="wp-block-paragraph">In Spring, the operator is a <em>proxy</em>: a stand-in object that Spring creates around your bean and injects everywhere instead of the real thing. Spring Boot builds these as CGLIB subclasses by default, its AOP reference says. When a controller calls <code>orderService.placeAll(...)</code>, it calls the proxy. The proxy sees no annotation on <code>placeAll</code>, so it simply passes the call on to the real <code>OrderService</code> object.</p>



<p class="wp-block-paragraph">Now the code runs inside the real object. Line 41 says <code>placeOne(draft)</code>, which Java reads as <code>this.placeOne(draft)</code>, and <code>this</code> is the real object, not the proxy. The transactional advice never runs.</p>



<p class="wp-block-paragraph">That is <em>self-invocation</em>: one method of a bean calling another method of the same bean through <code>this</code>. Spring&#8217;s transaction chapter states the rule plainly: in proxy mode &#8220;only external method calls coming in through the proxy are intercepted&#8221;, so a self-invocation &#8220;does not lead to an actual transaction at runtime&#8221;. The same paragraph points at the AspectJ mode we cover in fix 4.</p>



<p class="wp-block-paragraph">One more detail turns a silent no-op into partial data. Spring Data JPA&#8217;s repository methods carry their own <code>@Transactional</code>, according to its reference. With no outer transaction to join, every <code>save</code> opens and commits a transaction of its own.</p>



<figure class="wp-block-video"><video height="1080" style="aspect-ratio: 1080 / 1080;" width="1080" autoplay loop muted poster="https://www.gradientco.net/wp-content/uploads/2026/11/transactional-self-invocation-clip-poster.webp" src="https://www.gradientco.net/wp-content/uploads/2026/11/transactional-self-invocation-clip.mp4" playsinline></video><figcaption class="wp-element-caption">No transaction wraps placeOne, so each save commits on its own.</figcaption></figure>



<h2 class="wp-block-heading">Where exactly does the rollback go missing?</h2>



<p class="wp-block-paragraph">Follow <code>ACME-1002</code>, the order whose third line names a discontinued mug:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #e1e4e8">placeAll, running on the real object        the database</span></span>
<span class="line"><span style="color: #e1e4e8">------------------------------------        ------------</span></span>
<span class="line"><span style="color: #e1e4e8">this.placeOne(ACME-1002)   &lt;- no proxy, so no transaction</span></span>
<span class="line"><span style="color: #e1e4e8">  orders.save(order)                        tx 1: insert order, commit</span></span>
<span class="line"><span style="color: #e1e4e8">  lines.save(MUG-BLUE x4)                   tx 2: insert line, commit</span></span>
<span class="line"><span style="color: #e1e4e8">  lines.save(LID-BAMBOO x4)                 tx 3: insert line, commit</span></span>
<span class="line"><span style="color: #e1e4e8">  catalog: MUG-CLASSIC-RED is discontinued  -&gt; exception</span></span>
<span class="line"><span style="color: #e1e4e8">catch: add ACME-1002 to &quot;rejected&quot;          3 rows remain; nothing to roll back</span></span></code></pre></div>



<p class="wp-block-paragraph">The rule breaks on the first line, long before the exception. By the time <code>DiscontinuedProductException</code> flies, there&#8217;s nothing to mark for rollback, because no transaction ever wrapped the work.</p>



<h2 class="wp-block-heading">What did the test record?</h2>



<p class="wp-block-paragraph">We didn&#8217;t want to infer the missing transaction from the rows alone, so the test watches the transaction manager itself. A test-only <code>BeanPostProcessor</code> wraps Spring&#8217;s <code>transactionManager</code> bean and records every physical transaction it starts: its name, its propagation and whether it committed.</p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/11/transactional-self-invocation-graphic-1.webp" alt="Transactional self invocation in our test: 3 orders in the table while the report said 2 imported, 2 lines left by rejected ACME-1002, 8 transactions all from repository saves with 0 rolled back, and none of the 6 catalog checks inside placeOne ran in a transaction" class="wp-image-10248" srcset="https://www.gradientco.net/wp-content/uploads/2026/11/transactional-self-invocation-graphic-1.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/11/transactional-self-invocation-graphic-1-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/11/transactional-self-invocation-graphic-1-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/11/transactional-self-invocation-graphic-1-768x512.webp 768w" sizes="(max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Source: Gradient test project, November 2026</figcaption></figure>



<p class="wp-block-paragraph">A second hook replaces the <code>ProductCatalog</code> bean. At each SKU check it asks <code>TransactionSynchronizationManager.isActualTransactionActive()</code>, which its Javadoc says reports whether the current thread has a real transaction behind it. Then the test runs the import:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-start:36;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2f363c;color:#d3d7dd">TransactionalSelfInvocationBugTest.java</span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">ImportReport</span><span style="color: #E1E4E8"> report </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> orderService.</span><span style="color: #B392F0">placeAll</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">Scenarios</span><span style="color: #E1E4E8">.</span><span style="color: #79B8FF">NIGHTLY_FILE</span><span style="color: #E1E4E8">);</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">TransactionRecorder</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">Snapshot</span><span style="color: #E1E4E8"> txs </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> recorder.</span><span style="color: #B392F0">snapshot</span><span style="color: #E1E4E8">();</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">assertThat</span><span style="color: #E1E4E8">(report.</span><span style="color: #B392F0">imported</span><span style="color: #E1E4E8">()).</span><span style="color: #B392F0">isEqualTo</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">2</span><span style="color: #E1E4E8">);</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">assertThat</span><span style="color: #E1E4E8">(report.</span><span style="color: #B392F0">rejected</span><span style="color: #E1E4E8">()).</span><span style="color: #B392F0">containsExactly</span><span style="color: #E1E4E8">(</span><span style="color: #9ECBFF">&quot;ACME-1002&quot;</span><span style="color: #E1E4E8">);</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #6A737D">// The report says ACME-1002 was rejected, but its order row and first two lines were committed.</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">assertThat</span><span style="color: #E1E4E8">(orders.</span><span style="color: #B392F0">count</span><span style="color: #E1E4E8">()).</span><span style="color: #B392F0">isEqualTo</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">3</span><span style="color: #E1E4E8">);</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">assertThat</span><span style="color: #E1E4E8">(lines.</span><span style="color: #B392F0">count</span><span style="color: #E1E4E8">()).</span><span style="color: #B392F0">isEqualTo</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">5</span><span style="color: #E1E4E8">);</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">assertThat</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">Scenarios</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">linesPerOrder</span><span style="color: #E1E4E8">(lines)).</span><span style="color: #B392F0">containsEntry</span><span style="color: #E1E4E8">(</span><span style="color: #9ECBFF">&quot;ACME-1002&quot;</span><span style="color: #E1E4E8">, </span><span style="color: #79B8FF">2L</span><span style="color: #E1E4E8">);</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #6A737D">// Inside placeOne no transaction was active at any catalog check...</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">assertThat</span><span style="color: #E1E4E8">(recorder.</span><span style="color: #B392F0">activeAtCatalogCheck</span><span style="color: #E1E4E8">()).</span><span style="color: #B392F0">hasSize</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">6</span><span style="color: #E1E4E8">).</span><span style="color: #B392F0">containsOnly</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">false</span><span style="color: #E1E4E8">);</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #6A737D">// ...and the only transactions were the repositories&#039; own, one per save, each committed alone.</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">assertThat</span><span style="color: #E1E4E8">(txs.</span><span style="color: #B392F0">count</span><span style="color: #E1E4E8">(</span><span style="color: #9ECBFF">&quot;OrderService.placeOne&quot;</span><span style="color: #E1E4E8">)).</span><span style="color: #B392F0">isZero</span><span style="color: #E1E4E8">();</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">assertThat</span><span style="color: #E1E4E8">(txs.</span><span style="color: #B392F0">count</span><span style="color: #E1E4E8">(</span><span style="color: #9ECBFF">&quot;SimpleJpaRepository.save&quot;</span><span style="color: #E1E4E8">)).</span><span style="color: #B392F0">isEqualTo</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">8</span><span style="color: #E1E4E8">);</span></span></code></pre></div>



<p class="wp-block-paragraph">Lines 42 to 44 are the partial data; lines 48 and 49 show why. The run printed this (on our 4-core cloud build machine; this test is deterministic, so the counts do not vary):</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #e1e4e8">&#91;bug&#93; report: imported=2, rejected=&#91;ACME-1002&#93; | database: orders=3, lines=5</span></span>
<span class="line"><span style="color: #e1e4e8">&#91;bug&#93; lines per order: {ACME-1001=2, ACME-1002=2, ACME-1003=1}</span></span>
<span class="line"><span style="color: #e1e4e8">&#91;bug&#93; transaction active at the 6 catalog checks inside placeOne: &#91;false, false, false, false, false, false&#93;</span></span>
<span class="line"><span style="color: #e1e4e8">&#91;bug&#93; transactions started: SimpleJpaRepository.save REQUIRED x8 (committed 8, rolled back 0)</span></span></code></pre></div>



<p class="wp-block-paragraph"><code>OrderService.placeOne</code> appears nowhere in the last line. All 8 transactions belong to the repository: 3 orders and 5 lines.</p>



<h3 class="wp-block-heading">The REQUIRES_NEW audit row that vanished</h3>



<p class="wp-block-paragraph">The cancel test is the mirror image. Instead of a missing transaction, it is a missing <em>new</em> one. We cancelled an order that had already shipped:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #e1e4e8">&#91;bug&#93; cancel(shipped order) -&gt; OrderAlreadyShippedException, audit rows=0</span></span>
<span class="line"><span style="color: #e1e4e8">&#91;bug&#93; transactions started: OrderService.cancel REQUIRED x1 (committed 0, rolled back 1)</span></span></code></pre></div>



<figure class="wp-block-image size-large"><img decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/11/transactional-self-invocation-graphic-2.webp" alt="Step by step: the proxy starts tx 1 for cancel; writeAudit is called via this, so its REQUIRES_NEW is never read and it joins tx 1; the audit row is written uncommitted; the shipped order throws and the rollback of tx 1 takes the audit row with it" class="wp-image-10249" srcset="https://www.gradientco.net/wp-content/uploads/2026/11/transactional-self-invocation-graphic-2.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/11/transactional-self-invocation-graphic-2-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/11/transactional-self-invocation-graphic-2-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/11/transactional-self-invocation-graphic-2-768x512.webp 768w" sizes="(max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Source: Gradient test project, cancel test, November 2026</figcaption></figure>



<p class="wp-block-paragraph">One transaction, not two. <code>writeAudit</code> joined <code>cancel</code>&#8216;s transaction, wrote its row there, and the rollback took the audit with it. In our view, this is where many &#8220;@Transactional not working&#8221; puzzles end: the setting was fine, and nothing ever read it.</p>



<h2 class="wp-block-heading">Why do mocks, reviews and happy paths all miss it?</h2>



<p class="wp-block-paragraph">A unit test with Mockito builds the service with <code>new OrderService(...)</code>. There is no Spring context, so there is no proxy, and the test can&#8217;t tell a working annotation from a decorative one. A review reads the annotation and trusts it, because transactional self invocation looks exactly like a normal method call.</p>



<p class="wp-block-paragraph">The happy path is the strongest disguise. With a clean file, the buggy service and the fixed one produce identical results:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #e1e4e8">&#91;happy path&#93; buggy: ImportReport[imported=2, rejected=[]], orders=2, lines=3 | fixed: ImportReport[imported=2, rejected=[]], orders=2, lines=3</span></span></code></pre></div>



<p class="wp-block-paragraph">A transaction only shows its value when something fails halfway. <strong>If no test throws in the middle of a transactional method, no test checks the transaction.</strong></p>



<figure class="wp-block-image size-large"><img decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/11/transactional-self-invocation-graphic-3.webp" alt="Three fixes we ran: fix 1 moves placeOne to OrderPlacement so the call crosses a proxy, which Spring calls the best, least-invasive approach; fix 2 opens the transaction in code with TransactionTemplate, coupling you to Spring; fix 3 calls self.placeOne through its own proxy, a last resort" class="wp-image-10250" srcset="https://www.gradientco.net/wp-content/uploads/2026/11/transactional-self-invocation-graphic-3.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/11/transactional-self-invocation-graphic-3-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/11/transactional-self-invocation-graphic-3-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/11/transactional-self-invocation-graphic-3-768x512.webp 768w" sizes="(max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Source: Gradient test project and the Spring Framework reference. Fix 4, AspectJ, was not part of our test.</figcaption></figure>



<h2 class="wp-block-heading">Fix 1: give the transaction its own bean</h2>



<p class="wp-block-paragraph">Spring&#8217;s AOP chapter calls refactoring &#8220;the best, least-invasive approach&#8221;: arrange the code so the self-call no longer exists. We moved <code>placeOne</code> into an <code>OrderPlacement</code> bean and <code>writeAudit</code> into an <code>AuditTrail</code> bean, both unchanged apart from their new home:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-start:28;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2f363c;color:#d3d7dd">SplitOrderService.java (fix 1)</span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">ImportReport</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">placeAll</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">List</span><span style="color: #F97583">&lt;</span><span style="color: #B392F0">OrderDraft</span><span style="color: #F97583">&gt;</span><span style="color: #E1E4E8"> drafts) {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">int</span><span style="color: #E1E4E8"> imported </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #79B8FF">0</span><span style="color: #E1E4E8">;</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">List</span><span style="color: #F97583">&lt;</span><span style="color: #B392F0">String</span><span style="color: #F97583">&gt;</span><span style="color: #E1E4E8"> rejected </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">ArrayList</span><span style="color: #F97583">&lt;&gt;</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">for</span><span style="color: #E1E4E8"> (</span><span style="color: #B392F0">OrderDraft</span><span style="color: #E1E4E8"> draft </span><span style="color: #F97583">:</span><span style="color: #E1E4E8"> drafts) {</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #F97583">try</span><span style="color: #E1E4E8"> {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">                placement.</span><span style="color: #B392F0">placeOne</span><span style="color: #E1E4E8">(draft);</span></span>
<span class="line"><span style="color: #E1E4E8">                imported</span><span style="color: #F97583">++</span><span style="color: #E1E4E8">;</span></span>
<span class="line"><span style="color: #E1E4E8">            } </span><span style="color: #F97583">catch</span><span style="color: #E1E4E8"> (</span><span style="color: #B392F0">DiscontinuedProductException</span><span style="color: #E1E4E8"> e) {</span></span>
<span class="line"><span style="color: #E1E4E8">                rejected.</span><span style="color: #B392F0">add</span><span style="color: #E1E4E8">(draft.</span><span style="color: #B392F0">customerRef</span><span style="color: #E1E4E8">());</span></span>
<span class="line"><span style="color: #E1E4E8">            }</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">ImportReport</span><span style="color: #E1E4E8">(imported, rejected);</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">@Transactional</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">void</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">cancel</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">long</span><span style="color: #E1E4E8"> orderId, </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8"> requestedBy) {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        auditTrail.</span><span style="color: #B392F0">write</span><span style="color: #E1E4E8">(orderId, </span><span style="color: #9ECBFF">&quot;CANCEL_REQUESTED by &quot;</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">+</span><span style="color: #E1E4E8"> requestedBy);</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">PurchaseOrder</span><span style="color: #E1E4E8"> order </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> orders.</span><span style="color: #B392F0">findById</span><span style="color: #E1E4E8">(orderId).</span><span style="color: #B392F0">orElseThrow</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">if</span><span style="color: #E1E4E8"> (order.</span><span style="color: #B392F0">isShipped</span><span style="color: #E1E4E8">()) {</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #F97583">throw</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">OrderAlreadyShippedException</span><span style="color: #E1E4E8">(orderId);</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span>
<span class="line"><span style="color: #E1E4E8">        order.</span><span style="color: #B392F0">markCancelled</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span></code></pre></div>



<p class="wp-block-paragraph">Lines 33 and 44 now call other beans, so each call crosses a proxy, and the <code>REQUIRES_NEW</code> on <code>AuditTrail.write</code> finally takes effect. The same file and the same cancellation gave this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #e1e4e8">&#91;separatebean&#93; report: imported=2, rejected=&#91;ACME-1002&#93; | database: orders=2, lines=3</span></span>
<span class="line"><span style="color: #e1e4e8">&#91;separatebean&#93; transaction active at the 6 catalog checks: &#91;true, true, true, true, true, true&#93;</span></span>
<span class="line"><span style="color: #e1e4e8">&#91;separatebean&#93; transactions started: OrderPlacement.placeOne REQUIRED x3 (committed 2, rolled back 1)</span></span>
<span class="line"><span style="color: #e1e4e8">&#91;separatebean&#93; cancel(shipped order) -&gt; OrderAlreadyShippedException, audit rows=1</span></span>
<span class="line"><span style="color: #e1e4e8">&#91;separatebean&#93; transactions started: SplitOrderService.cancel REQUIRED x1 (committed 0, rolled back 1); AuditTrail.write REQUIRES_NEW x1 (committed 1, rolled back 0)</span></span></code></pre></div>



<p class="wp-block-paragraph"><code>ACME-1002</code> is gone completely, the repository saves now join <code>placeOne</code>&#8216;s transaction, and the audit row survived the failed cancel.</p>



<p class="wp-block-paragraph">You pay with one more class per boundary, and sometimes a name that&#8217;s hard to choose. In our view that is a feature: a unit of work you can name is a unit of work you can test on its own.</p>



<h2 class="wp-block-heading">Fix 2: write the boundary out with TransactionTemplate</h2>



<p class="wp-block-paragraph">Sometimes a new class feels like too much for one loop body. <code>TransactionTemplate</code> takes the boundary out of the annotation and puts it in the code, so there is no proxy to miss:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-start:39;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2f363c;color:#d3d7dd">TemplateOrderService.java (fix 2)</span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">this</span><span style="color: #E1E4E8">.inTransaction </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">TransactionTemplate</span><span style="color: #E1E4E8">(transactionManager);</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">this</span><span style="color: #E1E4E8">.inNewTransaction </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">TransactionTemplate</span><span style="color: #E1E4E8">(transactionManager);</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #F97583">this</span><span style="color: #E1E4E8">.inNewTransaction.</span><span style="color: #B392F0">setPropagationBehavior</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">TransactionDefinition</span><span style="color: #E1E4E8">.</span><span style="color: #79B8FF">PROPAGATION_REQUIRES_NEW</span><span style="color: #E1E4E8">);</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">ImportReport</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">placeAll</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">List</span><span style="color: #F97583">&lt;</span><span style="color: #B392F0">OrderDraft</span><span style="color: #F97583">&gt;</span><span style="color: #E1E4E8"> drafts) {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">int</span><span style="color: #E1E4E8"> imported </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #79B8FF">0</span><span style="color: #E1E4E8">;</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">List</span><span style="color: #F97583">&lt;</span><span style="color: #B392F0">String</span><span style="color: #F97583">&gt;</span><span style="color: #E1E4E8"> rejected </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">ArrayList</span><span style="color: #F97583">&lt;&gt;</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">for</span><span style="color: #E1E4E8"> (</span><span style="color: #B392F0">OrderDraft</span><span style="color: #E1E4E8"> draft </span><span style="color: #F97583">:</span><span style="color: #E1E4E8"> drafts) {</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #F97583">try</span><span style="color: #E1E4E8"> {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">                inTransaction.</span><span style="color: #B392F0">execute</span><span style="color: #E1E4E8">(status </span><span style="color: #F97583">-&gt;</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">placeOne</span><span style="color: #E1E4E8">(draft));</span></span>
<span class="line"><span style="color: #E1E4E8">                imported</span><span style="color: #F97583">++</span><span style="color: #E1E4E8">;</span></span>
<span class="line"><span style="color: #E1E4E8">            } </span><span style="color: #F97583">catch</span><span style="color: #E1E4E8"> (</span><span style="color: #B392F0">DiscontinuedProductException</span><span style="color: #E1E4E8"> e) {</span></span>
<span class="line"><span style="color: #E1E4E8">                rejected.</span><span style="color: #B392F0">add</span><span style="color: #E1E4E8">(draft.</span><span style="color: #B392F0">customerRef</span><span style="color: #E1E4E8">());</span></span>
<span class="line"><span style="color: #E1E4E8">            }</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">ImportReport</span><span style="color: #E1E4E8">(imported, rejected);</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span></code></pre></div>



<p class="wp-block-paragraph">Line 49 runs each order inside <code>execute</code>, and <code>placeOne</code> can now be private. The <code>TransactionTemplate</code> Javadoc says a <code>RuntimeException</code> thrown by the callback &#8220;enforces a rollback&#8221; and then reaches the caller. That is why the catch on line 51 still sees the discontinued SKU.</p>



<p class="wp-block-paragraph">Line 41 builds a second template whose propagation is <code>REQUIRES_NEW</code>. A class needing different settings, the programmatic-transactions chapter says, should use two distinct <code>TransactionTemplate</code> instances, and the cancel method nests one inside the other:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-start:67;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2f363c;color:#d3d7dd">TemplateOrderService.java (fix 2, cancel)</span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">void</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">cancel</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">long</span><span style="color: #E1E4E8"> orderId, </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8"> requestedBy) {</span></span>
<span class="line"><span style="color: #E1E4E8">        inTransaction.</span><span style="color: #B392F0">executeWithoutResult</span><span style="color: #E1E4E8">(status </span><span style="color: #F97583">-&gt;</span><span style="color: #E1E4E8"> {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">            inNewTransaction.</span><span style="color: #B392F0">executeWithoutResult</span><span style="color: #E1E4E8">(</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">                    inner </span><span style="color: #F97583">-&gt;</span><span style="color: #E1E4E8"> audit.</span><span style="color: #B392F0">save</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">AuditEntry</span><span style="color: #E1E4E8">(orderId, </span><span style="color: #9ECBFF">&quot;CANCEL_REQUESTED by &quot;</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">+</span><span style="color: #E1E4E8"> requestedBy)));</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #B392F0">PurchaseOrder</span><span style="color: #E1E4E8"> order </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> orders.</span><span style="color: #B392F0">findById</span><span style="color: #E1E4E8">(orderId).</span><span style="color: #B392F0">orElseThrow</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #F97583">if</span><span style="color: #E1E4E8"> (order.</span><span style="color: #B392F0">isShipped</span><span style="color: #E1E4E8">()) {</span></span>
<span class="line"><span style="color: #E1E4E8">                </span><span style="color: #F97583">throw</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">OrderAlreadyShippedException</span><span style="color: #E1E4E8">(orderId);</span></span>
<span class="line"><span style="color: #E1E4E8">            }</span></span>
<span class="line"><span style="color: #E1E4E8">            order.</span><span style="color: #B392F0">markCancelled</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #E1E4E8">        });</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #e1e4e8">&#91;transactiontemplate&#93; transactions started: TransactionTemplate REQUIRED x3 (committed 2, rolled back 1)</span></span>
<span class="line"><span style="color: #e1e4e8">&#91;transactiontemplate&#93; cancel(shipped order) -&gt; OrderAlreadyShippedException, audit rows=1</span></span>
<span class="line"><span style="color: #e1e4e8">&#91;transactiontemplate&#93; transactions started: TransactionTemplate REQUIRED x1 (committed 0, rolled back 1); TransactionTemplate REQUIRES_NEW x1 (committed 1, rolled back 0)</span></span></code></pre></div>



<p class="wp-block-paragraph">In exchange, your business code leans on Spring&#8217;s API: the reference says the template &#8220;absolutely couples you to Spring&#8217;s transaction infrastructure&#8221;. The boundary also hides inside a lambda, and a reader scanning for <code>@Transactional</code> won&#8217;t find it.</p>



<h2 class="wp-block-heading">Fix 3: inject the proxy into itself</h2>



<p class="wp-block-paragraph">What if the method has to stay where it is, say in a large legacy service? The bean can ask Spring for a reference to its own proxy and call through that:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-start:29;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2f363c;color:#d3d7dd">SelfInjectedOrderService.java (fix 3)</span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">private</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">final</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">SelfInjectedOrderService</span><span style="color: #E1E4E8"> self;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">SelfInjectedOrderService</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">PurchaseOrderRepository</span><span style="color: #E1E4E8"> orders, </span><span style="color: #B392F0">OrderLineRepository</span><span style="color: #E1E4E8"> lines,</span></span>
<span class="line"><span style="color: #E1E4E8">                                    </span><span style="color: #B392F0">AuditEntryRepository</span><span style="color: #E1E4E8"> audit, </span><span style="color: #B392F0">ProductCatalog</span><span style="color: #E1E4E8"> catalog,</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">                                    </span><span style="color: #F97583">@Lazy</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">SelfInjectedOrderService</span><span style="color: #E1E4E8"> self) {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">this</span><span style="color: #E1E4E8">.orders </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> orders;</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">this</span><span style="color: #E1E4E8">.lines </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> lines;</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">this</span><span style="color: #E1E4E8">.audit </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> audit;</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">this</span><span style="color: #E1E4E8">.catalog </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> catalog;</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">this</span><span style="color: #E1E4E8">.self </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> self;</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">ImportReport</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">placeAll</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">List</span><span style="color: #F97583">&lt;</span><span style="color: #B392F0">OrderDraft</span><span style="color: #F97583">&gt;</span><span style="color: #E1E4E8"> drafts) {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">int</span><span style="color: #E1E4E8"> imported </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #79B8FF">0</span><span style="color: #E1E4E8">;</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">List</span><span style="color: #F97583">&lt;</span><span style="color: #B392F0">String</span><span style="color: #F97583">&gt;</span><span style="color: #E1E4E8"> rejected </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">ArrayList</span><span style="color: #F97583">&lt;&gt;</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">for</span><span style="color: #E1E4E8"> (</span><span style="color: #B392F0">OrderDraft</span><span style="color: #E1E4E8"> draft </span><span style="color: #F97583">:</span><span style="color: #E1E4E8"> drafts) {</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #F97583">try</span><span style="color: #E1E4E8"> {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">                self.</span><span style="color: #B392F0">placeOne</span><span style="color: #E1E4E8">(draft);</span></span>
<span class="line"><span style="color: #E1E4E8">                imported</span><span style="color: #F97583">++</span><span style="color: #E1E4E8">;</span></span></code></pre></div>



<p class="wp-block-paragraph">Line 33 is the trick. The <code>@Lazy</code> Javadoc says the annotation on an injection point creates &#8220;a lazy-resolution proxy&#8221;, resolved on first use, so Spring can finish building the bean before anything resolves <code>self</code>.</p>



<p class="wp-block-paragraph">Line 46 calls <code>self.placeOne</code>, and <code>cancel</code> calls <code>self.writeAudit</code> the same way. Our test gave the same counts as fix 1: orders=2, lines=3, audit rows=1.</p>



<p class="wp-block-paragraph">It works, and we&#8217;d still treat it as a code smell. The <code>@Autowired</code> chapter tells you to use self references &#8220;as a last resort only&#8221;, and suggests a separate delegate bean instead. Every new method in the class also needs its author to remember the <code>self</code> prefix: the same kind of invisible rule that caused the bug.</p>



<p class="wp-block-paragraph"><code>ObjectProvider&lt;SelfInjectedOrderService&gt;</code> does the same job with an explicit <code>getObject()</code> call, and the <code>@Lazy</code> Javadoc points to it as the programmatic equivalent. Avoid <code>AopContext.currentProxy()</code>; the reference says that approach &#8220;is highly discouraged&#8221;.</p>



<h2 class="wp-block-heading">Fix 4: weave the advice in with AspectJ</h2>



<p class="wp-block-paragraph">The last option removes the proxy altogether. With <code>@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)</code>, Spring applies the transaction logic by changing the class&#8217;s bytecode, so even <code>this.placeOne()</code> runs inside a transaction. Per the transaction chapter, this &#8220;requires <code>spring-aspects.jar</code> in the classpath&#8221; plus compile-time or load-time weaving.</p>



<p class="wp-block-paragraph">That last clause is the cost. Load-time weaving usually means starting the JVM with a <code>-javaagent</code> flag; compile-time weaving means the AspectJ compiler in your build. AspectJ also ignores <code>@Transactional</code> on interfaces, since Java doesn&#8217;t inherit annotations from interfaces.</p>



<p class="wp-block-paragraph">We did not build this variant: weaving would also change the buggy classes the other tests rely on. Treat fix 4 as backed by the documentation, not by our test.</p>



<h2 class="wp-block-heading">Which way out should you pick?</h2>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th></th><th>Fix 1: separate bean</th><th>Fix 2: TransactionTemplate</th><th>Fix 3: self-injection</th><th>Fix 4: AspectJ</th></tr></thead><tbody><tr><td>Rows left by the rejected order (our test)</td><td>0</td><td>0</td><td>0</td><td>not tested</td></tr><tr><td>Audit row after the failed cancel</td><td>kept</td><td>kept</td><td>kept</td><td>not tested</td></tr><tr><td>Still uses the proxy</td><td>yes</td><td>no proxy needed</td><td>yes</td><td>no proxy at all</td></tr><tr><td>Main cost</td><td>one class per boundary</td><td>Spring API in business code</td><td>a hidden <code>self.</code> rule</td><td>a build plugin or JVM agent</td></tr><tr><td>Choose it when</td><td>almost always</td><td>the boundary is a few lines inside a loop</td><td>a refactor is out of reach today</td><td>many self-calls and the build team agrees</td></tr></tbody></table></figure>



<p class="wp-block-paragraph">Our default is fix 1. It keeps <code>@Transactional</code> visible and keeps the rule at the bean boundary, where Spring can enforce it. Reach for fix 2 when the boundary is small and local, as in a batch loop.</p>



<p class="wp-block-paragraph">Don&#8217;t move <code>@Transactional</code> up to <code>placeAll</code> as a shortcut. The tests would pass, but one bad order would then roll back the whole file, which is a different business rule.</p>



<h2 class="wp-block-heading">Do @Async, @Cacheable and final methods fall into the same trap?</h2>



<p class="wp-block-paragraph">Every annotation that works through a proxy has the same blind spot. For <code>@Cacheable</code>, the caching chapter says self-invocation &#8220;does not lead to actual caching at runtime&#8221;. The scheduling chapter says local calls within the same class cannot be intercepted for <code>@Async</code> either. A self-called <code>@Async</code> method runs synchronously on the caller&#8217;s thread, and a self-called <code>@Cacheable</code> method hits the database every time.</p>



<p class="wp-block-paragraph">We covered how <code>@Async</code> hands work to another thread in <a href="https://www.gradientco.net/engineering/spring-boot-mdc-async/">Spring Boot MDC async logs lose the request id</a>, and what a cache that fails to share one load costs in <a href="https://www.gradientco.net/engineering/spring-boot-cache-stampede/">Spring Boot cache stampede: 50 requests, 50 identical database queries</a>.</p>



<p class="wp-block-paragraph">Visibility matters too. Since Spring Framework 6.0, <code>protected</code> and package-visible methods can be transactional with class-based proxies; private methods never can. A <code>final</code> method is worse than ignored, because the CGLIB subclass can&#8217;t override it. The call runs on the proxy object itself, whose injected fields were never set:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-start:18;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2f363c;color:#d3d7dd">ShippingService.java (buggy)</span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">@Transactional</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">    </span><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">final</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">void</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">markShipped</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">long</span><span style="color: #E1E4E8"> orderId) {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">PurchaseOrder</span><span style="color: #E1E4E8"> order </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> orders.</span><span style="color: #B392F0">findById</span><span style="color: #E1E4E8">(orderId).</span><span style="color: #B392F0">orElseThrow</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #E1E4E8">        order.</span><span style="color: #B392F0">markShipped</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #e1e4e8">&#91;final&#93; markShipped on the proxy: NullPointerException: Cannot invoke &quot;dev.gradient.selfinvocation.PurchaseOrderRepository.findById(Object)&quot; because &quot;this.orders&quot; is null</span></span></code></pre></div>



<p class="wp-block-paragraph">Spring does warn about this one at startup, at WARN level: &#8220;Public final method [&#8230;] cannot get proxied via CGLIB&#8221;. Removing <code>final</code> fixed it in our test.</p>



<h2 class="wp-block-heading">What breaks when the import runs on more servers and retries?</h2>



<p class="wp-block-paragraph">More servers don&#8217;t change transactional self invocation, because it lives inside one method call on one JVM. They only multiply the partial rows.</p>



<p class="wp-block-paragraph">Retries make it worse. A partner who fixes the SKU and resends the file gets <code>ACME-1002</code> imported again, so in our reading the database would then hold the stale partial order and the complete new one. We didn&#8217;t run that retry, but nothing in the buggy code would stop it.</p>



<p class="wp-block-paragraph">The fix brings a cost of its own. A working <code>REQUIRES_NEW</code> holds two pooled connections per cancel, one for the suspended outer transaction and one for the audit. Spring&#8217;s propagation chapter warns this can exhaust the pool, and asks for a pool larger than the number of concurrent threads by at least 1.</p>



<h3 class="wp-block-heading">The signal you can actually watch</h3>



<p class="wp-block-paragraph">The bug itself never moves a metric; it moves data. Run a reconciliation query that looks for orders with fewer lines than the source file listed, or for orders whose reference appears in a rejection report.</p>



<p class="wp-block-paragraph">To see it in logs, set <code>org.springframework.transaction.interceptor</code> to TRACE. Spring then logs &#8220;Getting transaction for [&#8230;]&#8221; for each intercepted method. If <code>placeOne</code> never shows up there, nothing is intercepting it. After shipping a working <code>REQUIRES_NEW</code>, watch <code>hikaricp.connections.pending</code>: a value that stays above zero means threads are waiting for that second connection.</p>



<h2 class="wp-block-heading">Back to the import that saved too much</h2>



<p class="wp-block-paragraph">So nobody in Spring decided against the rollback; there was simply nothing to roll back. If you change one thing this week, make it a rollback test that runs inside a real Spring context. That one test would have flagged our import before any partner sent a bad file.</p>



<h2 class="wp-block-heading">A checklist for your own services</h2>



<ul class="wp-block-list">
<li>Grep for methods annotated <code>@Transactional</code>, <code>@Async</code> or <code>@Cacheable</code> that are called from the same class without a bean reference in front.</li>



<li>Search for <code>@Transactional</code> on <code>private</code> or <code>final</code> methods, and for startup warnings that contain &#8220;cannot get proxied via CGLIB&#8221;.</li>



<li>For each <code>REQUIRES_NEW</code>, check that the caller lives in a different bean, and that the pool is larger than your concurrent request threads.</li>



<li>Find unit tests that construct services with <code>new</code> and claim to cover rollback; move one of them to <code>@SpringBootTest</code>.</li>



<li>Add one assertion of <code>TransactionSynchronizationManager.isActualTransactionActive()</code> inside the work your batch jobs consider atomic.</li>
</ul>



<h2 class="wp-block-heading">Frequently asked questions</h2>


<div id="rank-math-faq" class="rank-math-block">
<div class="rank-math-list ">
<div id="faq-question-1" class="rank-math-list-item">
<h3 class="rank-math-question ">Does transactional self invocation also happen with JDK interface proxies?</h3>
<div class="rank-math-answer ">

<p>Yes. The proxy type changes how the wrapper is built, not who it wraps. With a JDK proxy, the target object still calls itself through <code>this</code>, and Spring&#8217;s transaction chapter is explicit that, for both kinds of proxy, only calls coming in through the proxy are intercepted.</p>

</div>
</div>
<div id="faq-question-2" class="rank-math-list-item">
<h3 class="rank-math-question ">Does making the outer method @Transactional fix it?</h3>
<div class="rank-math-answer ">

<p>It hides it. The inner method then joins the outer transaction, so rollbacks happen, but its own annotation is still never read. Our <code>cancel</code> method is exactly this case: it was <code>@Transactional</code>, and the <code>REQUIRES_NEW</code> on the self-called <code>writeAudit</code> still had no effect.</p>

</div>
</div>
<div id="faq-question-3" class="rank-math-list-item">
<h3 class="rank-math-question ">How can I tell whether a transaction is active inside a method?</h3>
<div class="rank-math-answer ">

<p>Call <code>TransactionSynchronizationManager.isActualTransactionActive()</code> from a test or a debug hook. It returns true only when a real resource transaction backs the current thread, not merely when transaction synchronization is active.</p>

</div>
</div>
<div id="faq-question-4" class="rank-math-list-item">
<h3 class="rank-math-question ">Is calling the method from a @PostConstruct method safe?</h3>
<div class="rank-math-answer ">

<p>No. Spring&#8217;s documentation warns that the proxy must be fully initialised for <code>@Transactional</code> to work, and that you should not rely on it in initialisation code such as a <code>@PostConstruct</code> method. We&#8217;d move start-up work into an <code>ApplicationReadyEvent</code> listener in another bean, which Spring Boot sends after the context has been refreshed and the runners have been called.</p>

</div>
</div>
</div>
</div>


<h2 class="wp-block-heading">What we do not know yet</h2>



<p class="wp-block-paragraph">We ran every test in one JVM on a 4-core cloud build machine in November 2026, with H2 2.4.240 and Hibernate ORM 7.4.5.Final. The bug sits in Spring&#8217;s proxy layer, so we expect PostgreSQL and MySQL to show the same rows, but we did not run them. We did not build or run fix 4 (AspectJ weaving), so its behaviour here rests on the Spring reference. We also did not measure the cost of AspectJ weaving on build or startup time.</p>



<p class="wp-block-paragraph"><strong>Read next:</strong> what a working <code>REQUIRES_NEW</code> costs under load, measured, in <a href="https://www.gradientco.net/engineering/hikari-connection-is-not-available/">Hikari connection is not available? Your REQUIRES_NEW is starving the pool</a>.</p>



<p class="wp-block-paragraph"><em>Researched and drafted with AI assistance; facts checked against the sources linked in this article.</em></p>



<h2 class="wp-block-heading">References</h2>



<ol class="wp-block-list">
<li>Using @Transactional (proxy mode, method visibility, mode attribute), Spring Framework reference, 7.0: <a href="https://docs.spring.io/spring-framework/reference/data-access/transaction/declarative/annotations.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>Understanding AOP Proxies, Spring Framework reference, 7.0: <a href="https://docs.spring.io/spring-framework/reference/core/aop/proxying.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>Transaction Propagation (PROPAGATION_REQUIRES_NEW), Spring Framework reference, 7.0: <a href="https://docs.spring.io/spring-framework/reference/data-access/transaction/declarative/tx-propagation.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>Programmatic Transaction Management, Spring Framework reference, 7.0: <a href="https://docs.spring.io/spring-framework/reference/data-access/transaction/programmatic.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>Using @Autowired (self injection), Spring Framework reference, 7.0: <a href="https://docs.spring.io/spring-framework/reference/core/beans/annotation-config/autowired.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>Using AspectJ with Spring Applications, Spring Framework reference, 7.0: <a href="https://docs.spring.io/spring-framework/reference/core/aop/using-aspectj.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>Declarative Annotation-based Caching, Spring Framework reference, 7.0: <a href="https://docs.spring.io/spring-framework/reference/integration/cache/annotations.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>Task Execution and Scheduling (@Async advice mode), Spring Framework reference, 7.0: <a href="https://docs.spring.io/spring-framework/reference/integration/scheduling.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>TransactionTemplate, Spring Framework 7.0 Javadoc: <a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/support/TransactionTemplate.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>TransactionSynchronizationManager, Spring Framework 7.0 Javadoc: <a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/support/TransactionSynchronizationManager.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>Lazy, Spring Framework 7.0 Javadoc: <a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Lazy.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>EnableTransactionManagement, Spring Framework 7.0 Javadoc: <a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/annotation/EnableTransactionManagement.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>CglibAopProxy.java (final method warnings), Spring Framework source code: <a href="https://github.com/spring-projects/spring-framework/blob/main/spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java" target="_blank" rel="noopener">github.com</a></li>



<li>TransactionAspectSupport.java (TRACE log messages), Spring Framework source code: <a href="https://github.com/spring-projects/spring-framework/blob/main/spring-tx/src/main/java/org/springframework/transaction/interceptor/TransactionAspectSupport.java" target="_blank" rel="noopener">github.com</a></li>



<li>Transactionality, Spring Data JPA reference, 4.1: <a href="https://docs.spring.io/spring-data/jpa/reference/jpa/transactions.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>Aspect-Oriented Programming, Spring Boot reference, 4.1: <a href="https://docs.spring.io/spring-boot/reference/features/aop.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>MicrometerMetricsTracker.java, HikariCP source code, 7.0.2: <a href="https://github.com/brettwooldridge/HikariCP/blob/HikariCP-7.0.2/src/main/java/com/zaxxer/hikari/metrics/micrometer/MicrometerMetricsTracker.java" target="_blank" rel="noopener">github.com</a></li>



<li>SpringApplication (application events and listeners), Spring Boot reference, 4.1: <a href="https://docs.spring.io/spring-boot/reference/features/spring-application.html" target="_blank" rel="noopener">docs.spring.io</a></li>
</ol>
]]></content:encoded>
					
		
		<enclosure url="https://www.gradientco.net/wp-content/uploads/2026/11/transactional-self-invocation-clip.mp4" length="232355" type="video/mp4" />

			</item>
		<item>
		<title>How to Enable TPM and Secure Boot for Call of Duty and Windows 11</title>
		<link>https://www.gradientco.net/tech-science/enable-tpm-and-secure-boot/</link>
		
		<dc:creator><![CDATA[Nabeel Ahmed]]></dc:creator>
		<pubDate>Tue, 10 Nov 2026 02:23:23 +0000</pubDate>
				<category><![CDATA[Tech & Science]]></category>
		<category><![CDATA[technology]]></category>
		<guid isPermaLink="false">https://www.gradientco.net/?p=10237</guid>

					<description><![CDATA[Modern Warfare 4, Black Ops 7, Warzone and Windows 11 all need TPM 2.0 and Secure Boot. Most PCs from the last several years can run both, though TPM often ships switched off. Here is how to check, what to change in the firmware, and how to convert an old MBR disk without wiping it.]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Check first, then change. To enable TPM and Secure Boot on a Windows PC, confirm the current state with <code>tpm.msc</code> and <code>msinfo32</code>, switch on Intel PTT or AMD fTPM in the UEFI firmware, make sure Windows boots in UEFI mode from a GPT disk, and then turn Secure Boot on. <strong>Most PCs with an Intel 8th Gen or AMD Ryzen 2000 chip or newer already have the hardware.</strong> The one risky step, converting an MBR disk to GPT, is one many readers won&#8217;t need.</p>



<h2 class="wp-block-heading">Who needs TPM 2.0 and Secure Boot now?</h2>



<p class="wp-block-paragraph">Call of Duty players on PC and anyone moving to Windows 11 need both. <a href="https://support.activision.com/uk/en/articles/trusted-platform-module-and-secure-boot" target="_blank" rel="noopener">Activision&#8217;s support page</a> says players of Modern Warfare 4 and Warzone who do not meet the requirements &#8220;will be restricted from playing any online game mode&#8221;. On Black Ops 7 the penalty is lighter: certain modes, including Ranked Play, are locked, and you may be matched in a separate pool.</p>



<p class="wp-block-paragraph">A Trusted Platform Module (TPM) is a security chip, or a firmware feature built into the processor, that stores keys and can vouch for how the PC started. Secure Boot is a firmware check that, at every start, verifies the signature of each piece of boot software, including the operating system, according to <a href="https://learn.microsoft.com/en-us/windows-hardware/design/device-experiences/oem-secure-boot" target="_blank" rel="noopener">Microsoft&#8217;s Secure Boot overview</a>. Together they let the game, or Windows, check that the start-up chain was not tampered with.</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Requirement</th><th>What Activision and Microsoft ask for</th></tr></thead><tbody><tr><td>Windows version</td><td>Windows 10 version 22H2 or later, or Windows 11</td></tr><tr><td>TPM</td><td>Version 2.0: Intel PTT, AMD fTPM or a discrete TPM chip</td></tr><tr><td>Processor for firmware TPM</td><td>Intel 8th Gen or newer, or AMD Ryzen 2000 series or newer</td></tr><tr><td>Boot mode</td><td>UEFI, not Legacy (CSM)</td></tr><tr><td>Disk partition style</td><td>GPT, not MBR</td></tr><tr><td>Secure Boot</td><td>On</td></tr><tr><td>Windows 11 firmware</td><td>&#8220;UEFI, Secure Boot capable&#8221; (<a href="https://www.microsoft.com/en-us/windows/windows-11-specifications" target="_blank" rel="noopener">Microsoft</a>)</td></tr></tbody></table></figure>



<p class="wp-block-paragraph">The <a href="https://support.activision.com/modern-warfare-4/articles/pc-system-requirements-for-modern-warfare-4" target="_blank" rel="noopener">Modern Warfare 4 PC requirements page</a> adds that &#8220;other security measures may be enforced&#8221;, so read this list as today&#8217;s minimum.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/11/enable-tpm-and-secure-boot-graphic-1.webp" alt="The order to enable TPM and Secure Boot: 1, check what you have with tpm.msc, msinfo32 and Disk Management; 2, turn on TPM 2.0 (Intel PTT or AMD fTPM) in the firmware; 3, convert MBR to GPT only if the disk shows MBR, after a backup; 4, switch to UEFI boot by turning off CSM or Legacy mode; 5, enable Secure Boot on the Boot tab, press F10, then check msinfo32" class="wp-image-10239" srcset="https://www.gradientco.net/wp-content/uploads/2026/11/enable-tpm-and-secure-boot-graphic-1.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/11/enable-tpm-and-secure-boot-graphic-1-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/11/enable-tpm-and-secure-boot-graphic-1-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/11/enable-tpm-and-secure-boot-graphic-1-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Skip any step your PC already passes; the MBR to GPT conversion is the only one that rewrites the disk. Sources: Activision Support, Microsoft Support and Microsoft Learn.</figcaption></figure>



<h2 class="wp-block-heading">How do you check what your PC already has?</h2>



<p class="wp-block-paragraph">Before you enable TPM and Secure Boot, run three quick checks to see which steps you can skip.</p>



<ol class="wp-block-list">
<li><strong>TPM:</strong> press Windows + R, type <code>tpm.msc</code> and press Enter. &#8220;The TPM is ready for use&#8221; with a Specification Version of 2.0 means you&#8217;re done with TPM. &#8220;Compatible TPM cannot be found&#8221; may simply mean it is switched off, says <a href="https://support.microsoft.com/en-us/windows/enable-tpm-2-0-on-your-pc-1fd5a332-360d-4f46-a1e7-ae6b0c90645c" target="_blank" rel="noopener">Microsoft&#8217;s TPM guide</a>.</li>

<li><strong>Boot mode and Secure Boot:</strong> press Windows + R and run <code>msinfo32</code>. Look at &#8220;BIOS Mode&#8221; (you want UEFI) and &#8220;Secure Boot State&#8221; (you want On).</li>

<li><strong>Disk style:</strong> right-click Start, open Disk Management, right-click the disk that holds Windows (usually Disk 0), choose Properties and open the Volumes tab. &#8220;Partition style&#8221; shows GPT or MBR.</li>
</ol>



<p class="wp-block-paragraph">If BIOS Mode already says UEFI and the disk shows GPT, skip the conversion section below. Activision also offers a Secure Attestation Wizard (version 1.3.0, updated 21 October 2026), which scans your BIOS information to confirm whether the PC meets its security requirements.</p>



<h2 class="wp-block-heading">How to turn on Intel PTT or AMD fTPM</h2>



<p class="wp-block-paragraph">TPM is a single switch in the firmware, and Microsoft notes that most retail motherboards ship with it turned off. If <code>tpm.msc</code> found nothing, this is likely your fix. Getting into the firmware is the same on every PC running Windows: Settings, then System, then Recovery, then Restart now under Advanced startup. Choose Troubleshoot, Advanced options, UEFI Firmware Settings, and Restart.</p>



<p class="wp-block-paragraph">Each brand names the switch differently. Look in an Advanced, Security or Trusted Computing menu for:</p>



<ul class="wp-block-list">
<li>Intel PTT, or Intel Platform Trust Technology (Intel processors)</li>

<li>AMD fTPM switch, or AMD PSP fTPM (AMD processors)</li>

<li>Security Device Support, or TPM State (common on laptops)</li>
</ul>



<p class="wp-block-paragraph">On ASUS boards, for example, the <a href="https://www.asus.com/support/faq/1046215/" target="_blank" rel="noopener">ASUS guide</a> puts Intel PTT under Advanced, PCH-FW Configuration, and the AMD option under Advanced, AMD fTPM configuration, where &#8220;TPM Device Selection&#8221; is set to &#8220;Firmware TPM&#8221;. Press F10 to save and restart, then run <code>tpm.msc</code> again.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="405" src="https://www.gradientco.net/wp-content/uploads/2026/11/enable-tpm-and-secure-boot-inline-1.webp" alt="UEFI firmware Security tab with AMD Platform Security Processor set to Enabled and Secure Boot set to Enabled, Secure Boot Status Enabled and Platform Mode User Mode" class="wp-image-10240" srcset="https://www.gradientco.net/wp-content/uploads/2026/11/enable-tpm-and-secure-boot-inline-1.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/11/enable-tpm-and-secure-boot-inline-1-300x101.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/11/enable-tpm-and-secure-boot-inline-1-1024x346.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/11/enable-tpm-and-secure-boot-inline-1-768x259.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">A real Insyde firmware Security tab with AMD Platform Security Processor and Secure Boot both enabled. Your board may name and place these options differently. Photo: <a href="https://commons.wikimedia.org/wiki/File%3AInsydeH2O_UEFI_AMD_PSP_screenshot.jpg" target="_blank" rel="noopener">InsydeH2O UEFI AMD PSP screenshot</a> by <a href="https://commons.wikimedia.org/wiki/File%3AInsydeH2O_UEFI_AMD_PSP_screenshot.jpg" target="_blank" rel="noopener">Software: Insyde Software Screenshot: VulcanSphere</a>, <a href="https://creativecommons.org/publicdomain/mark/1.0/" target="_blank" rel="noopener">Public domain</a></figcaption></figure>



<p class="wp-block-paragraph">One caution from <a href="https://www.pcgameshardware.de/Call-of-Duty-Modern-Warfare-4-Spiel-78140/Specials/TPM-20-Secure-Boot-Probleme-1483719/" target="_blank" rel="noopener">PC Games Hardware&#8217;s troubleshooting list</a>: do not use the &#8220;Clear TPM&#8221; option to fix an error. Clearing can wipe keys that encrypted data depends on.</p>



<h2 class="wp-block-heading">Is your disk MBR? How to convert it to GPT safely</h2>



<p class="wp-block-paragraph">If your disk is MBR, Activision&#8217;s check fails until it is GPT, and Microsoft&#8217;s built-in MBR2GPT tool converts it <strong>without deleting your files</strong>. According to <a href="https://learn.microsoft.com/en-us/windows/deployment/mbr-to-gpt" target="_blank" rel="noopener">Microsoft Learn</a>, the tool already sits in Windows\System32. It&#8217;s still a one-way change to how the disk starts, and the tool itself warns: &#8220;These changes cannot be undone!&#8221;</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/11/enable-tpm-and-secure-boot-graphic-2.webp" alt="Checklist before running MBR2GPT: 64-bit Windows 10, version 1703 or later; three partitions or fewer on the disk; UEFI-capable firmware and no dual boot; Secure Boot switched off for now; BitLocker off, with the recovery key saved" class="wp-image-10241" srcset="https://www.gradientco.net/wp-content/uploads/2026/11/enable-tpm-and-secure-boot-graphic-2.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/11/enable-tpm-and-secure-boot-graphic-2-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/11/enable-tpm-and-secure-boot-graphic-2-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/11/enable-tpm-and-secure-boot-graphic-2-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Activision&#8217;s conditions for converting the system disk without wiping it. Source: Activision Support, TPM and Secure Boot.</figcaption></figure>



<p class="wp-block-paragraph">Before you start, meet the conditions Activision lists: 64-bit Windows 10 version 1703 or later, no more than three partitions on the disk, no dual boot, firmware that supports UEFI, and Secure Boot off. Then prepare:</p>



<ol class="wp-block-list">
<li><strong>Back up anything you can&#8217;t lose</strong> to an external drive or cloud storage.</li>

<li><strong>Save your BitLocker recovery key</strong> if the drive is encrypted. It is a 48-digit number; if it was backed up to your Microsoft account, you can find it at aka.ms/myrecoverykey. <a href="https://support.microsoft.com/en-us/windows/find-your-bitlocker-recovery-key-6b71ad27-0b89-ea08-f143-056f5ab347d6" target="_blank" rel="noopener">Microsoft says</a> its support staff cannot recover a lost one. Then turn BitLocker off, as Activision advises, and turn it back on after the switch.</li>

<li><strong>Plug the PC into a UPS</strong> if power cuts are common where you live. In our view, a power cut halfway through a conversion or firmware update is not a risk worth taking.</li>
</ol>



<p class="wp-block-paragraph">Next, open Command Prompt as administrator and run a check that changes nothing: <code>mbr2gpt /validate /allowFullOS</code>. Leaving out <code>/disk:</code> makes the tool pick the system disk, so you can&#8217;t convert the wrong drive by mistake.</p>



<p class="wp-block-paragraph">&#8220;Validation completed successfully&#8221; means you can go ahead. If it fails, nothing has been touched, and the reason is written to setupact.log and setuperr.log in the Windows folder. When it passes, run the real conversion with <code>mbr2gpt /convert /allowFullOS</code>.</p>



<p class="wp-block-paragraph">Restart straight into the firmware (on ASUS boards, press Del at the logo; other brands list their key in the manual) and change the boot mode from Legacy or CSM to UEFI. Microsoft is explicit that the firmware must boot in UEFI mode after conversion, or Windows will not start.</p>



<h2 class="wp-block-heading">How to enable TPM and Secure Boot in the right order</h2>



<p class="wp-block-paragraph">Order matters because each setting depends on the one before it. With TPM on and the disk on GPT, go back into the firmware and work through these:</p>



<ol class="wp-block-list">
<li>If you didn&#8217;t already do it after converting, set the boot mode to UEFI only, or switch off CSM (Compatibility Support Module). Microsoft&#8217;s <a href="https://support.microsoft.com/en-us/windows/windows-11-and-secure-boot-a8ff1202-c0d9-42f5-940f-843abef64fad" target="_blank" rel="noopener">Windows 11 and Secure Boot page</a> says to make UEFI &#8220;the first or only option&#8221;.</li>

<li>Open the Boot tab (Security on some boards) and set Secure Boot to Enabled.</li>

<li>Press F10 to save and exit.</li>

<li>Back in Windows, run <code>msinfo32</code> and <code>tpm.msc</code> once more. Secure Boot State should now say On.</li>
</ol>



<p class="wp-block-paragraph">Activision&#8217;s warning is worth taking seriously: changing firmware settings the wrong way &#8220;can cause system issues, including boot failures&#8221;. If a menu doesn&#8217;t match these steps, follow your motherboard or laptop maker&#8217;s manual.</p>



<h2 class="wp-block-heading">What if your PC is too old for firmware TPM?</h2>



<p class="wp-block-paragraph">The processor cutoff in the table above rules out firmware TPM on many older desktops. If you&#8217;re buying a used or refurbished PC, check the processor generation before paying. If yours is older, you still have a few options.</p>



<ul class="wp-block-list">
<li><strong>Check for a TPM header.</strong> Activision accepts a discrete TPM chip, so look in your board&#8217;s manual for a TPM socket and match any module to that exact model. One oddity here: Activision&#8217;s Modern Warfare 4 minimum list names the 6th Gen Intel Core i5-6600, yet its TPM page names 8th Gen for firmware TPM, so we think a 6th Gen PC would need a discrete chip.</li>

<li><strong>Update the firmware first.</strong> Your board maker&#8217;s release notes say whether a newer version adds TPM or UEFI options.</li>

<li><strong>Plan an upgrade</strong> if neither works.</li>
</ul>



<figure class="wp-block-image size-large"><img decoding="async" src="https://images.unsplash.com/photo-1555616635-640b71bdb185?ixid=M3wxMDkxNzE3fDB8MXxhbGx8fHx8fHx8fHwxNzk0MTg4NDEyfA&amp;ixlib=rb-4.1.0&amp;w=1200&amp;q=80&amp;fm=jpg&amp;fit=max" alt="A hand holding an Intel Core i9 desktop processor with its model number printed on the metal lid" /><figcaption class="wp-element-caption">Check the processor before you pay for a used PC: Activision names Intel 8th Gen or AMD Ryzen 2000 and newer for firmware TPM. Photo by <a href="https://unsplash.com/@christianw?utm_source=gradientco&amp;utm_medium=referral" target="_blank" rel="noopener">Christian Wiediger</a> on <a href="https://unsplash.com/?utm_source=gradientco&amp;utm_medium=referral" target="_blank" rel="noopener">Unsplash</a></figcaption></figure>



<p class="wp-block-paragraph">We&#8217;d skip registry tweaks and tools that claim to bypass TPM checks, and this guide doesn&#8217;t cover them. Microsoft ended Windows 10 support on 14 October 2025, so an older unsupported machine is already missing security fixes.</p>



<h2 class="wp-block-heading">What do the common errors mean?</h2>



<p class="wp-block-paragraph">Activision&#8217;s messages are fairly literal, and most point back to one missing step:</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Message</th><th>What to do</th></tr></thead><tbody><tr><td>TPM 2.0: Not Enabled</td><td>Turn on Intel PTT or AMD fTPM</td></tr><tr><td>Secure Boot: Not Enabled</td><td>Enable Secure Boot in the firmware</td></tr><tr><td>Boot Partition not set to GPT</td><td>Convert the disk with MBR2GPT</td></tr><tr><td>BIOS Boot mode not set to UEFI</td><td>Switch off CSM or Legacy boot</td></tr><tr><td>BIOS Firmware Update Required</td><td>Update the motherboard firmware; Activision says it &#8220;may be required in the future&#8221;</td></tr><tr><td>CODBrokerInstaller.exe authorization declined</td><td>Approve the admin prompt; if none appears, reset the UAC slider to its default</td></tr></tbody></table></figure>



<p class="wp-block-paragraph">If the firmware says Secure Boot is on but Windows says it is off, PC Games Hardware points to Legacy mode or missing Secure Boot keys, which leave the board in Setup Mode. And if the <code>enrollaik.exe</code> prompt keeps returning on an AMD system, Activision links it to AMD firmware 3.<em>.0.</em> and says the motherboard BIOS needs an update.</p>



<p class="wp-block-paragraph">If you also manage a website, our guide to <a href="https://www.gradientco.net/tech-science/php2shell-php-update-guide/">patching the PHP2Shell flaws</a> takes the same check-before-you-change approach to a server update.</p>



<h2 class="wp-block-heading">Frequently asked questions</h2>


<div id="rank-math-faq" class="rank-math-block">
<div class="rank-math-list ">
<div id="faq-question-1" class="rank-math-list-item">
<h3 class="rank-math-question ">Will turning on TPM or Secure Boot delete my files?</h3>
<div class="rank-math-answer ">

<p>No. Both are firmware switches and don&#8217;t touch your data. The MBR to GPT conversion is the only step that rewrites the disk layout, which is why it needs a backup first.</p>

</div>
</div>
<div id="faq-question-2" class="rank-math-list-item">
<h3 class="rank-math-question ">Can I enable TPM and Secure Boot on Windows 10?</h3>
<div class="rank-math-answer ">

<p>Yes, and Call of Duty accepts Windows 10 version 22H2 or later. The same TPM and Secure Boot settings also cover two of Windows 11&#8217;s requirements if you upgrade later.</p>

</div>
</div>
<div id="faq-question-3" class="rank-math-list-item">
<h3 class="rank-math-question ">Why is my PC asking for a BitLocker recovery key after the change?</h3>
<div class="rank-math-answer ">

<p>Microsoft says BitLocker can ask for the key at startup after a hardware change or a possible security risk, and PC Games Hardware lists it among the common problems after these firmware changes. Enter the recovery key you saved before the change, then turn BitLocker protection back on in Windows.</p>

</div>
</div>
<div id="faq-question-4" class="rank-math-list-item">
<h3 class="rank-math-question ">Does Secure Boot stop Linux or USB rescue drives from booting?</h3>
<div class="rank-math-answer ">

<p>Sometimes. Secure Boot only starts signed boot software, so some Linux or rescue drives are rejected. You can switch it off briefly to use one, and Microsoft recommends turning it back on afterwards.</p>

</div>
</div>
<div id="faq-question-5" class="rank-math-list-item">
<h3 class="rank-math-question ">Do I need to do anything about the Secure Boot certificates expiring?</h3>
<div class="rank-math-answer ">

<p>Usually not. Microsoft says the 2011 Secure Boot certificates began expiring in June 2026 and that supported Windows versions replace them automatically through updates.</p>

</div>
</div>
</div>
</div>


<h2 class="wp-block-heading">What we do not know yet</h2>



<p class="wp-block-paragraph">We could not confirm whether Activision accepts every older processor that has a discrete TPM module for online play, because its requirement pages do not say. We also found no official Pakistani prices for TPM modules, so we left costs out.</p>



<p class="wp-block-paragraph"><strong>Read next:</strong> our <a href="https://www.gradientco.net/tech-science/gta-6-pc-release-date/">GTA 6 PC release date guide</a>, for what Rockstar has confirmed about the next big PC launch.</p>



<p class="wp-block-paragraph"><em>Researched and drafted with AI assistance; facts checked against the sources linked in this article.</em></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Idempotency key Spring Boot guide: why one retry still charges twice</title>
		<link>https://www.gradientco.net/engineering/idempotency-key-spring-boot/</link>
		
		<dc:creator><![CDATA[Nabeel Ahmed]]></dc:creator>
		<pubDate>Tue, 10 Nov 2026 02:22:24 +0000</pubDate>
				<category><![CDATA[Engineering]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[Distributed Systems]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<guid isPermaLink="false">https://www.gradientco.net/?p=10229</guid>

					<description><![CDATA[A checkout that stores every Idempotency-Key still creates two orders when a retry overlaps the first attempt. A test that forces it, why a lock in the JVM only hides it, and three database-backed fixes.]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">A teammate pastes two rows from the orders table and asks a fair question: how did one Idempotency-Key produce both? We rebuilt that scenario in an idempotency key Spring Boot test project whose checkout passes every sequential test. Yet in our run, one request and its overlapping retry left two orders, two charges and two rows for the same key.</p>



<p class="wp-block-paragraph">A third retry with that key then failed outright: <code>Query did not return a unique result: 2 results were returned</code>. So how does code that remembers every key still do the work twice?</p>



<p class="wp-block-paragraph"><strong>Short answer:</strong> a lookup followed by an insert is two separate moments, and a retry that arrives in between misses the lookup too. Let the database decide: put a unique constraint on the key, reserve it before you charge anything, store the response for replays, and answer 409 while the first attempt is in flight.</p>



<h2 class="wp-block-heading">The checkout that remembers every key</h2>



<p class="wp-block-paragraph">The service below comes from our test project (Java 25, Spring Boot 4.1.1, H2 as the test database). The client sends an <code>Idempotency-Key</code> header with each POST, the controller passes it in, and the whole method runs in one transaction:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-start:25;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2f363c;color:#d3d7dd">CheckoutService.java (buggy)</span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">@Transactional</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">OrderResult</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">placeOrder</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">String</span><span style="color: #E1E4E8"> idempotencyKey, </span><span style="color: #B392F0">PlaceOrderRequest</span><span style="color: #E1E4E8"> request) {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">Optional</span><span style="color: #F97583">&lt;</span><span style="color: #B392F0">IdempotencyKey</span><span style="color: #F97583">&gt;</span><span style="color: #E1E4E8"> seen </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> keys.</span><span style="color: #B392F0">findByIdempotencyKey</span><span style="color: #E1E4E8">(idempotencyKey);</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">if</span><span style="color: #E1E4E8"> (seen.</span><span style="color: #B392F0">isPresent</span><span style="color: #E1E4E8">()) {</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> seen.</span><span style="color: #B392F0">get</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">toResult</span><span style="color: #E1E4E8">(); </span><span style="color: #6A737D">// a retry: answer with the first order</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8"> chargeId </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> payments.</span><span style="color: #B392F0">charge</span><span style="color: #E1E4E8">(request.</span><span style="color: #B392F0">customerId</span><span style="color: #E1E4E8">(), request.</span><span style="color: #B392F0">amountCents</span><span style="color: #E1E4E8">());</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">CustomerOrder</span><span style="color: #E1E4E8"> order </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> orders.</span><span style="color: #B392F0">save</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">CustomerOrder</span><span style="color: #E1E4E8">(request, chargeId));</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        keys.</span><span style="color: #B392F0">save</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">IdempotencyKey</span><span style="color: #E1E4E8">(idempotencyKey, order.</span><span style="color: #B392F0">getId</span><span style="color: #E1E4E8">(), chargeId));</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">OrderResult</span><span style="color: #E1E4E8">(order.</span><span style="color: #B392F0">getId</span><span style="color: #E1E4E8">(), chargeId);</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span></code></pre></div>



<p class="wp-block-paragraph">Line 27 looks for an earlier request with this key, and line 29 returns that order instead of creating a new one. Line 34 records the key next to the new order. It reads like a textbook <em>idempotent</em> endpoint: one that&#8217;s safe to run twice, because the second call only repeats the first answer.</p>



<p class="wp-block-paragraph">HTTP doesn&#8217;t give you that for free. RFC 9110 lists PUT, DELETE and the safe methods as idempotent, and POST isn&#8217;t on the list. The header is how we make an idempotent REST API out of a POST, and the server has to earn it.</p>



<p class="wp-block-paragraph">The method is <code>@Transactional</code>, so a reviewer assumes the lookup and the save are protected together. They are, from partial failure, but not from another request.</p>



<h2 class="wp-block-heading">How can two requests both miss the lookup?</h2>



<p class="wp-block-paragraph">Think of a sign-up sheet on a door. Two people read it at the same moment, see no name, and both write theirs in. Reading the sheet never reserved anything.</p>



<p class="wp-block-paragraph">The same thing happens here. A phone sends the checkout, times out, and sends it again with the same key while the server is still working on the first copy. Both requests run line 27 before either one reaches line 34, so both see no key.</p>



<figure class="wp-block-video"><video height="1080" style="aspect-ratio: 1080 / 1080;" width="1080" autoplay loop muted poster="https://www.gradientco.net/wp-content/uploads/2026/11/idempotency-key-spring-boot-clip-poster.webp" src="https://www.gradientco.net/wp-content/uploads/2026/11/idempotency-key-spring-boot-clip.mp4" playsinline></video><figcaption class="wp-element-caption">Both lookups run before either key is saved.</figcaption></figure>



<p class="wp-block-paragraph">Both charge the card, both save an order, and both insert a key row. Nothing stops the second insert: the key column has an index, but no rule that a key may appear once.</p>



<p class="wp-block-paragraph">That pattern is a <em>check-then-insert race</em>: two requests check that a row is missing, and both insert it before either one can see the other&#8217;s. The transaction doesn&#8217;t help, because each request only sees rows the other has committed.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #e1e4e8">first attempt                              retry (same key)</span></span>
<span class="line"><span style="color: #e1e4e8">-------------                              ----------------</span></span>
<span class="line"><span style="color: #e1e4e8">look up key -&gt; not found</span></span>
<span class="line"><span style="color: #e1e4e8">                                           look up key -&gt; not found</span></span>
<span class="line"><span style="color: #e1e4e8">charge card, save order</span></span>
<span class="line"><span style="color: #e1e4e8">                                           charge card, save a second order   &lt;- the second charge</span></span>
<span class="line"><span style="color: #e1e4e8">save key -&gt; 1 row</span></span>
<span class="line"><span style="color: #e1e4e8">                                           save key -&gt; 2 rows</span></span>
<span class="line"><span style="color: #e1e4e8">commit                                     commit</span></span></code></pre></div>



<p class="wp-block-paragraph">The break is the second lookup; everything after it acts on stale information.</p>



<h2 class="wp-block-heading">How do you force the overlap in a test?</h2>



<p class="wp-block-paragraph">Waiting for a lucky overlap makes a flaky test, so we forced it. A test-only <code>BeanPostProcessor</code> wraps the repositories, and after <code>findByIdempotencyKey</code> returns, each request waits at a <code>CyclicBarrier</code> until the other has also looked up the key:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-start:49;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2f363c;color:#d3d7dd">IdempotencyBugTest.java</span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">CyclicBarrier</span><span style="color: #E1E4E8"> bothLookedUp </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">CyclicBarrier</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">2</span><span style="color: #E1E4E8">);</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        pauses.</span><span style="color: #B392F0">after</span><span style="color: #E1E4E8">(</span><span style="color: #9ECBFF">&quot;findByIdempotencyKey&quot;</span><span style="color: #E1E4E8">, () </span><span style="color: #F97583">-&gt;</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">Retries</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">await</span><span style="color: #E1E4E8">(bothLookedUp));</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">OrderResult</span><span style="color: #E1E4E8"> first, retry;</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">try</span><span style="color: #E1E4E8"> (</span><span style="color: #B392F0">ExecutorService</span><span style="color: #E1E4E8"> pool </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">Executors</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">newFixedThreadPool</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">2</span><span style="color: #E1E4E8">)) {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">            </span><span style="color: #B392F0">Future</span><span style="color: #F97583">&lt;</span><span style="color: #B392F0">OrderResult</span><span style="color: #F97583">&gt;</span><span style="color: #E1E4E8"> a </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> pool.</span><span style="color: #B392F0">submit</span><span style="color: #E1E4E8">(() </span><span style="color: #F97583">-&gt;</span><span style="color: #E1E4E8"> checkout.</span><span style="color: #B392F0">placeOrder</span><span style="color: #E1E4E8">(key, request));</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">            </span><span style="color: #B392F0">Future</span><span style="color: #F97583">&lt;</span><span style="color: #B392F0">OrderResult</span><span style="color: #F97583">&gt;</span><span style="color: #E1E4E8"> b </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> pool.</span><span style="color: #B392F0">submit</span><span style="color: #E1E4E8">(() </span><span style="color: #F97583">-&gt;</span><span style="color: #E1E4E8"> checkout.</span><span style="color: #B392F0">placeOrder</span><span style="color: #E1E4E8">(key, request));</span></span>
<span class="line"><span style="color: #E1E4E8">            first </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> a.</span><span style="color: #B392F0">get</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">10</span><span style="color: #E1E4E8">, </span><span style="color: #79B8FF">SECONDS</span><span style="color: #E1E4E8">);</span></span>
<span class="line"><span style="color: #E1E4E8">            retry </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> b.</span><span style="color: #B392F0">get</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">10</span><span style="color: #E1E4E8">, </span><span style="color: #79B8FF">SECONDS</span><span style="color: #E1E4E8">);</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span></code></pre></div>



<p class="wp-block-paragraph">Line 50 sets the trap; lines 54 and 55 send the same key from two threads. The run printed this (on our 4-core cloud build machine; your numbers will differ):</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #e1e4e8">&#91;bug&#93; same Idempotency-Key, 2 overlapping requests -&gt; orders=2, charges=2, rows for the key=2</span></span>
<span class="line"><span style="color: #e1e4e8">&#91;bug&#93; first got order 119 (ch_000119), retry got order 118 (ch_000118)</span></span>
<span class="line"><span style="color: #e1e4e8">&#91;bug&#93; third retry: IncorrectResultSizeDataAccessException: Query did not return a unique result: 2 results were returned</span></span></code></pre></div>



<p class="wp-block-paragraph">The third line is the clue from the opening. <code>findByIdempotencyKey</code> returns an <code>Optional</code>, and the Spring Data reference says more than one result &#8220;triggers an IncorrectResultSizeDataAccessException&#8221;. So the duplicate does not stay quiet: every later retry with that key becomes a 500.</p>



<p class="wp-block-paragraph">The barrier is not required. Without any pause, 8 copies of one request released together produced 4 orders and 4 charges on the first run, and 3 more copies failed with that same exception.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/11/idempotency-key-spring-boot-graphic-1.webp" alt="Buggy checkout in our verify run: one key and its retry made 2 orders, 2 charges and 2 key rows; 8 copies made 4 orders; later retries got a 500" class="wp-image-10233" srcset="https://www.gradientco.net/wp-content/uploads/2026/11/idempotency-key-spring-boot-graphic-1.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/11/idempotency-key-spring-boot-graphic-1-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/11/idempotency-key-spring-boot-graphic-1-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/11/idempotency-key-spring-boot-graphic-1-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Source: Gradient verify run, November 2026</figcaption></figure>



<h2 class="wp-block-heading">Why does an idempotency key Spring Boot test suite stay green?</h2>



<p class="wp-block-paragraph">Because your tests send retries politely, one after the other. Our sequential test does exactly that and gets 1 order, 1 charge, and the same order id twice. The bug needs overlap: a slow first attempt and a client with a short timeout.</p>



<p class="wp-block-paragraph">Production supplies both. A payment provider has a slow minute, and the mobile client&#8217;s timeout runs out before the server has answered. Not every layer retries: NGINX, by default, does not pass a POST to the next upstream once it has been sent. Clients, SDKs and impatient users retry anyway, so duplicate requests arrive even behind a careful proxy.</p>



<h3 class="wp-block-heading">Does synchronized make it safe?</h3>



<p class="wp-block-paragraph">On one server, yes. Wrapping the transactional call in a lock means each order commits before the next request runs its lookup:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-start:20;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2f363c;color:#d3d7dd">SynchronizedCheckoutService.java (looks like a fix)</span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">OrderResult</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">placeOrder</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">String</span><span style="color: #E1E4E8"> idempotencyKey, </span><span style="color: #B392F0">PlaceOrderRequest</span><span style="color: #E1E4E8"> request) {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #F97583">synchronized</span><span style="color: #E1E4E8"> (</span><span style="color: #F97583">this</span><span style="color: #E1E4E8">) {</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> checkout.</span><span style="color: #B392F0">placeOrder</span><span style="color: #E1E4E8">(idempotencyKey, request);</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span></code></pre></div>



<p class="wp-block-paragraph">Our test ran the same overlap through one instance, then through two sharing the database, the way two pods would.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #e1e4e8">&#91;synchronized&#93; 1 server, 2 overlapping requests -&gt; orders=1; 2 servers -&gt; orders=2</span></span></code></pre></div>



<p class="wp-block-paragraph">A JVM lock only coordinates threads inside one JVM. Once the retry reaches a second pod, the lock on line 21 protects nothing; on one pod, it queues every checkout behind every other. In our view that makes it more dangerous than the original bug, because it passes every test you can run on one machine. The only thing both pods share is the database, so the decision has to happen there.</p>



<h2 class="wp-block-heading">Fix 1: let a unique constraint pick the winner</h2>



<p class="wp-block-paragraph">The smallest change is the one the buggy table lacked: a <em>unique constraint</em> on the key, the database&#8217;s own rule that no two rows may share a value. <code>IdempotencyRecord</code> declares the column <code>unique = true</code>, and the service catches the violation:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-start:35;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2f363c;color:#d3d7dd">UniqueKeyCheckoutService.java (fix 1)</span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">OrderResult</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">placeOrder</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">String</span><span style="color: #E1E4E8"> idempotencyKey, </span><span style="color: #B392F0">PlaceOrderRequest</span><span style="color: #E1E4E8"> request) {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">try</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> tx.</span><span style="color: #B392F0">execute</span><span style="color: #E1E4E8">(status </span><span style="color: #F97583">-&gt;</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">                </span><span style="color: #F97583">var</span><span style="color: #E1E4E8"> seen </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> records.</span><span style="color: #B392F0">findByIdempotencyKey</span><span style="color: #E1E4E8">(idempotencyKey);</span></span>
<span class="line"><span style="color: #E1E4E8">                </span><span style="color: #F97583">if</span><span style="color: #E1E4E8"> (seen.</span><span style="color: #B392F0">isPresent</span><span style="color: #E1E4E8">()) {</span></span>
<span class="line"><span style="color: #E1E4E8">                    </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> seen.</span><span style="color: #B392F0">get</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">toResult</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #E1E4E8">                }</span></span>
<span class="line"><span style="color: #E1E4E8">                </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8"> chargeId </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> payments.</span><span style="color: #B392F0">charge</span><span style="color: #E1E4E8">(request.</span><span style="color: #B392F0">customerId</span><span style="color: #E1E4E8">(), request.</span><span style="color: #B392F0">amountCents</span><span style="color: #E1E4E8">());</span></span>
<span class="line"><span style="color: #E1E4E8">                </span><span style="color: #B392F0">CustomerOrder</span><span style="color: #E1E4E8"> order </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> orders.</span><span style="color: #B392F0">save</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">CustomerOrder</span><span style="color: #E1E4E8">(request, chargeId));</span></span>
<span class="line"><span style="color: #E1E4E8">                </span><span style="color: #F97583">var</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">record</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">IdempotencyRecord</span><span style="color: #E1E4E8">(idempotencyKey, </span><span style="color: #B392F0">RequestFingerprint</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">of</span><span style="color: #E1E4E8">(request), clock.</span><span style="color: #B392F0">instant</span><span style="color: #E1E4E8">());</span></span>
<span class="line"><span style="color: #E1E4E8">                </span><span style="color: #F97583">record</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">complete</span><span style="color: #E1E4E8">(order.</span><span style="color: #B392F0">getId</span><span style="color: #E1E4E8">(), chargeId, clock.</span><span style="color: #B392F0">instant</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">plus</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">RETENTION</span><span style="color: #E1E4E8">));</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">                records.</span><span style="color: #B392F0">saveAndFlush</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">record</span><span style="color: #E1E4E8">); </span><span style="color: #6A737D">// the INSERT that can fail on the unique key</span></span>
<span class="line"><span style="color: #E1E4E8">                </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">OrderResult</span><span style="color: #E1E4E8">(order.</span><span style="color: #B392F0">getId</span><span style="color: #E1E4E8">(), chargeId);</span></span>
<span class="line"><span style="color: #E1E4E8">            });</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        } </span><span style="color: #F97583">catch</span><span style="color: #E1E4E8"> (</span><span style="color: #B392F0">DataIntegrityViolationException</span><span style="color: #E1E4E8"> duplicate) {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">            </span><span style="color: #6A737D">// Another request with this key committed first. Our order rolled back; theirs is the answer.</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">            </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> records.</span><span style="color: #B392F0">findByIdempotencyKey</span><span style="color: #E1E4E8">(idempotencyKey).</span><span style="color: #B392F0">orElseThrow</span><span style="color: #E1E4E8">(() </span><span style="color: #F97583">-&gt;</span><span style="color: #E1E4E8"> duplicate).</span><span style="color: #B392F0">toResult</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span></code></pre></div>



<p class="wp-block-paragraph">Line 46 flushes the insert inside the transaction, so a duplicate fails right there, not at some later commit. Spring turns the failure into a <code>DataIntegrityViolationException</code>, and its Javadoc recommends catching that class rather than a subclass.</p>



<p class="wp-block-paragraph"><code>TransactionTemplate</code> rolls back on any runtime exception and rethrows it. So by the time the catch on line 49 runs, the loser&#8217;s order is gone, and line 51 answers with the winner&#8217;s.</p>



<p class="wp-block-paragraph">Under the same barrier and under load, the order count comes out right. The charges are another story:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #e1e4e8">&#91;unique&#93; same forced overlap -&gt; orders=1, charges=2, both callers got order 26: true</span></span>
<span class="line"><span style="color: #e1e4e8">&#91;unique&#93; 20 keys x 8 concurrent copies -&gt; orders=20, charges=82, errors=0</span></span></code></pre></div>



<p class="wp-block-paragraph"><strong>The database rolled back the order, but it can&#8217;t roll back a card.</strong> 160 requests made 82 charges for 20 orders. Fix 1 is complete only when everything the request does lives in that one transaction, and a payment call, an email or a Kafka message doesn&#8217;t.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/11/idempotency-key-spring-boot-graphic-2.webp" alt="Idempotency key Spring Boot fixes at 20 keys x 8 copies: a unique constraint gave 82 charges for 20 orders; reserving first gave 20 charges and a 409 in flight; a fingerprint adds 422 and a lease ends a crashed 409" class="wp-image-10234" srcset="https://www.gradientco.net/wp-content/uploads/2026/11/idempotency-key-spring-boot-graphic-2.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/11/idempotency-key-spring-boot-graphic-2-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/11/idempotency-key-spring-boot-graphic-2-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/11/idempotency-key-spring-boot-graphic-2-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Source: Gradient test project, November 2026</figcaption></figure>



<p class="wp-block-paragraph">You could move the key insert to the top of the transaction instead. On PostgreSQL, the manual says a second insert of a key held by an uncommitted transaction &#8220;must wait to see if that transaction commits&#8221;, so the retry would block and then fail.</p>



<p class="wp-block-paragraph">That works, but it holds a transaction and a pooled connection open for the whole payment call, plus a second one for the waiting retry. We showed what that does to a pool in <a href="https://www.gradientco.net/engineering/hikari-connection-is-not-available/">Hikari connection is not available? Your REQUIRES_NEW is starving the pool</a>.</p>



<h2 class="wp-block-heading">Fix 2: reserve the key before doing the work</h2>



<p class="wp-block-paragraph">So split the work into moments the database can referee. A tiny first transaction inserts the key with status <code>IN_PROGRESS</code> and commits. Only the request whose insert succeeded charges the card; everyone else reads the row and decides from it:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-start:38;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2f363c;color:#d3d7dd">IdempotentCheckoutService.java (fix 2)</span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">OrderResult</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">placeOrder</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">String</span><span style="color: #E1E4E8"> idempotencyKey, </span><span style="color: #B392F0">PlaceOrderRequest</span><span style="color: #E1E4E8"> request) {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8"> hash </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">RequestFingerprint</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">of</span><span style="color: #E1E4E8">(request);</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">Optional</span><span style="color: #F97583">&lt;</span><span style="color: #B392F0">IdempotencyRecord</span><span style="color: #F97583">&gt;</span><span style="color: #E1E4E8"> earlier </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">reserve</span><span style="color: #E1E4E8">(idempotencyKey, hash);</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">if</span><span style="color: #E1E4E8"> (earlier.</span><span style="color: #B392F0">isPresent</span><span style="color: #E1E4E8">()) {</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">replay</span><span style="color: #E1E4E8">(earlier.</span><span style="color: #B392F0">get</span><span style="color: #E1E4E8">(), hash);</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">try</span><span style="color: #E1E4E8"> {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">            </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8"> chargeId </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> payments.</span><span style="color: #B392F0">charge</span><span style="color: #E1E4E8">(request.</span><span style="color: #B392F0">customerId</span><span style="color: #E1E4E8">(), request.</span><span style="color: #B392F0">amountCents</span><span style="color: #E1E4E8">());</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> tx.</span><span style="color: #B392F0">execute</span><span style="color: #E1E4E8">(status </span><span style="color: #F97583">-&gt;</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">                </span><span style="color: #B392F0">CustomerOrder</span><span style="color: #E1E4E8"> order </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> orders.</span><span style="color: #B392F0">save</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">CustomerOrder</span><span style="color: #E1E4E8">(request, chargeId));</span></span>
<span class="line"><span style="color: #E1E4E8">                </span><span style="color: #B392F0">IdempotencyRecord</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">record</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> records.</span><span style="color: #B392F0">findByIdempotencyKey</span><span style="color: #E1E4E8">(idempotencyKey).</span><span style="color: #B392F0">orElseThrow</span><span style="color: #E1E4E8">();</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">                </span><span style="color: #F97583">record</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">complete</span><span style="color: #E1E4E8">(order.</span><span style="color: #B392F0">getId</span><span style="color: #E1E4E8">(), chargeId, clock.</span><span style="color: #B392F0">instant</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">plus</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">RETENTION</span><span style="color: #E1E4E8">));</span></span>
<span class="line"><span style="color: #E1E4E8">                </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">OrderResult</span><span style="color: #E1E4E8">(order.</span><span style="color: #B392F0">getId</span><span style="color: #E1E4E8">(), chargeId);</span></span>
<span class="line"><span style="color: #E1E4E8">            });</span></span>
<span class="line"><span style="color: #E1E4E8">        } </span><span style="color: #F97583">catch</span><span style="color: #E1E4E8"> (</span><span style="color: #B392F0">RuntimeException</span><span style="color: #E1E4E8"> failure) {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">            tx.</span><span style="color: #B392F0">executeWithoutResult</span><span style="color: #E1E4E8">(status </span><span style="color: #F97583">-&gt;</span><span style="color: #E1E4E8"> records.</span><span style="color: #B392F0">deleteByKey</span><span style="color: #E1E4E8">(idempotencyKey)); </span><span style="color: #6A737D">// free the key for a retry</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #F97583">throw</span><span style="color: #E1E4E8"> failure;</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">private</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">Optional</span><span style="color: #F97583">&lt;</span><span style="color: #B392F0">IdempotencyRecord</span><span style="color: #F97583">&gt;</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">reserve</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">String</span><span style="color: #E1E4E8"> idempotencyKey, </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8"> hash) {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">try</span><span style="color: #E1E4E8"> {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">            tx.</span><span style="color: #B392F0">executeWithoutResult</span><span style="color: #E1E4E8">(status </span><span style="color: #F97583">-&gt;</span><span style="color: #E1E4E8"> records.</span><span style="color: #B392F0">saveAndFlush</span><span style="color: #E1E4E8">(</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">                    </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">IdempotencyRecord</span><span style="color: #E1E4E8">(idempotencyKey, hash, clock.</span><span style="color: #B392F0">instant</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">plus</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">IN_FLIGHT_LEASE</span><span style="color: #E1E4E8">))));</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">Optional</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">empty</span><span style="color: #E1E4E8">(); </span><span style="color: #6A737D">// committed: this request owns the key</span></span>
<span class="line"><span style="color: #E1E4E8">        } </span><span style="color: #F97583">catch</span><span style="color: #E1E4E8"> (</span><span style="color: #B392F0">DataIntegrityViolationException</span><span style="color: #E1E4E8"> taken) {</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">Optional</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">of</span><span style="color: #E1E4E8">(records.</span><span style="color: #B392F0">findByIdempotencyKey</span><span style="color: #E1E4E8">(idempotencyKey).</span><span style="color: #B392F0">orElseThrow</span><span style="color: #E1E4E8">(() </span><span style="color: #F97583">-&gt;</span><span style="color: #E1E4E8"> taken));</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">private</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">OrderResult</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">replay</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">IdempotencyRecord</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">record</span><span style="color: #E1E4E8">, </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8"> hash) {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">if</span><span style="color: #E1E4E8"> (</span><span style="color: #F97583">!record</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">getRequestHash</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">equals</span><span style="color: #E1E4E8">(hash)) {</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #F97583">throw</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">IdempotencyKeyReusedException</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">record</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">getIdempotencyKey</span><span style="color: #E1E4E8">()); </span><span style="color: #6A737D">// 422</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #F97583">if</span><span style="color: #E1E4E8"> (</span><span style="color: #F97583">record</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">getStatus</span><span style="color: #E1E4E8">() </span><span style="color: #F97583">==</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">Status</span><span style="color: #E1E4E8">.</span><span style="color: #79B8FF">IN_PROGRESS</span><span style="color: #E1E4E8">) {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">            </span><span style="color: #F97583">throw</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">RequestInProgressException</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">record</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">getIdempotencyKey</span><span style="color: #E1E4E8">()); </span><span style="color: #6A737D">// 409</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">record</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">toResult</span><span style="color: #E1E4E8">(); </span><span style="color: #6A737D">// the stored answer; nothing runs twice</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span></code></pre></div>



<p class="wp-block-paragraph">Lines 60 and 61 are the reservation, committed before line 45 charges anything. A request that loses that insert lands on line 64 and reads the winner&#8217;s row: still <code>IN_PROGRESS</code> means a conflict (lines 72 and 73), complete means line 75 replays the stored order and charge id.</p>



<p class="wp-block-paragraph">The other two highlights close the loop. Line 49 completes the record in the same transaction that saves the order. Line 53 frees the key if the work fails, so the client&#8217;s next retry can run.</p>



<p class="wp-block-paragraph">We tested the in-flight case directly, pausing the first request after it had charged and saved its order but before it committed:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #e1e4e8">&#91;reserve&#93; retry while the first is in flight: 409 a request with Idempotency-Key 19d80319-eae6-4b00-a942-ed128454cff5 is still in progress</span></span>
<span class="line"><span style="color: #e1e4e8">&#91;reserve&#93; retry after it finished -&gt; order 22 (ch_000022), same as the first: true; orders=1, charges=1</span></span>
<span class="line"><span style="color: #e1e4e8">&#91;reserve&#93; 20 keys x 8 concurrent copies -&gt; orders=20, charges=20, replays=77, 409s=63, other errors=0</span></span></code></pre></div>



<p class="wp-block-paragraph">The load line is the one fix 1 couldn&#8217;t produce: one charge per order. Of the 140 copies that weren&#8217;t first, 77 got the stored answer and 63 were told to try again.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/11/idempotency-key-spring-boot-graphic-3.webp" alt="Fix 2 step by step: the first attempt reserves the key and charges; the retry hits the unique constraint, reads IN_PROGRESS and gets 409; after order 22 completes, the retry gets order 22 back: 1 order, 1 charge" class="wp-image-10235" srcset="https://www.gradientco.net/wp-content/uploads/2026/11/idempotency-key-spring-boot-graphic-3.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/11/idempotency-key-spring-boot-graphic-3-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/11/idempotency-key-spring-boot-graphic-3-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/11/idempotency-key-spring-boot-graphic-3-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Source: Gradient verify run, November 2026</figcaption></figure>



<p class="wp-block-paragraph">The controller maps those outcomes to the status codes the IETF&#8217;s Idempotency-Key header draft recommends: 409 while the first request is still processing, 422 when a key comes back with a different body, and 400 when a required key is missing. That last one comes free, since Spring MVC answers a missing required <code>@RequestHeader</code> with a binding exception and a 400.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-start:23;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2f363c;color:#d3d7dd">CheckoutController.java</span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">@PostMapping</span><span style="color: #E1E4E8">(</span><span style="color: #9ECBFF">&quot;/orders&quot;</span><span style="color: #E1E4E8">)</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">@ResponseStatus</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">HttpStatus</span><span style="color: #E1E4E8">.</span><span style="color: #79B8FF">CREATED</span><span style="color: #E1E4E8">)</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">    </span><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">OrderResult</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">placeOrder</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">@RequestHeader</span><span style="color: #E1E4E8">(</span><span style="color: #9ECBFF">&quot;Idempotency-Key&quot;</span><span style="color: #E1E4E8">) </span><span style="color: #B392F0">String</span><span style="color: #E1E4E8"> idempotencyKey,</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #F97583">@RequestBody</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">PlaceOrderRequest</span><span style="color: #E1E4E8"> request) {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> checkout.</span><span style="color: #B392F0">placeOrder</span><span style="color: #E1E4E8">(idempotencyKey, request);</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">@ExceptionHandler</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #B392F0">ProblemDetail</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">inProgress</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">RequestInProgressException</span><span style="color: #E1E4E8"> e) {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">ProblemDetail</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">forStatusAndDetail</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">HttpStatus</span><span style="color: #E1E4E8">.</span><span style="color: #79B8FF">CONFLICT</span><span style="color: #E1E4E8">, e.</span><span style="color: #B392F0">getMessage</span><span style="color: #E1E4E8">());</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">@ExceptionHandler</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #B392F0">ProblemDetail</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">reused</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">IdempotencyKeyReusedException</span><span style="color: #E1E4E8"> e) {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">ProblemDetail</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">forStatusAndDetail</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">HttpStatus</span><span style="color: #E1E4E8">.</span><span style="color: #79B8FF">UNPROCESSABLE_CONTENT</span><span style="color: #E1E4E8">, e.</span><span style="color: #B392F0">getMessage</span><span style="color: #E1E4E8">());</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span></code></pre></div>



<p class="wp-block-paragraph">Our MockMvc test confirmed all four: a replay returned 201 with the identical body, and the other cases returned 409, 422 and 400.</p>



<p class="wp-block-paragraph">You pay for it in two places. Every checkout now commits twice, and clients must treat 409 as &#8220;wait and retry&#8221;, not as failure. For anything that charges a card, we think both are cheap.</p>



<p class="wp-block-paragraph">A gap also remains: if the charge succeeds and the transaction on lines 46 to 51 fails to commit, line 53 frees the key and a retry charges again. Payment providers close that gap from their side; Stripe&#8217;s v1 API accepts an <code>Idempotency-Key</code> on every POST, so pass yours through.</p>



<h2 class="wp-block-heading">Fix 3: give every key a fingerprint and an expiry date</h2>



<p class="wp-block-paragraph">Fix 2 has two holes of its own. A client bug can send a new order with an old key, and a server that dies between reserve and complete leaves a row stuck <code>IN_PROGRESS</code>, answering 409 forever.</p>



<p class="wp-block-paragraph">A <em>fingerprint</em> closes the first hole: a SHA-256 hash of the fields that define the order, stored with the key. Line 69 above compares it before anything else. Every Java platform must support SHA-256, according to the <code>MessageDigest</code> Javadoc, so this needs no library. Sending quantity 3 under a key first used for quantity 1 got the 422 and created nothing.</p>



<p class="wp-block-paragraph">For the stuck row, the reservation gets a short <em>lease</em>, a deadline after which a stuck key may be cleared and taken over: one minute here. Completion replaces it with a 24-hour retention, the minimum Stripe documents for its own keys. A scheduled job deletes whatever is past its deadline:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-start:24;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2f363c;color:#d3d7dd">IdempotencyRecordCleaner.java (fix 3)</span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line cbp-line-highlight"><span style="color: #E1E4E8">    </span><span style="color: #F97583">@Scheduled</span><span style="color: #E1E4E8">(fixedDelayString </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">&quot;${checkout.idempotency.cleanup-interval:PT1M}&quot;</span><span style="color: #E1E4E8">)</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">@Transactional</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">int</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">deleteExpired</span><span style="color: #E1E4E8">() {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> records.</span><span style="color: #B392F0">deleteExpired</span><span style="color: #E1E4E8">(clock.</span><span style="color: #B392F0">instant</span><span style="color: #E1E4E8">());</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span></code></pre></div>



<p class="wp-block-paragraph"><code>@Scheduled</code> needs <code>@EnableScheduling</code>, which our application class declares. With a fixed <code>Clock</code> standing in for time, the test simulated a crash right after the reservation:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #e1e4e8">&#91;expiry&#93; stuck key: 409 at +30 s, cleaner removed 0 at +30 s and 1 at +61 s, retry -&gt; order 23, charges=1</span></span>
<span class="line"><span style="color: #e1e4e8">&#91;expiry&#93; replay at +23 h -&gt; same order 24; after the cleaner at +25 h -&gt; new order 25, orders=2</span></span></code></pre></div>



<p class="wp-block-paragraph">The second line is the price of expiry, and it is the same deal Stripe offers: after retention, a reused key is a new request. Pick a lease longer than your slowest successful checkout. A shorter one lets the cleaner delete a key that is still being worked on, a retry reserves it again, and you are back to two charges.</p>



<h2 class="wp-block-heading">Which fix should your checkout ship?</h2>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th></th><th>Fix 1: unique constraint</th><th>Fix 2: reserve first</th><th>Fix 3: fingerprint and expiry</th></tr></thead><tbody><tr><td>Orders, 20 keys x 8 copies</td><td>20</td><td>20</td><td>(adds to fix 2)</td></tr><tr><td>Charges, same load</td><td>82</td><td>20</td><td>(adds to fix 2)</td></tr><tr><td>Retry during the first attempt</td><td>Both do the work; the loser returns the winner&#8217;s order</td><td>409, retry later</td><td>409 until the lease ends after a crash</td></tr><tr><td>Same key, different body</td><td>Not detected</td><td>Not detected without fix 3</td><td>422</td></tr><tr><td>Extra cost</td><td>One unique index</td><td>A second commit per request</td><td>A scheduled delete</td></tr><tr><td>Choose it when</td><td>All the work is in one database transaction</td><td>The request calls anything outside the database</td><td>Always, together with fix 2</td></tr></tbody></table></figure>



<p class="wp-block-paragraph">Our default: fix 2 and fix 3 together for any POST that moves money or sends something to the outside world. Keep fix 1 alone for the endpoints the table&#8217;s last row describes, where a rollback really does undo everything.</p>



<p class="wp-block-paragraph">What we&#8217;d avoid: don&#8217;t guard with <code>synchronized</code> or an in-memory map of keys; they work until the second pod starts. Don&#8217;t check with <code>existsBy...</code> and insert later, which is the bug in a different spelling. And don&#8217;t write an idempotency filter that only stores responses after the handler returns, since that stores the answer after the second request has already started.</p>



<h2 class="wp-block-heading">What changes with ten pods and aggressive retries?</h2>



<p class="wp-block-paragraph">More pods change nothing for fixes 1 to 3, because the arbiter is the unique index, not any JVM. The idempotency table does become one of the service&#8217;s busiest: one insert, one update and one delete per checkout. Keep it narrow, and keep its index on the key alone. The cleaner runs on every pod, which is harmless: a second delete of the same expired rows finds nothing.</p>



<p class="wp-block-paragraph">Aggressive retries change the mix of answers. A client that retries faster than the payment call returns turns one checkout into a burst of duplicate requests, where one does the work and the rest get 409 or a replay. Give clients a backoff and a cap. This is the same in-flight problem as a cache miss, where we shared one load per key in <a href="https://www.gradientco.net/engineering/spring-boot-cache-stampede/">Spring Boot cache stampede: 50 requests, 50 identical database queries</a>.</p>



<h3 class="wp-block-heading">Which number would have warned you?</h3>



<p class="wp-block-paragraph">With the buggy code, the error log had it: <code>IncorrectResultSizeDataAccessException</code> on the key lookup means duplicates already got through. Count them with a <code>GROUP BY idempotency_key HAVING count(*) &gt; 1</code> query on the key table.</p>



<p class="wp-block-paragraph">After the fix, watch <code>http.server.requests</code> for <code>uri=/orders</code>, split by its <code>status</code> tag. A rising share of 409s means clients retry faster than you answer, and a nonzero 422 rate means a client is reusing keys. Also alert on <code>IN_PROGRESS</code> rows older than the lease: each one is a request that died between reserving its key and completing it.</p>



<h2 class="wp-block-heading">So how did one key make two orders?</h2>



<p class="wp-block-paragraph">The code asked the database whether the key existed, then acted on that answer after it had gone stale. The fix is to stop asking. Make the insert the question, and let nothing touch a card until it has won.</p>



<h2 class="wp-block-heading">What should you grep for in your own POST endpoints?</h2>



<ul class="wp-block-list">
<li>Grep for <code>findBy...Key</code>, <code>existsBy...</code> or <code>containsKey</code> followed by a <code>save</code> or <code>put</code> on the same key.</li>



<li>Check the key column for a unique constraint in the real schema, not only in the entity.</li>



<li>List every side effect between reading the key and committing: payment calls, emails, <code>KafkaTemplate.send</code>.</li>



<li>Find <code>synchronized</code>, <code>ReentrantLock</code> or <code>ConcurrentHashMap</code> guarding anything that runs on more than one pod.</li>



<li>Ask what happens to a key when the server dies mid-request, and how long keys live.</li>
</ul>



<h2 class="wp-block-heading">Frequently asked questions</h2>


<div id="rank-math-faq" class="rank-math-block">
<div class="rank-math-list ">
<div id="faq-question-1" class="rank-math-list-item">
<h3 class="rank-math-question ">Should the client or the server generate the Idempotency-Key?</h3>
<div class="rank-math-answer ">

<p>The client, once per logical operation, before the first attempt, and it reuses the same key for every retry. Stripe suggests random V4 UUIDs and allows keys of up to 255 characters; avoid personal data such as email addresses as keys.</p>

</div>
</div>
<div id="faq-question-2" class="rank-math-list-item">
<h3 class="rank-math-question ">Can I use INSERT &#8230; ON CONFLICT DO NOTHING instead of catching the exception?</h3>
<div class="rank-math-answer ">

<p>Yes, on PostgreSQL. <code>ON CONFLICT DO NOTHING</code> skips the duplicate row, and <code>RETURNING</code> only returns rows that were actually inserted, so an empty result tells you another request owns the key, with no exception to catch.</p>

</div>
</div>
<div id="faq-question-3" class="rank-math-list-item">
<h3 class="rank-math-question ">Should a replay return the original status code, even an error?</h3>
<div class="rank-math-answer ">

<p>Stripe does for its v1 API: it saves the status code and body of the first request once execution begins, and returns them on retries, including 500 errors. We freed the key on failure instead, so a retry after a failed charge can try again. Pick one rule and document it.</p>

</div>
</div>
<div id="faq-question-4" class="rank-math-list-item">
<h3 class="rank-math-question ">Is PUT or DELETE safe without an Idempotency-Key header?</h3>
<div class="rank-math-answer ">

<p>By definition, mostly. RFC 9110 defines PUT and DELETE as idempotent, so repeating them should leave the same state. Repeating a PUT can still trigger side effects twice, such as a webhook, so the same reserve-first pattern applies to those.</p>

</div>
</div>
</div>
</div>


<h2 class="wp-block-heading">What we do not know yet</h2>



<p class="wp-block-paragraph">We ran every test in one JVM on a 4-core cloud build machine in November 2026, against H2 2.4.240; the load counts change from run to run. &#8220;Two servers&#8221; were two service instances sharing one database, not two processes. We did not run the idempotency key Spring Boot code on PostgreSQL or MySQL, so the PostgreSQL waiting behaviour rests on its manual, and H2&#8217;s documentation does not say how it handles a concurrent duplicate insert. The Idempotency-Key header is still an IETF draft (version 08, updated 22 October 2026), so its status codes may change.</p>



<p class="wp-block-paragraph"><strong>Read next:</strong> the same lookup-then-write gap, applied to stock instead of keys, in <a href="https://www.gradientco.net/engineering/spring-boot-race-condition/">The Spring Boot race condition that sells your last unit twice</a>.</p>



<p class="wp-block-paragraph"><em>Researched and drafted with AI assistance; facts checked against the sources linked in this article.</em></p>



<h2 class="wp-block-heading">References</h2>



<ol class="wp-block-list">
<li>RFC 9110: HTTP Semantics, section 9.2.2 Idempotent Methods, IETF, June 2022: <a href="https://www.rfc-editor.org/rfc/rfc9110.html" target="_blank" rel="noopener">rfc-editor.org</a></li>



<li>The Idempotency-Key HTTP Header Field, draft-ietf-httpapi-idempotency-key-header-08, IETF HTTPAPI working group, 22 October 2026: <a href="https://datatracker.ietf.org/doc/draft-ietf-httpapi-idempotency-key-header/" target="_blank" rel="noopener">datatracker.ietf.org</a></li>



<li>Repository query return types, Spring Data JPA reference, 4.1: <a href="https://docs.spring.io/spring-data/jpa/reference/repositories/query-return-types-reference.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>DataIntegrityViolationException, Spring Framework 7.0 Javadoc: <a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/dao/DataIntegrityViolationException.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>TransactionTemplate, Spring Framework 7.0 Javadoc: <a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/support/TransactionTemplate.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>Programmatic Transaction Management, Spring Framework reference, 7.0: <a href="https://docs.spring.io/spring-framework/reference/data-access/transaction/programmatic.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>RequestHeader (required attribute), Spring Framework 7.0 Javadoc: <a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestHeader.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>DefaultHandlerExceptionResolver, Spring Framework 7.0 Javadoc: <a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>Task Execution and Scheduling, Spring Framework reference, 7.0: <a href="https://docs.spring.io/spring-framework/reference/integration/scheduling.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>Observability Support (HTTP server instrumentation), Spring Framework reference, 7.0: <a href="https://docs.spring.io/spring-framework/reference/integration/observability.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>Metrics, Spring Boot reference, 4.1: <a href="https://docs.spring.io/spring-boot/reference/actuator/metrics.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>MessageDigest, Java SE 25 API documentation: <a href="https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/security/MessageDigest.html" target="_blank" rel="noopener">docs.oracle.com</a></li>



<li>Index Uniqueness Checks, PostgreSQL 18 documentation: <a href="https://www.postgresql.org/docs/current/index-unique-checks.html" target="_blank" rel="noopener">postgresql.org</a></li>



<li>INSERT (ON CONFLICT), PostgreSQL 18 documentation: <a href="https://www.postgresql.org/docs/current/sql-insert.html" target="_blank" rel="noopener">postgresql.org</a></li>



<li>Advanced (transaction isolation and locking), H2 Database documentation: <a href="https://www.h2database.com/html/advanced.html" target="_blank" rel="noopener">h2database.com</a></li>



<li>Module ngx_http_proxy_module (proxy_next_upstream), NGINX documentation: <a href="https://nginx.org/en/docs/http/ngx_http_proxy_module.html" target="_blank" rel="noopener">nginx.org</a></li>



<li>Idempotent requests, Stripe API reference: <a href="https://docs.stripe.com/api/idempotent_requests" target="_blank" rel="noopener">docs.stripe.com</a></li>
</ol>
]]></content:encoded>
					
		
		<enclosure url="https://www.gradientco.net/wp-content/uploads/2026/11/idempotency-key-spring-boot-clip.mp4" length="235562" type="video/mp4" />

			</item>
		<item>
		<title>Snowfall in Pakistan 2026-27: Where to See Snow and Which Roads Stay Open</title>
		<link>https://www.gradientco.net/history-culture-travel/snowfall-in-pakistan/</link>
		
		<dc:creator><![CDATA[Muhammad Rehan]]></dc:creator>
		<pubDate>Thu, 05 Nov 2026 02:13:08 +0000</pubDate>
				<category><![CDATA[History, Culture & Travel]]></category>
		<category><![CDATA[pakistan]]></category>
		<category><![CDATA[Travel]]></category>
		<category><![CDATA[Traveling]]></category>
		<guid isPermaLink="false">https://www.gradientco.net/?p=10220</guid>

					<description><![CDATA[Murree and Malam Jabba are the most reliable snow trips from Islamabad, usually from late December. Naran and Babusar close for winter. Here is where to go and how to travel safely.]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Murree and the Galiyat are the easiest snow trips from Islamabad and Lahore, and Malam Jabba in Swat is the best bet for skiing. Snowfall in Pakistan&#8217;s hill stations usually arrives from late December and peaks in January and February, but no date is guaranteed. Naran, Kaghan and the Babusar Pass close to tourists for winter, so plan on Murree, Swat or a flight north instead.</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Destination</th><th>When snow usually comes</th><th>Winter access</th></tr></thead><tbody><tr><td>Murree and Galiyat</td><td>Late December to February; coldest nights in January</td><td>Open, but entry is stopped during heavy storms</td></tr><tr><td>Malam Jabba, Swat</td><td>Ski slopes opened on 13 December in 2024</td><td>Open in season; ski resort at 2,804 metres</td></tr><tr><td>Kalam, upper Swat</td><td>Snow that ended the dry spell came on 21 December in 2025</td><td>No official winter road status found; check before leaving</td></tr><tr><td>Naran and Kaghan</td><td>From November</td><td>Closed beyond Kaghan town until spring</td></tr><tr><td>Babusar Pass</td><td>From November</td><td>Usually closed November to June</td></tr><tr><td>Hunza and Skardu</td><td>Drier than the hills; heavy snow came in mid to late January in 2026</td><td>Open via the Karakoram Highway; flights are weather-dependent</td></tr><tr><td>Neelum Valley</td><td>First snow came on 12 November in 2025</td><td>Met Department warned of icy roads higher up</td></tr></tbody></table></figure>



<h2 class="wp-block-heading">When does it snow in Murree?</h2>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-graphic-1.webp" alt="Murree&apos;s average night lows: 1.3°C in December, -1.0°C in January, -0.4°C in February. January to March bring 479.6 mm of rain and snow." class="wp-image-10222" srcset="https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-graphic-1.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-graphic-1-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-graphic-1-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-graphic-1-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">January and February are the only months when Murree&#8217;s average night falls below freezing. Source: WMO, Murree station, PMD data for 1981-2010.</figcaption></figure>



<p class="wp-block-paragraph"><strong>January and February are the surest months.</strong> In the <a href="https://worldweather.wmo.int/en/city.html?cityId=900" target="_blank" rel="noopener">World Meteorological Organization&#8217;s climate data for Murree</a>, supplied by the Pakistan Meteorological Department (PMD) for 1981-2010, they are the only months when the average night drops below freezing: -1.0°C in January and -0.4°C in February. December nights average 1.3°C, so early snow is possible but less dependable.</p>



<p class="wp-block-paragraph">Late winter is wet too: January to March bring 479.6 mm of precipitation on average, some of it as snow. On 23 January 2026, up to 12 inches of Murree snowfall was recorded in 24 hours, <a href="https://english.aaj.tv/news/330451378" target="_blank" rel="noopener">Aaj News reported</a>.</p>



<p class="wp-block-paragraph">Punjab had prepared for it. In December 2025 it opened 19 facilitation centres on the routes into Murree, running 24 hours a day, and deployed 26 snow-removal vehicles, <a href="https://propakistani.pk/2025/12/16/murree-completes-arrangements-for-snowfall-season/" target="_blank" rel="noopener">ProPakistani reported</a>.</p>



<p class="wp-block-paragraph">The same storms usually reach the neighbouring Galiyat. PMD forecasts tend to name &#8220;Murree and Galliyat&#8221; together, as in its <a href="https://www.app.com.pk/?p=1136954" target="_blank" rel="noopener">late-December 2025 outlook carried by APP</a>.</p>



<h2 class="wp-block-heading">Why can Murree close during a snowstorm?</h2>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-graphic-2.webp" alt="Murree in January 2022: over 162,000 vehicles in five days, 16 hours of heavy snow, about 22 tourists dead. In January 2026, nearly 5,000 vehicles were moved out." class="wp-image-10223" srcset="https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-graphic-2.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-graphic-2-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-graphic-2-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-graphic-2-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Murree&#8217;s winter closures follow the lesson of the 2022 storm. Sources: Business Recorder, Reuters via Arab News, Aaj News.</figcaption></figure>



<p class="wp-block-paragraph">The road closes when too many cars meet too much snow. During the January 2026 storm, traffic police suspended all traffic from the Phulgran Toll Plaza to Murree, every entry route was closed, and the administration worked to get nearly 5,000 vehicles out of the area, according to the same Aaj report.</p>



<p class="wp-block-paragraph">Those closures follow a hard lesson from January 2022, when about 22 tourists died in vehicles stuck in Murree after 16 hours of heavy snow. The initial inquiry named carbon monoxide as the major cause, <a href="https://www.brecorder.com/news/40145908/probe-points-the-finger-at-carbon-monoxide-bureaucracy" target="_blank" rel="noopener">Business Recorder reported</a>. More than 162,000 vehicles had entered the town in the five days before. The final report also blamed slow snow-clearing and missing public warnings, <a href="https://www.arabnews.pk/node/2009271" target="_blank" rel="noopener">Reuters reported via Arab News</a>.</p>



<p class="wp-block-paragraph"><strong>If entry is stopped, turn back rather than look for a side road.</strong> After 2022, we&#8217;d take every closure at face value and wait until the police or the National Highways and Motorway Police (NHMP) say the road is open again.</p>



<h2 class="wp-block-heading">Is Malam Jabba good for snow and skiing?</h2>



<p class="wp-block-paragraph">Yes. Malam Jabba, in upper Swat, sits at 2,804 metres and has two ski tracks of over 800 metres, <a href="https://english.aaj.tv/news/330449763" target="_blank" rel="noopener">Aaj News reported</a> in December 2025. You can ski, snowboard and try ice hockey and curling there.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="900" src="https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-inline-2.webp" alt="Malam Jabba snow: the ski resort above snowy slopes and pines in upper Swat" class="wp-image-10224" srcset="https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-inline-2.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-inline-2-300x225.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-inline-2-1024x768.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-inline-2-768x576.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Malam Jabba sits at 2,804 metres. It has two ski tracks of over 800 metres. Photo: <a href="https://commons.wikimedia.org/wiki/File%3AMalam_Jabba_Ski_Resort.jpg" target="_blank" rel="noopener">Malam Jabba Ski Resort</a> by <a href="https://commons.wikimedia.org/wiki/File%3AMalam_Jabba_Ski_Resort.jpg" target="_blank" rel="noopener">Waqas.usman</a>, <a href="https://creativecommons.org/publicdomain/mark/1.0/" target="_blank" rel="noopener">Public domain</a></figcaption></figure>



<p class="wp-block-paragraph">The resort opened its ski slopes on 13 December in the 2024 season, <a href="https://english.aaj.tv/news/amp/330392833" target="_blank" rel="noopener">according to Aaj</a>. Treat that as a rough guide to Malam Jabba snow, not a fixed date: in 2025, the snowfall that ended a long dry spell in upper Swat arrived only on 21 December.</p>



<p class="wp-block-paragraph">Further up the valley, Kalam, Miandam, Matiltan, Utror and Gabral also draw snow visitors. Tourism Police are posted at key spots in upper Swat during the season. If you want snow without the Murree crowds, we think Swat is the better weekend choice, as long as you set off in daylight.</p>



<h2 class="wp-block-heading">Can you visit Naran and Kaghan in winter?</h2>



<p class="wp-block-paragraph">Only as far as Kaghan town. On 9 December 2025, the Mansehra deputy commissioner shut the road beyond Kaghan, and Naran was declared closed to tourists until spring, <a href="https://en.dailypakistan.com.pk/09-Dec-2025/these-popular-tourist-spots-in-pakistan-are-now-closed-due-to-heavy-snowfall" target="_blank" rel="noopener">Daily Pakistan reported</a>.</p>



<p class="wp-block-paragraph">The Babusar Pass above Naran is <strong>usually closed to vehicles from November to June</strong>, Diamer&#8217;s deputy commissioner said in October 2023, <a href="https://propakistani.pk/2023/10/16/babusar-top-closed-sooner-than-expected-due-to-heavy-snowfall/" target="_blank" rel="noopener">ProPakistani reported</a>. In 2025, the Babusar-Naran route closed on 30 November &#8220;until further orders&#8221;, <a href="https://www.thenews.pk/story/1383670-babusar-naran-route-closed-after-snow-turns-roads-risky" target="_blank" rel="noopener">The News reported</a>.</p>



<p class="wp-block-paragraph">With Babusar closed, traffic to Gilgit-Baltistan goes via the Karakoram Highway. The 2023 report puts the Diamer to Mansehra drive at about 14 hours that way, against seven over the pass. We&#8217;d only make that winter drive with spare days in hand. For Naran at its best, our <a href="https://www.gradientco.net/history-culture-travel/kaghan-expedition/">Kaghan Valley expedition</a> covers the summer route to Lulusar Lake.</p>



<h2 class="wp-block-heading">How much snowfall in Pakistan reaches the far north?</h2>



<p class="wp-block-paragraph">Hunza and Skardu get snow, but the far north is much drier than many visitors expect. Gilgit, the nearest station in the WMO&#8217;s records, averages only 17.0 mm of precipitation across December, January and February, in the <a href="https://worldweather.wmo.int/en/city.html?cityId=897" target="_blank" rel="noopener">WMO data for Gilgit</a>. Its nights are colder than Murree&#8217;s, though, at -2.6°C in January. If you&#8217;re choosing where to see snow in Pakistan in December, that makes Murree and Swat surer bets than the far north.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-graphic-3.webp" alt="Murree or Gilgit-Baltistan. January nights: -1.0°C against -2.6°C in Gilgit. Precipitation: 479.6 mm in Murree from January to March, 17.0 mm in Gilgit from December to February. Last winter&apos;s biggest snow: up to 12 inches in Murree, 3 to 5 inches in Skardu." class="wp-image-10225" srcset="https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-graphic-3.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-graphic-3-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-graphic-3-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-graphic-3-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">The far north is colder but much drier. Sources: WMO data for Murree and Gilgit (PMD, 1981-2010), Aaj News, ARY News.</figcaption></figure>



<p class="wp-block-paragraph">The heavy snow reports we found for last winter date from mid to late January. Skardu was under three to five inches of snow on 17 January 2026, <a href="https://arynews.tv/snowfall-gb-skardu-receives-five-inches-snow" target="_blank" rel="noopener">ARY News reported</a>. On 23 January, the <a href="https://visitgilgitbaltistan.gov.pk/pages/news/66" target="_blank" rel="noopener">Gilgit-Baltistan Tourism Department warned</a> of possible road closures in Hunza, Skardu and seven other districts, and asked tourists to carry snow chains and postpone non-essential travel. If you&#8217;re flying, allow spare days: winter flights to the north depend on the weather, as our <a href="https://www.gradientco.net/history-culture-travel/hunza-in-autumn/">guide to Hunza in autumn</a> explains.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="960" src="https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-inline-3.webp" alt="Snow-capped mountains above the lake at Shangrila Resort near Skardu in winter" class="wp-image-10226" srcset="https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-inline-3.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-inline-3-300x240.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-inline-3-1024x819.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/11/snowfall-in-pakistan-inline-3-768x614.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Shangrila, near Skardu, in winter. The Gilgit-Baltistan Tourism Department asks winter visitors to carry snow chains. Photo: <a href="https://commons.wikimedia.org/wiki/File%3AShangrila_Resort_-_Skardu_%284462621164%29.jpg" target="_blank" rel="noopener">Shangrila Resort &#8211; Skardu (4462621164)</a> by <a href="https://commons.wikimedia.org/wiki/File%3AShangrila_Resort_-_Skardu_%284462621164%29.jpg" target="_blank" rel="noopener">Muhammad Imran Saeed from City of London, Canada</a>, <a href="https://creativecommons.org/licenses/by/2.0" target="_blank" rel="noopener">CC BY 2.0</a></figcaption></figure>



<p class="wp-block-paragraph">The Neelum Valley in Azad Kashmir got snow much earlier: its first of the 2025 season came by 12 November, and tourists started arriving soon after, <a href="https://dailytimes.com.pk/1399186/neelum-valley-experiences-first-winter-snow-amid-reconstruction-challenges/" target="_blank" rel="noopener">Daily Times reported</a>. The Met Department warned of icy roads and limited access in the higher areas.</p>



<h2 class="wp-block-heading">How do you check weather and road alerts before you leave?</h2>



<p class="wp-block-paragraph">Snowfall in Pakistan can block a mountain road within hours, so check official sources on the morning you travel, not last week&#8217;s social media.</p>



<ol class="wp-block-list">
<li>Read the PMD&#8217;s <a href="https://weather.gov.pk/nwfc/press-releases" target="_blank" rel="noopener">press releases</a> and <a href="https://weather.gov.pk/nwfc/daily-forecast" target="_blank" rel="noopener">daily forecast</a>. Its press releases name the districts where snow is expected and warn of road closures.</li>

<li>Check the National Disaster Management Authority&#8217;s <a href="https://www.ndma.gov.pk/" target="_blank" rel="noopener">advisories page</a>, or call its helpline on 051-111-157-157.</li>

<li>For Murree, follow the Murree Traffic Police and call the Traffic Control Room on 051-9269200. For motorways and the expressway, call the NHMP helpline on 130.</li>

<li>In Punjab, the <a href="https://pdma.punjab.gov.pk/" target="_blank" rel="noopener">PDMA Punjab</a> helpline is 1129. For any emergency, call Rescue 1122.</li>

<li>If an advisory asks you to avoid non-essential travel, postpone. On 23 January 2026, the PDMA gave Murree tourists exactly that advice, as the Aaj report on the storm noted.</li>
</ol>



<h2 class="wp-block-heading">Carry snow chains, and know what to do if you&#8217;re stuck</h2>



<p class="wp-block-paragraph">Chains are the one item you can&#8217;t skip. In January 2026, Murree police said no vehicle would be allowed into Murree without them, <a href="https://www.app.com.pk/?p=1151418" target="_blank" rel="noopener">APP reported</a>.</p>



<ul class="wp-block-list">
<li><strong>Before you leave:</strong> fill the fuel tank and check brakes, wipers, heater and defogger. A four-wheel-drive car is better on snowy roads.</li>

<li><strong>In the car:</strong> carry chains, blankets, warm clothes, dry food and water.</li>

<li><strong>On the road:</strong> drive slowly, leave at least 50 metres to the car ahead, and travel in daylight. Black ice is transparent and very slippery.</li>

<li><strong>Don&#8217;t stop on the road</strong> for photos or park on slopes; it blocks the snow ploughs.</li>

<li><strong>If you&#8217;re stuck with the heater on:</strong> keep a window slightly open. Carbon monoxide has no smell, and it killed most of the 2022 victims. The <a href="https://www.ndma.gov.pk/storage/guidelines/February2025/pmqjfZlRLSdKmr0UICp9.pdf" target="_blank" rel="noopener">NDMA&#8217;s snowfall guidelines</a> give the same warning for stoves indoors.</li>
</ul>



<h2 class="wp-block-heading">Frequently asked questions</h2>


<div id="rank-math-faq" class="rank-math-block">
<div class="rank-math-list ">
<div id="faq-question-1" class="rank-math-list-item">
<h3 class="rank-math-question ">When is the best time to see snowfall in Pakistan?</h3>
<div class="rank-math-answer ">

<p>For hill stations near Islamabad, mid-January to mid-February. Those weeks sit in the middle of Murree&#8217;s only two months with sub-zero average nights.</p>

</div>
</div>
<div id="faq-question-2" class="rank-math-list-item">
<h3 class="rank-math-question ">Is Murree open to tourists in winter?</h3>
<div class="rank-math-answer ">

<p>Yes, most days. The administration stops entry only during heavy storms.</p>

</div>
</div>
<div id="faq-question-3" class="rank-math-list-item">
<h3 class="rank-math-question ">Is Babusar Top open in December?</h3>
<div class="rank-math-answer ">

<p>No. The pass usually stays closed to vehicles until June, and Naran is closed to tourists beyond Kaghan town until spring.</p>

</div>
</div>
<div id="faq-question-4" class="rank-math-list-item">
<h3 class="rank-math-question ">Do I need snow chains for Murree?</h3>
<div class="rank-math-answer ">

<p>Yes, during snowfall: police turn away cars without them. Keep them in the boot for the far north too: the Gilgit-Baltistan Tourism Department lists them as essential winter kit.</p>

</div>
</div>
<div id="faq-question-5" class="rank-math-list-item">
<h3 class="rank-math-question ">Can I see snow in Skardu in December?</h3>
<div class="rank-math-answer ">

<p>Not reliably. Gilgit, the nearest long-term station, averages only 6.3 mm of precipitation in December, which makes December a gamble.</p>

</div>
</div>
</div>
</div>


<h2 class="wp-block-heading">What we do not know yet</h2>



<p class="wp-block-paragraph">PMD had not published a 2026-27 winter outlook as of 1 November 2026, so we cannot say whether this season will be early, late or light. We could not confirm Malam Jabba&#8217;s 2026-27 opening date or ticket prices, official winter road status for Kalam, the upper Neelum Valley or Deosai, or winter hotel prices, so we have left costs out. We also could not open Dawn&#8217;s reports on Murree and Gilgit-Baltistan (a bot check blocked them), so we could not confirm the usual start of snow in upper Hunza.</p>



<p class="wp-block-paragraph">Read next: <a href="https://www.gradientco.net/history-culture-travel/hunza-in-autumn/">Hunza in autumn</a>, for the last weeks before the northern roads turn wintry.</p>



<p class="wp-block-paragraph"><em>Researched and drafted with AI assistance; facts checked against the sources linked in this article.</em></p>



<p class="has-small-font-size wp-block-paragraph">Featured image: <a href="https://commons.wikimedia.org/wiki/File%3AMurre_snow_-_panoramio.jpg" target="_blank" rel="noopener">Murre snow &#8211; panoramio</a> by <a href="https://commons.wikimedia.org/wiki/File%3AMurre_snow_-_panoramio.jpg" target="_blank" rel="noopener">TaLha Hussain (BAIG)</a>, <a href="https://creativecommons.org/licenses/by/3.0" target="_blank" rel="noopener">CC BY 3.0</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>5G in Pakistan: Which Cities Have It, Which Phones Work and What It Costs</title>
		<link>https://www.gradientco.net/tech-science/5g-in-pakistan/</link>
		
		<dc:creator><![CDATA[Nabeel Ahmed]]></dc:creator>
		<pubDate>Thu, 05 Nov 2026 02:13:03 +0000</pubDate>
				<category><![CDATA[Tech & Science]]></category>
		<category><![CDATA[technology]]></category>
		<guid isPermaLink="false">https://www.gradientco.net/?p=10204</guid>

					<description><![CDATA[5G works in parts of 24 Pakistani cities as of October 2026, mostly on Jazz and Zong. Your 4G SIM still works, but you need a 5G phone. Here is where it works, which phones qualify and how to turn it on.]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">5G in Pakistan is live in parts of 24 cities as of late October 2026, on 1,782 network sites run by Jazz, Zong and Ufone, according to figures from the IT ministry. To use it you need a 5G-capable phone and a spot inside a covered area. <strong>Your current 4G SIM still works</strong>, and Zong says its 4G packages carry over. The government has told the National Assembly that nationwide coverage could take about two more years.</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>5G at a glance (October 2026)</th><th>What the sources say</th></tr></thead><tbody><tr><td>Spectrum auction</td><td>10 March 2026: 480 MHz sold for about $507 million</td></tr><tr><td>Licences and launch</td><td>Licences signed 19 March; PTA authorised 5G on 20 March</td></tr><tr><td>Live sites</td><td>1,782 across 24 cities: Jazz 1,015, Zong 640, Ufone 127</td></tr><tr><td>Phones</td><td>A 5G handset is required; iPhone 12 or later works on Zong and Jazz</td></tr><tr><td>SIM and package</td><td>No new SIM needed; 4G packages work on 5G, Zong says</td></tr><tr><td>Cheapest 5G phone</td><td>About Rs36,000, the ministry says</td></tr><tr><td>Nationwide coverage</td><td>About two more years, the government told parliament</td></tr></tbody></table></figure>



<h2 class="wp-block-heading">Where is 5G available in Pakistan right now?</h2>



<p class="wp-block-paragraph"><strong>Signal reaches only parts of 24 cities, not whole cities.</strong> Minister for IT and Telecommunication Shaza Fatima Khawaja listed them in figures reported on 26 October 2026 by <a href="https://www.samenacouncil.org/samena_daily_news?news=113002" target="_blank" rel="noopener">Business Recorder, via SAMENA Council</a>: Islamabad, Karachi, Lahore, Rawalpindi, Faisalabad, Peshawar, Quetta, Multan, Gujranwala, Sialkot, Sheikhupura, Hyderabad, Abbottabad, Attock, Jhelum, Kasur, Kohat, Mardan, Rahim Yar Khan, Sahiwal, Sukkur, Swabi, Chitral and Gujrat.</p>



<p class="wp-block-paragraph">The big three cities hold more than half the sites. Karachi has 385, Lahore 373 and Islamabad 276, followed by Faisalabad with 144 and Peshawar with 122. A city on the list may still have no 5G in your neighbourhood, so check the operator&#8217;s map rather than the city name.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="645" src="https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-inline-1-2.webp" alt="Karachi&apos;s Clifton skyline of high-rise towers seen from above the city" class="wp-image-10214" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-inline-1-2.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-inline-1-2-300x161.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-inline-1-2-1024x550.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-inline-1-2-768x413.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Karachi has 385 5G sites, the most of any city in the IT ministry&#8217;s October 2026 count, but not every neighbourhood is covered. Photo: <a href="https://commons.wikimedia.org/wiki/File%3AClifton_Karachi_View.jpg" target="_blank" rel="noopener">Clifton Karachi View</a> by <a href="https://commons.wikimedia.org/wiki/File%3AClifton_Karachi_View.jpg" target="_blank" rel="noopener">Ktownboy103</a>, <a href="http://creativecommons.org/publicdomain/zero/1.0/deed.en" target="_blank" rel="noopener">CC0</a></figcaption></figure>



<p class="wp-block-paragraph">Your operator decides what you get. The same ministry figures show Jazz leading in Karachi, Lahore, Faisalabad and Multan, while Zong is stronger in Peshawar, Quetta, Sheikhupura and Sialkot. <a href="https://www.techjuice.pk/?p=259284" target="_blank" rel="noopener">TechJuice</a> carries the same breakdown.</p>



<h3 class="wp-block-heading">Jazz, Zong and Ufone compared</h3>



<ul class="wp-block-list">
<li><strong>Jazz 5G</strong> has the most sites, 1,015. It launched on 20 March 2026 with about 180 sites in Islamabad, the provincial capitals, Multan and Faisalabad, <a href="https://www.arabnews.pk/node/2637001" target="_blank" rel="noopener">Arab News reported</a>. Its own pages blocked us, so we couldn&#8217;t read its current map.</li>

<li><strong>Zong 5G</strong> launched a day earlier, on 19 March, in &#8220;more than 16 cities&#8221;, according to <a href="https://www.arabnews.pk/node/2636907" target="_blank" rel="noopener">Arab News</a>. The minister&#8217;s count gives Zong 640 sites, but Zong&#8217;s own <a href="https://www.zong.com.pk/5g-coverage-map" target="_blank" rel="noopener">coverage map</a> showed 760 active sites when we read it on 31 October.</li>

<li><strong>Ufone 5G</strong> is the hardest to pin down. The ministry credits it with 127 sites. <a href="https://propakistani.pk/2026/06/30/ufone-5g-starts-appearing-in-islamabad-with-over-300mb-speeds/" target="_blank" rel="noopener">ProPakistani found its signal</a> in Islamabad&#8217;s F and G sectors and Blue Area in June, yet Ufone has published no coverage map we could find.</li>
</ul>



<h2 class="wp-block-heading">Does your phone support 5G?</h2>



<p class="wp-block-paragraph">Only a phone with a 5G modem can connect, and it also needs your operator&#8217;s approval. <strong>Every iPhone from the iPhone 12 onward supports 5G</strong>, Apple says on its <a href="https://support.apple.com/108383" target="_blank" rel="noopener">5G support page</a>, as long as the carrier supports it.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="900" src="https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-inline-2-2.webp" alt="An iPhone 17 Pro lying face down on a wooden table, showing its three rear cameras" class="wp-image-10215" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-inline-2-2.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-inline-2-2-300x225.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-inline-2-2-1024x768.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-inline-2-2-768x576.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">The iPhone 17 series is on Zong&#8217;s list of certified 5G handsets; every iPhone from the iPhone 12 onward supports 5G. Photo: <a href="https://commons.wikimedia.org/wiki/File%3AIPhone_17_Pro_%282025-12-28%29.jpg" target="_blank" rel="noopener">IPhone 17 Pro (2025-12-28)</a> by <a href="https://commons.wikimedia.org/wiki/File%3AIPhone_17_Pro_%282025-12-28%29.jpg" target="_blank" rel="noopener">Olgierd Rudak</a>, <a href="http://creativecommons.org/publicdomain/zero/1.0/deed.en" target="_blank" rel="noopener">CC0</a></figcaption></figure>



<p class="wp-block-paragraph">Zong&#8217;s <a href="https://www.zong.com.pk/vas/compatible-5g-smartphones--handsets" target="_blank" rel="noopener">list of certified handsets</a> covers the iPhone 12 to 17 series and iPhone Air, plus budget Android models such as the Infinix HOT 50 5G, Tecno SPARK 30 5G, Redmi Note 14 5G, Samsung Galaxy A26 5G and OPPO A5 5G. A &#8220;5G&#8221; in the model name is the quickest clue, but check the list for your exact model before you assume.</p>



<p class="wp-block-paragraph">On Jazz, iPhones gained 5G support in May 2026 once users updated to the latest iOS, <a href="https://propakistani.pk/2026/05/12/iphones-now-compatible-with-jazz-5g-as-pakistan-wide-rollout-progresses/" target="_blank" rel="noopener">ProPakistani reported</a>. Ufone is the exception: in spring 2026 it hadn&#8217;t announced official 5G support for Apple devices, <a href="https://www.techjuice.pk/iphone-5g-is-officially-live-in-pakistan-on-zong-jazz-networks/" target="_blank" rel="noopener">TechJuice reported</a>, and we found no later news.</p>



<p class="wp-block-paragraph">The cheapest locally available 5G phone costs about Rs36,000, the ministry says, though it did not name the model. Plenty of 5G phones are already in use: a PTA document reported in July counted 3.81 million registered 5G-compatible devices, <a href="https://propakistani.pk/2026/07/15/pta-says-pakistan-now-has-3-81-million-5g-ready-smartphones/" target="_blank" rel="noopener">ProPakistani said</a>. If you use an OPPO phone, our <a href="https://www.gradientco.net/tech-science/coloros-17-update/">ColorOS 17 update guide</a> shows which Reno 5G models get the next software update.</p>



<h2 class="wp-block-heading">How do you switch 5G on and check it is working?</h2>



<p class="wp-block-paragraph">Most phones need one setting changed and up-to-date software. Zong&#8217;s <a href="https://www.zong.com.pk/vas/how-to-enable-5g-on-your-device" target="_blank" rel="noopener">step-by-step guide</a> gives these paths:</p>



<ol class="wp-block-list">
<li>Update your phone to the latest iOS or Android version, as the PTA advises.</li>

<li>On an iPhone, open Settings, then Cellular (or Mobile Data), then Cellular Data Options, then Voice &amp; Data, and choose <strong>5G Auto</strong>.</li>

<li>On a Samsung Android phone, open Settings, then Connections, then Mobile networks, and turn on <strong>Allow 5G service</strong>. Other brands use a similar &#8220;preferred network type&#8221; menu.</li>

<li>Move into a covered area and look at the status bar. A &#8220;5G&#8221; icon means your carrier&#8217;s 5G network is available and the phone can connect over it, Apple says.</li>
</ol>



<p class="wp-block-paragraph">We&#8217;d leave it on 5G Auto. Apple explains that it drops back to LTE when 5G would not make a noticeable difference, which saves battery, while &#8220;5G On&#8221; stays on 5G whenever it can.</p>



<p class="wp-block-paragraph">Still seeing 4G? Zong&#8217;s <a href="https://www.zong.com.pk/5g-faqs" target="_blank" rel="noopener">5G FAQ</a> lists three causes: you are outside coverage, 5G is switched off, or your phone is not compatible. Zong also lists its full set of covered areas in the MyZong app.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-graphic-1-1.webp" alt="Checklist for 5G in Pakistan: a 5G phone (iPhone 12 or later, or a model on your operator&apos;s list); the latest iOS or Android; 5G switched on (5G Auto on iPhone, Allow 5G service on Samsung); a covered area, in parts of 24 cities; and your current SIM, since no new SIM is needed and Zong says 4G packages work" class="wp-image-10216" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-graphic-1-1.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-graphic-1-1-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-graphic-1-1-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-graphic-1-1-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Miss any one of these and the phone stays on 4G. Source: Zong&#8217;s 5G guide and FAQ, Apple Support and IT ministry figures, October 2026.</figcaption></figure>



<h2 class="wp-block-heading">What does 5G cost on Jazz, Zong and Ufone?</h2>



<p class="wp-block-paragraph">On Zong, 5G in Pakistan runs on the package you already have, as of October 2026: its FAQ says &#8220;existing 4G packages will work on 5G networks&#8221;. So the real cost is the handset.</p>



<p class="wp-block-paragraph">We couldn&#8217;t find a 5G-only package with a published price on any of the three operators&#8217; pages. That will probably change as coverage grows. Until then, check your operator&#8217;s app before paying a reseller for anything labelled &#8220;5G&#8221;.</p>



<h2 class="wp-block-heading">What the March 2026 auction sold</h2>



<p class="wp-block-paragraph">The Pakistan Telecommunication Authority (PTA) sold 480 MHz of spectrum on 10 March 2026 for about $507 million, PTA chairman Hafeez-ur-Rehman told <a href="https://www.arabnews.pk/node/2635931/pakistan" target="_blank" rel="noopener">Arab News</a>. Before the sale, Pakistan&#8217;s operators were using about 274 MHz in total.</p>



<p class="wp-block-paragraph">The bands were 700 MHz, 2.3 GHz, 2.6 GHz and 3.5 GHz, according to <a href="https://www.rcrwireless.com/20260311/5g/pakistan-5g-auction" target="_blank" rel="noopener">RCR Wireless</a>, which reports 597 MHz on offer. Jazz won 190 MHz across all four bands. Ufone took 180 MHz including the largest share of the 3.5 GHz band, 120 MHz, and Zong won 110 MHz. Set that against Ufone&#8217;s site count, the smallest of the three, and it&#8217;s the oddest gap in this story.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-graphic-2.webp" alt="Jazz, Zong and Ufone compared: Jazz won 190 MHz and has 1,015 5G sites; Zong won 110 MHz and has 640 sites; Ufone won 180 MHz but has only 127 sites. iPhone 5G works on Jazz since May 2026 and on Zong, but Ufone has not announced it. Zong&apos;s map shows 760 sites, Jazz&apos;s pages were blocked for us and we found no Ufone map" class="wp-image-10217" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-graphic-2.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-graphic-2-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-graphic-2-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-graphic-2-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Spectrum from the March 2026 auction against sites live in October. Sources: Arab News and RCR Wireless (March 2026); IT ministry figures via Business Recorder (October 2026).</figcaption></figure>



<p class="wp-block-paragraph">Licences were signed in Islamabad on 19 March, <a href="https://www.dawn.com/news/1983726" target="_blank" rel="noopener">Dawn reported</a>. A day later the PTA authorised all three operators to start 5G &#8220;in a phased manner&#8221;, <a href="https://www.radio.gov.pk/20-03-2026/pta-formally-authorizes-cmos-to-start-5g-services" target="_blank" rel="noopener">Radio Pakistan said</a>. The same order requires each operator to publish its 5G areas and compatible phones on its website.</p>



<h2 class="wp-block-heading">Why will nationwide 5G in Pakistan take two more years?</h2>



<p class="wp-block-paragraph">Only the first of the rollout&#8217;s four phases is mandatory. Operators had to cover the federal and provincial capitals; everywhere else depends on each operator&#8217;s business plan, the minister&#8217;s figures show, so smaller towns wait for the economics to work.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-graphic-3.webp" alt="5G targets against the October count: the PTA gave operators one year to reach 1,000 sites; Jazz planned 2,500 sites by the end of 2026; the ministry&apos;s October count gives Jazz 1,015; and the government says nationwide 5G could take two more years" class="wp-image-10218" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-graphic-3.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-graphic-3-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-graphic-3-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/5g-in-pakistan-graphic-3-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Jazz has two months to close the gap to its own plan. Sources: Arab News (May 2026); IT ministry figures via Business Recorder; Pakistan &amp; Gulf Economist (October 2026).</figcaption></figure>



<p class="wp-block-paragraph">The government told the National Assembly in late October that nationwide 5G &#8220;could take another two years&#8221;, according to <a href="https://www.pakistangulfeconomist.com/2026/10/26/technology-42-45-26/" target="_blank" rel="noopener">Pakistan &amp; Gulf Economist</a>. Parliamentary Secretary for IT Sabeen Ghouri said work was under way to improve existing connectivity before coverage expands.</p>



<p class="wp-block-paragraph">Targets set the pace too. The PTA gave operators one year to put 5G on 1,000 sites, and Jazz planned 2,500 sites by the end of 2026, its president Kazim Mujtaba told <a href="https://www.arabnews.pk/node/2642623" target="_blank" rel="noopener">Arab News</a> in May. The ministry&#8217;s October count leaves Jazz well short of that plan, with two months to go. The government has also given no firm date for making 5G cheaper for ordinary users.</p>



<h2 class="wp-block-heading">Frequently asked questions</h2>


<div id="rank-math-faq" class="rank-math-block">
<div class="rank-math-list ">
<div id="faq-question-1" class="rank-math-list-item">
<h3 class="rank-math-question ">Is 5G in Pakistan available outside the big cities?</h3>
<div class="rank-math-answer ">

<p>Yes, in a few. The October list includes smaller cities such as Chitral, Swabi, Kohat, Kasur and Rahim Yar Khan, though coverage there is limited to some sites.</p>

</div>
</div>
<div id="faq-question-2" class="rank-math-list-item">
<h3 class="rank-math-question ">Do I need a new SIM for 5G?</h3>
<div class="rank-math-answer ">

<p>No. Zong says 4G SIMs work on 5G, and TechJuice&#8217;s report on the ministry&#8217;s figures says the same for existing 4G SIMs in general. You need a compatible phone and coverage.</p>

</div>
</div>
<div id="faq-question-3" class="rank-math-list-item">
<h3 class="rank-math-question ">Which network has the most 5G sites?</h3>
<div class="rank-math-answer ">

<p>Jazz, by a wide margin: 1,015 of the 1,782 sites in the ministry&#8217;s October count, more than Zong and Ufone combined.</p>

</div>
</div>
<div id="faq-question-4" class="rank-math-list-item">
<h3 class="rank-math-question ">Why does my iPhone not show 5G?</h3>
<div class="rank-math-answer ">

<p>Check that it is an iPhone 12 or newer, running the latest iOS, set to 5G Auto, and inside a covered area. On Ufone, we found no announcement of iPhone 5G support as of October 2026.</p>

</div>
</div>
<div id="faq-question-5" class="rank-math-list-item">
<h3 class="rank-math-question ">Is 5G faster than 4G in Pakistan?</h3>
<div class="rank-math-answer ">

<p>Zong says 5G &#8220;is designed to deliver significantly faster download and upload speeds&#8221; than 4G, though real speeds depend on coverage, the phone and network load. We found no official speed measurement we could verify.</p>

</div>
</div>
</div>
</div>


<h2 class="wp-block-heading">What we do not know yet</h2>



<p class="wp-block-paragraph">We could not open the PTA website, Jazz&#8217;s 5G pages or the State Bank&#8217;s exchange-rate pages while researching this guide.</p>



<p class="wp-block-paragraph">Ufone has no official coverage map or city list we could find. We found no 5G-only packages with prices, no official speed measurement, and no firm date for nationwide coverage. Zong&#8217;s own map and the ministry&#8217;s count also disagree on Zong&#8217;s site total. We&#8217;ll update this guide as operators publish more.</p>



<p class="wp-block-paragraph"><strong>Read next:</strong> our <a href="https://www.gradientco.net/tech-science/gta-6-pc-release-date/">GTA 6 PC release date guide</a>, on what Rockstar has confirmed and what is only reported.</p>



<p class="wp-block-paragraph"><em>Researched and drafted with AI assistance; facts checked against the sources linked in this article.</em></p>



<p class="has-small-font-size wp-block-paragraph">Featured image: <a href="https://commons.wikimedia.org/wiki/File%3ACellular_5G_Equipment_-_Cell_Tower_Antennas.jpg" target="_blank" rel="noopener">Cellular 5G Equipment &#8211; Cell Tower Antennas</a> by <a href="https://commons.wikimedia.org/wiki/File%3ACellular_5G_Equipment_-_Cell_Tower_Antennas.jpg" target="_blank" rel="noopener">Tony Webster</a>, <a href="https://creativecommons.org/licenses/by/2.0" target="_blank" rel="noopener">CC BY 2.0</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Winter Dry Skin in Pakistan: Why It Happens and What Dermatologists Advise</title>
		<link>https://www.gradientco.net/health-fitness/winter-dry-skin-pakistan/</link>
		
		<dc:creator><![CDATA[Ilsa Fatima]]></dc:creator>
		<pubDate>Thu, 05 Nov 2026 02:12:57 +0000</pubDate>
				<category><![CDATA[Health & Beauty]]></category>
		<category><![CDATA[Beauty]]></category>
		<category><![CDATA[Health]]></category>
		<category><![CDATA[Selfcare]]></category>
		<guid isPermaLink="false">https://www.gradientco.net/?p=10197</guid>

					<description><![CDATA[Cold air, heaters, hot showers and soap pull water out of the skin's outer layer. Here is a seven-step routine from dermatology bodies, how to pick a moisturiser, and the signs that need a doctor.]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Tight, flaky, itchy skin in the cold months happens because the outer layer of skin loses water faster than it can hold on to it. Cold air, wind, gas heaters, long hot showers and harsh soap all speed that loss. For winter dry skin, dermatology bodies recommend a simple routine: shorter, warm showers, a gentle cleanser, and a thick cream or ointment smoothed on while the skin is still damp. See a doctor if it cracks, bleeds or won&#8217;t settle.</p>



<h2 class="wp-block-heading">Why does skin dry out when the weather turns cold?</h2>



<p class="wp-block-paragraph"><strong>Dry skin is water loss from the outermost layer of the skin</strong>, and winter stacks up the conditions that cause it. <a href="https://medlineplus.gov/ency/article/000835.htm" target="_blank" rel="noopener">MedlinePlus</a>, from the US National Library of Medicine, names cold, dry winter air and dry indoor air from heating. <a href="https://www.mymlc.com/health-information/diseases-and-conditions/d/dry-skin/" target="_blank" rel="noopener">Mayo Clinic&#8217;s patient guide</a>, republished by Mosaic Life Care, adds low-humidity climates, long or frequent baths, and harsh soaps and detergents.</p>



<p class="wp-block-paragraph">In Pakistan, winter on the plains brings its own mix. The Punjab government&#8217;s <a href="https://lahoredivision.punjab.gov.pk/node/66" target="_blank" rel="noopener">Lahore Division climate page</a> describes a foggy winter from mid-November to mid-February, with January the coolest month. Fog mornings feel damp, which is misleading: the skin still suffers once you come indoors to a gas heater and a hot shower. If hazy days bother your lungs as well, our <a href="https://www.gradientco.net/health-fitness/smog-in-pakistan/">guide to smog and AQI levels in Pakistan</a> covers that side of winter.</p>



<p class="wp-block-paragraph">Some people lose more water than others. Mayo Clinic says the risk rises after 40, and in jobs that keep the hands wet or use harsh soaps, such as nursing, hairstyling and dishwashing. Bathing more than once a day can also strip the skin&#8217;s natural oils. In our view, the same logic applies if you wash your hands, face and feet several times a day for wudu, so moisturising after washing matters more.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/10/winter-dry-skin-pakistan-graphic-1.webp" alt="What pulls water out of skin in winter: cold, dry winter air and wind; dry indoor air from heating such as a gas heater; long, hot or frequent baths; harsh soaps and detergents; hands often wet at work, as in nursing, hairstyling and dishwashing, or being over 40" class="wp-image-10199" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/winter-dry-skin-pakistan-graphic-1.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/winter-dry-skin-pakistan-graphic-1-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/winter-dry-skin-pakistan-graphic-1-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/winter-dry-skin-pakistan-graphic-1-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">The usual suspects behind winter dryness. Sources: MedlinePlus, Mayo Clinic</figcaption></figure>



<h2 class="wp-block-heading">A 7-step routine for winter dry skin</h2>



<p class="wp-block-paragraph">The skincare routine that dermatology bodies agree on is short: less hot water, less soap, more moisturiser. The steps below come from the <a href="https://www.aad.org/public/everyday-care/skin-care-basics/dry/dermatologists-tips-relieve-dry-skin" target="_blank" rel="noopener">American Academy of Dermatology (AAD)</a>, the NHS and Mayo Clinic.</p>



<ol class="wp-block-list">
<li><strong>Keep showers to 5 to 10 minutes in warm water, not hot.</strong> Close the bathroom door so the steam stays in, and bathe no more than once a day.</li>

<li><strong>Use a mild, fragrance-free cleanser, and only where you need it.</strong> Use just enough to lift dirt and oil, mainly on the armpits and groin, without a thick lather.</li>

<li><strong>Pat yourself dry with a towel.</strong> Rubbing irritates very dry skin.</li>

<li><strong>Put on moisturiser within minutes, while the skin is still damp.</strong> This traps the water the shower left behind.</li>

<li><strong>Moisturise again during the day</strong>, every time you wash your hands and whenever your skin feels tight.</li>

<li><strong>Cover up outdoors.</strong> Mayo Clinic suggests gloves, a scarf and a hat against cold wind; next to the skin, the AAD prefers loose cotton, because wool and polyester can irritate dry skin.</li>

<li><strong>Keep using sunscreen.</strong> Winter sun still reaches your face and hands, so the AAD recommends a broad-spectrum product with SPF 30 or higher.</li>
</ol>



<p class="wp-block-paragraph">If you keep only one step, we&#8217;d make it step 4; the rest mostly stop you losing water you&#8217;ve just put back. If you can, also run a humidifier in the bedroom overnight, as the AAD suggests. Clean it often, because a dirty tank can grow mould.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="801" src="https://www.gradientco.net/wp-content/uploads/2026/10/winter-dry-skin-pakistan-inline-1.webp" alt="A portable humidifier releasing water vapour beside a window" class="wp-image-10200" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/winter-dry-skin-pakistan-inline-1.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/winter-dry-skin-pakistan-inline-1-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/winter-dry-skin-pakistan-inline-1-1024x684.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/winter-dry-skin-pakistan-inline-1-768x513.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">A bedroom humidifier adds moisture to heated indoor air overnight; the AAD advises cleaning it often. Photo: <a href="https://commons.wikimedia.org/wiki/File%3AModern_air_humidifier._Water_vapor_closeup.jpg" target="_blank" rel="noopener">Modern air humidifier. Water vapor closeup</a> by <a href="https://commons.wikimedia.org/wiki/File%3AModern_air_humidifier._Water_vapor_closeup.jpg" target="_blank" rel="noopener">Shixart1985</a>, <a href="https://creativecommons.org/licenses/by/2.0" target="_blank" rel="noopener">CC BY 2.0</a></figcaption></figure>



<h2 class="wp-block-heading">Which moisturiser works best: ointment, cream or lotion?</h2>



<p class="wp-block-paragraph">For dry skin in winter, a cream or an ointment does more than a lotion. The AAD says creams and ointments are more effective, and the NHS explains why in its <a href="https://www.nhs.uk/conditions/emollients/" target="_blank" rel="noopener">guide to emollients</a>, its word for medical moisturisers.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/10/winter-dry-skin-pakistan-graphic-2.webp" alt="Lotion, cream or ointment: lotions are thin, spread easily and moisturise least, so they suit hairy areas; creams are less greasy, sink in fast and suit daytime; ointments are thick, greasy and very moisturising, best at night for very dry skin, but not on weeping eczema" class="wp-image-10201" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/winter-dry-skin-pakistan-graphic-2.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/winter-dry-skin-pakistan-graphic-2-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/winter-dry-skin-pakistan-graphic-2-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/winter-dry-skin-pakistan-graphic-2-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Creams and ointments do more than lotions for winter dryness. Sources: NHS, AAD</figcaption></figure>



<p class="wp-block-paragraph">Lotions are thin and spread easily, so they suit hairy areas, but they moisturise least. Creams are less greasy and sink in fast, which makes them good for daytime. Ointments are thick and very moisturising, so the NHS says they are often used at night for very dry skin, though not on weeping eczema.</p>



<p class="wp-block-paragraph">Two habits from the <a href="https://www.skinhealthinfo.org.uk/condition/emollient-use-in-skin-conditions/" target="_blank" rel="noopener">British Association of Dermatologists (BAD)</a> are worth copying. Smooth the product on in the direction your hair grows, so it doesn&#8217;t clog the follicles. Keep using it every day, even when the skin looks fine.</p>



<p class="wp-block-paragraph">Whichever you choose, pick a fragrance-free one. The AAD warns that &#8220;unscented&#8221; isn&#8217;t the same thing: unscented products can contain chemicals that mask smell and irritate sensitive skin.</p>



<h2 class="wp-block-heading">What do glycerin, urea and petroleum jelly actually do?</h2>



<p class="wp-block-paragraph">Read a moisturiser&#8217;s label and you&#8217;ll usually find two kinds of ingredient: one that pulls water into the skin and one that seals it in. A third kind softens the surface. A 2016 review in the <a href="https://pmc.ncbi.nlm.nih.gov/articles/PMC4885180/" target="_blank" rel="noopener">Indian Journal of Dermatology</a> by Sethi and colleagues explains how each type works.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/10/winter-dry-skin-pakistan-graphic-3.webp" alt="Petroleum jelly seals in the most water: at a concentration of at least 5%, petrolatum cuts water loss from the skin by more than 98%, while lanolin, mineral oil or silicones cut it by 20 to 30%" class="wp-image-10202" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/winter-dry-skin-pakistan-graphic-3.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/winter-dry-skin-pakistan-graphic-3-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/winter-dry-skin-pakistan-graphic-3-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/winter-dry-skin-pakistan-graphic-3-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">How much each sealing ingredient cuts water loss through the skin. Source: Sethi et al., Indian Journal of Dermatology (2016)</figcaption></figure>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Ingredient type</th><th>What it does</th><th>Examples (AAD, MedlinePlus, the review)</th><th>The catch</th></tr></thead><tbody><tr><td>Humectant</td><td>Draws water into the outer layer of skin</td><td>Glycerin, urea, lactic acid, hyaluronic acid</td><td>Can pull water up from deeper skin, which is then lost if nothing seals it</td></tr><tr><td>Occlusive</td><td>Forms a film that stops water escaping</td><td>Petrolatum (petroleum jelly), mineral oil, lanolin, dimethicone</td><td>Greasy feel, and some people react to them</td></tr><tr><td>Emollient</td><td>Fills gaps between skin cells so skin feels soft</td><td>Ceramides, cholesterol, fatty acids, squalane</td><td>Wears off: the review says the effect peaks within an hour and lasts about 4 hours</td></tr></tbody></table></figure>



<p class="wp-block-paragraph">Glycerol (glycerin) is the most effective humectant, by the review&#8217;s account, but humectants are also &#8220;a double-edged weapon&#8221;: that&#8217;s why good products pair them with a sealing ingredient.</p>



<p class="wp-block-paragraph"><strong>Petroleum jelly is the strongest seal.</strong> The review reports that petrolatum at a concentration of at least 5% reduces water loss from the skin by more than 98%. Lanolin, mineral oil and silicones cut it by only 20 to 30%. We think that makes a thin layer of plain petroleum jelly at night a sensible choice for very dry patches, if you can live with the shine.</p>



<h2 class="wp-block-heading">How do you care for cracked lips and hands?</h2>



<p class="wp-block-paragraph">For lips, the AAD suggests a lip balm or petroleum jelly. If a balm stings or tingles, switch to another, because that feeling is irritation, not proof it is working.</p>



<p class="wp-block-paragraph">For hands, keep a non-greasy hand cream near the sink, in your bag or at your desk, so step 5 is easy to keep. Wear gloves for washing dishes. If your feet crack at the heels, a urea cream, which MedlinePlus names for dry skin, is worth asking a pharmacist about.</p>



<h2 class="wp-block-heading">Which habits make dry skin worse?</h2>



<p class="wp-block-paragraph"><strong>Some everyday products undo the routine above, whatever moisturiser you use.</strong> While your skin is dry, the AAD advises avoiding three things: products with alcohol (hand sanitiser is the exception), anything with fragrance, deodorant soaps included, and retinoids. Mayo Clinic adds over-scrubbing to the list.</p>



<p class="wp-block-paragraph">Two less obvious habits to drop:</p>



<ul class="wp-block-list">
<li><strong>Aqueous cream as a moisturiser.</strong> If you&#8217;ve used it for years, this one may surprise you: the NHS no longer recommends it because its sodium lauryl sulphate can irritate skin.</li>

<li><strong>Moisturiser near an open flame.</strong> The NHS warns that clothes, bedding and dressings carrying emollient residue catch fire more easily, whether or not the product contains paraffin. If you sit close to a gas heater, keep a safe distance and wash clothes and bedsheets regularly.</li>
</ul>



<p class="wp-block-paragraph">One more risk, from the BAD: emollients make the bath or shower floor very slippery. A non-slip mat helps, especially for older relatives, and our <a href="https://www.gradientco.net/health-fitness/osteoporosis-in-pakistan/">osteoporosis guide</a> explains why a fall matters more for them.</p>



<h2 class="wp-block-heading">When does dry skin need a doctor?</h2>



<p class="wp-block-paragraph"><strong>Most winter dry skin is harmless, but some signs need a doctor rather than another cream.</strong> Book an appointment, Mayo Clinic advises, if:</p>



<ul class="wp-block-list">
<li>the dryness lasts despite self-care;</li>

<li>skin is inflamed or painful (Mayo notes irritation shows as redness on white skin but can look greyish on brown skin, so do not wait for redness);</li>

<li>the discomfort costs you sleep or distracts you from daily life;</li>

<li>open sores or infections develop where the skin has broken open;</li>

<li>large areas are scaling or peeling.</li>
</ul>



<p class="wp-block-paragraph">Cracks matter because they let bacteria in, and Mayo warns that untreated dryness can turn into eczema (atopic dermatitis). Dryness that does not respond to care can also be a sign of psoriasis or even kidney disease, the AAD says. Ask a dermatologist before trying anti-itch creams, and tell them if any moisturiser burns or stings.</p>



<h2 class="wp-block-heading">Frequently asked questions</h2>


<div id="rank-math-faq" class="rank-math-block">
<div class="rank-math-list ">
<div id="faq-question-1" class="rank-math-list-item">
<h3 class="rank-math-question ">Do I really need sunscreen in winter?</h3>
<div class="rank-math-answer ">

<p>Yes. Clouds and fog don&#8217;t remove the need: Mayo Clinic advises wearing it even on cloudy days and reapplying every two hours when you are outside.</p>

</div>
</div>
<div id="faq-question-2" class="rank-math-list-item">
<h3 class="rank-math-question ">How often should I moisturise when my skin is very dry?</h3>
<div class="rank-math-answer ">

<p>The NHS says at least three or four times a day for dry skin conditions, with extra care for hands and face. You can&#8217;t really overdo it, as emollients are very safe.</p>

</div>
</div>
<div id="faq-question-3" class="rank-math-list-item">
<h3 class="rank-math-question ">Can a moisturiser replace soap for washing?</h3>
<div class="rank-math-answer ">

<p>Often, yes. The BAD says most emollients can be used instead of soap; the NHS suggests mixing about a teaspoonful with a little warm water, spreading it on, then rinsing.</p>

</div>
</div>
<div id="faq-question-4" class="rank-math-list-item">
<h3 class="rank-math-question ">Is winter dry skin the same as eczema?</h3>
<div class="rank-math-answer ">

<p>No. Eczema (atopic dermatitis) is a skin condition with its own treatment plan, and the AAD lists it among the conditions that dryness resistant to home care can point to.</p>

</div>
</div>
</div>
</div>


<h2 class="wp-block-heading">What we do not know yet</h2>



<p class="wp-block-paragraph">As of October 2026, we could not open the Pakistan Meteorological Department&#8217;s humidity records, so we cannot give official winter humidity figures for Lahore, Islamabad or Peshawar. We found no dermatology body that backs popular home remedies for winter dry skin such as desi ghee or rosewater-glycerine mixes, so we have left them out. We also found no study on whether hard water in Pakistani cities makes dry skin worse.</p>



<p class="wp-block-paragraph"><em>This article is general information, not medical advice. Talk to a doctor before changing your diet, exercise or medication.</em></p>



<p class="wp-block-paragraph"><em>Read next: <a href="https://www.gradientco.net/health-fitness/flu-vaccine-in-pakistan/">Flu vaccine in Pakistan and the best time to get the shot before winter peaks</a>.</em></p>



<p class="wp-block-paragraph"><em>Researched and drafted with AI assistance; facts checked against the sources linked in this article.</em></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Flu Vaccine in Pakistan: The Best Time to Get the Shot Before Winter Peaks</title>
		<link>https://www.gradientco.net/health-fitness/flu-vaccine-in-pakistan/</link>
		
		<dc:creator><![CDATA[Ilsa Fatima]]></dc:creator>
		<pubDate>Tue, 27 Oct 2026 03:07:40 +0000</pubDate>
				<category><![CDATA[Health & Beauty]]></category>
		<category><![CDATA[Health]]></category>
		<category><![CDATA[healthy]]></category>
		<guid isPermaLink="false">https://www.gradientco.net/?p=10176</guid>

					<description><![CDATA[Flu cases in Pakistan climb as winter sets in, and the yearly shot takes about two weeks to protect you. Here is who needs it most, when to get it, how well it works and who should ask a doctor first.]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">The flu vaccine in Pakistan is worth getting every autumn, ideally in October or November, because flu cases here climb as temperatures fall between December and February and the shot takes about two weeks to build protection. Pregnant women, children under five, people over 65, anyone with a long-term illness and health workers gain the most. The vaccine can&#8217;t give you flu, and its usual side effects pass within a day or two.</p>



<h2 class="wp-block-heading">Who should get a flu shot first?</h2>



<p class="wp-block-paragraph"><strong>Pregnant women come first, followed by young children, older people, people with chronic illness and health workers.</strong> Pakistan&#8217;s National Institutes of Health (NIH) sets out that order in its <a href="https://nhsrc.gov.pk/SiteImage/Misc/files/Advisory%20for%20Influenza%20_%20December%202025.pdf" target="_blank" rel="noopener">December 2025 influenza advisory</a>, citing the World Health Organization (WHO), and calls pregnant women the &#8220;highest priority&#8221;. The <a href="https://www.who.int/news-room/fact-sheets/detail/influenza-(seasonal" target="_blank" rel="noopener">WHO fact sheet</a>) recommends a yearly shot for pregnant women, children aged 6 months to 5 years, people over 65, people with chronic medical conditions, and health workers.</p>



<p class="wp-block-paragraph">Most of these groups are the likeliest to become seriously ill with seasonal influenza. Health workers are on the list for a different reason: the WHO says they catch flu more often and can pass it to vulnerable patients.</p>



<p class="wp-block-paragraph">The NIH advisory adds people who are overweight or obese, and people with weak immunity, such as those with HIV or on chemotherapy. It also lists diabetes, heart disease and lung disease among the chronic conditions that raise the risk.</p>



<p class="wp-block-paragraph">Healthy adults can benefit too. A 2024 consensus of 18 Pakistani experts, published in the <a href="https://scholars.aku.edu/en/publications/consensus-statements-for-influenza-awareness-prevention-and-vacci/" target="_blank" rel="noopener">Journal of Infection in Developing Countries</a>, urged vaccination for everyone aged 6 months and older who has no reason to avoid it. The same paper calls influenza &#8220;a serious underestimated viral infection in Pakistan&#8221; and says vaccination rates are low. One of its authors lists an affiliation with Abbott Laboratories.</p>



<h2 class="wp-block-heading">When should you get the flu vaccine in Pakistan?</h2>



<p class="wp-block-paragraph"><strong>Aim for October or November, a few weeks before cases start to climb.</strong> NIH Pakistan&#8217;s <a href="https://www.nih.org.pk/wp-content/uploads/2023/11/Advisory%20for%20the%20Prevention%20and%20Control%20of%20Seasonal%20Influenza.pdf" target="_blank" rel="noopener">November 2023 advisory</a> says flu cases rise &#8220;as temperature falls during December and February.&#8221; The expert consensus puts the peak in January and February, although the virus circulates all year.</p>



<p class="wp-block-paragraph">Timing matters because protection isn&#8217;t instant. The US Centers for Disease Control and Prevention (CDC) says antibodies develop <a href="https://www.cdc.gov/flu/vaccines/keyfacts.html" target="_blank" rel="noopener">about two weeks after vaccination</a>, and the UK&#8217;s <a href="https://www.nhs.uk/vaccinations/flu-vaccine/" target="_blank" rel="noopener">National Health Service (NHS)</a> says the shot can take up to 14 days to work. In our view, a shot in late October or November covers you before the December rise.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-graphic-1.webp" alt="Flu shot timing in Pakistan: get the shot in October or November; protection builds over about two weeks (CDC) or up to 14 days (NHS); cases rise from December to February as temperatures fall (NIH Pakistan); the peak comes in January and February" class="wp-image-10178" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-graphic-1.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-graphic-1-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-graphic-1-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-graphic-1-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Plan the shot around the season. Sources: NIH Pakistan, CDC, NHS, Pakistani expert consensus</figcaption></figure>



<p class="wp-block-paragraph">Seasons don&#8217;t always keep to the calendar. In 2025, a fast-spreading H3N2 variant known as subclade K contributed to an earlier start to the season in many countries, according to the WHO. In Pakistan, NIH counted 340,856 suspected cases of influenza-like illness in just six weeks, from epidemiological weeks 44 to 49 of 2025. Of the samples tested, 12% were positive for the H3N2 strain.</p>



<p class="wp-block-paragraph">If you miss the autumn window, ask your doctor about a later shot. In the UK, for comparison, the NHS keeps offering the vaccine until the end of March.</p>



<h2 class="wp-block-heading">What is in this year&#8217;s influenza vaccine?</h2>



<p class="wp-block-paragraph"><strong>Each year&#8217;s vaccine is rebuilt to match the strains experts expect to spread, so last year&#8217;s shot isn&#8217;t enough.</strong> The WHO picks the strains at consultations held twice a year, once for each hemisphere, with experts from its Global Influenza Surveillance and Response System (GISRS). NIH Pakistan&#8217;s advisory applies the northern hemisphere recommendation to Pakistan.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-inline-1.webp" alt="A prefilled syringe labelled Injection Influenza vaccine, IM, for injection into the muscle" class="wp-image-10179" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-inline-1.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-inline-1-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-inline-1-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-inline-1-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">The flu vaccine is an injection into the muscle; its formula changes every season, so check the label says 2026-2027 northern hemisphere. Photo: <a href="https://commons.wikimedia.org/wiki/File%3AInfluenza_Vaccine_%2851551576704%29.jpg" target="_blank" rel="noopener">Influenza Vaccine (51551576704)</a> by <a href="https://commons.wikimedia.org/wiki/File%3AInfluenza_Vaccine_%2851551576704%29.jpg" target="_blank" rel="noopener">Pete from Liverpool, UK</a>, <a href="http://creativecommons.org/publicdomain/zero/1.0/deed.en" target="_blank" rel="noopener">CC0</a></figcaption></figure>



<p class="wp-block-paragraph">For the 2026 to 2027 northern season, the <a href="https://www.who.int/news/item/27-02-2026-recommendations-for-influenza-vaccine-composition-for-the-2026-2027-northern-hemisphere-season" target="_blank" rel="noopener">WHO&#8217;s 27 February 2026 recommendation</a> names three strains. They sit in the first two rows of this summary, with the other basics:</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Flu vaccine at a glance</th><th>What the sources say</th></tr></thead><tbody><tr><td>Strains in the 2026-27 vaccine</td><td>Three: an H1N1 strain, an H3N2 strain and a B/Victoria strain (WHO)</td></tr><tr><td>Egg-based version</td><td>A/Missouri/11/2025, A/Darwin/1454/2025, B/Tokyo/EIS13-175/2025 (WHO)</td></tr><tr><td>Who can have it</td><td>Most people from 6 months of age (CDC, Pakistani expert consensus)</td></tr><tr><td>Time to build protection</td><td>About two weeks (CDC); up to 14 days (NHS)</td></tr><tr><td>Pakistan&#8217;s flu season</td><td>Cases rise December to February (NIH Pakistan)</td></tr><tr><td>Usual side effects</td><td>Sore arm, slight fever, aches; settle in 1 to 2 days (NHS)</td></tr><tr><td>How often</td><td>Every year, because protection fades and the viruses change (CDC)</td></tr></tbody></table></figure>



<h2 class="wp-block-heading">How well does the flu shot work?</h2>



<p class="wp-block-paragraph"><strong>The flu shot usually cuts the risk of laboratory-confirmed flu by about 30% to 60%, and it also makes severe illness less likely.</strong> That range held in 14 of the 16 US seasons the CDC has estimated since 2009. It isn&#8217;t a guarantee. How well it works depends on how closely the vaccine matches the strains in circulation, and on your age and health.</p>



<p class="wp-block-paragraph">Those figures come from the CDC&#8217;s <a href="https://www.cdc.gov/flu-vaccines-work/php/effectiveness-studies/index.html" target="_blank" rel="noopener">yearly effectiveness estimates</a>, which put it at 60% in 2010-11, just 19% in 2014-15 and 36% in 2025-26, the last a preliminary figure (page dated 16 March 2026). We found no comparable estimate for Pakistan.</p>



<p class="wp-block-paragraph">For us, the stronger case is against serious illness. A 2021 study cited by the CDC linked vaccination in adults with a 26% lower risk of intensive care admission and a 31% lower risk of death from flu, compared with unvaccinated adults. NIH Pakistan makes the same point: even when the virus drifts, vaccination still reduces the severity of illness and deaths.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-graphic-2.webp" alt="How well the flu shot works, from CDC estimates in the US: it cuts the risk of laboratory-confirmed flu by 30% to 60% in 14 of 16 seasons, the preliminary 2025-26 estimate was 36%, and a 2021 study linked it to a 26% lower risk of intensive care admission and a 31% lower risk of death from flu" class="wp-image-10180" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-graphic-2.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-graphic-2-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-graphic-2-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-graphic-2-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Protection against flu varies by season; protection against serious illness is the stronger case. Source: CDC</figcaption></figure>



<p class="wp-block-paragraph">The WHO notes that the vaccine may work less well in older people, which gives the people around them one more reason to get it.</p>



<h2 class="wp-block-heading">What side effects should you expect?</h2>



<p class="wp-block-paragraph"><strong>Most people get a sore arm and perhaps a mild fever or aches, and these settle within one to two days.</strong> The NHS lists a sore arm, a slight temperature and aches as the most common effects. The <a href="https://www.cdc.gov/vaccine-safety/vaccines/flu.html" target="_blank" rel="noopener">CDC&#8217;s vaccine safety page</a> adds redness or swelling where the needle went in, headache and tiredness.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="858" src="https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-inline-2.webp" alt="A health worker holds a syringe and prepares to give a woman a flu shot in the upper arm" class="wp-image-10181" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-inline-2.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-inline-2-300x215.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-inline-2-1024x732.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-inline-2-768x549.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">The upper arm can feel sore for a day or two after the shot, the most common side effect the NHS lists. Photo: <a href="https://commons.wikimedia.org/wiki/File%3AAltus_AFB_fights_Influenza_120106-F-BT319-788.jpg" target="_blank" rel="noopener">Altus AFB fights Influenza 120106-F-BT319-788</a> by <a href="https://commons.wikimedia.org/wiki/File%3AAltus_AFB_fights_Influenza_120106-F-BT319-788.jpg" target="_blank" rel="noopener">Senior Airman Myles Stepp</a>, <a href="https://creativecommons.org/publicdomain/mark/1.0/" target="_blank" rel="noopener">Public domain</a></figcaption></figure>



<p class="wp-block-paragraph">The shot can&#8217;t give you flu. Injected vaccines contain an inactivated virus or only viral proteins, so there is no live virus to infect you.</p>



<p class="wp-block-paragraph">Serious reactions are rare. A severe allergic reaction, with hives, a swollen face or throat, or trouble breathing, needs emergency care. The CDC says that when flu vaccines have been linked to Guillain-Barré syndrome, a rare nerve disorder, the rise was about 1 to 2 extra cases per million doses, and that the risk of it is higher after flu itself than after the vaccine.</p>



<h2 class="wp-block-heading">Who should skip it or talk to a doctor first?</h2>



<p class="wp-block-paragraph"><strong>Babies under 6 months should not have the flu shot, and neither should anyone who had a severe allergic reaction to a previous dose.</strong> The <a href="https://www.cdc.gov/flu/vaccines/vaccinations.html" target="_blank" rel="noopener">CDC</a> also rules out a vaccine for anyone with a severe, life-threatening allergy to one of its ingredients, egg aside.</p>



<p class="wp-block-paragraph">Talk to a doctor first if you:</p>



<ol class="wp-block-list">
<li>Have had an allergic reaction to a flu vaccine or one of its ingredients.</li>

<li>Have a high temperature on the day; the NHS says to wait until you feel better.</li>

<li>Have a long-term condition or take treatment that weakens the immune system, and are unsure whether the shot suits you.</li>

<li>Are booking for a child having the vaccine for the first time, since some children need two doses at least four weeks apart.</li>
</ol>



<p class="wp-block-paragraph">An egg allergy is no longer a barrier. According to the CDC, people with an egg allergy can have any flu vaccine, egg-based or not, with no extra precautions beyond those for every vaccine.</p>



<p class="wp-block-paragraph">Pregnancy doesn&#8217;t rule it out either. The <a href="https://www.cdc.gov/flu/vaccine-safety/vaccine-pregnant.html" target="_blank" rel="noopener">CDC says</a> it can be given in any trimester and helps protect the baby for the first several months after birth. Pregnant women should have the injection, not the nasal spray.</p>



<h2 class="wp-block-heading">Is it flu or just a cold?</h2>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-graphic-3.webp" alt="Flu or a cold: flu usually hits suddenly with fever, body aches and exhaustion and can lead to pneumonia or a hospital stay; a cold is milder, mostly a runny or stuffy nose, and rarely serious; the flu shot protects against flu but not colds, which other viruses cause" class="wp-image-10182" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-graphic-3.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-graphic-3-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-graphic-3-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/flu-vaccine-in-pakistan-graphic-3-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">How flu and a cold usually differ; only a lab test confirms flu. Source: CDC</figcaption></figure>



<p class="wp-block-paragraph"><strong>Flu usually hits suddenly, with fever, body aches and exhaustion, while a cold is milder and mostly affects the nose.</strong> According to a <a href="https://www.cdc.gov/flu/about/coldflu.html" target="_blank" rel="noopener">CDC comparison</a>, people with colds are more likely to have a runny or stuffy nose. Colds rarely lead to pneumonia or a hospital stay; flu can.</p>



<p class="wp-block-paragraph">Symptoms alone can&#8217;t always tell them apart, and only a lab test confirms flu. A sudden high fever can also be dengue, so read our guide to <a href="https://www.gradientco.net/health-fitness/dengue-symptoms-pakistan/">dengue warning signs</a> if you have fever with severe body pain. If your cough gets worse on hazy days, our <a href="https://www.gradientco.net/health-fitness/smog-in-pakistan/">smog and AQI guide</a> explains how to protect your lungs.</p>



<p class="wp-block-paragraph">Get medical help at once for trouble breathing, chest pain or pressure, confusion, a seizure, or a fever or cough that improves and then returns worse. The <a href="https://www.cdc.gov/flu/signs-symptoms/index.html" target="_blank" rel="noopener">CDC&#8217;s list of warning signs</a> adds bluish lips, fast breathing and signs of dehydration in children, and any fever in a baby younger than 12 weeks.</p>



<h2 class="wp-block-heading">Where can you get the flu vaccine in Pakistan?</h2>



<p class="wp-block-paragraph"><strong>Start with your family doctor, gynaecologist or child specialist, who can tell you whether the vaccine is in stock and which version suits you.</strong> Hospital vaccination clinics and pharmacies may also stock it, but we could not confirm which ones have the 2026-27 vaccine yet, so you&#8217;ll want to call first.</p>



<p class="wp-block-paragraph">Before the injection, run three quick checks:</p>



<ol class="wp-block-list">
<li>Confirm the vaccine is labelled for the 2026-2027 northern hemisphere season.</li>

<li>Mention pregnancy, allergies, a current fever or any long-term illness before the injection.</li>

<li>Ask about the price in advance; we found no official price list from the Drug Regulatory Authority of Pakistan (DRAP).</li>
</ol>



<h2 class="wp-block-heading">Frequently asked questions</h2>


<div id="rank-math-faq" class="rank-math-block">
<div class="rank-math-list ">
<div id="faq-question-1" class="rank-math-list-item">
<h3 class="rank-math-question ">Can I get the flu shot while I have a cold?</h3>
<div class="rank-math-answer ">

<p>For a runny nose or other mild symptoms, ask the doctor or nurse giving the shot. It&#8217;s a high temperature that the NHS says to wait out.</p>

</div>
</div>
<div id="faq-question-2" class="rank-math-list-item">
<h3 class="rank-math-question ">Do I need a flu vaccine every year?</h3>
<div class="rank-math-answer ">

<p>Yes. For 2026-27 the WHO replaced all three strains used in the 2025-26 northern hemisphere vaccine, so a shot from last winter was built for different viruses.</p>

</div>
</div>
<div id="faq-question-3" class="rank-math-list-item">
<h3 class="rank-math-question ">Is the flu vaccine in Pakistan safe for pregnant women?</h3>
<div class="rank-math-answer ">

<p>Yes. Pakistani experts agreed in their 2024 consensus that the inactivated vaccine is safe and works in pregnancy.</p>

</div>
</div>
<div id="faq-question-4" class="rank-math-list-item">
<h3 class="rank-math-question ">Does the flu vaccine protect against COVID-19 or colds?</h3>
<div class="rank-math-answer ">

<p>No. The flu shot is made from influenza strains only, while colds and COVID-19 are caused by other viruses. The NHS says you can have the flu vaccine at the same time as a COVID-19 vaccine.</p>

</div>
</div>
</div>
</div>


<h2 class="wp-block-heading">What we do not know yet</h2>



<p class="wp-block-paragraph">As of 21 October 2026, NIH Pakistan has not posted an influenza advisory for the 2026-27 season, so we cannot say how this winter will compare with the last. We found no official vaccine price, no national figure for how many Pakistanis get vaccinated, and no Pakistan-specific estimate of how well the vaccine works here.</p>



<p class="wp-block-paragraph"><em>This article is general information, not medical advice. Talk to a doctor before changing your diet, exercise or medication.</em></p>



<p class="wp-block-paragraph"><em>Read next: <a href="https://www.gradientco.net/health-fitness/dengue-symptoms-pakistan/">Dengue symptoms in Pakistan and the warning signs that need a hospital</a>.</em></p>



<p class="wp-block-paragraph"><em>Researched and drafted with AI assistance; facts checked against the sources linked in this article.</em></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Two transfers, two locks: a Java deadlock example that passes code review</title>
		<link>https://www.gradientco.net/engineering/java-deadlock-example/</link>
		
		<dc:creator><![CDATA[Nabeel Ahmed]]></dc:creator>
		<pubDate>Tue, 27 Oct 2026 03:07:15 +0000</pubDate>
				<category><![CDATA[Engineering]]></category>
		<category><![CDATA[Concurrency]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<guid isPermaLink="false">https://www.gradientco.net/?p=10166</guid>

					<description><![CDATA[A transfer from A to B and one from B to A, at the same moment, can freeze a Java service for good. A test that forces it, how to detect it, and three fixes with their costs.]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">&#8220;LGTM: both accounts are locked before the balance changes.&#8221; Picture that review comment on the wallet transfer method below; everything it says is true. Yet in our test project, this Java deadlock example stopped all 8 threads moving money between 10 wallets after 902 transfers.</p>



<p class="wp-block-paragraph">Nothing finished in the next half second. There was no exception, no error log and no busy CPU, just a thread dump with one line for it: <code>Found one Java-level deadlock</code>. So what did the review miss in about fifteen lines of ordinary code, and how do you make the freeze impossible?</p>



<p class="wp-block-paragraph"><strong>Short answer:</strong> two transfers that lock the same two accounts in opposite orders can each hold one lock while waiting for the other, forever. Make every code path take locks in one global order (lowest account id first). Where you can&#8217;t, wait with a timeout and back off, or give each account a single owner thread so no transfer needs two locks at once.</p>



<h2 class="wp-block-heading">Does this transfer method look correct to you?</h2>



<p class="wp-block-paragraph">The service comes from our test project (Java 25, Spring Boot 4.1.1). Each <code>Account</code> is one shared object per id, and the transfer locks it with <code>synchronized</code> before touching the balance:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-start:18;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2f363c;color:#d3d7dd">TransferService.java (buggy)</span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">void</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">transfer</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">long</span><span style="color: #E1E4E8"> fromId, </span><span style="color: #F97583">long</span><span style="color: #E1E4E8"> toId, </span><span style="color: #F97583">long</span><span style="color: #E1E4E8"> amountCents) {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">if</span><span style="color: #E1E4E8"> (fromId </span><span style="color: #F97583">==</span><span style="color: #E1E4E8"> toId) {</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #F97583">throw</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">IllegalArgumentException</span><span style="color: #E1E4E8">(</span><span style="color: #9ECBFF">&quot;Cannot transfer to the same account&quot;</span><span style="color: #E1E4E8">);</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">Account</span><span style="color: #E1E4E8"> from </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> accounts.</span><span style="color: #B392F0">require</span><span style="color: #E1E4E8">(fromId);</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">Account</span><span style="color: #E1E4E8"> to </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> accounts.</span><span style="color: #B392F0">require</span><span style="color: #E1E4E8">(toId);</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #F97583">synchronized</span><span style="color: #E1E4E8"> (from) {</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #F97583">if</span><span style="color: #E1E4E8"> (from.</span><span style="color: #B392F0">balanceCents</span><span style="color: #E1E4E8">() </span><span style="color: #F97583">&lt;</span><span style="color: #E1E4E8"> amountCents) {</span></span>
<span class="line"><span style="color: #E1E4E8">                </span><span style="color: #F97583">throw</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">InsufficientFundsException</span><span style="color: #E1E4E8">(fromId, amountCents, from.</span><span style="color: #B392F0">balanceCents</span><span style="color: #E1E4E8">());</span></span>
<span class="line"><span style="color: #E1E4E8">            }</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">            </span><span style="color: #F97583">synchronized</span><span style="color: #E1E4E8"> (to) {</span></span>
<span class="line"><span style="color: #E1E4E8">                from.</span><span style="color: #B392F0">debit</span><span style="color: #E1E4E8">(amountCents);</span></span>
<span class="line"><span style="color: #E1E4E8">                to.</span><span style="color: #B392F0">credit</span><span style="color: #E1E4E8">(amountCents);</span></span>
<span class="line"><span style="color: #E1E4E8">            }</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span></code></pre></div>



<p class="wp-block-paragraph">Line 24 locks the source and checks its funds early, so a doomed transfer never touches the destination. Line 28 locks the destination before any money moves. Both balances change under both locks, which is exactly what a reviewer looks for.</p>



<p class="wp-block-paragraph">What the code never decides is which lock comes first. The caller does: whoever is <code>from</code> gets locked first.</p>



<h2 class="wp-block-heading">What happens when 1 to 2 meets 2 to 1?</h2>



<p class="wp-block-paragraph">Picture two drivers entering a single-lane bridge from opposite ends. Each has gone far enough to block the other, and neither will back up.</p>



<p class="wp-block-paragraph">In Java, every object has a <em>monitor</em>: a lock built into the object, which <code>synchronized (account)</code> takes and releases. The Java Language Specification says a thread that asks for a monitor &#8220;does not proceed further until the lock action has successfully completed&#8221;. There is no timeout and no way to give up.</p>



<p class="wp-block-paragraph">Now run <code>transfer(1, 2)</code> and <code>transfer(2, 1)</code> on two threads at once. The first takes account 1&#8217;s monitor, the second takes account 2&#8217;s, and each then asks for the account the other holds. Neither can finish, so neither releases anything.</p>



<figure class="wp-block-video"><video height="1080" style="aspect-ratio: 1080 / 1080;" width="1080" autoplay loop muted poster="https://www.gradientco.net/wp-content/uploads/2026/10/java-deadlock-example-clip-poster.webp" src="https://www.gradientco.net/wp-content/uploads/2026/10/java-deadlock-example-clip.mp4" playsinline></video><figcaption class="wp-element-caption">Each transfer holds one account and waits for the other. Nothing times out.</figcaption></figure>



<p class="wp-block-paragraph">That cycle is a <em>deadlock</em>: threads that each wait for a lock another one in the group holds. Here it comes from two code paths taking the same two locks in opposite orders. JLS chapter 17 is blunt about whose problem that is: Java &#8220;neither prevents nor requires detection of deadlock conditions&#8221;.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">transfer-1-to-2                         transfer-2-to-1</span></span>
<span class="line"><span style="color: #E1E4E8">---------------                         ---------------</span></span>
<span class="line"><span style="color: #E1E4E8">lock account 1         (held)</span></span>
<span class="line"><span style="color: #E1E4E8">                                        lock account 2         (held)</span></span>
<span class="line"><span style="color: #E1E4E8">check balance of 1: ok</span></span>
<span class="line"><span style="color: #E1E4E8">                                        check balance of 2: ok</span></span>
<span class="line"><span style="color: #E1E4E8">lock account 2 -&gt; BLOCKED (2 is held)</span></span>
<span class="line"><span style="color: #E1E4E8">                                        lock account 1 -&gt; BLOCKED (1 is held)</span></span>
<span class="line"><span style="color: #E1E4E8">            both wait forever; no timeout, no exception</span></span></code></pre></div>



<p class="wp-block-paragraph">The break is the fifth line; until then, neither thread did anything wrong on its own.</p>



<h2 class="wp-block-heading">Forcing the freeze with a barrier</h2>



<p class="wp-block-paragraph">Two threads rarely land in that window by chance, so a test that just starts them would pass most of the time. We wanted this Java deadlock example to fail on every run, so we forced the timing. The test&#8217;s accounts are a small subclass whose first balance check waits at a <code>CyclicBarrier</code> shared by both transfers:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-start:28;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2f363c;color:#d3d7dd">PausingAccount.java (test only)</span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">@Override</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">long</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">balanceCents</span><span style="color: #E1E4E8">() {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #F97583">if</span><span style="color: #E1E4E8"> (paused.</span><span style="color: #B392F0">compareAndSet</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">false</span><span style="color: #E1E4E8">, </span><span style="color: #79B8FF">true</span><span style="color: #E1E4E8">)) {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">            </span><span style="color: #F97583">try</span><span style="color: #E1E4E8"> {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">                bothHoldTheirFirstLock.</span><span style="color: #B392F0">await</span><span style="color: #E1E4E8">(maxPauseMillis, </span><span style="color: #B392F0">TimeUnit</span><span style="color: #E1E4E8">.</span><span style="color: #79B8FF">MILLISECONDS</span><span style="color: #E1E4E8">);</span></span>
<span class="line"><span style="color: #E1E4E8">            } </span><span style="color: #F97583">catch</span><span style="color: #E1E4E8"> (</span><span style="color: #B392F0">TimeoutException</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">|</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">BrokenBarrierException</span><span style="color: #E1E4E8"> e) {</span></span>
<span class="line"><span style="color: #E1E4E8">                </span><span style="color: #6A737D">// the other transfer did not reach its first lock; carry on</span></span>
<span class="line"><span style="color: #E1E4E8">            } </span><span style="color: #F97583">catch</span><span style="color: #E1E4E8"> (</span><span style="color: #B392F0">InterruptedException</span><span style="color: #E1E4E8"> e) {</span></span>
<span class="line"><span style="color: #E1E4E8">                </span><span style="color: #B392F0">Thread</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">currentThread</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">interrupt</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #E1E4E8">            }</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">super</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">balanceCents</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span></code></pre></div>



<p class="wp-block-paragraph">The balance check runs inside the first lock, so when the barrier opens, each thread already holds its first account. The test then gives both threads two seconds and asks the JVM what it sees:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-start:36;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2f363c;color:#d3d7dd">TransferDeadlockBugTest.java</span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">void</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">oppositeTransfersThatOverlapFreezeForever</span><span style="color: #E1E4E8">() </span><span style="color: #F97583">throws</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">Exception</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">CyclicBarrier</span><span style="color: #E1E4E8"> bothHoldTheirFirstLock </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">CyclicBarrier</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">2</span><span style="color: #E1E4E8">);</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">AccountRepository</span><span style="color: #E1E4E8"> accounts </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">AccountRepository</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #E1E4E8">        accounts.</span><span style="color: #B392F0">save</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">PausingAccount</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">1</span><span style="color: #E1E4E8">, </span><span style="color: #79B8FF">100_000</span><span style="color: #E1E4E8">, bothHoldTheirFirstLock, </span><span style="color: #79B8FF">5_000</span><span style="color: #E1E4E8">));</span></span>
<span class="line"><span style="color: #E1E4E8">        accounts.</span><span style="color: #B392F0">save</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">PausingAccount</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">2</span><span style="color: #E1E4E8">, </span><span style="color: #79B8FF">100_000</span><span style="color: #E1E4E8">, bothHoldTheirFirstLock, </span><span style="color: #79B8FF">5_000</span><span style="color: #E1E4E8">));</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">TransferService</span><span style="color: #E1E4E8"> service </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">TransferService</span><span style="color: #E1E4E8">(accounts);</span></span>
<span class="line"></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">Thread</span><span style="color: #E1E4E8"> aliceToBob </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">Thread</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">ofPlatform</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">daemon</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">name</span><span style="color: #E1E4E8">(</span><span style="color: #9ECBFF">&quot;transfer-1-to-2&quot;</span><span style="color: #E1E4E8">).</span><span style="color: #B392F0">start</span><span style="color: #E1E4E8">(() </span><span style="color: #F97583">-&gt;</span><span style="color: #E1E4E8"> service.</span><span style="color: #B392F0">transfer</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">1</span><span style="color: #E1E4E8">, </span><span style="color: #79B8FF">2</span><span style="color: #E1E4E8">, </span><span style="color: #79B8FF">2_500</span><span style="color: #E1E4E8">));</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">Thread</span><span style="color: #E1E4E8"> bobToAlice </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">Thread</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">ofPlatform</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">daemon</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">name</span><span style="color: #E1E4E8">(</span><span style="color: #9ECBFF">&quot;transfer-2-to-1&quot;</span><span style="color: #E1E4E8">).</span><span style="color: #B392F0">start</span><span style="color: #E1E4E8">(() </span><span style="color: #F97583">-&gt;</span><span style="color: #E1E4E8"> service.</span><span style="color: #B392F0">transfer</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">2</span><span style="color: #E1E4E8">, </span><span style="color: #79B8FF">1</span><span style="color: #E1E4E8">, </span><span style="color: #79B8FF">4_000</span><span style="color: #E1E4E8">));</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E1E4E8">        aliceToBob.</span><span style="color: #B392F0">join</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">2_000</span><span style="color: #E1E4E8">);</span></span>
<span class="line"><span style="color: #E1E4E8">        bobToAlice.</span><span style="color: #B392F0">join</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">10</span><span style="color: #E1E4E8">);</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">assertThat</span><span style="color: #E1E4E8">(aliceToBob.</span><span style="color: #B392F0">isAlive</span><span style="color: #E1E4E8">()).</span><span style="color: #B392F0">isTrue</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">assertThat</span><span style="color: #E1E4E8">(bobToAlice.</span><span style="color: #B392F0">isAlive</span><span style="color: #E1E4E8">()).</span><span style="color: #B392F0">isTrue</span><span style="color: #E1E4E8">();</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">assertThat</span><span style="color: #E1E4E8">(aliceToBob.</span><span style="color: #B392F0">getState</span><span style="color: #E1E4E8">()).</span><span style="color: #B392F0">isEqualTo</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">Thread</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">State</span><span style="color: #E1E4E8">.</span><span style="color: #79B8FF">BLOCKED</span><span style="color: #E1E4E8">);</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">assertThat</span><span style="color: #E1E4E8">(bobToAlice.</span><span style="color: #B392F0">getState</span><span style="color: #E1E4E8">()).</span><span style="color: #B392F0">isEqualTo</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">Thread</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">State</span><span style="color: #E1E4E8">.</span><span style="color: #79B8FF">BLOCKED</span><span style="color: #E1E4E8">);</span></span>
<span class="line"></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">List</span><span style="color: #F97583">&lt;</span><span style="color: #B392F0">DeadlockedThread</span><span style="color: #F97583">&gt;</span><span style="color: #E1E4E8"> cycle </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">mine</span><span style="color: #E1E4E8">(detector.</span><span style="color: #B392F0">findDeadlocks</span><span style="color: #E1E4E8">(), </span><span style="color: #9ECBFF">&quot;transfer-&quot;</span><span style="color: #E1E4E8">);</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">assertThat</span><span style="color: #E1E4E8">(cycle).</span><span style="color: #B392F0">extracting</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">DeadlockedThread</span><span style="color: #F97583">::</span><span style="color: #E1E4E8">threadName)</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">                .</span><span style="color: #B392F0">containsExactlyInAnyOrder</span><span style="color: #E1E4E8">(</span><span style="color: #9ECBFF">&quot;transfer-1-to-2&quot;</span><span style="color: #E1E4E8">, </span><span style="color: #9ECBFF">&quot;transfer-2-to-1&quot;</span><span style="color: #E1E4E8">);</span></span></code></pre></div>



<p class="wp-block-paragraph">The run printed this (on our 4-core cloud build machine; your timings will differ):</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">&#091;bug&#093; after 2 s: transfer-1-to-2 BLOCKED, transfer-2-to-1 BLOCKED; findDeadlockedThreads() returned 2 of them</span></span>
<span class="line"><span style="color: #E1E4E8">&#091;bug&#093; transfer-1-to-2 waits for dev.gradient.deadlock.PausingAccount@38831718, held by transfer-2-to-1</span></span></code></pre></div>



<p class="wp-block-paragraph">The same test ran <code>jstack</code> against its own process. Trimmed, the dump names the cycle and the method each thread is stuck in:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">Found one Java-level deadlock:</span></span>
<span class="line"><span style="color: #E1E4E8">=============================</span></span>
<span class="line"><span style="color: #E1E4E8">&quot;transfer-1-to-2&quot;:</span></span>
<span class="line"><span style="color: #E1E4E8">  waiting to lock monitor 0x00007fed30000e00 (object 0x0000000731e1e580, a dev.gradient.deadlock.PausingAccount),</span></span>
<span class="line"><span style="color: #E1E4E8">  which is held by &quot;transfer-2-to-1&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E1E4E8">&quot;transfer-1-to-2&quot;:</span></span>
<span class="line"><span style="color: #E1E4E8">	at dev.gradient.deadlock.buggy.TransferService.transfer(TransferService.java:29)</span></span>
<span class="line"><span style="color: #E1E4E8">	- waiting to lock &lt;0x0000000731e1e580&gt; (a dev.gradient.deadlock.PausingAccount)</span></span>
<span class="line"><span style="color: #E1E4E8">	- locked &lt;0x0000000731e1e4a0&gt; (a dev.gradient.deadlock.PausingAccount)</span></span></code></pre></div>



<p class="wp-block-paragraph">You don&#8217;t need the barrier if you have load. A second test ran 8 threads doing random transfers between 10 plain accounts and polled the JVM every millisecond. The very first check, 1 ms in, already found a cycle. Within 200 ms the other six threads had walked into the two locked accounts too: 8 of 8 threads <code>BLOCKED</code> after 902 transfers in all, and 0 transfers in the next 500 ms.</p>



<h2 class="wp-block-heading">Why does a Java deadlock example like this survive review?</h2>



<p class="wp-block-paragraph">Every single-threaded test passes. Ours ran 100,000 alternating transfers, 1 to 2 then 2 to 1, on one thread, and all of them finished. With one thread, nobody else ever holds the second lock, so there is nothing to wait for.</p>



<p class="wp-block-paragraph">Code review reads one method at a time, and this one is locally correct. The bug lives in a pair of calls, <code>transfer(a, b)</code> and <code>transfer(b, a)</code>, that never sit side by side in a diff. In our view, opposite transfers are rare in test environments and common in production: refunds, split bills.</p>



<p class="wp-block-paragraph">And because the freeze is quiet, your first symptom is a slow service, then a hung one.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/10/java-deadlock-example-graphic-1.webp" alt="The same transfer code: 100,000 alternating transfers on one thread all finished; with 8 threads on 10 accounts the first check found a cycle at 1 ms, 8 of 8 threads were BLOCKED within 200 ms though the cycle held only 2, and 0 transfers finished in the next 500 ms" class="wp-image-10170" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/java-deadlock-example-graphic-1.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/java-deadlock-example-graphic-1-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/java-deadlock-example-graphic-1-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/java-deadlock-example-graphic-1-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Source: Gradient verify run, October 2026</figcaption></figure>



<h2 class="wp-block-heading">How do you spot a deadlock in a running service?</h2>



<p class="wp-block-paragraph">The JVM can find the cycle even though it won&#8217;t break it. <code>ThreadMXBean.findDeadlockedThreads()</code> returns the platform threads stuck in a cycle on monitors or on <em>ownable synchronizers</em>, the JDK&#8217;s name for locks like <code>ReentrantLock</code> that a thread owns until it unlocks. Our test wraps it in a small Spring component:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-start:18;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2f363c;color:#d3d7dd">DeadlockDetector.java</span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">private</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">final</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">ThreadMXBean</span><span style="color: #E1E4E8"> threads </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">ManagementFactory</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">getThreadMXBean</span><span style="color: #E1E4E8">();</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">List</span><span style="color: #F97583">&lt;</span><span style="color: #B392F0">DeadlockedThread</span><span style="color: #F97583">&gt;</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">findDeadlocks</span><span style="color: #E1E4E8">() {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #F97583">long</span><span style="color: #E1E4E8">[] ids </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> threads.</span><span style="color: #B392F0">findDeadlockedThreads</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">if</span><span style="color: #E1E4E8"> (ids </span><span style="color: #F97583">==</span><span style="color: #E1E4E8"> </span><span style="color: #79B8FF">null</span><span style="color: #E1E4E8">) {</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">List</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">of</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">Arrays</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">stream</span><span style="color: #E1E4E8">(threads.</span><span style="color: #B392F0">getThreadInfo</span><span style="color: #E1E4E8">(ids))</span></span>
<span class="line"><span style="color: #E1E4E8">                .</span><span style="color: #B392F0">filter</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">Objects</span><span style="color: #F97583">::</span><span style="color: #E1E4E8">nonNull)</span></span>
<span class="line"><span style="color: #E1E4E8">                .</span><span style="color: #B392F0">map</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">DeadlockDetector</span><span style="color: #F97583">::</span><span style="color: #E1E4E8">describe)</span></span>
<span class="line"><span style="color: #E1E4E8">                .</span><span style="color: #B392F0">toList</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">private</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">static</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">DeadlockedThread</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">describe</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">ThreadInfo</span><span style="color: #E1E4E8"> info) {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">DeadlockedThread</span><span style="color: #E1E4E8">(info.</span><span style="color: #B392F0">getThreadId</span><span style="color: #E1E4E8">(), info.</span><span style="color: #B392F0">getThreadName</span><span style="color: #E1E4E8">(), info.</span><span style="color: #B392F0">getLockName</span><span style="color: #E1E4E8">(), info.</span><span style="color: #B392F0">getLockOwnerName</span><span style="color: #E1E4E8">());</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span></code></pre></div>



<p class="wp-block-paragraph">Line 21 is the whole check; line 25 turns ids into names and lock owners you can log. The Javadoc calls it a tool &#8220;for troubleshooting use, but not for synchronization control&#8221; that &#8220;might be an expensive operation&#8221;. Run it from a health check or a metric every few seconds, not before each transfer.</p>



<p class="wp-block-paragraph">Spring Boot gives you two helpers. Actuator&#8217;s <code>/actuator/threaddump</code> returns each thread&#8217;s state, <code>lockOwnerName</code> and held monitors. Micrometer 1.17.1, the version Boot 4.1.1 manages, has a <code>JvmThreadDeadlockMetrics</code> binder with a <code>jvm.threads.deadlocked</code> gauge. Boot&#8217;s JVM metrics auto-configuration doesn&#8217;t register it, so declare <code>new JvmThreadDeadlockMetrics()</code> as a <code>MeterBinder</code> bean.</p>



<p class="wp-block-paragraph">On virtual threads, the Javadoc warns that &#8220;cycles that include virtual threads are not found by this method&#8221;. Since JEP 491 (Java 24), a virtual thread blocked on <code>synchronized</code> releases its carrier, but the cycle itself remains, unnamed; request timeouts become your alarm.</p>



<h2 class="wp-block-heading">Fix 1: one global lock order, lowest id first</h2>



<p class="wp-block-paragraph">Make the cycle impossible. If every thread takes locks in the same order, nobody can hold a &#8220;later&#8221; lock while waiting for an &#8220;earlier&#8221; one. That rule is <em>lock ordering</em>, and here the order is the account id:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-start:18;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2f363c;color:#d3d7dd">OrderedLockTransferService.java (fix 1)</span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">void</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">transfer</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">long</span><span style="color: #E1E4E8"> fromId, </span><span style="color: #F97583">long</span><span style="color: #E1E4E8"> toId, </span><span style="color: #F97583">long</span><span style="color: #E1E4E8"> amountCents) {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">if</span><span style="color: #E1E4E8"> (fromId </span><span style="color: #F97583">==</span><span style="color: #E1E4E8"> toId) {</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #F97583">throw</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">IllegalArgumentException</span><span style="color: #E1E4E8">(</span><span style="color: #9ECBFF">&quot;Cannot transfer to the same account&quot;</span><span style="color: #E1E4E8">);</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">Account</span><span style="color: #E1E4E8"> from </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> accounts.</span><span style="color: #B392F0">require</span><span style="color: #E1E4E8">(fromId);</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">Account</span><span style="color: #E1E4E8"> to </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> accounts.</span><span style="color: #B392F0">require</span><span style="color: #E1E4E8">(toId);</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">Account</span><span style="color: #E1E4E8"> first </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> fromId </span><span style="color: #F97583">&lt;</span><span style="color: #E1E4E8"> toId </span><span style="color: #F97583">?</span><span style="color: #E1E4E8"> from </span><span style="color: #F97583">:</span><span style="color: #E1E4E8"> to;</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">Account</span><span style="color: #E1E4E8"> second </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> fromId </span><span style="color: #F97583">&lt;</span><span style="color: #E1E4E8"> toId </span><span style="color: #F97583">?</span><span style="color: #E1E4E8"> to </span><span style="color: #F97583">:</span><span style="color: #E1E4E8"> from;</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #F97583">synchronized</span><span style="color: #E1E4E8"> (first) {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">            </span><span style="color: #F97583">synchronized</span><span style="color: #E1E4E8"> (second) {</span></span>
<span class="line"><span style="color: #E1E4E8">                </span><span style="color: #F97583">if</span><span style="color: #E1E4E8"> (from.</span><span style="color: #B392F0">balanceCents</span><span style="color: #E1E4E8">() </span><span style="color: #F97583">&lt;</span><span style="color: #E1E4E8"> amountCents) {</span></span>
<span class="line"><span style="color: #E1E4E8">                    </span><span style="color: #F97583">throw</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">InsufficientFundsException</span><span style="color: #E1E4E8">(fromId, amountCents, from.</span><span style="color: #B392F0">balanceCents</span><span style="color: #E1E4E8">());</span></span>
<span class="line"><span style="color: #E1E4E8">                }</span></span>
<span class="line"><span style="color: #E1E4E8">                from.</span><span style="color: #B392F0">debit</span><span style="color: #E1E4E8">(amountCents);</span></span>
<span class="line"><span style="color: #E1E4E8">                to.</span><span style="color: #B392F0">credit</span><span style="color: #E1E4E8">(amountCents);</span></span>
<span class="line"><span style="color: #E1E4E8">            }</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span></code></pre></div>



<p class="wp-block-paragraph">Lines 24 and 25 pick the lower id as <code>first</code> whatever the direction, so <code>transfer(1, 2)</code> and <code>transfer(2, 1)</code> both lock account 1 first. The funds check moves inside both locks, where it checks the balance that will really be debited.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/10/java-deadlock-example-graphic-2.webp" alt="Three fixes compared: lock ordering takes both locks lowest id first and ran 400,000 transfers in 105 ms; timed tryLock backs off 1 to 5 ms and took 16,835 ms with 5,263 back-offs; one owner thread per account takes no locks and took 3,334 ms" class="wp-image-10171" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/java-deadlock-example-graphic-2.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/java-deadlock-example-graphic-2-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/java-deadlock-example-graphic-2-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/java-deadlock-example-graphic-2-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Source: Gradient test project, 8 threads x 50,000 transfers</figcaption></figure>



<p class="wp-block-paragraph">Under the forced interleaving, both opposite transfers finished, with balances of 101,500 and 98,500 cents. Under load, 8 threads ran 50,000 random transfers each: 390,389 moved and 9,611 were declined for funds, in 105 ms. The total was 1,000,000 cents before and after.</p>



<p class="wp-block-paragraph">What you pay is discipline. Every method that takes two of these locks must use the same order, and the order needs a key no two accounts share. Ordering also says nothing about locks you take indirectly, such as calling a listener while holding a monitor.</p>



<h2 class="wp-block-heading">Fix 2: when you can&#8217;t control the order, give up and retry</h2>



<p class="wp-block-paragraph">Sometimes the second lock isn&#8217;t yours to order: it sits in a library, or you only learn which locks you need halfway through. Then make the wait finite. <code>ReentrantLock.tryLock(timeout, unit)</code> returns <code>false</code> instead of waiting forever, and the thread can release what it holds and try again.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-start:29;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2f363c;color:#d3d7dd">TryLockTransferService.java (fix 2)</span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">Account</span><span style="color: #E1E4E8"> from </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> accounts.</span><span style="color: #B392F0">require</span><span style="color: #E1E4E8">(fromId);</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #B392F0">Account</span><span style="color: #E1E4E8"> to </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> accounts.</span><span style="color: #B392F0">require</span><span style="color: #E1E4E8">(toId);</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">long</span><span style="color: #E1E4E8"> deadline </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">System</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">nanoTime</span><span style="color: #E1E4E8">() </span><span style="color: #F97583">+</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">TimeUnit</span><span style="color: #E1E4E8">.</span><span style="color: #79B8FF">MILLISECONDS</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">toNanos</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">DEADLINE_MILLIS</span><span style="color: #E1E4E8">);</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">while</span><span style="color: #E1E4E8"> (</span><span style="color: #79B8FF">true</span><span style="color: #E1E4E8">) {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">            </span><span style="color: #F97583">if</span><span style="color: #E1E4E8"> (from.</span><span style="color: #B392F0">lock</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">tryLock</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">LOCK_WAIT_MILLIS</span><span style="color: #E1E4E8">, </span><span style="color: #B392F0">TimeUnit</span><span style="color: #E1E4E8">.</span><span style="color: #79B8FF">MILLISECONDS</span><span style="color: #E1E4E8">)) {</span></span>
<span class="line"><span style="color: #E1E4E8">                </span><span style="color: #F97583">try</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">                    </span><span style="color: #F97583">if</span><span style="color: #E1E4E8"> (from.</span><span style="color: #B392F0">balanceCents</span><span style="color: #E1E4E8">() </span><span style="color: #F97583">&lt;</span><span style="color: #E1E4E8"> amountCents) {</span></span>
<span class="line"><span style="color: #E1E4E8">                        </span><span style="color: #F97583">throw</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">InsufficientFundsException</span><span style="color: #E1E4E8">(fromId, amountCents, from.</span><span style="color: #B392F0">balanceCents</span><span style="color: #E1E4E8">());</span></span>
<span class="line"><span style="color: #E1E4E8">                    }</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">                    </span><span style="color: #F97583">if</span><span style="color: #E1E4E8"> (to.</span><span style="color: #B392F0">lock</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">tryLock</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">LOCK_WAIT_MILLIS</span><span style="color: #E1E4E8">, </span><span style="color: #B392F0">TimeUnit</span><span style="color: #E1E4E8">.</span><span style="color: #79B8FF">MILLISECONDS</span><span style="color: #E1E4E8">)) {</span></span>
<span class="line"><span style="color: #E1E4E8">                        </span><span style="color: #F97583">try</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">                            from.</span><span style="color: #B392F0">debit</span><span style="color: #E1E4E8">(amountCents);</span></span>
<span class="line"><span style="color: #E1E4E8">                            to.</span><span style="color: #B392F0">credit</span><span style="color: #E1E4E8">(amountCents);</span></span>
<span class="line"><span style="color: #E1E4E8">                            </span><span style="color: #F97583">return</span><span style="color: #E1E4E8">;</span></span>
<span class="line"><span style="color: #E1E4E8">                        } </span><span style="color: #F97583">finally</span><span style="color: #E1E4E8"> {</span></span>
<span class="line"><span style="color: #E1E4E8">                            to.</span><span style="color: #B392F0">lock</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">unlock</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #E1E4E8">                        }</span></span>
<span class="line"><span style="color: #E1E4E8">                    }</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">                } </span><span style="color: #F97583">finally</span><span style="color: #E1E4E8"> {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">                    from.</span><span style="color: #B392F0">lock</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">unlock</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #E1E4E8">                }</span></span>
<span class="line"><span style="color: #E1E4E8">            }</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #F97583">if</span><span style="color: #E1E4E8"> (</span><span style="color: #B392F0">System</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">nanoTime</span><span style="color: #E1E4E8">() </span><span style="color: #F97583">-</span><span style="color: #E1E4E8"> deadline </span><span style="color: #F97583">&gt;</span><span style="color: #E1E4E8"> </span><span style="color: #79B8FF">0</span><span style="color: #E1E4E8">) {</span></span>
<span class="line"><span style="color: #E1E4E8">                </span><span style="color: #F97583">throw</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">TransferTimeoutException</span><span style="color: #E1E4E8">(fromId, toId);</span></span>
<span class="line"><span style="color: #E1E4E8">            }</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">            backOffs.</span><span style="color: #B392F0">increment</span><span style="color: #E1E4E8">();</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">            </span><span style="color: #B392F0">Thread</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">sleep</span><span style="color: #E1E4E8">(</span><span style="color: #B392F0">ThreadLocalRandom</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">current</span><span style="color: #E1E4E8">().</span><span style="color: #B392F0">nextLong</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">1</span><span style="color: #E1E4E8">, </span><span style="color: #79B8FF">6</span><span style="color: #E1E4E8">));</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span></code></pre></div>



<p class="wp-block-paragraph">This keeps the buggy order on purpose: source first (line 33), then destination (line 38). When the second <code>tryLock</code> fails, the <code>finally</code> on line 48 releases the source so the other transfer can finish. Line 55 sleeps a random 1 to 5 ms first. Without that jitter, both threads could retry in lockstep forever, a <em>livelock</em>: threads that are busy, not blocked, yet make no progress.</p>



<p class="wp-block-paragraph">Against the same barrier, both transfers finished in 15 ms after 1 back-off. Under the full load, all 400,000 transfers ended with money conserved and 0 timeouts, but they took 16,835 ms and 5,263 back-offs. That&#8217;s a steep bill for a fix whose only job is to cover the cases fix 1 can&#8217;t.</p>



<p class="wp-block-paragraph">It also adds a failure. A caller can get <code>TransferTimeoutException</code> after two seconds, so the transfer must be safe to retry. Export the back-off count; a climbing count is your early warning.</p>



<h2 class="wp-block-heading">Fix 3: give each account one owner thread</h2>



<p class="wp-block-paragraph">The third design removes shared locks altogether. Each account belongs to one single-threaded partition, picked from its id, and only that thread touches its balance. A transfer becomes two messages: debit on the source&#8217;s thread, then credit on the destination&#8217;s.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-start:29;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2f363c;color:#d3d7dd">PartitionedTransferService.java (fix 3)</span><pre class="shiki github-dark" style="background-color: #24292e" tabindex="0"><code><span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">CompletableFuture</span><span style="color: #F97583">&lt;</span><span style="color: #B392F0">Void</span><span style="color: #F97583">&gt;</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">transfer</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">long</span><span style="color: #E1E4E8"> fromId, </span><span style="color: #F97583">long</span><span style="color: #E1E4E8"> toId, </span><span style="color: #F97583">long</span><span style="color: #E1E4E8"> amountCents) {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">if</span><span style="color: #E1E4E8"> (fromId </span><span style="color: #F97583">==</span><span style="color: #E1E4E8"> toId) {</span></span>
<span class="line"><span style="color: #E1E4E8">            </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">CompletableFuture</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">failedFuture</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">IllegalArgumentException</span><span style="color: #E1E4E8">(</span><span style="color: #9ECBFF">&quot;Cannot transfer to the same account&quot;</span><span style="color: #E1E4E8">));</span></span>
<span class="line"><span style="color: #E1E4E8">        }</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">CompletableFuture</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">runAsync</span><span style="color: #E1E4E8">(() </span><span style="color: #F97583">-&gt;</span><span style="color: #E1E4E8"> {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">            </span><span style="color: #F97583">var</span><span style="color: #E1E4E8"> from </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> accounts.</span><span style="color: #B392F0">require</span><span style="color: #E1E4E8">(fromId);</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">            </span><span style="color: #F97583">if</span><span style="color: #E1E4E8"> (from.</span><span style="color: #B392F0">balanceCents</span><span style="color: #E1E4E8">() </span><span style="color: #F97583">&lt;</span><span style="color: #E1E4E8"> amountCents) {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">                </span><span style="color: #F97583">throw</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">InsufficientFundsException</span><span style="color: #E1E4E8">(fromId, amountCents, from.</span><span style="color: #B392F0">balanceCents</span><span style="color: #E1E4E8">());</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">            }</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">            from.</span><span style="color: #B392F0">debit</span><span style="color: #E1E4E8">(amountCents);</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        }, </span><span style="color: #B392F0">ownerOf</span><span style="color: #E1E4E8">(fromId)).</span><span style="color: #B392F0">thenRunAsync</span><span style="color: #E1E4E8">(() </span><span style="color: #F97583">-&gt;</span><span style="color: #E1E4E8"> accounts.</span><span style="color: #B392F0">require</span><span style="color: #E1E4E8">(toId).</span><span style="color: #B392F0">credit</span><span style="color: #E1E4E8">(amountCents), </span><span style="color: #B392F0">ownerOf</span><span style="color: #E1E4E8">(toId));</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">long</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">balanceOf</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">long</span><span style="color: #E1E4E8"> accountId) {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">CompletableFuture</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">supplyAsync</span><span style="color: #E1E4E8">(() </span><span style="color: #F97583">-&gt;</span><span style="color: #E1E4E8"> accounts.</span><span style="color: #B392F0">require</span><span style="color: #E1E4E8">(accountId).</span><span style="color: #B392F0">balanceCents</span><span style="color: #E1E4E8">(), </span><span style="color: #B392F0">ownerOf</span><span style="color: #E1E4E8">(accountId)).</span><span style="color: #B392F0">join</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">    </span><span style="color: #F97583">private</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">ExecutorService</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">ownerOf</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">long</span><span style="color: #E1E4E8"> accountId) {</span></span>
<span class="line cbp-line-highlight"><span style="color: #E1E4E8">        </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> partitions&#091;(</span><span style="color: #F97583">int</span><span style="color: #E1E4E8">) </span><span style="color: #B392F0">Math</span><span style="color: #E1E4E8">.</span><span style="color: #B392F0">floorMod</span><span style="color: #E1E4E8">(accountId, partitions.length)&#093;;</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span></code></pre></div>



<p class="wp-block-paragraph">Lines 33 to 38 run the check and the debit on the source&#8217;s owner; line 39 hands the credit to the destination&#8217;s owner. No thread holds one account while waiting for another, so there is nothing to order. <code>Executors.newSingleThreadExecutor</code> guarantees its tasks &#8220;execute sequentially&#8221;, which makes the unlocked balance safe.</p>



<p class="wp-block-paragraph">With 4 ledger threads, the same load moved 391,904 transfers and declined 8,096, in 3,334 ms, with the total conserved. Our callers waited on each transfer&#8217;s future, which dominates that time.</p>



<p class="wp-block-paragraph">Between debit and credit the money is in flight, so reading both balances at that moment can come up short. A crash in that gap loses the credit unless the queue is durable. Each executor&#8217;s queue is unbounded, so a hot account can build an endless backlog.</p>



<h2 class="wp-block-heading">Which fix should a wallet service use?</h2>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th></th><th>Fix 1: lock ordering</th><th>Fix 2: timed <code>tryLock</code> + back-off</th><th>Fix 3: one owner per account</th></tr></thead><tbody><tr><td>Can it deadlock?</td><td>No, by construction</td><td>No; it times out instead</td><td>No locks to order</td></tr><tr><td>400,000 transfers, our run</td><td>105 ms</td><td>16,835 ms, 5,263 back-offs</td><td>3,334 ms (waiting per transfer)</td></tr><tr><td>New failure mode</td><td>None</td><td><code>TransferTimeoutException</code> after 2 s</td><td>Money in flight; queue backlog</td></tr><tr><td>Code change</td><td>Two lines</td><td>A retry loop and its tests</td><td>An async API for callers</td></tr><tr><td>Choose it when</td><td>You own every lock and can sort them</td><td>The lock set is dynamic or partly in a library</td><td>Accounts already live behind a queue</td></tr></tbody></table></figure>



<p class="wp-block-paragraph">Our default: fix 1 whenever the locks are yours. It&#8217;s the cheapest change and leaves the happy path untouched. Use fix 2 at the edges you can&#8217;t order, with a short timeout and an idempotent retry. We&#8217;d only reach for fix 3 when designing a ledger; it&#8217;s too big to be a patch for one method.</p>



<p class="wp-block-paragraph">What not to do: don&#8217;t poll <code>findDeadlockedThreads()</code> and interrupt threads to &#8220;recover&#8221;. The <code>Thread.interrupt()</code> Javadoc lists the waits it can end, and waiting for a monitor isn&#8217;t one of them. Don&#8217;t swap in one global lock for all accounts either: it can&#8217;t deadlock, but it turns every transfer into one queue.</p>



<h2 class="wp-block-heading">Does the database have the same problem?</h2>



<p class="wp-block-paragraph">Yes, and most real wallets keep balances in a database, where JVM locks would only protect one server.</p>



<p class="wp-block-paragraph">In Spring Boot, the database version is a <code>@Transactional</code> transfer that loads the source with <code>@Lock(LockModeType.PESSIMISTIC_WRITE)</code>, then the destination. On H2, our oversell test saw Hibernate run that lock as <code>select ... for update</code>; on PostgreSQL, Hibernate 7.4 sends <code>for no key update</code>, which still makes a second locker wait. Two opposite transfers then lock the two rows in opposite orders.</p>



<p class="wp-block-paragraph">Here the database notices. The PostgreSQL manual says it &#8220;automatically detects deadlock situations and resolves them by aborting one of the transactions involved&#8221;. It waits for <code>deadlock_timeout</code>, one second by default, before checking. One transfer fails and has to be retried.</p>



<p class="wp-block-paragraph">So does fix 1: lock the two rows in id order, whichever is the source. PostgreSQL&#8217;s advice is to &#8220;acquire locks on multiple objects in a consistent order&#8221;. We covered <code>SELECT ... FOR UPDATE</code> and its costs in <a href="https://www.gradientco.net/engineering/spring-boot-race-condition/">The Spring Boot race condition that sells your last unit twice</a>.</p>



<h2 class="wp-block-heading">What changes with more servers, threads and retries?</h2>



<p class="wp-block-paragraph">More servers make in-memory locks the wrong tool, since two instances never see each other&#8217;s monitors. Balances move to database rows, and the deadlock moves with them, where it surfaces as an error.</p>



<p class="wp-block-paragraph">Add threads and the freeze grows. In our run, a cycle of two threads stopped all eight, because the stuck pair held two of the ten accounts. Request threads pile up the same way until the pool is empty, much like the pool lock-up in <a href="https://www.gradientco.net/engineering/hikari-connection-is-not-available/">Hikari connection is not available? Your REQUIRES_NEW is starving the pool</a>.</p>



<p class="wp-block-paragraph">Client retries during a freeze only add blocked threads. Retries inside fix 2, or after a database deadlock error, are what make those fixes work.</p>



<h3 class="wp-block-heading">Which metric goes red first?</h3>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/10/java-deadlock-example-graphic-3-1.webp" alt="Where a lock cycle shows up: blocked threads climbing while CPU falls is the hint, jvm.threads.deadlocked above zero is the alarm, PostgreSQL counts deadlocks in pg_stat_database after aborting one transaction, and cycles with virtual threads show up only as timeouts" class="wp-image-10174" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/java-deadlock-example-graphic-3-1.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/java-deadlock-example-graphic-3-1-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/java-deadlock-example-graphic-3-1-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/java-deadlock-example-graphic-3-1-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Sources: Micrometer 1.17.1, ThreadMXBean Javadoc, PostgreSQL 18 docs</figcaption></figure>



<p class="wp-block-paragraph"><code>jvm.threads.deadlocked</code> above zero is the alarm with no false positives. Before it, <code>jvm.threads.states</code> with <code>state=blocked</code> climbing while CPU falls is the shape of a freeze. In PostgreSQL, a rising <code>deadlocks</code> count in <code>pg_stat_database</code> means some path still locks rows in two orders.</p>



<h2 class="wp-block-heading">Back to that review comment</h2>



<p class="wp-block-paragraph">The LGTM was right about everything it checked. It just never asked who picks the first lock. Add that question to any review of a method that takes two locks, and if the answer is &#8220;the caller&#8221;, sort them.</p>



<h2 class="wp-block-heading">What should you search your codebase for?</h2>



<ul class="wp-block-list">
<li>Nested <code>synchronized</code> blocks, or a <code>synchronized</code> method that calls another object&#8217;s <code>synchronized</code> method.</li>



<li>Two <code>lock()</code> or <code>tryLock()</code> calls in one method, especially on parameters such as <code>from</code> and <code>to</code>.</li>



<li><code>@Lock(LockModeType.PESSIMISTIC_WRITE)</code> or <code>FOR UPDATE</code> queries run twice in one transaction on rows the caller chose.</li>



<li>Listeners, callbacks or repository calls made while a monitor is held.</li>



<li>Thread dumps from past incidents: search them for &#8220;Java-level deadlock&#8221;.</li>
</ul>



<h2 class="wp-block-heading">Frequently asked questions</h2>


<div id="rank-math-faq" class="rank-math-block">
<div class="rank-math-list ">
<div id="faq-question-1" class="rank-math-list-item">
<h3 class="rank-math-question ">How do I detect a deadlock in Java at runtime?</h3>
<div class="rank-math-answer ">

<p>Call <code>ManagementFactory.getThreadMXBean().findDeadlockedThreads()</code>, which returns the ids of platform threads in a lock cycle, or <code>null</code> if there is none. From outside the process, <code>jstack &lt;pid&gt;</code> prints a &#8220;Found one Java-level deadlock&#8221; section with the stack of each waiting thread.</p>

</div>
</div>
<div id="faq-question-2" class="rank-math-list-item">
<h3 class="rank-math-question ">Does ReentrantLock prevent deadlocks?</h3>
<div class="rank-math-answer ">

<p>Not by itself. <code>lock()</code> waits forever, just like <code>synchronized</code>. Only <code>tryLock</code> with a timeout, or <code>lockInterruptibly()</code> plus an interrupt, gives a waiting thread a way out.</p>

</div>
</div>
<div id="faq-question-3" class="rank-math-list-item">
<h3 class="rank-math-question ">Can a deadlock happen with only one lock?</h3>
<div class="rank-math-answer ">

<p>Not a lock cycle, which needs two locks. A thread can still wait forever on one <code>ReentrantLock</code> that another thread never unlocked, for example when <code>unlock()</code> isn&#8217;t in a <code>finally</code> block. That is a leak, and <code>findDeadlockedThreads()</code> won&#8217;t report it.</p>

</div>
</div>
<div id="faq-question-4" class="rank-math-list-item">
<h3 class="rank-math-question ">Does Spring&#8217;s @Transactional prevent deadlocks?</h3>
<div class="rank-math-answer ">

<p>No. It opens and commits a database transaction, and the database takes row locks in the order your code touches the rows. PostgreSQL breaks the cycle by aborting one transaction; Spring then throws, and nothing retries it unless you add a retry.</p>

</div>
</div>
</div>
</div>


<h2 class="wp-block-heading">What we do not know yet</h2>



<p class="wp-block-paragraph">We ran every test of this Java deadlock example in one JVM on a 4-core cloud build machine in October 2026, so the timings come from one run. We didn&#8217;t test the row-lock version on PostgreSQL or MySQL; that section rests on the PostgreSQL manual, and MySQL&#8217;s InnoDB pages were unavailable when we checked. We also didn&#8217;t test virtual threads or the Micrometer gauge in a running service.</p>



<p class="wp-block-paragraph"><strong>Read next:</strong> another story of threads handing work to each other, in <a href="https://www.gradientco.net/engineering/spring-boot-mdc-async/">Spring Boot MDC async logs lose the request id, then log the wrong one</a>.</p>



<p class="wp-block-paragraph"><em>Researched and drafted with AI assistance; facts checked against the sources linked in this article.</em></p>



<h2 class="wp-block-heading">References</h2>



<ol class="wp-block-list">
<li>The Java Language Specification, Java SE 25 Edition, Chapter 17: Threads and Locks, Oracle: <a href="https://docs.oracle.com/javase/specs/jls/se25/html/jls-17.html" target="_blank" rel="noopener">docs.oracle.com</a></li>



<li>ThreadMXBean, Java SE 25 API documentation: <a href="https://docs.oracle.com/en/java/javase/25/docs/api/java.management/java/lang/management/ThreadMXBean.html" target="_blank" rel="noopener">docs.oracle.com</a></li>



<li>Thread, Java SE 25 API documentation: <a href="https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Thread.html" target="_blank" rel="noopener">docs.oracle.com</a></li>



<li>ReentrantLock, Java SE 25 API documentation: <a href="https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/concurrent/locks/ReentrantLock.html" target="_blank" rel="noopener">docs.oracle.com</a></li>



<li>Lock, Java SE 25 API documentation: <a href="https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/concurrent/locks/Lock.html" target="_blank" rel="noopener">docs.oracle.com</a></li>



<li>Executors, Java SE 25 API documentation: <a href="https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/concurrent/Executors.html" target="_blank" rel="noopener">docs.oracle.com</a></li>



<li>JEP 491: Synchronize Virtual Threads without Pinning, OpenJDK, JDK 24: <a href="https://openjdk.org/jeps/491" target="_blank" rel="noopener">openjdk.org</a></li>



<li>Deadlock, The Java Tutorials, Oracle: <a href="https://docs.oracle.com/javase/tutorial/essential/concurrency/deadlock.html" target="_blank" rel="noopener">docs.oracle.com</a></li>



<li>Starvation and Livelock, The Java Tutorials, Oracle: <a href="https://docs.oracle.com/javase/tutorial/essential/concurrency/starvelive.html" target="_blank" rel="noopener">docs.oracle.com</a></li>



<li>Thread Dump (threaddump) endpoint, Spring Boot Actuator REST API, 4.1.1: <a href="https://docs.spring.io/spring-boot/api/rest/actuator/threaddump.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>Metrics, Spring Boot reference, 4.1.1: <a href="https://docs.spring.io/spring-boot/reference/actuator/metrics.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>JvmMetricsAutoConfiguration.java, Spring Boot source code, 4.1.1: <a href="https://github.com/spring-projects/spring-boot/blob/v4.1.1/module/spring-boot-micrometer-metrics/src/main/java/org/springframework/boot/micrometer/metrics/autoconfigure/jvm/JvmMetricsAutoConfiguration.java" target="_blank" rel="noopener">github.com</a></li>



<li>JVM Metrics, Micrometer reference, 1.17.1: <a href="https://docs.micrometer.io/micrometer/reference/reference/jvm.html" target="_blank" rel="noopener">docs.micrometer.io</a></li>



<li>JvmThreadDeadlockMetrics.java, Micrometer source code, 1.17.1: <a href="https://github.com/micrometer-metrics/micrometer/blob/v1.17.1/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jvm/JvmThreadDeadlockMetrics.java" target="_blank" rel="noopener">github.com</a></li>



<li>Locking, Spring Data JPA reference, 4.1.1: <a href="https://docs.spring.io/spring-data/jpa/reference/jpa/locking.html" target="_blank" rel="noopener">docs.spring.io</a></li>



<li>PostgreSQLDialect.java, Hibernate ORM source code, 7.4.5: <a href="https://github.com/hibernate/hibernate-orm/blob/7.4.5/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLDialect.java" target="_blank" rel="noopener">github.com</a></li>



<li>Explicit Locking (13.3.4 Deadlocks), PostgreSQL 18 documentation: <a href="https://www.postgresql.org/docs/current/explicit-locking.html" target="_blank" rel="noopener">postgresql.org</a></li>



<li>Lock Management (deadlock_timeout), PostgreSQL 18 documentation: <a href="https://www.postgresql.org/docs/current/runtime-config-locks.html" target="_blank" rel="noopener">postgresql.org</a></li>



<li>The Cumulative Statistics System (pg_stat_database), PostgreSQL 18 documentation: <a href="https://www.postgresql.org/docs/current/monitoring-stats.html" target="_blank" rel="noopener">postgresql.org</a></li>
</ol>
]]></content:encoded>
					
		
		<enclosure url="https://www.gradientco.net/wp-content/uploads/2026/10/java-deadlock-example-clip.mp4" length="214326" type="video/mp4" />

			</item>
		<item>
		<title>PHP2Shell Explained: Is Your Website Affected and How to Update PHP</title>
		<link>https://www.gradientco.net/tech-science/php2shell-php-update-guide/</link>
		
		<dc:creator><![CDATA[Nabeel Ahmed]]></dc:creator>
		<pubDate>Tue, 27 Oct 2026 03:00:01 +0000</pubDate>
				<category><![CDATA[Tech & Science]]></category>
		<category><![CDATA[technology]]></category>
		<guid isPermaLink="false">https://www.gradientco.net/?p=10184</guid>

					<description><![CDATA[Two critical PHP flaws, nicknamed PHP2Shell, can let an attacker run code on a web server without logging in. PHP 8.5.12, 8.4.27, 8.3.36 and 8.2.35 fix them. Here is how to check your version and update on shared hosting, a VPS or a managed host.]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">PHP2Shell is the nickname for two critical flaws in PHP, the server language WordPress runs on, that can let an attacker run code on a web server without logging in. The PHP project fixed both on 21 October 2026 in versions 8.5.12, 8.4.27, 8.3.36 and 8.2.35, and its <a href="https://www.php.net/archive/2026.php" target="_blank" rel="noopener">security advisory</a> asks every user to update immediately. If your site runs an older patch level, update now or ask your host to.</p>



<h2 class="wp-block-heading">What are the two PHP2Shell flaws?</h2>



<p class="wp-block-paragraph">Both are memory-handling bugs inside PHP itself, not in WordPress or a plugin. The weakness sits below your application, so a fully updated WordPress install is still exposed if the PHP underneath isn&#8217;t.</p>



<p class="wp-block-paragraph">The PHP security team describes them in plain terms. <strong>CVE-2026-109618</strong> (&#8220;RCE via OOM corruption&#8221;) happens when PHP runs out of memory midway through setting up a request, and the clean-up step then trusts half-built data. <strong>CVE-2026-109619</strong> (&#8220;RCE via stale global state&#8221;) leaves leftover data from one request in memory after it&#8217;s been freed, where a later request handled by the same worker can pick it up.</p>



<p class="wp-block-paragraph">Each carries a CVSS v4.0 score of 9.2, rated Critical, in the <a href="https://github.com/php/php-src/security/advisories/GHSA-w8hw-w9fx-j79w" target="_blank" rel="noopener">GitHub advisory for CVE-2026-109618</a> and the <a href="https://github.com/php/php-src/security/advisories/GHSA-6wmf-4rv5-p3rp" target="_blank" rel="noopener">one for CVE-2026-109619</a>. The nickname came from researchers, and <a href="https://thehackernews.com/2026/10/php2shell-two-unpatched-critical-php.html" target="_blank" rel="noopener">The Hacker News</a> is blunt about it: &#8220;the name is theirs, not PHP&#8217;s.&#8221;</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/10/php2shell-php-update-guide-graphic-1.webp" alt="PHP2Shell in four numbers: 2 critical flaws in PHP itself, each scored CVSS v4.0 9.2; 4 fixed releases, 8.5.12, 8.4.27, 8.3.36 and 8.2.35; and about 884,000 hosts reporting PHP 8.1 or older on 20 October, per Mallory" class="wp-image-10186" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/php2shell-php-update-guide-graphic-1.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/php2shell-php-update-guide-graphic-1-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/php2shell-php-update-guide-graphic-1-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/php2shell-php-update-guide-graphic-1-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">The flaws sit in PHP, not WordPress. Source: php.net advisory, GitHub advisories, Mallory via The Hacker News</figcaption></figure>



<h2 class="wp-block-heading">Which PHP versions fix it?</h2>



<p class="wp-block-paragraph">Four releases close both holes, one per supported branch. Anything below them on the same branch is affected, according to both GitHub advisories.</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>PHP2Shell at a glance (as of 22 October 2026)</th><th>What the sources say</th></tr></thead><tbody><tr><td>Fixed versions</td><td>8.5.12, 8.4.27, 8.3.36, 8.2.35 (released 21 October 2026)</td></tr><tr><td>Affected</td><td>Every version below those on each branch; 8.1 and older too</td></tr><tr><td>Severity</td><td>Critical, CVSS v4.0 9.2 for each flaw</td></tr><tr><td>Login needed to attack</td><td>No (unauthenticated remote code execution)</td></tr><tr><td>Highest risk setup</td><td>Apache with mod_php</td></tr><tr><td>Also at risk</td><td>PHP-FPM, php-cgi, FrankenPHP, LiteSpeed</td></tr><tr><td>Complete fix</td><td>Only an update; settings changes reduce exposure but do not make a server safe</td></tr><tr><td>PHP 8.2 end of life</td><td>31 December 2026</td></tr></tbody></table></figure>



<p class="wp-block-paragraph">The branch dates come from PHP&#8217;s <a href="https://www.php.net/supported-versions.php" target="_blank" rel="noopener">supported versions page</a>: PHP 8.3 gets security fixes until 31 December 2027, 8.4 until 31 December 2028 and 8.5 until 31 December 2029.</p>



<h2 class="wp-block-heading">Is your site at risk?</h2>



<p class="wp-block-paragraph">Yes, if it serves web pages through PHP and runs an unpatched version. The php.net advisory lists &#8220;all PHP installations serving web requests&#8221; as at risk, and names <strong>Apache with mod_php</strong> as the setup with &#8220;a demonstrated exploit&#8221;, where one request can trigger the attack before your code even runs. That last detail is the one that worries us most.</p>



<p class="wp-block-paragraph">PHP-FPM, the other common setup on hosting servers, is susceptible by source analysis, though the first advisory says no complete FPM exploit has been shown. The second advisory did validate one session-related path against both mod_php and FPM. FrankenPHP and LiteSpeed are described as likely affected but not tested.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/10/php2shell-php-update-guide-graphic-2.webp" alt="PHP2Shell risk by server setup: Apache with mod_php is highest risk with a demonstrated exploit; PHP-FPM is susceptible with one session path validated; FrankenPHP and LiteSpeed are likely affected but untested; only PHP 8.5.12, 8.4.27, 8.3.36 or 8.2.35 fully closes both flaws" class="wp-image-10188" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/php2shell-php-update-guide-graphic-2.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/php2shell-php-update-guide-graphic-2-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/php2shell-php-update-guide-graphic-2-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/php2shell-php-update-guide-graphic-2-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Risk by setup, as the advisories describe it. Source: php.net and GitHub advisories</figcaption></figure>



<p class="wp-block-paragraph">Reports on attacks differ, so read them with their dates. On 21 October, <a href="https://watchtowr.com/intelligence/php-remote-code-execution-php2shell/" target="_blank" rel="noopener">watchTowr</a> said it was &#8220;observing active exploitation attempts&#8221; of both CVEs, and its <a href="https://watchtowr.com/intelligence/php-cve-2026-109618-faq/" target="_blank" rel="noopener">FAQ</a>, updated 22 October, reports exploitation in the wild from its honeypots. <a href="https://www.huntress.com/blog/php2shell-leaked-critical-php-vulnerabilities" target="_blank" rel="noopener">Huntress</a> had not seen exploitation on partner systems as of 4:30pm ET on 21 October. <a href="https://cert.europa.eu/publications/security-advisories/2026-018/" target="_blank" rel="noopener">CERT-EU</a> says proofs of concept &#8220;have begun to circulate,&#8221; and products that bundle PHP inside them may be affected too.</p>



<h2 class="wp-block-heading">How do you check your PHP version?</h2>



<p class="wp-block-paragraph">You need all three numbers, not just the first two, and this is where we expect people to slip. WordPress&#8217;s own help page treats 8.3 or higher as fine for everyday use, but for this flaw <strong>8.3.35 is unpatched and 8.3.36 is safe</strong>.</p>



<figure class="wp-block-video"><video height="676" style="aspect-ratio: 1200 / 676;" width="1200" autoplay loop muted poster="https://www.gradientco.net/wp-content/uploads/2026/10/php2shell-php-update-guide-clip-poster.webp" src="https://www.gradientco.net/wp-content/uploads/2026/10/php2shell-php-update-guide-clip.mp4" playsinline></video><figcaption class="wp-element-caption">One digit separates an exposed PHP version from a patched one</figcaption></figure>



<p class="wp-block-paragraph">On WordPress, the <a href="https://wordpress.org/support/update-php/" target="_blank" rel="noopener">Update PHP page</a> gives the quickest route:</p>



<ol class="wp-block-list">
<li>Log in to your WordPress dashboard.</li>

<li>Open <strong>Tools &gt; Site Health</strong> and switch to the <strong>Info</strong> tab.</li>

<li>Expand the <strong>Server</strong> section and read the PHP version line.</li>

<li>Compare it with the fixed version for your branch in the table above.</li>
</ol>



<p class="wp-block-paragraph">The <a href="https://wordpress.org/documentation/article/site-health-screen/" target="_blank" rel="noopener">Site Health screen</a> may also show a warning on the Status tab that your site runs an outdated PHP version. Don&#8217;t treat a missing warning as a pass: the help page doesn&#8217;t say the check looks at security patch levels.</p>



<p class="wp-block-paragraph">Without WordPress, a page that prints <code>phpversion()</code> shows the version of the PHP that runs your site, according to the <a href="https://www.php.net/manual/en/function.phpversion.php" target="_blank" rel="noopener">PHP manual</a>. Delete that page as soon as you&#8217;ve read it. On a server you control, <code>php -v</code> on the command line prints the version and the SAPI, per the <a href="https://www.php.net/manual/en/features.commandline.options.php" target="_blank" rel="noopener">command-line docs</a>. The command-line PHP can differ from the one your web server uses, so check the web one as well.</p>



<h2 class="wp-block-heading">How do you update PHP on your hosting?</h2>



<p class="wp-block-paragraph">Who does the update depends on who runs the server. On shared hosting the host patches PHP for you; on a VPS you do it yourself; on a managed host you confirm it was done.</p>



<p class="wp-block-paragraph"><strong>Shared hosting with cPanel.</strong> The <a href="https://docs.cpanel.net/cpanel/software/multiphp-manager-for-cpanel/" target="_blank" rel="noopener">MultiPHP Manager</a> lets you tick your domain, pick a version from the PHP Version menu and click Apply. That switches branches, for example from 8.1 to 8.3. The patch level inside a branch is installed by your host, so send them a short message asking whether their PHP packages include the 21 October fixes.</p>



<p class="wp-block-paragraph"><strong>A VPS or dedicated server.</strong> Linux distributions and long-term support vendors received the patches before release, the PHP advisory says, so install your distribution&#8217;s updated PHP packages. Then restart every PHP worker, as watchTowr advises, so the fix takes effect across the whole server. If you used the volunteer-run remirepo repository, PHP advises switching to distribution packages or building from source, because that repository had no update on release day.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="675" src="https://www.gradientco.net/wp-content/uploads/2026/10/php2shell-php-update-guide-inline-1.webp" alt="Rows of server racks lit blue in a data centre, the kind of hosting servers where PHP security updates must be installed" class="wp-image-10191" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/php2shell-php-update-guide-inline-1.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/php2shell-php-update-guide-inline-1-300x169.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/php2shell-php-update-guide-inline-1-1024x576.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/php2shell-php-update-guide-inline-1-768x432.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">On a VPS or dedicated server, installing the patched PHP packages and restarting every worker is your job. Photo: <a href="https://www.rawpixel.com/image/5906639/photo-image-light-desktop-wallpapers-public-domain" target="_blank" rel="noopener">Free computer server room image</a> by <a href="https://www.rawpixel.com/image/5906639/photo-image-light-desktop-wallpapers-public-domain" target="_blank" rel="noopener">unknown</a>, <a href="https://creativecommons.org/publicdomain/zero/1.0/" target="_blank" rel="noopener">CC0 1.0</a></figcaption></figure>



<p class="wp-block-paragraph"><strong>Managed WordPress hosting.</strong> Ask your provider in writing which PHP version and patch your site now runs. Don&#8217;t rely on the number alone: a host that uses distribution packages with backported fixes may show one that doesn&#8217;t match php.net&#8217;s.</p>



<p class="wp-block-paragraph">Before switching branches on any of these, follow WordPress&#8217;s checklist: take a backup, update WordPress, themes and plugins, and test compatibility. If the site breaks, ask the host to restore the previous PHP version and then restore your backup. Our <a href="https://www.gradientco.net/tech-science/coloros-17-update/">guide to installing a major phone update safely</a> follows the same back-up-first logic.</p>



<h2 class="wp-block-heading">What if you are still on PHP 8.1 or older?</h2>



<p class="wp-block-paragraph">The PHP project no longer ships releases for 8.1 and older, so no official patched version exists. Its security team did publish minimal source backports for 7.4, 8.0 and 8.1, aimed at people who build PHP themselves.</p>



<p class="wp-block-paragraph">For most site owners the realistic move is to upgrade to 8.3 or newer. <a href="https://wordpress.org/about/requirements/" target="_blank" rel="noopener">WordPress&#8217;s requirements page</a> recommends PHP 8.3 or greater and warns that older versions &#8220;may expose your site to security vulnerabilities.&#8221; Some distributions and paid support vendors backport fixes to old branches, so if you can&#8217;t move, ask your vendor. Security firm Mallory counted about 884,000 internet-facing hosts whose banners reported PHP 8.1 or older on 20 October, <a href="https://thehackernews.com/2026/10/php2shell-two-unpatched-critical-php.html" target="_blank" rel="noopener">The Hacker News</a> reports. A banner isn&#8217;t proof a host is affected, but that&#8217;s still a lot of old PHP facing the internet.</p>



<h2 class="wp-block-heading">What can you do if you can&#8217;t update today?</h2>



<p class="wp-block-paragraph">Reduce exposure, and treat it as a stopgap. php.net is direct: the measures &#8220;reduce exposure to specific attack paths but do not make an installation safe.&#8221; CERT-EU goes further and suggests taking internet-facing applications off public networks until they are patched.</p>



<p class="wp-block-paragraph">For whoever manages the server, the advisory lists settings that narrow the known attack paths:</p>



<ul class="wp-block-list">
<li>Turn off session upload progress (<code>session.upload_progress.enabled=0</code>) in php.ini or the FPM pool, then restart every worker. Setting it from inside the app does not work.</li>

<li>Keep <code>post_max_size</code> low: 8M if the site takes no uploads, 32M if it does.</li>

<li>On PHP 8.5 only, set <code>fatal_error_backtraces=0</code> and restart workers.</li>
</ul>



<p class="wp-block-paragraph">The full list, including memory-limit changes, is in the <a href="https://www.php.net/archive/2026.php" target="_blank" rel="noopener">php.net advisory</a>. On shared hosting you usually can&#8217;t change these yourself, which is one more reason to push your host for the patch.</p>



<h2 class="wp-block-heading">Signs a site was already hit</h2>



<p class="wp-block-paragraph">Look for activity a web server should never produce. The checklist below starts with the four main signs Huntress lists (the commands it means are reconnaissance tools such as <code>whoami</code>), then adds two WordPress checks of our own.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/10/php2shell-php-update-guide-graphic-3.webp" alt="Checklist of signs a site was already hit by PHP2Shell: PHP workers starting shells or download tools, commands such as whoami run from the web server, unknown files in web folders, new scheduled jobs or user accounts, and WordPress admins or PHP files in uploads you don&apos;t recognise" class="wp-image-10192" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/php2shell-php-update-guide-graphic-3.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/php2shell-php-update-guide-graphic-3-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/php2shell-php-update-guide-graphic-3-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/php2shell-php-update-guide-graphic-3-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">What a web server should never do. Source: Huntress, 21 October 2026</figcaption></figure>



<p class="wp-block-paragraph">On WordPress, check for administrator accounts you didn&#8217;t create and PHP files you don&#8217;t recognise in the uploads folder; in our view these are the signs a non-technical owner can spot. If you find anything, isolate the server, as Huntress advises. Keep the web server and PHP logs before you clean up; both Huntress and watchTowr recommend it. Our explainer on <a href="https://www.gradientco.net/tech-science/what-is-cyber-security-challenges-in-cybersecurity-security-threats-of-cybercrime-prevention-and-solutions/">common cyber security threats and how to prevent them</a> covers the basics of recovery and prevention.</p>



<h2 class="wp-block-heading">Frequently asked questions</h2>


<div id="rank-math-faq" class="rank-math-block">
<div class="rank-math-list ">
<div id="faq-question-1" class="rank-math-list-item">
<h3 class="rank-math-question ">Is WordPress itself vulnerable to PHP2Shell?</h3>
<div class="rank-math-answer ">

<p>No. The flaws are in PHP, the engine WordPress runs on, so updating WordPress, themes or plugins changes nothing here. The fix has to come from your host or your server&#8217;s PHP packages.</p>

</div>
</div>
<div id="faq-question-2" class="rank-math-list-item">
<h3 class="rank-math-question ">Does PHP2Shell affect sites on PHP-FPM?</h3>
<div class="rank-math-answer ">

<p>Possibly, and we&#8217;d treat it as affected. The advisories call PHP-FPM susceptible, and one session-related attack path was validated against it, even though no complete FPM exploit had been demonstrated as of 21 October 2026.</p>

</div>
</div>
<div id="faq-question-3" class="rank-math-list-item">
<h3 class="rank-math-question ">Do I need to restart anything after a PHP security update?</h3>
<div class="rank-math-answer ">

<p>Yes, on servers you manage. watchTowr advises restarting every PHP worker after installing the update so the fix takes effect across the whole environment, so restart PHP-FPM or your web server once the new packages are in.</p>

</div>
</div>
<div id="faq-question-4" class="rank-math-list-item">
<h3 class="rank-math-question ">Will changing PHP settings protect my site instead of updating?</h3>
<div class="rank-math-answer ">

<p>Not fully, and one setting is worth keeping after you patch: the advisory for the second flaw suggests leaving session upload progress off if your site doesn&#8217;t use it.</p>

</div>
</div>
<div id="faq-question-5" class="rank-math-list-item">
<h3 class="rank-math-question ">Is PHP 8.2 still worth updating to?</h3>
<div class="rank-math-answer ">

<p>Only as a quick fix. PHP 8.2.35 closes both flaws, but the branch stops getting security fixes on 31 December 2026, so plan a move to 8.3 or newer.</p>

</div>
</div>
</div>
</div>


<h2 class="wp-block-heading">What we do not know yet</h2>



<p class="wp-block-paragraph">How widely the flaws are being exploited is still unclear, and neither CVE was in CISA&#8217;s catalogue of known exploited vulnerabilities as of watchTowr&#8217;s 22 October update. We couldn&#8217;t confirm which hosting providers have patched, and we didn&#8217;t read any host&#8217;s own statement, so we name none. We&#8217;ll update this page as official sources add detail.</p>



<p class="wp-block-paragraph"><strong>Read next:</strong> if this patch has you rethinking where your site runs, our guide to <a href="https://www.gradientco.net/tech-science/amazon-web-a-cloud-computing-colossal/">Amazon Web Services and cloud computing</a> explains the alternative to shared hosting.</p>



<p class="wp-block-paragraph"><em>Researched and drafted with AI assistance; facts checked against the sources linked in this article.</em></p>
]]></content:encoded>
					
		
		<enclosure url="https://www.gradientco.net/wp-content/uploads/2026/10/php2shell-php-update-guide-clip.mp4" length="211295" type="video/mp4" />

			</item>
		<item>
		<title>Hunza in Autumn 2026: When the Colours Peak, Weather and Getting There</title>
		<link>https://www.gradientco.net/history-culture-travel/hunza-in-autumn/</link>
		
		<dc:creator><![CDATA[Muhammad Rehan]]></dc:creator>
		<pubDate>Mon, 19 Oct 2026 03:10:15 +0000</pubDate>
				<category><![CDATA[History, Culture & Travel]]></category>
		<category><![CDATA[pakistan]]></category>
		<category><![CDATA[Travel]]></category>
		<category><![CDATA[Traveling]]></category>
		<guid isPermaLink="false">https://www.gradientco.net/?p=10157</guid>

					<description><![CDATA[Hunza's poplars and apricot trees turn gold from mid-October to mid-November. Here is how cold it gets, how to reach the valley from Islamabad, and what to see before winter.]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Hunza in autumn is at its most colourful from about 15 October to mid-November, when the valley&#8217;s trees turn gold, orange and red, according to a <a href="https://www.dawn.com/news/1861247" target="_blank" rel="noopener">Dawn travel feature</a>. Days are mild and nights are cold: in nearby Gilgit, October averages a high of 25.8°C and a low of 6.0°C. Most visitors from Islamabad drive the Karakoram Highway, and the second half of October is the safest time to aim for.</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Hunza autumn trip at a glance</th><th>What the sources say</th></tr></thead><tbody><tr><td>Colour season</td><td>15 October to mid-November (Dawn)</td></tr><tr><td>Gilgit, October average (1981-2010)</td><td>High 25.8°C, low 6.0°C, 6.8 mm of rain (WMO)</td></tr><tr><td>Gilgit, November average (1981-2010)</td><td>High 19.0°C, low 0.3°C, 3.2 mm of rain (WMO)</td></tr><tr><td>Drive from Islamabad</td><td>About 14 hours via the Babusar Pass (Dawn)</td></tr><tr><td>Babusar Pass</td><td>13,691 feet; open April to October; Basham route in winter (Dawn)</td></tr><tr><td>Baltit Fort</td><td>Earliest parts more than 700 years old; now a museum</td></tr><tr><td>Khunjerab Pass</td><td>15,500 feet; moved to year-round operation in December 2024</td></tr></tbody></table></figure>



<h2 class="wp-block-heading">When do the autumn colours peak in Hunza?</h2>



<p class="wp-block-paragraph">Colour arrives around mid-October and holds until about mid-November, going by Dawn&#8217;s 2024 Prism feature by Faisal Farooq. If you&#8217;re weighing the best time to visit Hunza for colour, <strong>we&#8217;d pick the last two weeks of October</strong>: they sit inside that window and come before November&#8217;s colder nights.</p>



<p class="wp-block-paragraph">The feature singles out Passu, north of Attabad Lake, for trees in red, orange and gold, and Attabad Lake for the contrast between bare mountains and autumn trees. It also names the peaks that frame the valley, all above 7,000 metres: Rakaposhi, Ultar, Diran, Spantik, Hunza Peak, Lady Finger and Dastgil Sir.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="379" src="https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-inline-1.webp" alt="Yellow autumn trees in Passu, below jagged peaks" class="wp-image-10159" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-inline-1.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-inline-1-300x95.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-inline-1-1024x323.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-inline-1-768x243.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Passu in autumn, where Dawn&#8217;s writer found some of the season&#8217;s strongest colour. Photo: <a href="https://commons.wikimedia.org/wiki/File%3APassu_Panorama.jpg" target="_blank" rel="noopener">Passu Panorama</a> by <a href="https://commons.wikimedia.org/wiki/File%3APassu_Panorama.jpg" target="_blank" rel="noopener">Isahmed</a>, <a href="https://creativecommons.org/publicdomain/mark/1.0/" target="_blank" rel="noopener">Public domain</a></figcaption></figure>



<p class="wp-block-paragraph">We found no official or scientific source on how the timing differs between central Hunza, around Karimabad, and the higher villages of upper Hunza, so treat any day-by-day colour forecast online as a guess.</p>



<h2 class="wp-block-heading">How cold is Hunza in autumn?</h2>



<p class="wp-block-paragraph">Expect warm afternoons and nights near freezing. The nearest station with published long-term data is Gilgit, about 90 kilometres down the road from Ganesh village in Hunza. The <a href="https://worldweather.wmo.int/en/city.html?cityId=897" target="_blank" rel="noopener">World Meteorological Organization&#8217;s climate page for Gilgit</a>, built on 1981-2010 averages, shows how fast the season turns: September averages a high of 31.9°C and a low of 12.1°C, while December nights fall to -2.2°C.</p>



<p class="wp-block-paragraph"><strong>Between October and November, the average night in Gilgit drops by almost six degrees</strong>, to just above freezing. Hunza&#8217;s villages sit further up the valley, so in our view you should plan for colder nights than Gilgit&#8217;s figures suggest. Rain barely registers in either month.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-graphic-1.webp" alt="Gilgit night lows, 1981-2010: 12.1°C in September, 6.0°C in October. Then 0.3°C in November and -2.2°C in December. Days fall from 31.9°C to 19.0°C, with 6.8 mm of rain in October." class="wp-image-10160" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-graphic-1.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-graphic-1-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-graphic-1-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-graphic-1-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Nights cool fast from September to December. Source: World Meteorological Organization, Gilgit station</figcaption></figure>



<p class="wp-block-paragraph">Pack layers rather than one heavy coat: a warm jacket, a fleece or sweater, a hat and gloves for the evenings, and sunscreen for the bright days.</p>



<h2 class="wp-block-heading">How do you get to Hunza from Islamabad?</h2>



<p class="wp-block-paragraph">Most people go by road on the <strong>Karakoram Highway</strong>, which runs up the Hunza Valley. In the Dawn feature, the writer left Islamabad after midnight and reached Hunza after about 14 hours, crossing the Babusar Pass at 13,691 feet (about 4,170 metres).</p>



<p class="wp-block-paragraph">Babusar is open only from April to October, the same feature notes; once it closes, traffic goes the longer way via Basham. In early November, check which route is open and allow extra hours.</p>



<p class="wp-block-paragraph">Pakistan International Airlines (PIA) also flies from Islamabad to Gilgit, followed by a road transfer into Hunza. Those flights are weather-dependent: fog grounded both Islamabad-Gilgit services, PK 601 and PK 602, on 3 January 2025, <a href="https://english.aaj.tv/news/amp/330396086" target="_blank" rel="noopener">Aaj News reported</a>.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="800" src="https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-graphic-2.webp" alt="Three ways to Hunza. Babusar Pass: 13,691 feet, open April to October, about 14 hours. Basham: once Babusar closes, and slower. Fly to Gilgit: weather permitting, then drive." class="wp-image-10161" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-graphic-2.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-graphic-2-300x200.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-graphic-2-1024x683.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-graphic-2-768x512.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Which route works depends on the month. Sources: Dawn; Aaj News</figcaption></figure>



<h2 class="wp-block-heading">What should you see in Hunza in October?</h2>



<p class="wp-block-paragraph">Start in Karimabad, the capital of Hunza district, and work outwards along the valley.</p>



<h3 class="wp-block-heading">Karimabad and Baltit Fort</h3>



<p class="wp-block-paragraph">Baltit Fort sits at the highest point of Karimabad. Carbon dating shows some parts existed as early as the 13th century, and surveyors found more than 70 construction phases, according to an <a href="https://web.mit.edu/akpia/www/AKPsite/4.239/baltit/baltit.html" target="_blank" rel="noopener">MIT case study of the restoration</a>. The fort opened as a museum in September 1996, after restoration work from 1992-96.</p>



<p class="wp-block-paragraph">That project was the <a href="https://the.akdn/en/where-we-work/south-asia/pakistan/cultural-development-pakistan" target="_blank" rel="noopener">Aga Khan Trust for Culture&#8217;s</a> first major intervention in Pakistan, and it also stabilised Karimabad&#8217;s historic core. It won the 2004 UNESCO Asia-Pacific Heritage Conservation Award for Excellence, the <a href="https://the.akdn/en/resources-media/whats-new/spotlights/sustaining-restored-monuments" target="_blank" rel="noopener">Aga Khan Development Network</a> says.</p>



<h3 class="wp-block-heading">Altit Fort and the Eagle&#8217;s Nest</h3>



<p class="wp-block-paragraph">Altit village is about 10 minutes from Baltit. Its fort stands more than 1,000 feet (about 300 metres) above the Hunza River and has a royal garden. Altit Fort received the UNESCO Asia-Pacific Award of Distinction in 2011.</p>



<p class="wp-block-paragraph">For a view over both Hunza and Nagar, go up to Duikar, known as the Eagle&#8217;s Nest. If you only make one climb in late October, we&#8217;d make it this one.</p>



<h3 class="wp-block-heading">Attabad Lake and Passu</h3>



<p class="wp-block-paragraph">The lake that looks so settled in photos did not exist until 2010. On 4 January that year, a landslide killed 20 people, destroyed 26 homes and dammed the Hunza River, <a href="https://science.nasa.gov/earth/earth-observatory/landslide-lake-in-northwest-pakistan-43175" target="_blank" rel="noopener">NASA&#8217;s Earth Observatory</a> reports. By March 2010, the lake behind the slide was about 11 kilometres long and had drowned 5 kilometres of the Karakoram Highway.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="675" src="https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-inline-2.webp" alt="Turquoise Attabad Lake between bare rock walls, with boats near the shore" class="wp-image-10162" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-inline-2.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-inline-2-300x169.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-inline-2-1024x576.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-inline-2-768x432.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">Attabad Lake in autumn: the landslide lake that formed in 2010 now drowns a stretch of the old Karakoram Highway. Photo: <a href="https://commons.wikimedia.org/wiki/File%3AAttabad_Lake_In_autumn.png" target="_blank" rel="noopener">Attabad Lake In autumn</a> by <a href="https://commons.wikimedia.org/wiki/File%3AAttabad_Lake_In_autumn.png" target="_blank" rel="noopener">Mr.KhalidJawed</a>, <a href="http://creativecommons.org/publicdomain/zero/1.0/deed.en" target="_blank" rel="noopener">CC0</a></figcaption></figure>



<p class="wp-block-paragraph">North of the lake, Passu is where Dawn&#8217;s writer found some of the season&#8217;s strongest colour. Ganesh, one of Hunza&#8217;s oldest settlements, is worth a stop on the way up, for its view of four peaks at once.</p>



<h2 class="wp-block-heading">Does Khunjerab Pass close for winter?</h2>



<p class="wp-block-paragraph"><strong>Not officially, since December 2024.</strong> For decades, a 1985 protocol kept the pass open only from April to November. In late 2024, the Foreign Office said operations at the Khunjerab-Sost crossing were &#8220;henceforth expanded to year-round operations,&#8221; <a href="https://www.dawn.com/news/1876341/khunjerab-pass-to-remain-open-round-the-year" target="_blank" rel="noopener">Dawn reported</a>.</p>



<p class="wp-block-paragraph">That does not make a winter visit easy. The pass is 15,500 feet (about 4,720 metres) high, and the first snow of November 2023 left up to six inches on the road, according to <a href="https://www.dawn.com/news/amp/1788582" target="_blank" rel="noopener">an earlier Dawn report</a>. Traders told Dawn in 2024 that they doubted the Pakistan side was ready for winter operations.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1200" height="900" src="https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-inline-3.webp" alt="The road at Khunjerab Pass under cloud, with snow on the peaks" class="wp-image-10163" srcset="https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-inline-3.webp 1200w, https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-inline-3-300x225.webp 300w, https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-inline-3-1024x768.webp 1024w, https://www.gradientco.net/wp-content/uploads/2026/10/hunza-in-autumn-inline-3-768x576.webp 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /><figcaption class="wp-element-caption">The road through Khunjerab Pass, 15,500 feet up, where snow can close the way on any given day. Photo: <a href="https://commons.wikimedia.org/wiki/File%3AKhunjerab_Pass.jpg" target="_blank" rel="noopener">Khunjerab Pass</a> by <a href="https://commons.wikimedia.org/wiki/File%3AKhunjerab_Pass.jpg" target="_blank" rel="noopener">SandersPoel</a>, <a href="https://creativecommons.org/publicdomain/mark/1.0/" target="_blank" rel="noopener">Public domain</a></figcaption></figure>



<p class="wp-block-paragraph">So if standing at the border is part of your plan for Hunza in autumn, go in October rather than late November. Check the day&#8217;s road status with the National Highway Authority or the Gilgit-Baltistan authorities before you set off.</p>



<h2 class="wp-block-heading">Planning the trip in four steps</h2>



<ol class="wp-block-list">
<li>Fix your dates and route together: October means colour and the Babusar road; after it closes, plan on Basham.</li>

<li>Book a base in Karimabad for the forts and the Eagle&#8217;s Nest, then a night further north for Attabad and Passu.</li>

<li>Keep one spare day for weather, landslides or a cancelled flight.</li>

<li>Save the forecast and road updates before you leave Gilgit.</li>
</ol>



<p class="wp-block-paragraph">Going alone? Our piece on <a href="https://www.gradientco.net/history-culture-travel/why-travel-solo-top-5-reasons-for-traveling-solo/">the case for travelling solo</a> covers the upsides.</p>



<h2 class="wp-block-heading">Frequently asked questions</h2>


<div id="rank-math-faq" class="rank-math-block">
<div class="rank-math-list ">
<div id="faq-question-1" class="rank-math-list-item">
<h3 class="rank-math-question ">Is October the best time to visit Hunza?</h3>
<div class="rank-math-answer ">

<p>For autumn colour, yes. It&#8217;s also the last month of the faster Babusar road, and the Khunjerab Pass is inside its traditional open season.</p>

</div>
</div>
<div id="faq-question-2" class="rank-math-list-item">
<h3 class="rank-math-question ">How cold is it in Hunza at night in November?</h3>
<div class="rank-math-answer ">

<p>Around freezing. Gilgit&#8217;s long-term November low is just above 0°C, and Hunza&#8217;s villages are higher up the valley.</p>

</div>
</div>
<div id="faq-question-3" class="rank-math-list-item">
<h3 class="rank-math-question ">Is Hunza in autumn still colourful in early November?</h3>
<div class="rank-math-answer ">

<p>Usually, yes, though you trade some comfort for it: nights are colder and Babusar has closed, so the drive goes via Basham.</p>

</div>
</div>
<div id="faq-question-4" class="rank-math-list-item">
<h3 class="rank-math-question ">Is Khunjerab Pass open in October?</h3>
<div class="rank-math-answer ">

<p>Normally, yes. October falls inside the old April to November season, and the crossing is now meant to run all year. Snow can still shut the road on a given day.</p>

</div>
</div>
<div id="faq-question-5" class="rank-math-list-item">
<h3 class="rank-math-question ">How old is Baltit Fort?</h3>
<div class="rank-math-answer ">

<p>Parts of it are more than 700 years old, and it has been a museum since 1996.</p>

</div>
</div>
</div>
</div>


<h2 class="wp-block-heading">What we do not know yet</h2>



<p class="wp-block-paragraph">We could not confirm whether Khunjerab stayed open through the winter of 2025-26, current PIA flight times and fares, temperature records for Karimabad itself, or 2026 entry prices and hours for Baltit and Altit forts. The Gilgit-Baltistan tourism department&#8217;s site was down while we researched this guide.</p>



<p class="wp-block-paragraph">Read next: our <a href="https://www.gradientco.net/history-culture-travel/kaghan-expedition/">Kaghan Valley expedition</a>, from Naran to Lulusar Lake.</p>



<p class="wp-block-paragraph"><em>Researched and drafted with AI assistance; facts checked against the sources linked in this article.</em></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
