<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sam</title>
    <description>The latest articles on DEV Community by Sam (@sdeonvacation).</description>
    <link>https://dev.to/sdeonvacation</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3897611%2F0e603b57-3b8b-4823-ad49-83153882b26a.gif</url>
      <title>DEV Community: Sam</title>
      <link>https://dev.to/sdeonvacation</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sdeonvacation"/>
    <language>en</language>
    <item>
      <title>A Background Job Was Crashing Our JVM Every Week - Until We Taught It to Stop</title>
      <dc:creator>Sam</dc:creator>
      <pubDate>Sat, 25 Apr 2026 14:04:43 +0000</pubDate>
      <link>https://dev.to/sdeonvacation/a-background-job-was-crashing-our-jvm-every-week-until-we-taught-it-to-stop-3ap9</link>
      <guid>https://dev.to/sdeonvacation/a-background-job-was-crashing-our-jvm-every-week-until-we-taught-it-to-stop-3ap9</guid>
      <description>&lt;p&gt;&lt;em&gt;Some Java services don't fail because of traffic. They fail because background jobs don't know when to stop.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A background job should never be able to take down your production system.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ours did. Every week.&lt;/p&gt;

&lt;p&gt;At SAP, we ran a multi-tenant service processing thousands of tenants. Every morning, a scheduled job kicked off - telemetry, cleanup, routine work. Nothing unusual.&lt;/p&gt;

&lt;p&gt;Until it overlapped with real traffic.&lt;/p&gt;

&lt;p&gt;Then, within minutes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Heap jumped from &lt;strong&gt;60% → 94%&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;GC went into panic mode&lt;/li&gt;
&lt;li&gt;Latency spiked &lt;strong&gt;5×&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;OOMKilled&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No memory leak. No bad deploy. Just a background job that didn't know when to stop.&lt;/p&gt;

&lt;h2&gt;
  
  
  If This Feels Familiar, You're Already at Risk
&lt;/h2&gt;

&lt;p&gt;If you've ever:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Used &lt;code&gt;@Scheduled&lt;/code&gt; and assumed it's safe&lt;/li&gt;
&lt;li&gt;Configured a thread pool and felt "this should handle it"&lt;/li&gt;
&lt;li&gt;Relied on auto-scaling to absorb spikes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You're sitting on the same failure mode we were.&lt;/p&gt;

&lt;p&gt;The JVM didn't fail. Our scheduling model did.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem Nobody Talks About
&lt;/h2&gt;

&lt;p&gt;We had everything "right":&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Thread pools&lt;/li&gt;
&lt;li&gt;Bounded queues&lt;/li&gt;
&lt;li&gt;Scheduled jobs&lt;/li&gt;
&lt;li&gt;Horizontal scaling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And yet, the system still collapsed.&lt;/p&gt;

&lt;p&gt;Because none of these answer one critical question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Should this job continue right now?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;They control how much work runs.&lt;/p&gt;

&lt;p&gt;They don't react to when the system is under pressure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the Usual Fixes Don't Work
&lt;/h2&gt;

&lt;p&gt;Let's cut through the usual advice:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Tool&lt;/th&gt;
&lt;th&gt;What You Think It Does&lt;/th&gt;
&lt;th&gt;What It Actually Misses&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Rate limiting&lt;/td&gt;
&lt;td&gt;Controls load&lt;/td&gt;
&lt;td&gt;Ignores CPU/memory pressure&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bulkheads&lt;/td&gt;
&lt;td&gt;Limits concurrency&lt;/td&gt;
&lt;td&gt;Still burns resources under stress&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Thread pools&lt;/td&gt;
&lt;td&gt;Caps parallelism&lt;/td&gt;
&lt;td&gt;Even 1 thread can OOM you&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Auto-scaling&lt;/td&gt;
&lt;td&gt;Adds capacity&lt;/td&gt;
&lt;td&gt;Too slow, too expensive&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Spring Batch&lt;/td&gt;
&lt;td&gt;Manages jobs&lt;/td&gt;
&lt;td&gt;No runtime awareness&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;All of these control throughput.&lt;/p&gt;

&lt;p&gt;None of them understand pressure.&lt;/p&gt;

&lt;p&gt;Background jobs don't fail loudly. They fail by slowly starving everything else.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Shift That Changed Everything
&lt;/h2&gt;

&lt;p&gt;We stopped asking:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;How do we run this more efficiently?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And started asking:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Does this job even have permission to run right now?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That one shift changed everything.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Fix (In One Sentence)
&lt;/h2&gt;

&lt;p&gt;Pause background work when CPU or memory crosses a threshold. Resume when the system recovers.&lt;/p&gt;

&lt;p&gt;Not slow down.&lt;/p&gt;

&lt;p&gt;Not retry.&lt;/p&gt;

&lt;p&gt;Pause. Completely.&lt;/p&gt;

&lt;p&gt;Zero CPU usage. Zero contention.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Pattern: Checkpoints That Can Say "Stop"
&lt;/h2&gt;

&lt;p&gt;Instead of running a massive job in one go:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Break it into chunks&lt;/li&gt;
&lt;li&gt;After each chunk, check system health&lt;/li&gt;
&lt;li&gt;If stressed, pause execution&lt;/li&gt;
&lt;li&gt;Resume later from the same point&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You don't control execution.&lt;/p&gt;

&lt;p&gt;You control permission to continue.&lt;/p&gt;

&lt;p&gt;Why this works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Java can't pause a thread mid-execution&lt;/li&gt;
&lt;li&gt;So you create safe pause points&lt;/li&gt;
&lt;li&gt;Like checkpoints in a game: not mid-jump, only at safe boundaries&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;em&gt;Workers process one chunk, check pressure, pause if hot, and resume when healthy.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What This Looked Like in Production
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fsdeonvacation%2Fthrottle%2Ffddbdfcdb83813d40aff4fcef121746c3038c7ab%2Fdocs%2Fdiagrams%2Fbefore-after-system-2026-04-25-143547.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fsdeonvacation%2Fthrottle%2Ffddbdfcdb83813d40aff4fcef121746c3038c7ab%2Fdocs%2Fdiagrams%2Fbefore-after-system-2026-04-25-143547.png" alt="Before and after resource behavior" width="800" height="297"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Before&lt;/strong&gt;: &lt;em&gt;background work kept pushing through heap pressure until GC thrash turned into OOM kills.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;After&lt;/strong&gt;: &lt;em&gt;work paused at the threshold, traffic stayed healthy, and the job finished later but safely.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Same system. Same workload.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;80% fewer OOM incidents.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Auto-scaling reacts.&lt;/p&gt;

&lt;p&gt;This prevents.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Trade-Off Most People Try to Avoid
&lt;/h2&gt;

&lt;p&gt;This is where people hesitate.&lt;/p&gt;

&lt;p&gt;Your code must become chunkable.&lt;/p&gt;

&lt;p&gt;No shortcuts.&lt;/p&gt;

&lt;p&gt;You need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Idempotent batches&lt;/li&gt;
&lt;li&gt;Clear boundaries&lt;/li&gt;
&lt;li&gt;Resume-safe execution&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If your job is one giant function, this pattern won't save you.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Most Engineers Get Wrong
&lt;/h2&gt;

&lt;p&gt;They obsess over:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Chunk size&lt;/li&gt;
&lt;li&gt;Overhead&lt;/li&gt;
&lt;li&gt;Implementation details&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Wrong focus.&lt;/p&gt;

&lt;p&gt;The real question is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Can my background work behave like a good citizen?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If not, your system will fail under pressure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where This Actually Works
&lt;/h2&gt;

&lt;p&gt;Use this for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ETL pipelines&lt;/li&gt;
&lt;li&gt;Batch APIs&lt;/li&gt;
&lt;li&gt;Cleanup jobs&lt;/li&gt;
&lt;li&gt;Migrations&lt;/li&gt;
&lt;li&gt;Scheduled processing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Avoid it for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ultra-low latency paths&lt;/li&gt;
&lt;li&gt;Non-interruptible logic&lt;/li&gt;
&lt;li&gt;Fire-and-forget tasks&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Bigger Insight
&lt;/h2&gt;

&lt;p&gt;The JVM gives you &lt;code&gt;ExecutorService&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That abstraction assumes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dedicated machines&lt;/li&gt;
&lt;li&gt;Predictable load&lt;/li&gt;
&lt;li&gt;No contention&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That world doesn't exist anymore.&lt;/p&gt;

&lt;p&gt;Today you have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Containers&lt;/li&gt;
&lt;li&gt;Shared CPU&lt;/li&gt;
&lt;li&gt;Hard memory limits&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;"Just run it" is no longer a valid strategy.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Hard Truth
&lt;/h2&gt;

&lt;p&gt;Your system didn't crash because Java failed.&lt;/p&gt;

&lt;p&gt;It crashed because your background jobs were selfish.&lt;/p&gt;

&lt;p&gt;They consumed resources blindly.&lt;/p&gt;

&lt;p&gt;They never asked:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Is now a good time?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Final Thought
&lt;/h2&gt;

&lt;p&gt;Auto-scaling throws hardware at the problem.&lt;/p&gt;

&lt;p&gt;This approach does something better:&lt;/p&gt;

&lt;p&gt;It teaches your system restraint.&lt;/p&gt;

&lt;p&gt;And in distributed systems, restraint is survival.&lt;/p&gt;

&lt;h2&gt;
  
  
  Want to Try It?
&lt;/h2&gt;

&lt;p&gt;We built this into an open-source library:&lt;/p&gt;

&lt;p&gt;👉 &lt;a href="https://github.com/sdeonvacation/throttle" rel="noopener noreferrer"&gt;https://github.com/sdeonvacation/throttle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Start with the simulator.&lt;/p&gt;

&lt;p&gt;Watch it pause and resume.&lt;/p&gt;

&lt;p&gt;Then apply it to one job - not everything.&lt;/p&gt;

&lt;h2&gt;
  
  
  One Last Thing
&lt;/h2&gt;

&lt;p&gt;If your background jobs run in the same JVM as your APIs, you don't have a performance problem.&lt;/p&gt;

&lt;p&gt;You have a priority problem.&lt;/p&gt;

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