<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Chanuka Dinuwan on Medium]]></title>
        <description><![CDATA[Stories by Chanuka Dinuwan on Medium]]></description>
        <link>https://medium.com/@cdinuwan?source=rss-9be9624c6d5a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*qbZj91ZeoyvfQnrWx8096Q@2x.jpeg</url>
            <title>Stories by Chanuka Dinuwan on Medium</title>
            <link>https://medium.com/@cdinuwan?source=rss-9be9624c6d5a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 11:00:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cdinuwan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Designing a Distributed Rate Limiter with Token Bucket, Redis and Lua]]></title>
            <link>https://blog.stackademic.com/designing-a-distributed-rate-limiter-with-token-bucket-redis-and-lua-f45668728c1c?source=rss-9be9624c6d5a------2</link>
            <guid isPermaLink="false">https://medium.com/p/f45668728c1c</guid>
            <category><![CDATA[system-design-concepts]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Chanuka Dinuwan]]></dc:creator>
            <pubDate>Mon, 16 Nov 2026 06:07:43 GMT</pubDate>
            <atom:updated>2026-11-16T06:07:43.327Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6VJHIGpwHK1ybVAhZdgJeA.png" /></figure><p>A rate limiter on one server is easy. A counter in memory is enough. The problem starts when you have many servers behind a load balancer. Each server only sees part of the traffic, so each one counts wrong. This article covers how to design a rate limiter that still works when your API runs on many servers.</p><h3>What the rate limiter must do</h3><ul><li>Limit how many requests a client can send in a time window.</li><li>Reject extra requests with <strong>HTTP 429 Too Many Requests</strong>.</li><li>Tell the client how many requests are left and when the limit resets, using response headers.</li><li>Add very little latency. A good target is under 3 ms at P95, because this check runs on every request.</li><li>Stay highly available. If the rate limiter goes down, it should not take the whole API with it.</li></ul><h3>Where to put it</h3><p>There are three options.</p><ul><li><strong>Client-side.</strong> Not reliable and not secure. The client can change or skip the check. It is only useful as a courtesy, never as protection.</li><li><strong>Server-side, inside the application.</strong> It works, but rate limiting code gets mixed with business logic, and every service has to implement it again.</li><li><strong>Middleware (API gateway or reverse proxy).</strong> The best balance. Rate limiting policies stay separate from business logic, and bad traffic is rejected before it reaches your services.</li></ul><p>In most systems the middleware option wins. Your services stay clean and the policy lives in one place.</p><h3>Choosing the algorithm</h3><h4>Fixed window counter</h4><p>Split time into fixed windows, for example one minute each, and keep one counter per user per window. It is simple and cheap, but it has a real flaw: the <strong>window boundary burst problem</strong>.</p><p>The counter resets fully at the start of each window. Say the limit is 100 requests per minute. A client sends 100 requests at 12:00:59, and then 100 more at 12:01:00. Both windows look fine, but your backend just got 200 requests in about two seconds. That is double the limit you thought you had.</p><h4>Token bucket</h4><p>This is the industry standard. AWS and Stripe both use it. Each client has a bucket that holds tokens:</p><ul><li>The bucket has a maximum <strong>capacity</strong>.</li><li>Tokens are added at a steady <strong>refill rate</strong>.</li><li>Each request takes one token. If the bucket is empty, the request is rejected.</li></ul><p>Two numbers give you two controls. Capacity sets how big a burst can be. Refill rate sets the long-term throughput. So a client can send a short burst, which is normal for real traffic, but cannot stay above the limit.</p><h4>Other options</h4><ul><li><strong>Sliding window log.</strong> Store a timestamp for every request. Very accurate, but memory grows with traffic.</li><li><strong>Sliding window counter.</strong> Mix the current and previous window counts using a weight. A good approximation with low memory.</li><li><strong>Leaky bucket.</strong> Requests leave at a constant rate. It gives a smooth output, but it does not allow bursts.</li></ul><p>For a general API, token bucket is the default choice.</p><h3>The distributed part: shared state in Redis</h3><p>If every server keeps its own counters, a client that hits ten servers gets ten times the limit. The counts must live in one shared place. Redis fits well here because it is in memory, fast, and supports key expiry.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*QHqmAVOrCCQh8j4b" /></figure><p>The flow is short. The request reaches the gateway. The rate limiter middleware asks Redis if this client still has tokens. If yes, the request goes on to the service. If no, the gateway returns 429.</p><h3>The race condition</h3><p>Shared state brings a new problem. The simple logic is: read the count, check it, then write the new count. With many servers, two requests can read the same value at the same time. Both see one token left, both pass, and the limit is broken.</p><p>The fix is to make “read, check, update” one <strong>atomic operation</strong>. In Redis, the usual way is a Lua script. Redis runs the whole script as one step, so no other command can run in the middle.</p><pre>-- KEYS[1] = bucket key, e.g. rl:{user42}<br>-- ARGV[1] = capacity (max tokens)<br>-- ARGV[2] = refill rate (tokens per second)<br>-- ARGV[3] = tokens requested (usually 1)<br>local capacity  = tonumber(ARGV[1])<br>local rate      = tonumber(ARGV[2])<br>local requested = tonumber(ARGV[3])</pre><pre>-- Use Redis time, not the app server clock<br>local t   = redis.call(&#39;TIME&#39;)<br>local now = tonumber(t[1]) * 1000 + math.floor(tonumber(t[2]) / 1000)</pre><pre>local data   = redis.call(&#39;HMGET&#39;, KEYS[1], &#39;tokens&#39;, &#39;ts&#39;)<br>local tokens = tonumber(data[1]) or capacity<br>local ts     = tonumber(data[2]) or now</pre><pre>-- Refill based on elapsed time<br>local elapsed = math.max(0, now - ts) / 1000<br>tokens = math.min(capacity, tokens + elapsed * rate)</pre><pre>local allowed = 0<br>if tokens &gt;= requested then<br>  tokens  = tokens - requested<br>  allowed = 1<br>end</pre><pre>redis.call(&#39;HSET&#39;, KEYS[1], &#39;tokens&#39;, tokens, &#39;ts&#39;, now)<br>-- Idle buckets expire on their own<br>redis.call(&#39;PEXPIRE&#39;, KEYS[1], math.ceil(capacity / rate) * 2000)</pre><pre>return { allowed, math.floor(tokens) }</pre><p>Notes on this script:</p><ul><li>It uses the Redis clock, not the application clock. App servers can have clock drift, and that would make refill wrong.</li><li>Tokens are not refilled by a background job. The script calculates the refill from the time since the last request. No timers are needed.</li><li>Idle buckets expire by themselves, so memory does not grow forever.</li><li>It is one round trip to Redis per request, which keeps latency low.</li></ul><h3>Tell the client what happened</h3><p>A good rate limiter does not only reject. It tells the client how to behave.</p><pre>HTTP/1.1 429 Too Many Requests<br>X-RateLimit-Limit: 100<br>X-RateLimit-Remaining: 0<br>X-RateLimit-Reset: 1794807600<br>Retry-After: 12</pre><p>Send the remaining count and reset time on successful responses too. Well-behaved clients can then slow down before they hit the limit.</p><h3>Things that break in production</h3><ul><li><strong>Redis is down or slow.</strong> Decide before it happens. Fail open (allow requests) keeps the API available but removes protection. Fail closed (reject requests) protects the backend but causes an outage. Most public APIs fail open, with a short Redis timeout and an alert.</li><li><strong>Redis is a single point of failure.</strong> Use replication or Redis Cluster. Replication is asynchronous, so a failover can lose a few recent counts. For rate limiting, this is usually acceptable.</li><li><strong>Redis Cluster and Lua.</strong> All keys in one script must be in the same hash slot. Use a hash tag such as rl:{user42} if you ever need more than one key per client.</li><li><strong>What you limit by.</strong> User ID or API key is better than IP address. Many users can share one IP behind a NAT, and one attacker can use many IPs.</li><li><strong>Hot keys.</strong> One very large client can put heavy load on one Redis node. A small local pre-check in the gateway can drop obvious abuse before it reaches Redis.</li></ul><h3>Summary</h3><ul><li>Put the rate limiter in middleware, not in business code.</li><li>Avoid fixed windows if boundary bursts matter. Token bucket is the safe default.</li><li>Keep the state in Redis so all servers share one view.</li><li>Make the check atomic with a Lua script to avoid race conditions.</li><li>Return 429 with clear headers, and decide your failure mode before Redis fails.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f45668728c1c" width="1" height="1" alt=""><hr><p><a href="https://blog.stackademic.com/designing-a-distributed-rate-limiter-with-token-bucket-redis-and-lua-f45668728c1c">Designing a Distributed Rate Limiter with Token Bucket, Redis and Lua</a> was originally published in <a href="https://blog.stackademic.com/">Stackademic</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Spring Boot Transactions in Production: 8 Patterns, the Real Bugs They Prevent, and a Demo You Can…]]></title>
            <link>https://blog.stackademic.com/spring-boot-transactions-in-production-8-patterns-the-real-bugs-they-prevent-and-a-demo-you-can-d5d56717df81?source=rss-9be9624c6d5a------2</link>
            <guid isPermaLink="false">https://medium.com/p/d5d56717df81</guid>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Chanuka Dinuwan]]></dc:creator>
            <pubDate>Sat, 17 Oct 2026 19:33:12 GMT</pubDate>
            <atom:updated>2026-10-17T19:34:28.018Z</atom:updated>
            <content:encoded><![CDATA[<h3>Spring Boot Transactions in Production: 8 Patterns, the Real Bugs They Prevent, and a Demo You Can Run</h3><p><em>Your code has @Transactional on it. That does not mean your data is safe.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/0*f7UCQT-S1WI4UV56.png" /></figure><p>Most transaction tutorials put @Transactional on a method and stop there. In production, the problems look different:</p><ul><li>A customer is charged, but the order is not saved.</li><li>A “payment declined” message goes out, but the money is gone anyway.</li><li>One slow payment gateway takes down the whole service.</li><li>Two people buy the last item in stock, and both succeed.</li><li>A customer gets an “order confirmed” email for an order that does not exist.</li><li>Kafka redelivers a message and an account is credited three times.</li></ul><p>Every one of these is a transaction problem. For each one, this article shows the real-life problem, why it happens, the fix, and a curl command you can run against a working demo to see it happen.</p><h3>The demo project</h3><p>Everything here comes from one small Spring Boot app. Each pattern has endpoints that show the <strong>broken</strong> version and the <strong>fixed</strong> version side by side. It uses in-memory H2, so there is no Docker and no database to set up.</p><ul><li>Spring Boot 4.1.1, Java 21, Spring Data JPA, H2</li><li>21 integration tests prove every behaviour shown here</li><li>Source code: <a href="https://github.com/ChanukD/springboot-transaction-patterns">github.com/ChanukD/springboot-transaction-patterns</a></li></ul><pre>tx-patterns-demo/<br>├── demo.sh            # runs every scenario<br>└── src/main/java/com/demo/tx/<br>    ├── basics/        1. proxy traps<br>    ├── propagation/   2. REQUIRES_NEW audit trail<br>    ├── readonly/      3. read-only transactions<br>    ├── template/      4. slow payment gateway<br>    ├── locking/       5. optimistic vs pessimistic<br>    ├── events/        6. @TransactionalEventListener<br>    ├── outbox/        7. transactional outbox<br>    └── idempotency/   8. idempotent consumer</pre><pre>git clone https://github.com/ChanukD/springboot-transaction-patterns<br>cd springboot-transaction-patterns<br>./mvnw spring-boot:run<br>./demo.sh</pre><pre># reset: account #1 &quot;alice&quot; with 1000.00<br>curl -X POST localhost:8080/demo/reset</pre><h3>The most useful setting for learning transactions</h3><pre>logging.level.org.springframework.orm.jpa.JpaTransactionManager=DEBUG<br>spring.jpa.open-in-view=false</pre><p>The first line logs every time Spring starts, joins, pauses, commits or rolls back a transaction. You will see lines like Creating new transaction, Participating in existing transaction and Rolling back JPA transaction. That makes the invisible visible.</p><p>The second line matters in production. Spring Boot turns open-in-view on by default, which keeps a database connection open for the whole HTTP request, including JSON rendering. Turn it off in every service.</p><p>Every demo endpoint returns the account balance before and after. If the balance changed, the work was committed. If not, it was rolled back.</p><h3>First: how @Transactional really works</h3><p>Spring does not change your class. It wraps your bean in a <strong>proxy</strong>. When another bean calls your method, the call goes through the proxy:</p><pre>Caller ─► [Proxy: begin] ─► your method ─► [Proxy: commit/rollback]</pre><p>Almost every transaction bug comes from one of three facts:</p><ul><li>Only calls that <strong>go through the proxy</strong> get a transaction.</li><li>The proxy decides commit or rollback from the <strong>exception that leaves the method</strong>.</li><li>Methods called inside a transaction <strong>share it</strong> by default.</li></ul><h3>1. Proxy traps: three ways @Transactional silently does nothing</h3><h3>Bug 1a: Self-invocation</h3><blockquote><strong><em>Real-life problem:</em></strong><em> Checkout debits the wallet, then calls the shipping service, which is down. The API returns an error, so the customer tries again. They are charged twice and have no order. The code has @Transactional, so everyone in code review thought it was safe.</em></blockquote><p><strong>Why it happens:</strong> the method calls another method of the same class through this. That call never goes through the proxy, so @Transactional is ignored.</p><pre>@Service<br>public class BasicsService {</pre><pre>  // no @Transactional; the controller calls this<br>  public void checkoutSelfInvocation(Long id, BigDecimal amt) {<br>    this.debitThenFail(id, amt);      // skips the proxy<br>  }</pre><pre>  @Transactional                      // ignored for the call above<br>  public void debitThenFail(Long id, BigDecimal amt) {<br>    Account account = accounts.findById(id).orElseThrow();<br>    account.debit(amt);<br>    accounts.save(account);           // commits on its own<br>    throw new IllegalStateException(&quot;shipping service down&quot;);<br>  }<br>}</pre><pre>curl -X POST localhost:8080/demo/basics/self-invocation</pre><pre>{ &quot;outcome&quot;: &quot;threw IllegalStateException: shipping service down&quot;,<br>  &quot;balanceBefore&quot;: 1000.00,<br>  &quot;balanceAfter&quot;:   900.00 }     ← money gone, request failed</pre><p><strong>Fix:</strong> move the transactional method into another bean, so the call goes through a proxy.</p><pre>@Service<br>public class AccountTxOps {<br>  @Transactional<br>  public void debitThenFail(Long id, BigDecimal amt) { ... }<br>}</pre><pre>// in BasicsService<br>public void checkoutViaProxy(Long id, BigDecimal amt) {<br>  txOps.debitThenFail(id, amt);       // through the proxy<br>}</pre><pre>curl -X POST localhost:8080/demo/basics/self-invocation-fixed</pre><pre>{ &quot;balanceBefore&quot;: 900.00, &quot;balanceAfter&quot;: 900.00 }  ← rolled back</pre><p>You can also put @Transactional on the public entry method, or use TransactionTemplate (pattern 4). Avoid injecting a bean into itself to work around this. It hides the design problem.</p><h3>Bug 1b: Checked exceptions commit</h3><blockquote><strong><em>Real-life problem:</em></strong><em> your code throws a checked PaymentDeclinedException. The customer sees “card declined”, but the debit made earlier in the same method was committed. Support tickets say “my card was declined but I was charged”.</em></blockquote><p><strong>Why it happens:</strong> by default Spring rolls back only for RuntimeException and Error. A checked exception means commit. The rule comes from old EJB conventions, and it surprises almost everyone.</p><pre>public class PaymentDeclinedException extends Exception { }</pre><pre>@Transactional<br>public void debitThenDecline(Long id, BigDecimal amt)<br>    throws PaymentDeclinedException {<br>  accounts.findById(id).orElseThrow().debit(amt);<br>  throw new PaymentDeclinedException(&quot;card declined&quot;); // COMMIT<br>}</pre><pre>curl -X POST localhost:8080/demo/basics/checked-exception</pre><pre>{ &quot;outcome&quot;: &quot;threw PaymentDeclinedException: card declined&quot;,<br>  &quot;balanceBefore&quot;: 900.00, &quot;balanceAfter&quot;: 800.00 } ← charged</pre><p><strong>Fix:</strong></p><pre>@Transactional(rollbackFor = Exception.class)<br>public void debitThenDecline(...) throws PaymentDeclinedException</pre><pre>curl -X POST localhost:8080/demo/basics/checked-exception-fixed</pre><pre>{ &quot;balanceBefore&quot;: 800.00, &quot;balanceAfter&quot;: 800.00 } ← rolled back</pre><p>A team rule that works well: use unchecked exceptions for business errors. If you must use checked ones, always add rollbackFor. Some teams create a meta-annotation such as @ServiceTransactional that includes it, so nobody forgets.</p><h3>Bug 1c: “I caught the exception. Why did everything roll back?”</h3><blockquote><strong><em>Real-life problem:</em></strong><em> checkout calls an optional loyalty-bonus service, wrapped in try/catch: “if the bonus fails, just continue”. Then the loyalty service has a bad day and every checkout fails with UnexpectedRollbackException. Nobody understands why, because the exception was caught.</em></blockquote><p><strong>Why it happens:</strong> the inner method is @Transactional (REQUIRED by default), so it joins the outer transaction. When it throws, the proxy marks the <strong>shared</strong> transaction as rollback-only. Catching the exception does not remove that mark. At commit, Spring rolls back and throws.</p><pre>@Transactional<br>public void checkoutWithOptionalBonus(Long id, BigDecimal amt) {<br>  accounts.findById(id).orElseThrow().debit(amt);<br>  try {<br>    txOps.applyLoyaltyBonus(id);   // @Transactional, throws<br>  } catch (IllegalStateException e) {<br>    log.warn(&quot;Bonus failed, continuing: {}&quot;, e.getMessage());<br>  }<br>}   // commit → UnexpectedRollbackException</pre><pre>curl -X POST localhost:8080/demo/basics/unexpected-rollback</pre><pre>{ &quot;outcome&quot;: &quot;threw UnexpectedRollbackException: Transaction<br>   silently rolled back because it has been marked as<br>   rollback-only&quot;,<br>  &quot;balanceBefore&quot;: 800.00, &quot;balanceAfter&quot;: 800.00 }</pre><p>The transaction log tells the story:</p><pre>Creating new transaction [...checkoutWithOptionalBonus]<br>Participating in existing transaction<br>Participating transaction failed - marking existing<br>  transaction as rollback-only<br>Bonus failed, continuing: loyalty service rejected the bonus<br>Committing JPA transaction ...  → UnexpectedRollbackException</pre><p><strong>Fixes, pick one:</strong></p><ul><li>If the optional step does not need the transaction, call it after the commit.</li><li>If it writes to the database on its own, give it REQUIRES_NEW (pattern 2), so its failure only rolls back its own work.</li><li>If it must never break the caller, catch the exception inside the inner method, or use noRollbackFor.</li></ul><h3>Bonus trap: fields on a proxied bean are null</h3><p>I hit this one myself while building the demo. A controller read a counter field directly from a @Transactional service:</p><pre>txService.optimisticAttempts.set(0);  // NullPointerException</pre><p>The controller holds the CGLIB proxy, not the real object. The proxy is a subclass with its own empty fields, and only method calls are forwarded. Always use methods, never fields, on Spring beans.</p><h3>2. REQUIRES_NEW: work that must survive a rollback</h3><blockquote><strong><em>Real-life problem:</em></strong><em> compliance and fraud teams need a record of every withdrawal attempt, especially failed ones, because many failed attempts in a row is a fraud signal. But the audit row is written in the same transaction as the withdrawal. When the withdrawal fails, the audit row is rolled back too, and the fraud team sees nothing.</em></blockquote><p><strong>Propagation</strong> decides what happens when a transactional method is called while a transaction already exists:</p><ul><li><strong>REQUIRED</strong> (default): join the current transaction, or start one. Use for normal business methods.</li><li><strong>REQUIRES_NEW</strong>: pause the current transaction and start a separate one. Use for audit logs, failure records and number allocation.</li><li><strong>NESTED</strong>: a savepoint inside the current transaction. Use for batch imports that skip bad rows. Works with JDBC; limited with JPA.</li><li><strong>MANDATORY</strong>: fail if there is no transaction. Use for helpers that must never run alone.</li><li><strong>NOT_SUPPORTED</strong>: pause the transaction and run without one. Use for slow external calls.</li><li><strong>SUPPORTS</strong> and <strong>NEVER</strong>: join if present, or fail if present. Rarely needed.</li></ul><pre>@Service<br>public class AuditService {</pre><pre>  @Transactional                     // joins the caller&#39;s tx<br>  public void recordInCallerTx(String msg) {<br>    audits.save(new AuditLog(&quot;REQUIRED&quot;, msg));<br>  }</pre><pre>  // own transaction, commits immediately<br>  @Transactional(propagation = Propagation.REQUIRES_NEW)<br>  public void recordIndependently(String msg) {<br>    audits.save(new AuditLog(&quot;REQUIRES_NEW&quot;, msg));<br>  }<br>}</pre><pre>@Service<br>public class WithdrawalService {<br>  @Transactional<br>  public void withdraw(Long id, BigDecimal amt) {<br>    String msg = &quot;attempt: withdraw %s from %d&quot;.formatted(amt, id);<br>    audit.recordIndependently(msg);<br>    audit.recordInCallerTx(msg);<br>    accounts.findById(id).orElseThrow().debit(amt); // may throw<br>  }<br>}</pre><p>Try one withdrawal that works and one that fails:</p><pre>curl -X POST &quot;localhost:8080/demo/propagation/withdraw?amount=100&quot;<br>curl -X POST &quot;localhost:8080/demo/propagation/withdraw?amount=999999&quot;<br>curl localhost:8080/demo/propagation/audit</pre><pre>[ { &quot;propagation&quot;: &quot;REQUIRES_NEW&quot;, &quot;message&quot;: &quot;...withdraw 100&quot; },<br>  { &quot;propagation&quot;: &quot;REQUIRED&quot;,     &quot;message&quot;: &quot;...withdraw 100&quot; },<br>  { &quot;propagation&quot;: &quot;REQUIRES_NEW&quot;, &quot;message&quot;: &quot;...withdraw 999999&quot; } ]</pre><p>The failed withdrawal kept only its REQUIRES_NEW row. The log shows exactly what happened:</p><pre>Creating new transaction [...WithdrawalService.withdraw]<br>Suspending current transaction, creating new transaction<br>  [...AuditService.recordIndependently]<br>Committing JPA transaction ...<br>Resuming suspended transaction after completion of inner<br>Participating in existing transaction<br>Initiating transaction rollback</pre><h3>Watch out: REQUIRES_NEW can freeze your service</h3><p>While the inner transaction runs, the outer one is paused but <strong>still holds its database connection</strong>. One request now needs two connections at the same time.</p><p>Say the HikariCP pool has 10 connections and 10 requests arrive together. Each takes one connection for the outer transaction, and the pool is empty. Each then waits for a second connection for REQUIRES_NEW, and nobody can continue. After the connection timeout (30 seconds by default) they all fail with “Connection is not available, request timed out”.</p><p>Use REQUIRES_NEW only for small, fast writes, size the pool for it, and never nest REQUIRES_NEW inside REQUIRES_NEW.</p><h3>3. Read-only transactions</h3><blockquote><strong><em>Real-life problem:</em></strong><em> a “get profile” endpoint formats the name for display by calling setOwner(name.toUpperCase()) on the entity. The entity is managed by Hibernate, so the change is saved at commit. Customer names in the database slowly turn UPPERCASE, and nobody wrote an UPDATE.</em></blockquote><p>With readOnly = true, Hibernate skips dirty checking and does not flush, so accidental changes are never written. It is also faster and uses less memory on large reads, because Hibernate keeps no snapshot copies of entities.</p><pre>@Service<br>@Transactional(readOnly = true)   // class default: read-only<br>public class ProfileService {</pre><pre>  public String displayName(Long id) {<br>    Account a = accounts.findById(id).orElseThrow();<br>    a.setOwner(a.getOwner().toUpperCase()); // NOT saved<br>    return a.getOwner();<br>  }</pre><pre>  @Transactional                   // write methods opt out<br>  public String rename(Long id, String owner) {<br>    accounts.findById(id).orElseThrow().setOwner(owner);<br>    return owner;                  // saved by dirty checking<br>  }<br>}</pre><pre>curl -X POST localhost:8080/demo/readonly/modify-in-readonly<br>{ &quot;valueInsideMethod&quot;: &quot;ALICE&quot;, &quot;valueInDatabase&quot;: &quot;alice&quot; }</pre><pre>curl -X POST &quot;localhost:8080/demo/readonly/rename?owner=bob&quot;<br>{ &quot;valueInDatabase&quot;: &quot;bob&quot; }</pre><p>In production you can also route read-only transactions to a <strong>read replica</strong> (Aurora, Cloud SQL) with AbstractRoutingDataSource and LazyConnectionDataSourceProxy. Replicas lag a little, so don&#39;t read from one right after a write in the same flow.</p><p>The real fix for the UPPERCASE bug is to map to a DTO for display instead of changing entities. readOnly is a safety net.</p><h3>4. TransactionTemplate: keep slow remote calls out of the transaction</h3><blockquote><strong><em>Real-life problem:</em></strong><em> the payment gateway (Stripe, Adyen, a bank API) slows down from 200 ms to 5 seconds. Your payment method is @Transactional and calls the gateway inside it, so each request holds a database connection for 5 seconds. The connection pool runs out, and every endpoint in the service starts failing, including ones that have nothing to do with payments.</em></blockquote><blockquote><em>There is a second problem. When the gateway call fails, the transaction rolls back the PENDING payment row too. Now there is no record that you tried to charge the customer.</em></blockquote><p>The bad version:</p><pre>@Transactional<br>public PayResult payInsideTx(BigDecimal amt, boolean decline) {<br>  Payment p = payments.save(Payment.pending(amt));<br>  var result = gateway.charge(amt, decline); // 1.5 s, conn held<br>  p.complete(result.reference());<br>  return new PayResult(p, result.dbTxOpenDuringCall());<br>}</pre><p>The good version: two short transactions with the remote call in between. TransactionTemplate is auto-configured by Spring Boot, so just inject it.</p><pre>public PayResult pay(BigDecimal amt, boolean decline) {<br>  // tx 1: record the attempt, committed before the gateway call<br>  Long id = tx.execute(s -&gt;<br>      payments.save(Payment.pending(amt)).getId());</pre><pre>  ChargeResult result;<br>  try {<br>    result = gateway.charge(amt, decline);  // no tx, no conn<br>  } catch (RuntimeException e) {<br>    tx.executeWithoutResult(s -&gt;<br>        payments.findById(id).orElseThrow().fail());<br>    return new PayResult(payments.findById(id).orElseThrow(), false);<br>  }</pre><pre>  // tx 2: record the result<br>  tx.executeWithoutResult(s -&gt;<br>      payments.findById(id).orElseThrow()<br>              .complete(result.reference()));<br>  return new PayResult(payments.findById(id).orElseThrow(),<br>                       result.dbTxOpenDuringCall());<br>}</pre><p>The fake gateway in the demo checks TransactionSynchronizationManager.isActualTransactionActive() during the call, so we can prove where the transaction was:</p><pre>curl -X POST &quot;localhost:8080/demo/payments/inside-tx?amount=50&quot;<br>{ &quot;payment&quot;: { &quot;status&quot;: &quot;COMPLETED&quot; },<br>  &quot;dbTransactionOpenDuringGatewayCall&quot;: true }</pre><pre>curl -X POST &quot;localhost:8080/demo/payments/inside-tx?amount=60&amp;decline=true&quot;<br>{ &quot;error&quot;: &quot;gateway declined charge of 60&quot; }   ← no PENDING row left</pre><pre>curl -X POST &quot;localhost:8080/demo/payments/split-tx?amount=80&amp;decline=true&quot;<br>{ &quot;payment&quot;: { &quot;amount&quot;: 80.00, &quot;status&quot;: &quot;FAILED&quot; },<br>  &quot;dbTransactionOpenDuringGatewayCall&quot;: false }</pre><p><strong>The trade-off:</strong> splitting the transaction means you lose all-or-nothing. If the app crashes after the gateway succeeds but before tx 2, the row stays PENDING while the customer was charged. Handle that with an <strong>idempotency key</strong> sent to the gateway (use the payment id), so a retry never charges twice, and a <strong>reconciliation job</strong> that checks old PENDING rows with the gateway. That is still far better than a frozen service and missing records.</p><p>The rule: never make network calls (HTTP, Kafka, S3, email) inside a database transaction, and give transactions on hot tables a timeout, such as @Transactional(timeout = 5).</p><h3>5. Locking: when two requests update the same row</h3><blockquote><strong><em>Real-life problem:</em></strong><em> a flash sale has one item left. Two users click Buy at the same moment. Both read stock = 1, both subtract 1, both save stock = 0. You sold two items and had one. The same thing happens with wallet balances, seat booking and coupon limits. This is a lost update, and the default isolation level (READ COMMITTED) does not prevent it.</em></blockquote><h3>Option A: optimistic locking (the default choice)</h3><p>Add a version column. Hibernate adds WHERE version = ? to every UPDATE. If someone else changed the row first, no row is updated and Spring throws ObjectOptimisticLockingFailureException.</p><pre>@Entity<br>public class Account {<br>  @Id private Long id;<br>  private BigDecimal balance;<br>  @Version private Long version;<br>}</pre><pre>@Transactional<br>public void debitOptimistic(Long id, BigDecimal amt) {<br>  accounts.findById(id).orElseThrow().debit(amt);<br>}</pre><p>The demo fires 8 debits of 10.00 at the same account at the same moment. Exact counts change between runs because thread timing is random.</p><pre>curl -X POST localhost:8080/demo/locking/optimistic-no-retry</pre><pre>{ &quot;requests&quot;: 8, &quot;succeeded&quot;: 1, &quot;failed&quot;: 7,<br>  &quot;balanceBefore&quot;: 1000.00, &quot;balanceAfter&quot;: 990.00,<br>  &quot;errors&quot;: [&quot;7 x ObjectOptimisticLockingFailureException&quot;] }</pre><p>No update was lost, so the data is correct. But 7 of 8 users got an error. Add a retry:</p><pre>@Service<br>public class RetryingAccountService {</pre><pre>  @Retryable(includes = ObjectOptimisticLockingFailureException.class,<br>             maxRetries = 10, delay = 20, multiplier = 2,<br>             maxDelay = 500, jitter = 10)<br>  public void debit(Long id, BigDecimal amt) {<br>    txService.debitOptimistic(id, amt);   // a DIFFERENT bean<br>  }<br>}</pre><pre>curl -X POST localhost:8080/demo/locking/optimistic-retry</pre><pre>{ &quot;requests&quot;: 8, &quot;succeeded&quot;: 8, &quot;failed&quot;: 0,<br>  &quot;totalAttempts&quot;: 30,<br>  &quot;balanceBefore&quot;: 1000.00, &quot;balanceAfter&quot;: 920.00 }</pre><p>The retry must sit <strong>outside</strong> the transaction, in a different bean. If retry and @Transactional are on the same method, you may retry inside a transaction already marked rollback-only, with stale data in the Hibernate session. Each retry has to start a fresh transaction and read the new version.</p><h3>Option B: pessimistic locking (for hot rows)</h3><p>Look at totalAttempts: 30 above. Eight requests needed 30 attempts. When many requests fight over the same row, optimistic locking wastes work, and it is better to lock the row and make the others wait:</p><pre>public interface AccountRepository<br>    extends JpaRepository&lt;Account, Long&gt; {</pre><pre>  @Lock(LockModeType.PESSIMISTIC_WRITE)   // SELECT ... FOR UPDATE<br>  @QueryHints(@QueryHint(<br>      name = &quot;jakarta.persistence.lock.timeout&quot;, value = &quot;3000&quot;))<br>  @Query(&quot;select a from Account a where a.id = :id&quot;)<br>  Optional&lt;Account&gt; findForUpdate(Long id);<br>}</pre><pre>@Transactional(timeout = 10)<br>public void debitPessimistic(Long id, BigDecimal amt) {<br>  accounts.findForUpdate(id).orElseThrow().debit(amt);<br>}</pre><pre>curl -X POST localhost:8080/demo/locking/pessimistic</pre><pre>{ &quot;requests&quot;: 8, &quot;succeeded&quot;: 8, &quot;failed&quot;: 0,<br>  &quot;balanceBefore&quot;: 1000.00, &quot;balanceAfter&quot;: 920.00 }</pre><h3>Which one?</h3><ul><li><strong>Optimistic (@Version)</strong> fits when conflicts are rare, like profile or order edits. It costs nothing without a conflict. The risk is retry storms under heavy contention, so always retry outside the transaction with backoff and jitter.</li><li><strong>Pessimistic (FOR UPDATE)</strong> fits when conflicts are common, like a stock counter or a busy wallet. Requests wait in line. The risks are deadlocks, long waits and pool exhaustion, so always set a lock timeout, keep transactions short and lock rows in a fixed order.</li><li><strong>An atomic SQL update</strong> is often best for simple counters: UPDATE product SET stock = stock - 1 WHERE id = ? AND stock &gt; 0, then check that one row changed. No lock is held while Java code runs, and no retry is needed.</li></ul><p>On isolation levels: PostgreSQL, Oracle and SQL Server default to READ COMMITTED, and MySQL InnoDB to REPEATABLE READ. SERIALIZABLE prevents the most, but you must retry serialization failures. In practice, keep the database default and protect the specific rows that need it.</p><h3>6. Transactional events: side effects only after commit</h3><blockquote><strong><em>Real-life problem:</em></strong><em> the order service saves an order, publishes an OrderPlaced event, and a listener sends “Your order is confirmed!”. Later in the same method a stock check fails and the transaction rolls back. The customer has a confirmation email for an order that does not exist.</em></blockquote><p><strong>Why it happens:</strong> a normal @EventListener runs immediately, when publishEvent() is called. At that moment nobody knows whether the transaction will commit.</p><pre>@Transactional<br>public CustomerOrder placeOrder(String item, boolean fail) {<br>  CustomerOrder order = orders.save(new CustomerOrder(item));<br>  events.publishEvent(new OrderPlaced(order.getId(), item));<br>  if (fail) throw new IllegalStateException(&quot;stock check failed&quot;);<br>  return order;<br>}</pre><pre>@Component<br>public class OrderEventListeners {</pre><pre>  @EventListener                      // BAD for side effects<br>  public void emailNow(OrderPlaced e) { ... }</pre><pre>  @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)<br>  public void emailAfterCommit(OrderPlaced e) { ... }  // GOOD</pre><pre>  @TransactionalEventListener(phase = TransactionPhase.AFTER_ROLLBACK)<br>  public void onRollback(OrderPlaced e) { ... }<br>}</pre><p>First an order that succeeds, then one that rolls back:</p><pre>curl -X POST &quot;localhost:8080/demo/events/order?item=laptop&quot;<br>{ &quot;ordersInDatabase&quot;: 1,<br>  &quot;notificationsSent&quot;: [<br>    &quot;[@EventListener] email: order #1 (laptop) confirmed&quot;,<br>    &quot;[AFTER_COMMIT] email: order #1 (laptop) confirmed&quot; ] }</pre><pre>curl -X POST &quot;localhost:8080/demo/events/order?item=camera&amp;fail=true&quot;<br>{ &quot;error&quot;: &quot;stock check failed after the event was published&quot;,<br>  &quot;ordersInDatabase&quot;: 1,<br>  &quot;notificationsSent&quot;: [<br>    &quot;[@EventListener] email: order #2 (camera) confirmed&quot;,<br>    &quot;[AFTER_ROLLBACK] order #2 (camera) rolled back&quot; ] }</pre><p>Order #2 was never saved, but the plain @EventListener had already “sent” the email. The AFTER_COMMIT listener correctly did nothing.</p><p>Things to watch:</p><ul><li><strong>AFTER_COMMIT events are not durable.</strong> If the pod is killed between the commit and the listener, the email is never sent. For events you cannot lose, use the outbox (pattern 7).</li><li><strong>Database writes inside an AFTER_COMMIT listener are silently lost</strong> unless the listener has @Transactional(propagation = REQUIRES_NEW), because the original transaction has already committed.</li><li>The listener runs on the same thread and delays the HTTP response. For slow work add @Async, and accept that failures are no longer visible to the caller.</li><li>Good uses: cache eviction, metrics, search-index updates and nice-to-have notifications.</li></ul><h3>7. Transactional outbox: database and Kafka without losing events</h3><blockquote><strong><em>Real-life problem (the dual write):</em></strong><em> the order service must save the order and publish OrderPlaced to Kafka for inventory, billing and shipping. Save then send: the database commits, Kafka is down or the pod restarts, and the event is lost, so billing never invoices. Send then save: Kafka gets the event, the commit fails, and shipping ships an order that does not exist.</em></blockquote><p>A database and Kafka cannot commit together without distributed transactions (2PC/XA), which are slow, fragile and unsupported by most cloud services.</p><p><strong>The fix:</strong> write the event to an outbox_event table in the same local transaction as the order. A separate relay reads the table and publishes to Kafka.</p><pre>Request ─► [ one DB tx: INSERT order + INSERT outbox_event ]<br>                           │<br>            relay (poller or Debezium CDC)<br>                           ▼<br>                Kafka ─► inventory, billing, shipping</pre><p>Writing the event has no Kafka call at all:</p><pre>@Transactional<br>public CustomerOrder placeOrder(String item, boolean fail) {<br>  CustomerOrder order = orders.save(new CustomerOrder(item));<br>  String payload = json.writeValueAsString(<br>      Map.of(&quot;orderId&quot;, order.getId(), &quot;item&quot;, item));<br>  outbox.save(new OutboxEvent(&quot;orders.placed&quot;,<br>      String.valueOf(order.getId()), payload));<br>  if (fail) throw new IllegalStateException(&quot;validation failed&quot;);<br>  return order;<br>}</pre><p>The relay locks a batch with SKIP LOCKED, so several pods can run it without stepping on each other:</p><pre>@Query(value = &quot;&quot;&quot;<br>    SELECT * FROM outbox_event<br>    WHERE published_at IS NULL<br>    ORDER BY id<br>    LIMIT :batchSize<br>    FOR UPDATE SKIP LOCKED<br>    &quot;&quot;&quot;, nativeQuery = true)<br>List&lt;OutboxEvent&gt; lockNextBatch(int batchSize);</pre><pre>@Transactional(timeout = 30)<br>public int relayBatch() {<br>  int published = 0;<br>  for (OutboxEvent e : outbox.lockNextBatch(50)) {<br>    e.recordAttempt();<br>    try {<br>      publisher.publish(e.getTopic(), e.getMessageKey(),<br>                        e.getEventId(), e.getPayload());<br>      e.markPublished();<br>      published++;<br>    } catch (RuntimeException ex) {<br>      break;   // keep order; the rest stay pending<br>    }<br>  }<br>  return published;<br>}</pre><pre>@Scheduled(fixedDelayString = &quot;${demo.outbox.poll-ms:3000}&quot;)<br>public void poll() { relay.relayBatch(); }</pre><p>In a real service, publish() wraps kafkaTemplate.send(...).get(5, SECONDS). Sending synchronously matters: if Kafka rejects the message, the row must not be marked as published.</p><p>Yes, this relay makes a network call inside a transaction, which pattern 4 warned against. Here it is a small, bounded background batch with a timeout, not a user request, so it is a deliberate trade-off. Debezium avoids it completely.</p><p>Now watch it survive a Kafka outage:</p><pre># 1. Validation fails: order and event roll back together<br>curl -X POST &quot;localhost:8080/demo/outbox/order?item=phone&amp;fail=true&quot;<br>{ &quot;ordersInDatabase&quot;: 0, &quot;outboxRows&quot;: 0 }</pre><pre># 2. Kafka goes down<br>curl -X POST &quot;localhost:8080/demo/outbox/broker?down=true&quot;</pre><pre># 3. Orders still work. Customers notice nothing.<br>curl -X POST &quot;localhost:8080/demo/outbox/order?item=phone&quot;<br>curl -X POST &quot;localhost:8080/demo/outbox/order?item=tablet&quot;<br>{ &quot;brokerDown&quot;: true, &quot;ordersInDatabase&quot;: 2,<br>  &quot;outboxPending&quot;: 2, &quot;deliveredToBroker&quot;: [] }</pre><pre># 4. Kafka returns; the scheduler catches up in seconds<br>curl -X POST &quot;localhost:8080/demo/outbox/broker?down=false&quot;<br>curl localhost:8080/demo/outbox<br>{ &quot;outboxPending&quot;: 0,<br>  &quot;deliveredToBroker&quot;: [<br>    { &quot;topic&quot;: &quot;orders.placed&quot;, &quot;key&quot;: &quot;4&quot;, ... },<br>    { &quot;topic&quot;: &quot;orders.placed&quot;, &quot;key&quot;: &quot;5&quot;, ... } ] }</pre><p>A production checklist for the outbox:</p><ul><li><strong>Delivery is at-least-once.</strong> If the relay crashes after sending but before commit, the event is sent again, so consumers must be idempotent (pattern 8). That is why each row has a unique event id.</li><li><strong>Polling or CDC:</strong> polling is simple and fine for most loads. For high volume or low latency, use Debezium with its Outbox Event Router, which reads the database log instead of polling.</li><li><strong>Ordering:</strong> use the order id as the Kafka key, so events for one order land in one partition, in order.</li><li><strong>Cleanup:</strong> delete or archive published rows after a few days, or the table grows forever.</li><li><strong>Monitoring:</strong> alert on the age of the oldest pending row. That is your “events are stuck” signal.</li><li><strong>Kafka transactions</strong> give exactly-once only inside Kafka (consume, process, produce). They do not make a database write and a Kafka send atomic, and ChainedKafkaTransactionManager is deprecated. The outbox is the answer.</li></ul><h3>8. Idempotent consumer: receive twice, apply once</h3><blockquote><strong><em>Real-life problem:</em></strong><em> a payment provider sends a PaymentReceived webhook. Your service credits the account but responds slowly, so the provider times out and sends it again. A Kafka rebalance or an SQS visibility timeout causes the same thing. The account is credited three times. Every at-least-once system does this sooner or later.</em></blockquote><p>The naive handler:</p><pre>@Transactional<br>public void applyNaive(PaymentReceived event) {<br>  accounts.findById(event.accountId()).orElseThrow()<br>          .credit(event.amount());<br>}</pre><pre>curl -X POST \<br>  &quot;localhost:8080/demo/idempotency/naive?eventId=pay-001&amp;deliveries=3&quot;</pre><pre>{ &quot;outcomes&quot;: [&quot;APPLIED&quot;, &quot;APPLIED&quot;, &quot;APPLIED&quot;],<br>  &quot;balanceBefore&quot;: 1000.00, &quot;balanceAfter&quot;: 1300.00,<br>  &quot;expectedAfter&quot;: 1100.00 }</pre><p><strong>The fix:</strong> record the event id in a table with a unique constraint, in the same transaction as the business change, and insert it first.</p><pre>@Entity<br>public class ProcessedMessage {<br>  @Id @GeneratedValue(strategy = GenerationType.IDENTITY)<br>  private Long id;<br>  @Column(nullable = false, unique = true)<br>  private String eventId;<br>  private Instant processedAt;<br>}</pre><pre>@Transactional<br>public void applyOnce(PaymentReceived event) {<br>  // INSERT now; a duplicate fails here, before the credit<br>  processed.saveAndFlush(new ProcessedMessage(event.eventId()));<br>  accounts.findById(event.accountId()).orElseThrow()<br>          .credit(event.amount());<br>}</pre><p>The entry point is deliberately not transactional, because it must catch the duplicate after the transaction has rolled back:</p><pre>@Component<br>public class PaymentReceivedConsumer {</pre><pre>  @Retryable(includes = PessimisticLockingFailureException.class,<br>             maxRetries = 5, delay = 50, jitter = 25)<br>  public Outcome onMessage(PaymentReceived event) {<br>    try {<br>      handler.applyOnce(event);<br>      return Outcome.APPLIED;<br>    } catch (DataIntegrityViolationException duplicate) {<br>      return Outcome.DUPLICATE_SKIPPED;  // ack: already handled<br>    }<br>  }<br>}</pre><pre>curl -X POST \<br>  &quot;localhost:8080/demo/idempotency/safe?eventId=pay-002&amp;deliveries=3&quot;<br>{ &quot;outcomes&quot;: [&quot;APPLIED&quot;, &quot;DUPLICATE_SKIPPED&quot;, &quot;DUPLICATE_SKIPPED&quot;],<br>  &quot;balanceBefore&quot;: 1000.00, &quot;balanceAfter&quot;: 1100.00 }</pre><pre># five copies at the same moment<br>curl -X POST &quot;localhost:8080/demo/idempotency/safe\<br>?eventId=pay-003&amp;deliveries=5&amp;concurrent=true&quot;<br>{ &quot;outcomes&quot;: [&quot;DUPLICATE_SKIPPED&quot;, &quot;APPLIED&quot;,<br>               &quot;DUPLICATE_SKIPPED&quot;, &quot;DUPLICATE_SKIPPED&quot;,<br>               &quot;DUPLICATE_SKIPPED&quot;],<br>  &quot;balanceBefore&quot;: 1100.00, &quot;balanceAfter&quot;: 1200.00 }</pre><p><strong>Why not “check if it exists, then insert”?</strong> Two copies arriving together both check, both see “not processed”, and both apply. Only the database’s unique constraint is safe under concurrency, and the concurrent run above proves it.</p><p><strong>Why the retry?</strong> When two copies hit the unique index at the same moment, some databases report a lock error instead of a duplicate-key error. Retrying turns that into a clean duplicate.</p><p>On PostgreSQL you can use INSERT ... ON CONFLICT DO NOTHING and check the row count instead of catching an exception. Keep processed ids longer than your longest redelivery window, then clean them up. The id must come from the producer (the outbox event id or the provider’s webhook id), never be generated by the consumer.</p><h3>9. Saga: transactions across services</h3><blockquote><strong><em>Real-life problem:</em></strong><em> placing an order touches three services: inventory reserves stock, payment charges the card, shipping books a courier. Each has its own database. If the courier booking fails, the customer must be refunded and the stock released, and no single transaction covers all three.</em></blockquote><p>A saga splits the flow into local transactions. Each step has a <strong>compensating action</strong> that undoes it:</p><ol><li>Create the order as PENDING. Compensation: cancel the order.</li><li>Reserve stock. Compensation: release the stock.</li><li>Charge the payment. Compensation: refund it.</li><li>Book the courier. This is the last step, so there is nothing to undo.</li></ol><p>If step 4 fails, the saga runs refund, then release stock, then cancel order.</p><ul><li><strong>Choreography:</strong> services react to each other’s events. Fine for two or three simple steps, but the full flow is hard to see.</li><li><strong>Orchestration:</strong> one orchestrator tells each service what to do. Better for longer flows, error handling and monitoring. Tools include Temporal, Camunda and AWS Step Functions.</li></ul><p>A saga is built from the earlier patterns: each step is a local @Transactional method, publishes through the outbox, and has an idempotent consumer. Sagas give eventual consistency, not isolation, so use a status such as PENDING to stop other requests from acting on a half-finished order. The saga is not in the demo because it needs several services.</p><h3>Production checklist</h3><ul><li>spring.jpa.open-in-view=false</li><li>@Transactional on the service layer, not on controllers</li><li>No this.method() calls to transactional methods</li><li>Business exceptions are unchecked, or rollbackFor = Exception.class is set</li><li>No exceptions swallowed inside transactional methods</li><li>No HTTP, Kafka, S3 or email calls inside a database transaction</li><li>readOnly = true on query methods</li><li>@Version on entities updated concurrently, with retry outside the transaction</li><li>Every pessimistic lock and every transaction has a timeout</li><li>REQUIRES_NEW used sparingly, with the pool sized for it</li><li>Side effects run after commit, or go through the outbox</li><li>Every message consumer is idempotent</li></ul><h3>Quick decision guide</h3><ul><li><strong>Normal create or update in one service:</strong> @Transactional</li><li><strong>Queries and reports:</strong> readOnly = true, optionally a read replica</li><li><strong>A record that must survive a rollback:</strong> REQUIRES_NEW, small and fast only</li><li><strong>A slow external call in the middle of database work:</strong> TransactionTemplate with short transactions and reconciliation</li><li><strong>Concurrent updates with few conflicts:</strong> @Version plus retry with backoff</li><li><strong>Concurrent updates on a hot row:</strong> a pessimistic lock with a timeout, or an atomic SQL update</li><li><strong>A side effect after commit, where losing one is acceptable:</strong> @TransactionalEventListener(AFTER_COMMIT)</li><li><strong>A database write plus an event that must not be lost:</strong> transactional outbox, with Debezium at scale</li><li><strong>Any at-least-once message or webhook:</strong> an idempotent consumer keyed on a unique event id</li><li><strong>A business flow across services:</strong> a saga, orchestrated for anything complex</li></ul><h3>Spring Boot 3 or 4?</h3><p>The demo uses Spring Boot 4.1, and everything here also works on Boot 3 with three changes:</p><ul><li><strong>Retry:</strong> Boot 4 (Spring Framework 7) has @Retryable built in, enabled with @EnableResilientMethods. On Boot 3, add spring-retry and AOP, use @EnableRetry, and write @Retryable(retryFor = ..., maxAttempts = ..., backoff = @Backoff(...)).</li><li><strong>Web starter:</strong> spring-boot-starter-webmvc on Boot 4, spring-boot-starter-web on Boot 3.</li><li><strong>JSON:</strong> Jackson 3 (JsonMapper) on Boot 4, Jackson 2 (ObjectMapper) on Boot 3.</li></ul><h3>Wrapping up</h3><p>@Transactional is easy to add and easy to get wrong. The bugs it causes rarely show up in unit tests. They show up under load, during outages, and when messages arrive twice. Each one has a well-known fix, and once you have seen the bug happen, you remember it.</p><p>Clone the demo, turn on the transaction log, and break things on purpose. That is the fastest way to build the instinct.</p><p><strong>Source code:</strong> <a href="https://github.com/ChanukD/springboot-transaction-patterns">github.com/ChanukD/springboot-transaction-patterns</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d5d56717df81" width="1" height="1" alt=""><hr><p><a href="https://blog.stackademic.com/spring-boot-transactions-in-production-8-patterns-the-real-bugs-they-prevent-and-a-demo-you-can-d5d56717df81">Spring Boot Transactions in Production: 8 Patterns, the Real Bugs They Prevent, and a Demo You Can…</a> was originally published in <a href="https://blog.stackademic.com/">Stackademic</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[AWS Lambda MicroVMs: The Serverless Primitive for Code You Didn’t Write]]></title>
            <link>https://aws.plainenglish.io/aws-lambda-microvms-the-serverless-primitive-for-code-you-didnt-write-04acc88534ad?source=rss-9be9624c6d5a------2</link>
            <guid isPermaLink="false">https://medium.com/p/04acc88534ad</guid>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[aws-certification]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <dc:creator><![CDATA[Chanuka Dinuwan]]></dc:creator>
            <pubDate>Tue, 11 Aug 2026 16:37:13 GMT</pubDate>
            <atom:updated>2026-08-11T17:02:32.033Z</atom:updated>
            <content:encoded><![CDATA[<p>Firecracker, snapshots, and an eight-hour session — what actually changes for builders.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YkqRu-P8nBum3PCZ.png" /></figure><p>For eleven years, it was a simple trade-off: give us a function, get back an invocation. Stateless event-driven, millisecond billing granularity. That model has built a lot of good systems.</p><p>It also quietly omitted an entire class of application that has exploded in the past two years: software whose entire job is to run code the developer never wrote.</p><p>AI code assistants that run Python code generated by LLMs. Spark script running on the tenant’s data platforms. Vuln scanners. **CI/CD runners consuming a pull request from a stranger on the internet. Web IDEs. All of them have the same requirement: each user or job gets their own compute environment, isolated enough so that malicious code can’t get to anyone else’s.</p><p>Building that meant you had to pick your poison until now. Full VMs actually do isolate but they take minutes to boot so you keep warm pools. Containers start in seconds but share a kernel, so you spend months hardening them. Functions start fast, isolate well, but are stateless and limited to 15 minutes.</p><p>On June 22, AWS shipped Lambda MicroVMs. A new primitive that makes you stop choosing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cW4hh_yvOPMAKwXZZcWWXQ.png" /></figure><h4>The one-line version</h4><p>A MicroVM is a Firecracker virtual machine you start by API, which boots from a pre-initialized snapshot, has its own HTTPS endpoint, keeps memory and disk state over a session, and pauses to storage-only cost when not in use.</p><p>Same Firecracker that already powers 15 trillion+ monthly invocations at Lambda. New API surface, new resource type, new mental model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C3vIoohGK2KUzK0v5kB4lA.png" /></figure><h4>Forget “invoke.” Think “image, then launch.”</h4><p>There is no invoke model here. That single fact reshapes everything downstream.</p><p><strong>Step one — build an image.</strong> You put a Dockerfile and your application artifacts in a zip in S3, then call create-microvm-image. Lambda spins up a clean microVM, runs your Dockerfile, starts your app, waits for it to finish initializing, and takes a Firecracker snapshot of the <em>running</em> memory and disk state.</p><pre>aws lambda-microvms create-microvm-image \<br>  --code-artifact uri=s3://my-bucket/artifact.zip \<br>  --name my-sandbox-image \<br>  --base-image-arn arn:aws:lambda:us-east-1:aws:microvm-image:al2023-1 \<br>  --build-role-arn arn:aws:iam::123456789012:role/MicroVMBuildRole</pre><p><strong>Step two — launch from it.</strong> Every microVM you start from that image restores the snapshot instead of cold-booting. Your app isn’t starting; it’s <em>resuming</em>, already warm.</p><pre>aws lambda-microvms run-microvm \<br>  --image-identifier arn:aws:lambda:us-east-1:123456789012:microvm-image:my-sandbox-image \<br>  --execution-role-arn arn:aws:iam::123456789012:role/MicroVMExecutionRole \<br>  --idle-policy &#39;{&quot;maxIdleDurationSeconds&quot;:900,&quot;suspendedDurationSeconds&quot;:300,&quot;autoResumeEnabled&quot;:true}&#39;</pre><p>You get a microVM ID and a dedicated HTTPS URL in return. No balancer. No target groups. No infrastructure to enter.</p><p>AWS bills this as “near-instant” but doesn’t publish numbers so it’s worth taking a look at Aidan Steele’s independent measurements shortly after launch: about two seconds from RunMicrovm to RUNNING, another two before the app was returning 200s, and about a second each way for suspend and resume. Fast, but budget for it, not zero.</p><p>You know the drill if you’ve used SnapStart. The difference is in the scope: SnapStart snapshots a Java function’s init phase. This takes snapshots of a whole Linux userspace running whatever you want And it’s the same idea, but more general than the handler model, which is why it fits things that were never going to be functions.</p><p>That generalization’s headline is for anyone who has spent a career watching a JVM spool up for eight seconds before it can serve its first useful request. Load the classes once at build time, snapshot, and every subsequent environment starts on the far side of that cost.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BkOflpBZKXxypzSCVNq3xw.png" /></figure><h4>Snapshots come with rules</h4><p>Every microVM you launch is a copy of that same frozen machine. That creates problems a new boot doesn’t have. The links in the snapshot are already broken. Each copy has the same random seed. The cached credentials may have expired.</p><p>You handle this with lifecycle hooks. You run a small HTTP listener on a port of your choice (9000 is the convention) and Lambda calls it at each stage.</p><p><strong>At build time:</strong></p><ul><li><strong>/ready</strong> — return 200 when your app has finished starting up. That&#39;s Lambda&#39;s cue to take the snapshot.</li><li><strong>/validate</strong> — optional, but worth it. Lambda launches a test microVM so you can send real requests through it. While you do, Lambda notes which parts of the snapshot get touched and prefetches them on future launches, so later startups are faster.</li></ul><p><strong>At run time:</strong></p><ul><li><strong>/run</strong> — fires when a new microVM starts. Reset anything that has to be unique per instance, reseed randomness, rebuild connections. Return 200 and traffic starts flowing. You can pass a payload here too, which is the easiest place to inject tenant identity.</li><li><strong>/resume</strong> — fires when a suspended microVM wakes up. Refresh credentials, reconnect, check state.</li><li><strong>/suspend</strong> and <strong>/terminate</strong> — flush logs, save in-flight work, tell other systems you&#39;re going away.</li></ul><h4>Networking: one VM, one URL, one token</h4><p>A Function URL is in front of many execution environments. The other way around is with MicroVMs: each gets its own endpoint with its ID in the URL. That’s the whole point. You’re going after a specific user on a specific machine.</p><p>Allow only HTTPS traffic. HTTP/2, WebSockets, gRPC and SSE all work. For each request, you create a token with create-microvm-auth-token and put it in the X-aws-proxy-auth header. Tokens are valid for a maximum of 60 min. Unless you specify an X-aws-proxy-port header, requests are directed to port 8080.</p><p>The smart part is the token. You scope it to a port, a range or all ports. Anything outside of that scope gets a 403.</p><p>Say you’re building a prompt-to-code tool: builder runs on 8080, preview on 3000. Only give the user a token for 3000. They see their app and cannot get to the thing that made it.” If you run your own Firecracker fleet, you’ll have to write that proxy yourself. Here it is one API call.</p><p>You attach a Network Connector for private access to a VPC. You create it first (you pass it to run-microvm) and you cannot change it while the VM is running.</p><p>One thing that will cost you an afternoon: outbound UDP is blocked by default, so DNS for containers running inside your microVM is broken.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a1E5x3uGBTASLdugUnBvng.png" /></figure><h4>How pricing works</h4><p>Give every user their own environment and usage varies wildly. One session pins four cores. The next sits idle in a text editor.</p><p>Most compute makes you pay for the peak all the time. MicroVMs don’t. You set a baseline in memory, and CPU comes with it at a fixed 2:1 ratio — 2 GB / 1 vCPU by default, up to 8 GB / 4 vCPU. You pay that baseline while the VM runs. When load spikes, it scales up to 4x on its own, to a ceiling of 32 GB / 16 vCPU, and you only pay extra for the seconds you’re actually above baseline.</p><p>Suspend the VM and compute charges stop. You still pay to store the snapshot, plus a per-GB fee each time it’s written and read.</p><p>That last fee is the one people miss. Billing is per second across compute, snapshot operations and data transfer — closer to Fargate than to Lambda functions. A VM that suspends and resumes every few minutes racks those charges up fast. Set idle timeouts in minutes, not seconds, and do the math before assuming idle time is free.</p><p>It wins on long sessions with low average usage and occasional spikes. It loses on anything heavy and constant. If you’re pinning CPU for hours, buy an EC2 instance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EBKpLCKVeLUMkRJ1HjJ9TQ.png" /></figure><h4>When not to use it</h4><p>Keep using functions for event-driven, request-response work. Functions give you event source mappings, managed concurrency and warm environment reuse. MicroVMs give you none of that — you build the control plane, map users to instances, and manage the fleet yourself.</p><p>Look at AgentCore Runtime if you want an agent-shaped API rather than a raw VM. It’s built on this same primitive, one level up. The two also work together: an agent running in AgentCore can hand generated code to a microVM, so it executes somewhere the agent’s own credentials aren’t sitting in memory.</p><p>The real fit is narrower than the launch buzz suggests. You’re building a multi-tenant platform. Each user or job needs its own environment. The code is untrusted. Environments come and go constantly. Startup time is something users feel. If you don’t need the isolation, this is an expensive answer to a question you don’t have.</p><h4>What it signals</h4><p>Lambda started by hiding the VM behind a handler. MicroVMs hand it back — no hosts to patch, no capacity to plan, but the lifecycle and the state are yours.</p><p>That tracks where compute is heading. When a model writes the code, “who wrote this and what will it do” stops being a question you can answer, and isolation stops being optional. Disposable, isolated Linux boxes on demand is a useful thing to have around.</p><p>Just make sure isolation is what you’re actually paying for.</p><p>Docs: <a href="https://docs.aws.amazon.com/lambda/latest/dg/lambda-microvms-guide.html">Lambda MicroVMs Developer Guide</a> · <a href="https://aws.amazon.com/blogs/aws/run-isolated-sandboxes-with-full-lifecycle-control-aws-lambda-introduces-microvms/">Launch announcement</a> · <a href="https://aws.amazon.com/lambda/pricing/">Pricing</a></p><p>Happy Coding :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=04acc88534ad" width="1" height="1" alt=""><hr><p><a href="https://aws.plainenglish.io/aws-lambda-microvms-the-serverless-primitive-for-code-you-didnt-write-04acc88534ad">AWS Lambda MicroVMs: The Serverless Primitive for Code You Didn’t Write</a> was originally published in <a href="https://aws.plainenglish.io/">AWS in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Amazon Managed Streaming for Kafka]]></title>
            <link>https://aws.plainenglish.io/amazon-managed-streaming-for-kafka-07c638aa807e?source=rss-9be9624c6d5a------2</link>
            <guid isPermaLink="false">https://medium.com/p/07c638aa807e</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[aws-msk]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[apache-kafka]]></category>
            <dc:creator><![CDATA[Chanuka Dinuwan]]></dc:creator>
            <pubDate>Fri, 10 Jul 2026 14:18:57 GMT</pubDate>
            <atom:updated>2026-07-13T06:35:47.352Z</atom:updated>
            <content:encoded><![CDATA[<p>Amazon MSK</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*sCmD196dERQSfDZw.png" /></figure><h3>Introduction to Apache Kafka</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*60OxUNU5ey38q41EIcGW8g.png" /></figure><h4>Challenges with Self-Managed Clusters</h4><ol><li><strong>Setup and Configuration</strong> (Complex Initial Setup, Hardware and Network Design, Cluster Configuration)</li><li><strong>Operational Overhead </strong>(Monitoring, Scaling, Maintenance)</li><li><strong>Security </strong>(Authentication and Authorisation, Encryption)</li></ol><h4>Fully managed Kafka Services</h4><ol><li>Confluent Cloud</li><li>AWS MSK</li><li>Azure Event Hub</li></ol><h3>What is AWS MSK?</h3><ol><li><strong>Fully Managed Kafka</strong> (cluster provisioning, configuration, scaling, and maintenance)</li><li><strong>Highly Available and Scalable</strong> (brokers are provisioned across multiple Availability Zones, ensuring fault tolerance and scalability)</li><li>Integrated Security (built-in encryption, AWS IAM for access control, and VPC integration for secure network configurations)</li><li>Cost-Effective (Pay-as-you-go pricing with no upfront costs)</li></ol><h4>How to use AWS MSK?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IDe3MYtPPFtTQgcnhqUqCA.png" /></figure><h4><strong>Two Types of Clusters</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xyDnSkc_JMeIj_2aNIvfvQ.png" /></figure><h4>Architecture</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0kq8ZtHX98J9-zmUJoAz9Q.png" /></figure><h3>Steps</h3><p>Here is the end-to-end flow we will follow in the hands-on:</p><ol><li>Create an <strong>MSK provisioned cluster</strong>.</li><li>Create an <strong>EC2 instance</strong> in the same VPC (this will host our producer and consumer).</li><li>Install <strong>Java and Kafka</strong> on the EC2 instance.</li><li>Configure <strong>security</strong> — an IAM role for the EC2 instance and a security group rule so the instance can reach the cluster.</li><li>Create a <strong>Kafka topic</strong> on the cluster.</li><li><strong>Produce and consume</strong> messages to verify everything works.</li></ol><blockquote><em>A key point to keep clear throughout: the EC2 instance is </em><strong><em>not</em></strong><em> the Kafka cluster. The cluster lives inside AWS MSK and is fully managed. The EC2 instance is only there to run the Kafka CLI tools that act as our producer and consumer.</em></blockquote><h4>Create MSK Provisioned Cluster</h4><p>Open the <strong>AWS Console → MSK → Create cluster</strong>.</p><ul><li><strong>Creation method:</strong> Quick create (fine for a demo).</li><li><strong>Cluster name:</strong> demo-cluster-1</li><li><strong>Cluster type:</strong> Provisioned</li><li><strong>Apache Kafka version:</strong> 3.6.0 (note down whichever version you pick — you will need to match it when downloading Kafka on EC2)</li><li><strong>Broker type:</strong> Standard, size kafka.t3.small (small and cheap, good enough for a demo)</li><li><strong>Storage:</strong> 1 GB per broker</li></ul><p>Leave the rest at their defaults and click <strong>Create cluster</strong>. Provisioning takes roughly <strong>10–15 minutes</strong>, so kick this off first and set up the EC2 instance while it spins up.</p><h4>Create the EC2 Instance</h4><p>While the cluster is being created, launch an EC2 instance that will act as the client machine.</p><ul><li><strong>Name:</strong> kafka-producer-instance</li><li><strong>AMI:</strong> Amazon Linux</li><li><strong>Instance type:</strong> t2.micro</li><li><strong>Key pair:</strong> Create a new one (e.g. kafka-instance) and download the .pem file.</li><li><strong>VPC:</strong> The <strong>same VPC</strong> as your MSK cluster (this is important — the client must be able to reach the brokers on the private network).</li></ul><p>Launch the instance and wait for it to reach the running state.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h-j_lAODjIn0gVtrN3wGjA.png" /></figure><p>Configure security — IAM Role, Security Group</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-ZpVkdZFwkxVXtlUOHEo2w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KEQT4mS4_8QgdOCWfgDt8A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iNpN-jbp2VWOd4Z1jgmVtQ.png" /></figure><h4>Install Java and Kafka on EC2</h4><p>SSH into the instance. First fix the key permissions, then connect:</p><pre>chmod 400 kafka-instance.pem<br>ssh -i &quot;kafka-instance.pem&quot; ec2-user@&lt;EC2_PUBLIC_DNS&gt;</pre><p>Install Java (Kafka needs a JRE/JDK):</p><pre>sudo yum install -y java-11-amazon-corretto</pre><p>Download and extract Kafka. <strong>Match the version to your MSK cluster’s Kafka version</strong> (3.6.0 in this example):</p><pre>wget https://archive.apache.org/dist/kafka/3.6.0/kafka_2.13-3.6.0.tgz<br>tar -xzf kafka_2.13-3.6.0.tgz<br>cd kafka_2.13-3.6.0</pre><p>Now install the <strong>AWS MSK IAM authentication JAR</strong> into the libs folder. This JAR is what lets the Kafka client authenticate to the cluster using IAM instead of managing usernames/passwords or certificates:</p><pre>cd libs<br>wget https://github.com/aws/aws-msk-iam-auth/releases/download/v2.2.0/aws-msk-iam-auth-2.2.0-all.jar<br>cd ..</pre><p>Finally, create a client.properties file (inside the bin folder is convenient) that tells the Kafka client how to authenticate. With IAM auth the client uses SASL_SSL and the AWS_MSK_IAM mechanism:</p><pre>cd bin<br>vi client.properties</pre><pre>security.protocol=SASL_SSL<br>sasl.mechanism=AWS_MSK_IAM<br>sasl.jaas.config=software.amazon.msk.auth.iam.IAMLoginModule required;<br>sasl.client.callback.handler.class=software.amazon.msk.auth.iam.IAMClientCallbackHandler</pre><p>Every Kafka command we run will point at this file so it knows to authenticate via IAM.</p><h4>Security Group</h4><p>By default the MSK cluster’s security group won’t accept traffic from your EC2 instance. Open the <strong>cluster’s security group</strong> and add an inbound rule that allows traffic from the <strong>EC2 instance’s security group</strong>:</p><ul><li><strong>Type:</strong> All traffic (fine for a demo; tighten to the Kafka broker ports in production)</li><li><strong>Source:</strong> the EC2 instance’s security group</li></ul><p>Save the rule. The instance can now reach the brokers.</p><h4>Create a Kafka Topic</h4><p>You need the <strong>bootstrap server</strong> connection string, which you get from the MSK console under your cluster’s <strong>View client information</strong> → the <strong>IAM</strong> bootstrap server endpoint. Copy one of the endpoints.</p><p>From the Kafka installation directory on EC2, create a topic (pass the full path to client.properties to avoid &quot;file not found&quot; issues):</p><pre>bin/kafka-topics.sh --create \<br>  --bootstrap-server &lt;BOOTSTRAP_SERVER_IAM_STRING&gt; \<br>  --command-config /home/ec2-user/kafka_2.13-3.6.0/bin/client.properties \<br>  --replication-factor 2 \<br>  --partitions 1 \<br>  --topic msk-tutorial-topic</pre><p>You should see Created topic msk-tutorial-topic.</p><h4>Produce and Consume Messages</h4><p>Open <strong>two SSH sessions</strong> into the same EC2 instance — one for the producer, one for the consumer.</p><p><strong>Session 1 — Producer</strong> (a simple console producer; in the real world this is your application publishing events):</p><pre>bin/kafka-console-producer.sh \<br>  --broker-list &lt;BOOTSTRAP_SERVER_IAM_STRING&gt; \<br>  --producer.config /home/ec2-user/kafka_2.13-3.6.0/bin/client.properties \<br>  --topic msk-tutorial-topic</pre><p><strong>Session 2 — Consumer</strong> (reads from the beginning so you see all messages):</p><pre>bin/kafka-console-consumer.sh \<br>  --bootstrap-server &lt;BOOTSTRAP_SERVER_IAM_STRING&gt; \<br>  --consumer.config /home/ec2-user/kafka_2.13-3.6.0/bin/client.properties \<br>  --topic msk-tutorial-topic \<br>  --from-beginning</pre><p>Now type messages into the producer session (hi, hello, how are you) and watch them appear in real time in the consumer session. That&#39;s the core of Kafka: as soon as a message is written to the topic by the producer, any consumer subscribed to that topic can read it — producers and consumers are fully decoupled.</p><p>In a real deployment, the console producer is replaced by your application code that publishes events, and the console consumer is replaced by one or more downstream services that react to them.</p><h3>Before you go</h3><ul><li>Please take a moment to like the post and follow the writer!</li><li>Did you know that over 400,000 developers share what they’re building, learning, and discovering across our platforms every month? Learn how you can contribute <a href="https://plainenglish.io/write-for-us">here</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=07c638aa807e" width="1" height="1" alt=""><hr><p><a href="https://aws.plainenglish.io/amazon-managed-streaming-for-kafka-07c638aa807e">Amazon Managed Streaming for Kafka</a> was originally published in <a href="https://aws.plainenglish.io/">AWS in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JPA Secret: Auto Save & Update with @PrePersist and @PreUpdate]]></title>
            <link>https://blog.stackademic.com/jpa-secret-auto-save-update-with-prepersist-and-preupdate-3bddc1be8ee1?source=rss-9be9624c6d5a------2</link>
            <guid isPermaLink="false">https://medium.com/p/3bddc1be8ee1</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[spring]]></category>
            <dc:creator><![CDATA[Chanuka Dinuwan]]></dc:creator>
            <pubDate>Fri, 17 Apr 2026 05:41:59 GMT</pubDate>
            <atom:updated>2026-04-21T12:03:29.787Z</atom:updated>
            <content:encoded><![CDATA[<h3>The underrated annotations that quietly clean up half of your service layer</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*O-NahwLPgD95Qw11.png" /></figure><p>Most Spring Boot service classes I’ve reviewed have the same problem: the first 5–10 lines of every create and update method are just “getting the object ready” before the save. Generating IDs. Setting timestamps. Defaulting status. Deriving fields.</p><p>@PrePersist and @PreUpdate exist to kill that boilerplate. Yet most developers never reach for them.</p><h3>The problem</h3><p>Here’s the pattern you’ve almost certainly written:</p><pre>public Product createProduct(Product product) {<br>    product.setSku(&quot;SKU-&quot; + UUID.randomUUID().toString().substring(0, 8));<br>    product.setCreatedAt(LocalDateTime.now());<br>    product.setUpdatedAt(LocalDateTime.now());<br>    product.setStatus(ProductStatus.ACTIVE);<br>    product.setSearchKeywords(<br>        (product.getName() + &quot; &quot; + product.getCategory()).toLowerCase()<br>    );<br>    return productRepository.save(product);<br>}<br><br>public Product updateProduct(Long id, Product updated) {<br>    Product existing = productRepository.findById(id).orElseThrow();<br>    existing.setName(updated.getName());<br>    existing.setCategory(updated.getCategory());<br>    existing.setUpdatedAt(LocalDateTime.now());<br>    existing.setSearchKeywords(<br>        (updated.getName() + &quot; &quot; + updated.getCategory()).toLowerCase()<br>    );<br>    return productRepository.save(existing);<br>}</pre><p>Notice the duplication. The keyword logic lives in two places. Change it once, you have to remember to change it twice. Nobody remembers. Multiply across 15 entities and you’ve got yourself a maintenance problem.</p><h3>The fix</h3><p>JPA gives you two lifecycle callbacks. @PrePersist runs automatically before an INSERT. @PreUpdate runs before an UPDATE. Put the derivation logic on the entity, and the service gets out of the way.</p><pre>@Entity<br>@Table(name = &quot;products&quot;)<br>@Data<br>public class Product {<br>    @Id<br>    @GeneratedValue(strategy = GenerationType.IDENTITY)<br>    private Long id;<br>    private String name;<br>    private String category;<br>    private String sku;<br>    private String searchKeywords;<br>    <br>    @Enumerated(EnumType.STRING)<br>    private ProductStatus status;<br>    <br>    private LocalDateTime createdAt;<br>    private LocalDateTime updatedAt;<br>    @PrePersist<br>    public void beforeInsert() {<br>        LocalDateTime now = LocalDateTime.now();<br>        this.createdAt = now;<br>        this.updatedAt = now;<br>        this.sku = &quot;SKU-&quot; + UUID.randomUUID().toString().substring(0, 8);<br>        <br>        if (this.status == null) {<br>            this.status = ProductStatus.ACTIVE;<br>        }<br>        <br>        this.searchKeywords = buildKeywords();<br>    }<br>    @PreUpdate<br>    public void beforeUpdate() {<br>        this.updatedAt = LocalDateTime.now();<br>        this.searchKeywords = buildKeywords();<br>    }<br>    private String buildKeywords() {<br>        return (name + &quot; &quot; + category).toLowerCase();<br>    }<br>}</pre><p>Now the service shrinks to this:</p><pre>public Product createProduct(Product product) {<br>    return productRepository.save(product);<br>}<br><br>public Product updateProduct(Long id, Product updated) {<br>    Product existing = productRepository.findById(id).orElseThrow();<br>    existing.setName(updated.getName());<br>    existing.setCategory(updated.getCategory());<br>    return productRepository.save(existing);<br>}</pre><p>One place for the keyword logic. One place for the timestamps. The service does what a service should orchestrate, not derive.</p><h3>The rules</h3><p>Callback methods must be void and take no arguments. That&#39;s the whole contract.</p><h3>Four things that will bite you</h3><p><strong>1. </strong><strong>@PreUpdate only fires on actual updates.</strong> If you call save() without changing any managed field, Hibernate skips the UPDATE, and your callback never runs. Don&#39;t rely on it as a heartbeat.</p><p><strong>2. JPQL bulk updates bypass callbacks completely.</strong> A query like UPDATE Product p SET p.price = p.price * 1.1 goes straight to the database. No @PreUpdate, no timestamp refresh, no version bump. If you need the callback, you need an entity-level update.</p><p><strong>3. Don’t call other repositories inside callbacks.</strong> Triggering queries or touching lazy collections while Hibernate is mid-flush is how you get mysterious exceptions at 2 AM. Keep callback logic self-contained to the entity’s own fields.</p><p><strong>4. For audit fields specifically, use Spring Data’s auditing.</strong> @CreatedDate, @LastModifiedDate, @CreatedBy, and @LastModifiedBy with @EnableJpaAuditing is cleaner for that one use case and integrates with Spring Security. Save @PrePersist for actual domain derivation, slugs, counts, defaults, computed fields.</p><h3>When not to use them</h3><p>If your “derivation” needs external data, an API call, the security context, another entity, a Kafka publish, that&#39;s service work. Lifecycle callbacks are for logic the entity can compute from its own fields alone.</p><p>Gut check: if the method would be trivial to unit-test without mocks, it belongs on the entity. If you’d need to mock three things, it doesn’t.</p><h3>Wrapping up</h3><p>These aren’t glamorous annotations. They won’t show up in a system design interview. But the first time you delete ten lines of setup boilerplate from a service class and watch the tests still pass, you’ll wonder why you didn’t start using them years ago.</p><p>Pick your messiest service method this week. Move the derivation logic to the entity. See how it feels.</p><p>You can find the GitHub project here: <a href="https://github.com/cdin8619-0/jpa-secret.git">https://github.com/cdin8619-0/jpa-secret.git</a></p><p>Happy Coding! 🚀</p><p><em>If this was useful, a clap helps more people find it. I write about Java, Spring Boot, and the everyday engineering things that don’t make it into official docs.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3bddc1be8ee1" width="1" height="1" alt=""><hr><p><a href="https://blog.stackademic.com/jpa-secret-auto-save-update-with-prepersist-and-preupdate-3bddc1be8ee1">JPA Secret: Auto Save &amp; Update with @PrePersist and @PreUpdate</a> was originally published in <a href="https://blog.stackademic.com/">Stackademic</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[AWS Solutions Architect Guide: RDS, Route 53, S3, and Content Delivery]]></title>
            <link>https://aws.plainenglish.io/aws-solutions-architect-guide-rds-route-53-s3-and-content-delivery-2b0efb67d13f?source=rss-9be9624c6d5a------2</link>
            <guid isPermaLink="false">https://medium.com/p/2b0efb67d13f</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[aws-certification]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[aws-s3]]></category>
            <category><![CDATA[aws-rds]]></category>
            <dc:creator><![CDATA[Chanuka Dinuwan]]></dc:creator>
            <pubDate>Sun, 15 Feb 2026 07:04:12 GMT</pubDate>
            <atom:updated>2026-02-15T07:04:12.587Z</atom:updated>
            <content:encoded><![CDATA[<p>A comprehensive guide to essential AWS services for the Solutions Architect certification and real-world applications.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CKabM3OPiPSNc37oz40_jA.jpeg" /></figure><h3>Understanding Amazon RDS: Managed Database Solutions</h3><p>Amazon’s Relational Database Service (RDS) provides a managed solution for SQL databases, supporting popular engines like MySQL and PostgreSQL. Unlike EC2-based databases, you cannot SSH into RDS instances; AWS handles the infrastructure management for you.</p><h3>Scaling with RDS Auto Scaling</h3><p>RDS can dynamically increase storage capacity when needed. Auto Scaling triggers when free storage drops below 10%, remains low for 5 minutes, and at least 6 hours have passed since the last modification. You simply set a maximum storage threshold, and RDS handles the rest.</p><h3>Read Replicas: Boosting Read Performance</h3><p>Read Replicas allow you to scale read operations by creating copies of your database. These replicas can only process SELECT queries, making them ideal for analytics workloads. A common use case: running reporting queries on a replica while your production database handles regular traffic without performance degradation.</p><p><strong>Network Cost Consideration:</strong> Keep replicas in the same region to avoid cross-AZ data transfer charges.</p><h3>Multi-AZ Deployments: High Availability Through Disaster Recovery</h3><p>Multi-AZ uses synchronous replication to maintain a standby instance in a different availability zone. This setup increases availability but doesn’t provide scaling benefits; it’s purely for disaster recovery. The transition from single-AZ to multi-AZ is a zero-downtime operation.</p><h3>Encryption and Security</h3><p>RDS encryption must be enabled at creation time. For existing unencrypted databases, create a snapshot, encrypt the snapshot, then restore from it to get an encrypted instance. RDS supports IAM authentication, security groups, and audit logs that can be sent to CloudWatch.</p><h3>RDS Custom: When You Need More Control</h3><p>For Oracle and Microsoft SQL Server, RDS Custom allows you to customize configuration settings, install patches, and enable native features. Simply deactivate automation mode when you need to perform customizations.</p><h3>Backup Strategies</h3><p><strong>Automated Backups:</strong> Daily full backups with transaction logs backed up every 5 minutes. Retention ranges from 1 to 35 days.</p><p><strong>Manual Snapshots:</strong> For long-term storage needs, especially when planning to stop your RDS instance for extended periods.</p><h3>RDS Proxy: Connection Pooling for Better Performance</h3><p>RDS Proxy allows applications to pool and share database connections, reducing connection overhead. It provides 66% faster failover times for Multi-AZ deployments and uses IAM authentication with Secrets Manager for credential management.</p><h3>Amazon Aurora: Cloud-Optimized Performance</h3><p>Aurora is AWS’s proprietary database engine, compatible with PostgreSQL and MySQL but optimized for cloud performance, delivering 5x MySQL and 3x PostgreSQL performance improvements.</p><h3>Key Aurora Features</h3><ul><li><strong>Automatic storage scaling:</strong> Starts at 10GB and grows automatically</li><li><strong>High availability:</strong> Maintains 6 copies of your data across 3 availability zones</li><li><strong>Self-healing:</strong> Peer-to-peer replication repairs corrupted data automatically</li><li><strong>Backtrack:</strong> Restore data to any point in time without using backups</li></ul><h3>Aurora Architecture</h3><p>Aurora uses separate endpoints for reads and writes. The Writer Endpoint handles all write operations, while the Reader Endpoint load-balances across read replicas. Custom Endpoints let you define subsets of instances for specific workloads.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dMiUoKlcif_QazFsYwI5PA.png" /></figure><h3>Aurora Serverless: Pay-Per-Use Databases</h3><p>Aurora Serverless automatically scales based on actual usage, eliminating capacity planning. It’s perfect for intermittent or unpredictable workloads.</p><h3>Aurora Global Databases</h3><p>Span up to 6 regions (1 primary + 5 secondary) with cross-region replication typically under 1 second. This enables disaster recovery and low-latency global reads.</p><h3>Aurora ML Integration</h3><p>Add machine learning predictions directly to your database using Amazon SageMaker or Amazon Comprehend without moving data.</p><h3>Amazon ElastiCache: In-Memory Data Store</h3><p>ElastiCache provides managed Redis or Memcached for caching frequently accessed data. While implementation requires code changes, the performance benefits are substantial.</p><h3>Common Use Cases</h3><p><strong>Session Storage:</strong> Make applications stateless by storing user sessions in ElastiCache</p><p><strong>Cache Strategy:</strong> Implement cache-aside pattern, query ElastiCache first (cache hit), fall back to RDS on cache miss, then populate the cache</p><h3>Redis vs Memcached</h3><p><strong>Redis:</strong> Multi-AZ, data persistence, backup/restore, advanced features <strong>Memcached:</strong> Simple caching only, no persistence or high availability</p><h3>Security Options</h3><ul><li>Redis: IAM authentication and Redis AUTH</li><li>Memcached: SASL-based authentication</li></ul><h3>Route 53: AWS DNS Service</h3><p>Route 53 is AWS’s fully managed, authoritative DNS service that also functions as a domain registrar with built-in health checking.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kMPHHYFH61TKjIuI4pt35A.png" /></figure><h3>DNS Records and TTL</h3><p>Records contain the routing policy, record type (A, AAAA, CNAME, NS), value, and Time To Live (TTL). High TTL (24 hours) reduces DNS queries but slows propagation; low TTL (60 seconds) enables faster updates but increases query costs.</p><h3>CNAME vs Alias Records</h3><p><strong>CNAME:</strong> Points hostname to another hostname (cannot be used for apex/root domain) <strong>Alias:</strong> AWS-specific, points hostname to AWS resources, works with apex domains, automatically managed TTL</p><h3>Routing Policies for Different Scenarios</h3><p><strong>Simple Routing:</strong> Single resource, no health checks</p><p><strong>Weighted Routing:</strong> Distribute traffic by percentage across resources (ideal for A/B testing or gradual migrations)</p><p><strong>Failover Routing:</strong> Primary-secondary setup with automatic failover on health check failure</p><p><strong>Latency-Based Routing:</strong> Routes users to the region with lowest latency</p><p><strong>Geolocation Routing:</strong> Routes based on user’s geographic location</p><p><strong>Geoproximity Routing:</strong> Routes based on geographic location of users AND resources, allows bias adjustment</p><p><strong>IP-Based Routing:</strong> Routes traffic based on client IP addresses using CIDR blocks</p><p><strong>Multi-Value Routing:</strong> Returns multiple IP addresses with health checks</p><h3>Health Checks</h3><p>Available only for public resources. Three types:</p><ul><li>Monitor specific endpoints</li><li>Calculated health checks (combine checks using AND/OR/NOT)</li><li>Private hosted zones (use CloudWatch metrics and alarms)</li></ul><h3>Amazon S3: Object Storage at Scale</h3><p>S3 stores objects (files) in buckets with globally unique names. The key is the full path including the filename.</p><h3>Security Layers</h3><p><strong>User-Based:</strong> IAM policies control access <strong>Resource-Based:</strong> Bucket policies, Access Control Lists (ACLs)</p><p>Bucket policies specify the resource, effect (allow/deny), actions, and principals (accounts/users).</p><h3>Versioning and Replication</h3><p>Enable versioning at the bucket level to maintain object history.</p><p><strong>Cross-Region Replication (CRR):</strong> Compliance, lower latency access, cross-account replication <strong>Same-Region Replication (SRR):</strong> Log aggregation, live replication between accounts</p><p>Use S3 Batch Replication for existing objects.</p><h3>Storage Classes for Cost Optimization</h3><p><strong>S3 Standard:</strong> Frequently accessed data, low latency, high throughput</p><p><strong>S3 Infrequent Access (IA):</strong> Lower cost for data accessed less frequently but needs rapid retrieval</p><p><strong>S3 Glacier Tiers:</strong></p><ul><li>Instant Retrieval: Millisecond access</li><li>Flexible Retrieval: Minutes to hours</li><li>Deep Archive: Lowest cost, 12-hour retrieval</li></ul><p><strong>S3 Intelligent-Tiering:</strong> Automatically moves objects between tiers based on access patterns</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H0uwtCvjwvsqmJf1ChoHdA.png" /></figure><h3>Lifecycle Rules</h3><p>Automate object transitions and expirations using lifecycle policies. S3 Analytics helps determine optimal transition timing.</p><h3>Performance Optimization</h3><p><strong>Multi-Part Upload:</strong> Required for files over 5GB, recommended for files over 100MB</p><p><strong>S3 Transfer Acceleration:</strong> Upload to edge locations for faster transfers</p><p><strong>Byte-Range Fetches:</strong> Parallelize downloads, resilient to failures</p><h3>Advanced Features</h3><p><strong>S3 Select:</strong> Query data using SQL without retrieving entire objects</p><p><strong>S3 Batch Operations:</strong> Perform bulk operations on existing objects</p><p><strong>S3 Event Notifications:</strong> Trigger actions on S3 events (example: create thumbnails on image upload)</p><h3>Encryption Options</h3><p><strong>Server-Side Encryption:</strong></p><ul><li>SSE-S3: AWS-managed keys (AES-256)</li><li>SSE-KMS: Customer-managed keys with audit trails and automatic rotation</li><li>SSE-C: Customer-provided keys</li></ul><p><strong>Client-Side Encryption:</strong> Encrypt before uploading</p><p><strong>Encryption in Transit:</strong> Use HTTPS endpoints, enforce with bucket policy (aws:SecureTransport: true)</p><h3>Access Control Features</h3><p><strong>Pre-Signed URLs:</strong> Provide temporary access to private objects with inherited permissions</p><p><strong>S3 Access Points:</strong> Simplify access management with dedicated DNS names and policies per application</p><p><strong>S3 Object Lambda:</strong> Transform objects on retrieval using Lambda functions</p><p><strong>VPC Access Points:</strong> Restrict access to specific VPCs</p><h3>Compliance and Security</h3><p><strong>MFA Delete:</strong> Requires MFA for object deletion (root account only setup)</p><p><strong>S3 Access Logs:</strong> Log all bucket access to a separate logging bucket</p><p><strong>S3 Glacier Vault Lock:</strong> Write Once Read Many (WORM) model</p><p><strong>S3 Object Lock:</strong> Prevent version deletion for specific retention periods</p><ul><li>Compliance Mode: Cannot be overridden</li><li>Governance Mode: Special permissions can override</li><li>Legal Hold: Indefinite retention</li></ul><h3>CloudFront: Global Content Delivery</h3><p>CloudFront is AWS’s Content Delivery Network (CDN), caching content at edge locations worldwide for improved read performance and DDoS protection.</p><h3>Origins and Use Cases</h3><p><strong>Supported Origins:</strong> S3 buckets, custom origins (ALB, EC2, any HTTP server)</p><p><strong>Best For:</strong> Static content distribution</p><p><strong>Field-Level Encryption:</strong> Secure sensitive data upload to web servers</p><h3>Geographic Restrictions</h3><p>Use geo-restriction to create allow lists or block lists for specific countries.</p><h3>Pricing Optimization</h3><p><strong>Price Classes:</strong></p><ul><li>All: All edge locations (highest cost, best performance)</li><li>200: Excludes most expensive regions</li><li>100: Only least expensive regions</li></ul><h3>Cache Management</h3><p>Invalidate specific paths or entire cache before TTL expiration.</p><p><strong>Origin Access Control (OAC):</strong> Serve S3 content through CloudFront without exposing S3 URLs directly.</p><h3>AWS Global Accelerator: Network Performance</h3><p>Unlike CloudFront (which caches content), Global Accelerator routes traffic through AWS’s global network using Anycast IP addresses.</p><h3>How It Works</h3><ul><li><strong>Unicast IP:</strong> One server, one IP address</li><li><strong>Anycast IP:</strong> Multiple servers share one IP, client routes to nearest</li></ul><h3>Use Cases</h3><p><strong>Supported Endpoints:</strong> Elastic IP, Network Load Balancer, Application Load Balancer</p><p><strong>Best For:</strong> Non-HTTP protocols (UDP, TCP), gaming, IoT, applications requiring static IPs</p><p><strong>Key Difference from CloudFront:</strong> No caching, focuses on network optimization and global routing</p><h3>Elastic Beanstalk: Simplified Application Deployment</h3><p>Elastic Beanstalk provides a developer-centric deployment platform that manages infrastructure automatically.</p><h3>Components</h3><ul><li><strong>Application:</strong> Overall service</li><li><strong>Environment:</strong> Single deployment (dev, staging, prod)</li><li><strong>Application Version:</strong> Code iterations</li></ul><h3>Deployment Modes</h3><p><strong>Single Instance:</strong> Development environments <strong>High Availability with Load Balancer:</strong> Production environments</p><h3>Architecture Best Practices</h3><h3>Stateful Web Applications</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nADJ9m8D9GVMRZ-K9ppR6g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FsDD3CsdhHwxnGmuBtCOKg.png" /></figure><h3>Stateless Web Applications</h3><p>Store session data in ElastiCache with session IDs in browser cookies. Use load balancer stickiness to maintain user experience.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wb-1Z1wYb7XmnKMKW4Hs2Q.png" /></figure><h3>Storage for Multi-AZ Applications</h3><p><strong>Use EFS</strong> when applications across multiple availability zones need shared file access.</p><p><strong>Use EBS</strong> only for single-AZ scenarios.</p><h3>Deployment Strategies</h3><p><strong>Golden AMI:</strong> Pre-install software and dependencies for faster launches</p><p><strong>Bootstrapping:</strong> Install dependencies at launch time</p><p><strong>Hybrid Approach:</strong> Golden AMI with bootstrap scripts for environment-specific configuration</p><p><strong>Snapshots:</strong> Use RDS and EBS snapshots for quick environment replication</p><h3>Preparing for the AWS Solutions Architect Exam</h3><p>Understanding these services and their use cases is crucial for the exam. Focus on:</p><ul><li>When to use each service type</li><li>Cost optimization strategies</li><li>Security best practices</li><li>High availability and disaster recovery patterns</li><li>Performance optimization techniques</li></ul><p>Remember: AWS certifications test not just knowledge, but your ability to architect appropriate solutions for different scenarios.</p><p><em>This guide covers core AWS services for the Solutions Architect certification. For the most current information, always refer to official AWS documentation.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2b0efb67d13f" width="1" height="1" alt=""><hr><p><a href="https://aws.plainenglish.io/aws-solutions-architect-guide-rds-route-53-s3-and-content-delivery-2b0efb67d13f">AWS Solutions Architect Guide: RDS, Route 53, S3, and Content Delivery</a> was originally published in <a href="https://aws.plainenglish.io/">AWS in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[OpenTelemetry with Spring Boot 4: A Complete Guide to Achieving Full Observability]]></title>
            <link>https://blog.devgenius.io/opentelemetry-with-spring-boot-4-a-complete-guide-to-achieving-full-observability-f4063e19bde2?source=rss-9be9624c6d5a------2</link>
            <guid isPermaLink="false">https://medium.com/p/f4063e19bde2</guid>
            <category><![CDATA[observability]]></category>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[opentelemetry]]></category>
            <dc:creator><![CDATA[Chanuka Dinuwan]]></dc:creator>
            <pubDate>Wed, 31 Dec 2025 20:56:08 GMT</pubDate>
            <atom:updated>2026-01-07T05:55:53.554Z</atom:updated>
            <content:encoded><![CDATA[<p>OpenTelemetry with Spring Boot 4</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/855/0*NwU8t1A5Jau8FF_v.png" /></figure><p><em>Hope is not a production strategy.</em> This simple yet powerful statement captures why observability has become non-negotiable in modern application development. You can’t just deploy your application and hope everything works; you need real-time insights into what’s happening in production.</p><p>Spring Boot 4 introduces a game-changing feature: native OpenTelemetry support through a dedicated starter. This marks a significant shift in how we approach observability in Spring applications, and in this guide, we’ll explore everything you need to know to implement it effectively.</p><h3>Understanding the Observability Landscape</h3><p>Observability rests on three fundamental pillars: <strong>logs</strong>, <strong>traces</strong>, and <strong>metrics</strong>. Together, these provide a comprehensive view of your application’s health and behavior in production.</p><p>While Spring Boot 3.0 rewrote observability from the ground up and made it a major theme throughout the 3.x releases, there was a catch: you needed to include the full Actuator dependency, even if you only wanted the observability features. Spring Boot 4’s modularization work changed this, paving the way for the standalone OpenTelemetry starter we have today.</p><h3>Why OpenTelemetry Matters</h3><p>OpenTelemetry has emerged as the industry standard for observability. The key insight here is that <strong>the protocol matters, not the library</strong>. Spring Boot uses Micrometer internally but exports everything via OTLP (OpenTelemetry Protocol) to any compatible backend. This vendor-neutral approach gives you the flexibility to choose your observability backend without being locked into a specific implementation.</p><p>The Spring team now offers three ways to use OpenTelemetry:</p><ul><li>The Java agent</li><li>Third-party OpenTelemetry starters</li><li>The official Spring Boot OpenTelemetry starter (our focus today)</li></ul><h3>Getting Started: Project Setup</h3><p>Head over to <a href="https://start.spring.io/">start.spring.io</a> and create a new Spring Boot 4.0.1+ project. The version is <strong>critical 4.0.1 includes essential fixes for log export functionality</strong>.</p><p>Select these dependencies:</p><ul><li><strong>Spring Web</strong> (to build our demo endpoints)</li><li><strong>OpenTelemetry</strong> (the star of our show)</li><li><strong>Docker Compose Support</strong> (automatically sets up our observability stack)</li></ul><p>The Docker Compose support is particularly clever. When it detects OpenTelemetry as a dependency, it automatically configures the LGTM stack from Grafana Labs, an open-source observability suite that includes:</p><ul><li><strong>Loki</strong> for log aggregation</li><li><strong>Grafana</strong> for visualization and dashboards</li><li><strong>Tempo</strong> for distributed tracing</li><li><strong>Mimir</strong> for long-term Prometheus metrics storage</li></ul><p>Your pom.xml will include:</p><pre>&lt;dependency&gt;<br>    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>    &lt;artifactId&gt;spring-boot-starter-opentelemetry&lt;/artifactId&gt;<br>&lt;/dependency&gt;<br>&lt;dependency&gt;<br>    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>    &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;<br>&lt;/dependency&gt;<br>&lt;dependency&gt;<br>    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>    &lt;artifactId&gt;spring-boot-docker-compose&lt;/artifactId&gt;<br>&lt;/dependency&gt;</pre><h3>Configuring OpenTelemetry</h3><p>Create your application.properties with these essential configurations:</p><pre>spring.application.name: Springboot4WithOpenTelemetry<br># Metrics Export<br>spring.otlp.metrics.export.url: http://localhost:4318/v1/metrics<br># OpenTelemetry - Traces Export<br>spring.opentelemetry.tracing.export.otlp.endpoint: http://localhost:4318/v1/traces<br># OpenTelemetry - Logs Export<br>spring.opentelemetry.logging.export.otlp.endpoint: http://localhost:4318/v1/logs<br># Management Tracing<br>management.tracing.sampling.probability: 1.0<br># Server shutdown behaviour<br>server.shutdown: immediate<br># Logging Levels<br>logging.level.dev.danvega: INFO<br>logging.level.org.springframework.boot.actuator.autoconfigure.opentelemetry: DEBUG<br>logging.level.org.springframework.boot.docker.compose: DEBUG</pre><p><strong>Important note about sampling probability</strong>: The default is 0.1 (10% of requests), which is reasonable for production. We’re setting it to 1.0 (100%) for development to see every trace. Unless you have unlimited resources, don’t use 100% sampling in production. it can get expensive quickly.</p><h3>Building Demo Endpoints</h3><p>Let’s create a simple REST controller to generate observable traffic:</p><pre>@RestController<br>public class HomeController {<br>private static final Logger log = LoggerFactory.getLogger(HomeController.class);<br>    @Observed(name = &quot;home.count&quot;)<br>    @GetMapping(&quot;/&quot;)<br>    public String home() {<br>        log.info(&quot;Home endpoint called&quot;);<br>        return &quot;Hello World!&quot;;<br>    }<br>    @GetMapping(&quot;/greet/{name}&quot;)<br>    public String greet(@PathVariable String name) {<br>        log.info(&quot;Greeting user: {}&quot;, name);<br>        simulateWork();<br>        return &quot;Hello, &quot; + name + &quot;!&quot;;<br>    }<br>    @GetMapping(&quot;/slow&quot;)<br>    public String slow() throws InterruptedException {<br>        log.info(&quot;Starting slow operation&quot;);<br>        Thread.sleep(500);<br>        log.info(&quot;Slow operation completed&quot;);<br>        return &quot;Done!&quot;;<br>    }<br>    private void simulateWork() {<br>        try {<br>            Thread.sleep(50);<br>        } catch (InterruptedException e) {<br>            Thread.currentThread().interrupt();<br>        }<br>    }<br>}</pre><p>These endpoints give us different scenarios to observe: a quick response, a moderate delay, and a deliberately slow operation.</p><h3>Adding Custom Metrics</h3><p>Spring Boot provides numerous built-in metrics out of the box, but you’ll often need custom metrics for business-specific observations. Spring offers two approaches:</p><h3>The Annotation Approach (Recommended)</h3><p>First, add the AOP dependency:</p><pre>&lt;dependency&gt;<br>    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>    &lt;artifactId&gt;spring-boot-starter-aop&lt;/artifactId&gt;<br>&lt;/dependency&gt;</pre><p>Then simply annotate your methods:</p><pre>@Observed(name = &quot;home.count&quot;)<br>@GetMapping(&quot;/&quot;)<br>public String home() {<br>    log.info(&quot;Home endpoint called&quot;);<br>    return &quot;Hello World!&quot;;<br>}</pre><p>This approach keeps your code clean and focused. The annotation handles the observability concerns without cluttering your business logic.</p><h3>The Programmatic Approach</h3><p>If you need more control over your observations, you can inject the ObservationRegistry:</p><pre>private final ObservationRegistry registry;<br>public HomeController(ObservationRegistry registry) {<br>    this.registry = registry;<br>}<br>@GetMapping(&quot;/example&quot;)<br>public String example() {<br>    return Observation.createNotStarted(&quot;counter&quot;, registry)<br>        .observe(() -&gt; {<br>            // Your business logic here<br>            return &quot;Result&quot;;<br>        });<br>}</pre><p>This provides more flexibility for tracking key-value pairs and complex observation scenarios, though it can pollute your code if overused.</p><h3>Viewing Traces and Metrics in Grafana</h3><p>Start your application, and Docker Compose will automatically spin up the Grafana stack. Make some requests to your endpoints:</p><pre>curl http://localhost:8080/<br>curl http://localhost:8080/greet/John<br>curl http://localhost:8080/slow</pre><p>Open Grafana at http://localhost:3000 and explore the <strong>Traces</strong> section. You&#39;ll see a visual representation of each request, including:</p><ul><li>The trace name (GET /, GET /greet/{name}, GET /slow)</li><li>Request duration</li><li>Status codes</li><li>Span attributes (HTTP method, URL, status)</li><li>Service metadata (name, language, SDK version)</li></ul><p>For a request to the /slow endpoint, you can drill down and see the complete request flow. In a real-world scenario with database calls and external service interactions, you&#39;d see each operation as a separate span within the trace, giving you precise insight into where time is spent.</p><p>Switch to the <strong>Metrics</strong> view to see built-in Spring metrics like JVM memory usage, HTTP request statistics, and any custom metrics you’ve added.</p><h3>Understanding Traces and Spans</h3><p>Here’s where the magic happens. Every log in your application automatically includes:</p><ul><li>A <strong>Trace ID</strong>: Identifies the entire request across all services</li><li>A <strong>Span ID</strong>: Identifies the specific operation within that trace</li></ul><p>Consider a multi-step request:</p><ol><li>Request hits the controller (Span A)</li><li>Controller calls an external service (Span B)</li><li>Service queries a database (Span C)</li></ol><p>All three spans share the same trace ID, but each has its unique span ID. This allows you to:</p><ul><li>Jump from a trace to its corresponding logs</li><li>Navigate from logs back to the trace view</li><li>Debug specific operations within complex request flows</li></ul><h3>Enabling Log Export</h3><p>By default, Spring Boot 4 doesn’t export logs via OTLP. Enabling this requires a few additional steps (made possible by the fix in version 4.0.1):</p><h3>Step 1: Add the Logback Appender Dependency</h3><pre>&lt;dependency&gt;<br>    &lt;groupId&gt;io.opentelemetry.instrumentation&lt;/groupId&gt;<br>    &lt;artifactId&gt;opentelemetry-logback-appender-1.0&lt;/artifactId&gt;<br>&lt;/dependency&gt;</pre><h3>Step 2: Create logback-spring.xml</h3><p>Create this file in src/main/resources:</p><pre>&lt;configuration&gt;<br>    &lt;appender name=&quot;OTEL&quot; <br>              class=&quot;io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender&quot;&gt;<br>    &lt;/appender&gt;<br>    <br>    &lt;root level=&quot;INFO&quot;&gt;<br>        &lt;appender-ref ref=&quot;CONSOLE&quot;/&gt;<br>        &lt;appender-ref ref=&quot;OTEL&quot;/&gt;<br>    &lt;/root&gt;<br>&lt;/configuration&gt;</pre><h3>Step 3: Install the OpenTelemetry Appender</h3><p>Create a component to wire everything together:</p><pre>@Component<br>class InstallOpenTelemetryAppender implements InitializingBean {<br>private final OpenTelemetry openTelemetry;<br>    InstallOpenTelemetryAppender(OpenTelemetry openTelemetry) {<br>        this.openTelemetry = openTelemetry;<br>    }<br>    @Override<br>    public void afterPropertiesSet() {<br>        OpenTelemetryAppender.install(this.openTelemetry);<br>    }<br>}</pre><p>This component:</p><ul><li>Gets the auto-configured OpenTelemetry instance from Spring</li><li>Installs it into the Logback appender after the Spring context is ready</li><li>Enables the appender to send logs through the OpenTelemetry SDK to the OTLP endpoint</li></ul><p>Now restart your application and make some requests. In Grafana’s trace view, you’ll see a “Logs for this span” section. Click through to see your application logs correlated directly with their corresponding traces. This connection between logs and traces is incredibly powerful for debugging production issues.</p><h3>When to Use OpenTelemetry vs Actuator</h3><p>The Spring Boot Actuator is still excellent and has its place. Here’s a quick comparison:</p><p><strong>Choose OpenTelemetry Starter when:</strong></p><ul><li>You only need observability (metrics, traces, logs)</li><li>You want vendor-neutral telemetry</li><li>You’re integrating with an existing OpenTelemetry ecosystem</li><li>You prefer a lightweight dependency footprint</li></ul><p><strong>Choose Actuator when:</strong></p><ul><li>You need management endpoints (health checks, info, etc.)</li><li>You want fine-grained control over metrics exposure</li><li>You’re building internal monitoring dashboards</li><li>You need the additional features that Actuator provides beyond observability</li></ul><p>The good news? You can use both together if your use case demands it.</p><h3>Best Practices and Considerations</h3><p><strong>Sampling in Production</strong>: Always use a reasonable sampling rate in production (the default 0.1 is a good starting point). Monitor your observability costs and adjust accordingly.</p><p><strong>Custom Metrics</strong>: Be thoughtful about which custom metrics you add. More isn’t always better. Focus on metrics that provide actionable insights into your business and technical operations.</p><p><strong>Log Correlation</strong>: The automatic inclusion of trace and span IDs in your logs is invaluable. Make sure your log aggregation strategy leverages this correlation.</p><p><strong>Testing</strong>: Spring Boot 4’s modularization means you need appropriate test dependencies. Include spring-boot-starter-opentelemetry-test and spring-boot-starter-web for comprehensive testing.</p><h3>Conclusion</h3><p>Spring Boot 4’s native OpenTelemetry support represents a significant step forward in making observability accessible and straightforward. The combination of automatic instrumentation, vendor neutrality, and seamless integration with the Spring ecosystem means you can achieve comprehensive observability with minimal effort.</p><p>The observability story that began with Spring Boot 3.0’s ground-up rewrite has matured into a production-ready solution that doesn’t require pulling in the entire Actuator dependency. With the OpenTelemetry starter, Docker Compose support, and tools like Grafana, you can go from zero to full observability in minutes.</p><p>Remember: hope is not a production strategy. But with the tools and patterns we’ve explored today, you can deploy your Spring Boot applications with confidence, knowing you have complete visibility into their behavior when it matters most.</p><p>Now it’s time to implement these patterns in your own applications. Your future self — debugging a production issue at 2 AM will thank you for the investment in observability you make today.</p><p><em>Want to dive deeper? Check out the Spring Boot 4 documentation and Moritz Halbritter’s excellent “Road to GA” blog post on OpenTelemetry for more advanced configurations and use cases.</em></p><p>GitHub Repo: <a href="https://github.com/cdin8619-0/Springboot4WithOpenTelemetry.git">https://github.com/cdin8619-0/Springboot4WithOpenTelemetry.git</a></p><p>Happy Coding! 🚀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f4063e19bde2" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/opentelemetry-with-spring-boot-4-a-complete-guide-to-achieving-full-observability-f4063e19bde2">OpenTelemetry with Spring Boot 4: A Complete Guide to Achieving Full Observability</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Automate your entire Kubernetes cluster with Amazon EKS Auto Mode]]></title>
            <link>https://cdinuwan.medium.com/automate-your-entire-kubernetes-cluster-with-amazon-eks-auto-mode-bd8b16a9a067?source=rss-9be9624c6d5a------2</link>
            <guid isPermaLink="false">https://medium.com/p/bd8b16a9a067</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[aws-eks]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <dc:creator><![CDATA[Chanuka Dinuwan]]></dc:creator>
            <pubDate>Mon, 10 Nov 2025 18:59:30 GMT</pubDate>
            <atom:updated>2025-11-13T01:44:01.253Z</atom:updated>
            <content:encoded><![CDATA[<p>A Revolutionary Shift in Kubernetes Management on AWS</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*SgBLDLhygCp1xgV6.jpeg" /></figure><p>Amazon EKS Auto mode. This new capability fundamentally alters how companies manage Kubernetes clusters on AWS by significantly reducing operational complexity and improving security, cost effectiveness, and dependability. EKS Auto mode allows for the automation of the entire Kubernetes cluster infrastructure. Why do we need EKS Auto mode?</p><p>According to surveys conducted by the Cloud Native Computing Foundation in 2023, 84% of organizations are either using Kubernetes in production or piloting it, making it the de facto standard for cloud operations.</p><p>However, there are a lot of obstacles to overcome when utilizing Kubernetes at scale. Customers still had a lot of work to do in managing their cluster data planes, even with Amazon EKS handling the control plane.</p><h4>The Traditional EKS Workflow</h4><p>Prior to Auto Mode, establishing a production-ready EKS cluster involved a number of intricate steps:</p><p><strong>1. Cluster Architecture Choices </strong>— Choosing between large shared clusters or dedicated application-specific clusters<br><strong>2. Configurations for Plugins</strong> — Choosing, setting up, and installing necessary Kubernetes plugins for networking, storage, and compute auto-scaling.<br><strong>3. Compute provisioning —</strong> The process of navigating and properly configuring AWS’s extensive portfolio of EC2 instance types.<br><strong>4. Continuous Operations — </strong>keeping an eye on infrastructure, fixing problems, installing security updates, and carrying out upgrades.<br>5.<strong> Cost optimization</strong> — The process of continuously evaluating and improving infrastructure for effectiveness.</p><p>Engineering teams must spend their valuable time setting these things up because of this operational overhead.</p><h4>EKS Auto mode</h4><p>AWS’s idea of how Kubernetes ought to function in the cloud is embodied in EKS Auto Mode.<br>For any new or existing cluster, Auto Mode offers managed networking, storage, and computation that is Kubernetes-compliant. One step gets you:</p><ul><li>Fully automated infra management</li><li>Dynamic compute scaling</li><li>Integrated capabilities</li><li>Enhanced security</li></ul><h4>Core Capabilities: Compute, Storage, and Networking</h4><p><strong>Compute Management</strong></p><p>Auto Mode introduces EC2 managed, a fundamentally new operating model where AWS maintains full responsibility for the compute layer:</p><ul><li><strong>Powered by Karpenter</strong> — Dynamically selects and scales the best-suited instance types</li><li><strong>Continuous optimisation</strong> — Automatically replaces underutilised or expensive compute with more cost-effective alternatives</li><li><strong>Flexible configuration</strong> — Support for specific instance types, Spot instances, Reserved Instances, and Compute Savings Plans</li><li><strong>Bottlerocket OS</strong> — Container-optimised operating system that improves security and performance while reducing operational overhead</li></ul><p><strong>Storage Integration</strong></p><p>Auto Mode includes a managed EBS CSI driver that enables:</p><ul><li>Automatic provisioning of block storage for stateful applications</li><li>Lifecycle management of EBS volumes</li><li>Seamless integration with Kubernetes persistent volume claims</li><li>Compatibility with other storage solutions through existing EKS add-ons</li></ul><p><strong>Networking Features</strong></p><p>Comprehensive networking capabilities come built-in:</p><ul><li><strong>Pod and cluster networking</strong> — Streamlined VPC CNI with default network policy enforcement</li><li><strong>CoreDNS</strong> — Runs locally on each node, eliminating common scaling challenges</li><li><strong>Managed network proxy</strong> — Maintains network rules on every node</li><li><strong>Load balancing</strong> — AWS Load Balancer Controller pre-configured with best practices, supporting both Application Load Balancers and Network Load Balancers</li></ul><h4>The Demo: From Zero to Production in Minutes</h4><ol><li>Cluster creation</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WG4X8UoSA7OVn-rbCEyUGw.png" /></figure><p>2. Enable EKS auto mode</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tLb4FOJhN6HQHQR7Y_xIVQ.png" /></figure><ul><li><strong>Automatic Infrastructure</strong> — Auto Mode automatically provisioned nodes, storage volumes, and load balancers</li><li><strong>One-Click Upgrades</strong> — Upgraded from Kubernetes 1.30 to 1.31 with zero manual intervention</li><li><strong>Self-Healing</strong> — Demonstrated automatic node health monitoring and replacement</li></ul><h4>Real-World Impact: Customer Stories</h4><h4>Astronomer</h4><p>Managing approximately 300 EKS clusters for their Astro Airflow-as-a-service platform, Astronomer faced significant operational challenges. After adopting Auto Mode:</p><ul><li><strong>50% reduction</strong> in engineering effort required to support clusters</li><li><strong>Lower compute infrastructure costs</strong></li><li><strong>Improved scalability</strong> for onboarding new customers</li></ul><h4>FICO</h4><p>The analytics software giant serving 95% of US financial institutions deployed Auto Mode for non-production and developer environments, achieving:</p><ul><li><strong>Reduced infrastructure spend</strong> for ephemeral environments</li><li><strong>Lower operational time investment</strong></li><li>Plans to extend Auto Mode to production environments</li></ul><h4>The Shared Responsibility Model Redefined</h4><p>Auto Mode significantly shifts the boundary of shared responsibility between AWS and customers. AWS now manages:</p><ul><li>Cluster control plane</li><li>Compute lifecycle management</li><li>Operating system patches</li><li>Health monitoring and repair</li><li>EC2 instance management</li><li>Core cluster components for compute, storage, and networking</li></ul><p>This leaves customers free to focus on:</p><ul><li>Application development and deployment</li><li>Application-level security</li><li>Monitoring and optimization of their specific use cases</li></ul><h4>Kubernetes Conformance and Partner Ecosystem</h4><p>EKS Auto Mode maintains full Kubernetes conformance, ensuring compatibility with:</p><ul><li>All CNCF open-source projects</li><li>Existing EKS partner solutions</li><li>Standard Kubernetes tools and workflows</li></ul><p>Launch partners include major platforms like Datadog, Dynatrace, New Relic, Prisma Cloud, Sysdig, and many others, all validated and ready to use with Auto Mode.</p><h4>Key Benefits: Why Auto Mode Matters</h4><h4>1. Increased Agility</h4><p>Dramatically reduce time-to-market for new products and modernized applications by eliminating infrastructure setup delays.</p><h4>2. Reduced Operational Overhead</h4><p>Focus engineering talent on business-critical activities instead of cluster management and maintenance tasks.</p><h4>3. Lower Total Costs</h4><p>Automatic right-sizing and continuous optimization reduce compute costs while saving team time and resources.</p><h4>4. Security by Default</h4><p>Container-optimized OS, automated patching, and ephemeral compute strengthen security posture without additional effort.</p><h4>5. Improved Reliability</h4><p>Automated health monitoring and self-healing capabilities improve application availability and performance.</p><h4>Getting Started with EKS Auto Mode</h4><p>EKS Auto Mode is available now for new and existing Amazon EKS clusters. Organizations can:</p><ul><li>Enable Auto Mode when creating new clusters through the console, CLI, or infrastructure-as-code tools</li><li>Migrate existing workloads to Auto Mode-enabled clusters</li><li>Use Auto Mode alongside traditional node groups during transition periods</li></ul><h4>The Future of Kubernetes Operations</h4><p>EKS Auto Mode is a fundamental rethinking of how Kubernetes should operate in the cloud, not just a small tweak. AWS has made enterprise-grade container orchestration available to businesses of all sizes by removing the operational complexity that has traditionally prevented Kubernetes adoption.<br>Innovations like EKS Auto Mode guarantee that the platform’s power is always available without requiring teams to become infrastructure experts, which is crucial as Kubernetes maintains its dominance as the cloud operating model. This is Kubernetes in its purest form: a strong tool that frees up team members to create incredible applications.</p><p>Happy Coding!🚀</p><h3>A message from our Founder</h3><p><strong>Hey, </strong><a href="https://linkedin.com/in/sunilsandhu"><strong>Sunil</strong></a><strong> here.</strong> I wanted to take a moment to thank you for reading until the end and for being a part of this community.</p><p>Did you know that our team run these publications as a volunteer effort to over 3.5m monthly readers? <strong>We don’t receive any funding, we do this to support the community. ❤️</strong></p><p>If you want to show some love, please take a moment to <strong>follow me on </strong><a href="https://linkedin.com/in/sunilsandhu"><strong>LinkedIn</strong></a><strong>, </strong><a href="https://tiktok.com/@messyfounder"><strong>TikTok</strong></a>, <a href="https://instagram.com/sunilsandhu"><strong>Instagram</strong></a>. You can also subscribe to our <a href="https://newsletter.plainenglish.io/"><strong>weekly newsletter</strong></a>.</p><p>And before you go, don’t forget to <strong>clap</strong> and <strong>follow</strong> the writer️!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bd8b16a9a067" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unlocking Java Virtual Threads: A Spring Boot Perspective]]></title>
            <link>https://blog.stackademic.com/unlocking-java-virtual-threads-a-spring-boot-perspective-deb895f8d880?source=rss-9be9624c6d5a------2</link>
            <guid isPermaLink="false">https://medium.com/p/deb895f8d880</guid>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[threads]]></category>
            <dc:creator><![CDATA[Chanuka Dinuwan]]></dc:creator>
            <pubDate>Sat, 18 Oct 2025 20:54:35 GMT</pubDate>
            <atom:updated>2025-10-23T06:51:51.271Z</atom:updated>
            <content:encoded><![CDATA[<p>Java Virtual Threads</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XX2dfQBb9-O3nFAs.png" /></figure><h3>Introduction</h3><p>For years, Java developers have faced a fundamental challenge: how to handle thousands of concurrent requests without overwhelming the server. Traditional thread-per-request models reach their limits quickly, as each platform thread consumes 1 to 2 MB of memory. Creating thousands of threads becomes prohibitively expensive.</p><p>Along the way, we’ve tried various solutions such as Thread pools and Reactive programming with Spring WebFlux. Thread pools helped manage resources but introduced complexity and could still bottleneck under heavy load. WebFlux promised massive scalability, but at the cost of a steep learning curve and a completely different programming model that felt alien to many developers.</p><p>Virtual Threads, Java’s game-changing answer through Project Loom. They promise the best of both worlds: write simple, imperative code while achieving the scalability that previously required reactive programming.</p><p>In this article, you will discover:</p><ul><li>What virtual threads are and how they work</li><li>How to integrate them into Spring Boot applications</li><li>Real-world performance comparisons and use cases</li><li>Important gotchas and best practices</li><li>When to use(and when not to use) virtual threads</li></ul><h4>What are Virtual Threads?</h4><p>Unlike Java Threads that are managed by the OS, Virtual Threads are lightweight threads managed entirely by the Java Virtual Machine.</p><h4>Fundamental difference:</h4><p><strong>Platform threads: </strong>1:1 mapping to OS threads. Creating 10,000 platform threads means creating 10,000 OS threads, each consuming significant memory and requiring OS-level levelling.</p><p><strong>Virtual threads:</strong> Millions can exist simultaneously because they’re multiplexed onto a small pool of platform threads (carrier threads). A virtual thread that blocks doesn’t block the underlying platform thread; it simply unmounts, allowing other virtual threads to use that carrier thread.</p><p><strong>Key characteristics:</strong></p><ul><li>Extremely cheap to create (you can spawn millions)</li><li>Designed to block freely — blocking is not expensive</li><li>Maintain the familiar thread-per-request programming model</li><li>Scheduled by the JVM, not the OS</li><li>Share the same Thread API, making migration easier</li></ul><h3>The problem Virtual Thread Solve</h3><p>Traditional Thread-Per-Request Limitations</p><h4>Traditional Thread-Per-Request Limitations</h4><p>In a classic servlet-based Spring Boot application, each incoming HTTP request is handled by a thread from a thread pool. This model has serious limitations:</p><p><strong>Memory overhead</strong>: Each platform thread requires substantial memory (typically 1–2MB for the stack alone). With a thread pool of 200 threads, you’re already using 200–400MB just for thread stacks. Want to handle 10,000 concurrent requests? That would require 10–20GB of memory just for threads.</p><p><strong>Thread pool exhaustion</strong>: When all threads in the pool are busy (often blocked on I/O operations like database queries or HTTP calls), new requests must wait in a queue. Under high load, this leads to increased latency and eventually request timeouts.</p><p><strong>Context switching costs</strong>: While OS threads are being switched constantly, each context switch has a cost. With thousands of threads, this overhead becomes significant.</p><h3>The Reactive Programming Trade-off</h3><p>Spring WebFlux and reactive programming (Reactor, RxJava) solved the scalability problem by using non-blocking I/O and event loops. A small number of threads could handle thousands of concurrent operations.</p><p>However, this came at a cost:</p><p><strong>Steep learning curve</strong>: Reactive programming requires thinking in streams, operators, and functional composition. It’s a fundamentally different mental model.</p><p><strong>Code complexity</strong>: Simple sequential operations become chains of operators:</p><pre> // Imperative<br>User user = userRepository.findById(id);<br>Order order = orderService.createOrder(user);<br>return order;<br><br>// Reactive<br>return userRepository.findById(id)<br>    .flatMap(user -&gt; orderService.createOrder(user));</pre><p><strong>Debugging nightmares</strong>: Stack traces in reactive code are notoriously difficult to follow, with dozens of framework-internal method calls obscuring what went wrong.</p><p><strong>Ecosystem limitations</strong>: Not all libraries support reactive patterns, forcing you to either find reactive alternatives or accept blocking operations that defeat the purpose.</p><h3>The Sweet Spot: Virtual Threads</h3><p>Virtual threads give us the best of both worlds:</p><ul><li>Write simple, imperative, blocking code</li><li>Achieve massive scalability (handle 100,000+ concurrent operations)</li><li>Use familiar debugging and profiling tools</li><li>Leverage existing blocking libraries and JDBC drivers</li><li>No need to rewrite your entire application in reactive style</li></ul><p>The trade-off? You need Java 21 or later. For most modern applications, this is a small price to pay for dramatically improved scalability with simpler code.</p><h3>Virtual Threads in Spring Boot</h3><p>Prerequisites and Setup<br>Before diving in, ensure you have:</p><ol><li><strong>Java 21 or later</strong> installed. You can verify with:</li></ol><pre>java -version</pre><p>2. <strong>Spring Boot 3.2 or later</strong>. Update your pom.xml:</p><pre>&lt;parent&gt;<br>    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>    &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;<br>    &lt;version&gt;3.2.0&lt;/version&gt;<br>&lt;/parent&gt;<br><br>&lt;properties&gt;<br>    &lt;java.version&gt;21&lt;/java.version&gt;<br>&lt;/properties&gt;</pre><p>3. <strong>Compatible web server</strong>. Spring Boot’s embedded Tomcat, Jetty, and Undertow all support virtual threads out of the box.</p><h3>Enabling Virtual Threads</h3><p>Enabling virtual threads in Spring Boot is remarkably simple. Add this single property to your application.properties:</p><pre>spring.threads.virtual.enabled=true</pre><p>Or in application.yml:</p><pre>spring:<br>  threads:<br>    virtual:<br>      enabled: true</pre><p>That’s it! This single line transforms your application.</p><h4><strong>What happens under the hood:</strong></h4><p>When you enable this property, Spring Boot automatically configures several components to use virtual threads:</p><ol><li><strong>Web server thread pools</strong>: Tomcat, Jetty, or Undertow will use virtual threads to handle incoming HTTP requests</li><li><strong>TaskExecutor beans</strong>: Any TaskExecutor or AsyncTaskExecutor beans will be backed by virtual threads</li><li><strong>@Async methods</strong>: Methods annotated with @Async will execute on virtual threads</li><li><strong>@Scheduled tasks</strong>: Scheduled tasks will run on virtual threads</li></ol><p>You don’t need to change a single line of your application code. The same imperative, blocking code that worked before now runs on virtual threads, handling far more concurrent requests.</p><h4>Where Virtual Threads Are Used</h4><p>Once enabled, virtual threads are automatically utilized in these areas:</p><p><strong>1. Web Request Handling</strong></p><p>Every HTTP request to your REST controllers runs on a virtual thread:</p><pre>@RestController<br>public class UserController {<br>    <br>    @GetMapping(&quot;/users/{id}&quot;)<br>    public User getUser(@PathVariable Long id) {<br>        // This runs on a virtual thread<br>        return userService.findById(id); // Blocking DB call is fine!<br>    }<br>}</pre><p><strong>2. @Async Methods</strong></p><p>Asynchronous methods execute on virtual threads:</p><pre>@Service<br>public class EmailService {<br>    <br>    @Async<br>    public void sendEmail(String to, String subject, String body) {<br>        // Runs on a virtual thread<br>        emailClient.send(to, subject, body); // Blocking call is fine!<br>    }<br>}</pre><p><strong>3. @Scheduled Tasks</strong></p><p>Scheduled tasks run on virtual threads:</p><pre>@Component<br>public class DataSyncJob {<br>    <br>    @Scheduled(fixedRate = 60000)<br>    public void syncData() {<br>        // Runs on a virtual thread<br>        externalApi.fetchData(); // Blocking call is fine!<br>    }<br>}</pre><p><strong>4. Custom Executors</strong></p><p>You can also create custom executors backed by virtual threads:</p><pre>@Configuration<br>public class ExecutorConfig {<br>    <br>    @Bean<br>    public ExecutorService virtualThreadExecutor() {<br>        return Executors.newVirtualThreadPerTaskExecutor();<br>    }<br>}</pre><h3>Hands-On Examples</h3><h4>Example 1: REST API with Database Calls</h4><p>Let’s compare a typical REST API endpoint that makes database calls.</p><p><strong>Traditional approach with thread pool:</strong></p><pre>@RestController<br>@RequestMapping(&quot;/api/orders&quot;)<br>public class OrderController {<br>    <br>    @Autowired<br>    private OrderRepository orderRepository;<br>    <br>    @Autowired<br>    private UserRepository userRepository;<br>    <br>    @GetMapping(&quot;/{orderId}&quot;)<br>    public OrderDTO getOrder(@PathVariable Long orderId) {<br>        // Running on a platform thread from the thread pool<br>        // Thread is blocked during DB calls<br>        Order order = orderRepository.findById(orderId)<br>            .orElseThrow(() -&gt; new OrderNotFoundException(orderId));<br>        <br>        // Another blocking DB call<br>        User user = userRepository.findById(order.getUserId())<br>            .orElseThrow(() -&gt; new UserNotFoundException(order.getUserId()));<br>        <br>        return new OrderDTO(order, user);<br>    }<br>}</pre><p><strong>With virtual threads (same code!):</strong></p><p>The exact same code, but now with spring.threads.virtual.enabled=true in your configuration. That&#39;s it. The code doesn&#39;t change at all.</p><p><strong>What’s different:</strong></p><ul><li>Platform threads: With a thread pool of 200 threads, you can handle ~200 concurrent requests. When all threads are blocked on DB calls, new requests wait.</li><li>Virtual threads: You can handle 10,000+ concurrent requests with the same code. Blocking on DB calls is cheap — the virtual thread unmounts, and the carrier thread handles other requests.</li></ul><p><strong>Performance comparison</strong> (simulated load test):</p><ul><li>Platform threads (200 thread pool): ~2,000 requests/second, latency spikes under load</li><li>Virtual threads: ~8,000 requests/second, consistent latency even at 10,000 concurrent requests</li></ul><h4>Example 2: Making Multiple External API Calls</h4><p>A common pattern is fetching data from multiple services. Here’s how virtual threads shine:</p><pre>@Service<br>public class ProductService {<br>    <br>    @Autowired<br>    private RestTemplate restTemplate;<br>    <br>    public ProductDetails getProductDetails(String productId) {<br>        // Create virtual threads for parallel calls<br>        try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {<br>            <br>            // Submit multiple tasks<br>            Future&lt;ProductInfo&gt; productFuture = executor.submit(() -&gt; <br>                restTemplate.getForObject(<br>                    &quot;https://api.inventory.com/products/&quot; + productId, <br>                    ProductInfo.class<br>                )<br>            );<br>            <br>            Future&lt;PriceInfo&gt; priceFuture = executor.submit(() -&gt;<br>                restTemplate.getForObject(<br>                    &quot;https://api.pricing.com/prices/&quot; + productId,<br>                    PriceInfo.class<br>                )<br>            );<br>            <br>            Future&lt;ReviewSummary&gt; reviewFuture = executor.submit(() -&gt;<br>                restTemplate.getForObject(<br>                    &quot;https://api.reviews.com/summary/&quot; + productId,<br>                    ReviewSummary.class<br>                )<br>            );<br>            <br>            // Wait for all results (each call blocks its virtual thread)<br>            ProductInfo product = productFuture.get();<br>            PriceInfo price = priceFuture.get();<br>            ReviewSummary reviews = reviewFuture.get();<br>            <br>            return new ProductDetails(product, price, reviews);<br>            <br>        } catch (InterruptedException | ExecutionException e) {<br>            throw new ServiceException(&quot;Failed to fetch product details&quot;, e);<br>        }<br>    }<br>}</pre><p><strong>Benefits:</strong></p><ul><li>Three API calls that would take 300ms sequentially complete in ~100ms in parallel</li><li>No complex reactive operators — just straightforward imperative code</li><li>You can create thousands of virtual threads without worrying about resource exhaustion</li><li>Blocking on network I/O is perfectly fine and efficient</li></ul><h4>Example 3: Custom Virtual Thread Executor</h4><p>Sometimes you want more control over virtual thread execution:</p><pre>@Configuration<br>public class AsyncConfig implements AsyncConfigurer {<br>    <br>    @Override<br>    public Executor getAsyncExecutor() {<br>        // Create a custom virtual thread executor with naming<br>        ThreadFactory factory = Thread.ofVirtual()<br>            .name(&quot;async-vt-&quot;, 0)<br>            .factory();<br>        <br>        return Executors.newThreadPerTaskExecutor(factory);<br>    }<br>    <br>    @Override<br>    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {<br>        return (ex, method, params) -&gt; {<br>            System.err.println(&quot;Exception in @Async method: &quot; + method.getName());<br>            ex.printStackTrace();<br>        };<br>    }<br>}</pre><p><strong>When to use custom executors:</strong></p><ul><li>You want specific thread naming for debugging</li><li>You need different executors for different types of tasks</li><li>You want to apply custom exception handling</li><li>You’re migrating gradually and need both virtual and platform thread executors</li></ul><p><strong>Usage:</strong></p><pre>@Service<br>public class ReportService {<br>    <br>    @Autowired<br>    @Qualifier(&quot;virtualThreadExecutor&quot;)<br>    private Executor executor;<br>    <br>    public void generateReport(Long reportId) {<br>        executor.execute(() -&gt; {<br>            // Heavy processing on a virtual thread<br>            Report report = processReport(reportId);<br>            saveReport(report);<br>        });<br>    }<br>}</pre><h3>Performance Characteristics</h3><h4>When Virtual Threads Excel</h4><p>Virtual threads are ideal for I/O-bound operations where threads spend most of their time waiting:</p><p><strong>1. High Concurrency with I/O Operations</strong></p><ul><li>REST APIs that call databases or external services</li><li>Microservices with numerous service-to-service calls</li><li>WebSocket servers handling many concurrent connections</li><li>File I/O operations (reading/writing large files)</li></ul><p><strong>Example scenario</strong>: A REST API handling 10,000 concurrent requests, each making 3 database queries. With platform threads, you’d need a massive thread pool and face memory pressure. With virtual threads, this is trivial — each request gets its own virtual thread that cheaply blocks during DB calls.</p><p><strong>2. Database-Heavy Applications</strong> Traditional JDBC drivers block during queries. Virtual threads make this blocking cheap:</p><pre>// This is fine with virtual threads!<br>List&lt;Order&gt; orders = jdbcTemplate.query(<br>    &quot;SELECT * FROM orders WHERE user_id = ?&quot;,<br>    new Object[]{userId},<br>    new OrderRowMapper()<br>);</pre><p><strong>3. Batch Processing</strong> Processing large batches where each item requires I/O:</p><pre>// Process 100,000 records in parallel<br>try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {<br>    records.forEach(record -&gt; <br>        executor.submit(() -&gt; processRecord(record))<br>    );<br>}</pre><h3>When to Stick with Platform Threads</h3><p>Virtual threads are not a silver bullet. Avoid them for:</p><ol><li><strong>CPU-Intensive Tasks</strong> If your thread is continuously computing (no blocking), virtual threads offer no advantage:</li></ol><pre>// Not ideal for virtual threads<br>public BigInteger calculatePrime(int n) {<br>    // Pure computation, no blocking<br>    // Better on platform threads or ForkJoinPool<br>}</pre><p><strong>2. Very Short-Lived Operations</strong> For tasks that complete in microseconds, the overhead of creating even a virtual thread might not be worth it:</p><pre>// Too short for virtual threads<br>public int add(int a, int b) {<br>    return a + b;<br>}</pre><p><strong>3. Operations Requiring Pinning Avoidance</strong> Some operations “pin” virtual threads to carrier threads (more on this later). If your code is heavy with synchronized blocks, platform threads might be better.</p><h3>Benchmark Results</h3><p>Here&#39;s a real-world comparison using a Spring Boot REST API that makes database queries:</p><p><strong>Test setup:</strong></p><ul><li>Endpoint: GET /api/orders/{id} (fetches order + user + items)</li><li>Database: PostgreSQL with connection pool of 50</li><li>Load: 10,000 concurrent requests</li><li>Server: 8 CPU cores, 16GB RAM</li></ul><p><strong>Results:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/694/1*X7dvNIWTUIOUASZvcfBfGg.png" /></figure><ul><li>4x improvement in throughput</li><li>60% reduction in average latency</li><li>90% reduction in memory usage for thread stacks</li><li>Far fewer timeouts under extreme load</li></ul><p><strong>Memory usage over time:</strong> Platform threads maintain consistent memory for thread stacks regardless of load. Virtual threads scale memory usage with actual concurrent operations—idle virtual threads consume almost no memory.</p><h3>Important Considerations and Gotchas</h3><h4>Thread Pinning</h4><p><strong>What is pinning?</strong></p><p>Pinning occurs when a virtual thread cannot unmount from its carrier platform thread. This defeats the purpose of virtual threads — the carrier thread is blocked just like a traditional platform thread would be.</p><p><strong>Two main causes:</strong></p><ol><li><strong>Synchronized blocks:</strong></li></ol><pre>// BAD: This pins the virtual thread<br>public synchronized void updateCounter() {<br>    counter++;<br>    // If this blocks (e.g., I/O), the carrier thread is stuck<br>    database.save(counter);<br>}</pre><p><strong>Solution</strong>: Use ReentrantLock instead:</p><pre>// GOOD: Virtual thread can unmount<br>private final ReentrantLock lock = new ReentrantLock();<br><br>public void updateCounter() {<br>    lock.lock();<br>    try {<br>        counter++;<br>        database.save(counter); // Can unmount during I/O<br>    } finally {<br>        lock.unlock();<br>    }<br>}</pre><p>2. Native method calls or foreign function calls:</p><p>Native methods may cause pinning. This is usually handled by the JVM, but be aware.</p><p><strong>How to detect pinning:</strong></p><p>Enable JVM flags to detect pinning:</p><pre>java -Djdk.tracePinnedThreads=full -jar myapp.jar</pre><p>This logs stack traces when virtual threads are pinned, helping you identify problem areas.</p><p><strong>Why it matters:</strong> If many virtual threads are pinned, you lose the scalability benefits. You effectively have the same limitations as platform threads.</p><h4>ThreadLocal Usage</h4><p>ThreadLocal variables can be problematic with virtual threads:</p><p><strong>The problem:</strong></p><pre>// Problematic with millions of virtual threads<br>private static final ThreadLocal&lt;UserContext&gt; userContext = new ThreadLocal&lt;&gt;();<br><br>public void processRequest() {<br>    userContext.set(new UserContext(...));<br>    // ... processing ...<br>    userContext.remove();<br>}</pre><p>With millions of virtual threads, ThreadLocal can cause memory pressure since each thread gets its own copy of the data.</p><p><strong>Best practices:</strong></p><ol><li><strong>Minimize ThreadLocal usage</strong>: Pass context explicitly when possible</li><li><strong>Always clean up</strong>: Use try-finally to ensure remove() is called</li><li><strong>Consider scoped values</strong>: Java 21 introduces scoped values as a better alternative (preview feature)</li></ol><pre>// Better approach: Pass context explicitly<br>public void processRequest(UserContext context) {<br>    // No ThreadLocal needed<br>    service.handleRequest(context);<br>}</pre><h4>Monitoring and Observability</h4><p><strong>How virtual threads appear in monitoring:</strong></p><p>Virtual threads show up differently in monitoring tools:</p><p><strong>Thread dumps</strong>: Instead of seeing 200 platform threads, you might see millions of virtual threads. Tools need updating to handle this sensibly.</p><p><strong>Java Flight Recorder (JFR)</strong>: JFR supports virtual threads and can track their lifecycle events. Enable with:</p><pre>java -XX:StartFlightRecording=filename=recording.jfr -jar myapp.jar</pre><p><strong>Metrics to track:</strong></p><ul><li>Number of platform (carrier) threads (should be small, ~CPU cores)</li><li>Number of virtual threads (can be massive)</li><li>Pinning events (should be rare)</li><li>Carrier thread utilization</li></ul><p><strong>Logging considerations:</strong></p><p>Thread names in logs will be different:</p><pre>// Name your virtual threads for better debugging<br>Thread.ofVirtual()<br>    .name(&quot;worker-&quot;, 0)<br>    .start(() -&gt; doWork());</pre><h4>Common Pitfalls</h4><p><strong>1. Overusing ThreadLocal</strong> As mentioned, ThreadLocal with millions of threads can consume significant memory.</p><p><strong>2. Unnecessary thread pool tuning</strong> You don’t need to carefully tune thread pools anymore:</p><pre>// OLD WAY: Careful tuning required<br>@Bean<br>public ThreadPoolTaskExecutor taskExecutor() {<br>    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();<br>    executor.setCorePoolSize(10);<br>    executor.setMaxPoolSize(50);<br>    executor.setQueueCapacity(100);<br>    return executor;<br>}<br><br>// NEW WAY: Just use virtual threads<br>@Bean<br>public Executor taskExecutor() {<br>    return Executors.newVirtualThreadPerTaskExecutor();<br>}</pre><p><strong>3. Mixing virtual and platform threads incorrectly</strong> Be intentional about which executor you’re using:</p><pre>// Confusing: mixing different thread types<br>@Async // Uses virtual threads<br>public void task1() { ... }<br><br>@Async(&quot;platformThreadExecutor&quot;) // Uses platform threads<br>public void task2() { ... }</pre><p><strong>4. Expecting CPU-bound performance improvements</strong> Virtual threads don’t make CPU-intensive work faster. They only help with I/O-bound concurrency.</p><p><strong>5. Not testing under realistic load</strong> Virtual threads behave differently under load. Always load test before production deployment.</p><h3>Real-World Use Cases</h3><h4>Use Case 1: High-Traffic REST API</h4><p><strong>Company</strong>: E-commerce platform handling 50,000 requests/minute</p><p><strong>Problem Statement:</strong> During peak shopping hours (Black Friday), the order processing API would experience severe latency spikes. Each order request made:</p><ul><li>1 database query to fetch user data</li><li>1 database query to fetch product inventory</li><li>1 call to payment gateway</li><li>1 call to shipping calculation service</li></ul><p>With a 200-thread platform thread pool, requests would queue up, leading to timeouts and abandoned carts.</p><p><strong>Solution with Virtual Threads:</strong></p><pre>@RestController<br>public class OrderController {<br>    <br>    @PostMapping(&quot;/orders&quot;)<br>    public OrderResponse createOrder(@RequestBody OrderRequest request) {<br>        // All these blocking calls now run efficiently on virtual threads<br>        User user = userService.getUser(request.getUserId());<br>        Product product = productService.getProduct(request.getProductId());<br>        PaymentResult payment = paymentGateway.processPayment(request.getPayment());<br>        ShippingCost shipping = shippingService.calculateCost(request.getAddress());<br>        <br>        Order order = orderService.createOrder(user, product, payment, shipping);<br>        return new OrderResponse(order);<br>    }<br>}</pre><p>Simply enabled spring.threads.virtual.enabled=true in production.</p><p><strong>Results Achieved:</strong></p><ul><li>Throughput increased from 2,500 to 12,000 requests/minute</li><li>P95 latency dropped from 2.1s to 450ms</li><li>Timeout rate dropped from 8% to 0.2%</li><li>Server count reduced from 12 to 8 instances (cost savings)</li><li>Zero code changes required (just configuration)</li></ul><p><strong>Key Insight</strong>: The multiple blocking I/O calls per request were perfect for virtual threads. Each request could block freely while waiting for database or external service responses.</p><h4>Use Case 2: Batch Processing Service</h4><p><strong>Company</strong>: Financial institution processing daily transaction reports</p><p><strong>Problem Statement:</strong> A nightly batch job needed to process 500,000 transaction records. For each record:</p><ul><li>Fetch account details from database</li><li>Validate against fraud detection API</li><li>Enrich with customer data from CRM system</li><li>Write to data warehouse</li></ul><p>Processing with a 50-thread pool took 6 hours, creating a tight window before market opening.</p><p><strong>Traditional Approach Limitations:</strong></p><pre>// Slow: Sequential processing<br>for (Transaction txn : transactions) {<br>    Account account = accountRepo.findById(txn.getAccountId());<br>    FraudCheck fraud = fraudApi.check(txn);<br>    Customer customer = crmSystem.getCustomer(account.getCustomerId());<br>    dataWarehouse.save(new EnrichedTransaction(txn, account, fraud, customer));<br>}</pre><p>Increasing the thread pool to 500 threads caused memory pressure and database connection pool exhaustion.</p><p><strong>Virtual Threads Implementation:</strong></p><pre>@Service<br>public class TransactionProcessor {<br>    <br>    public void processBatch(List&lt;Transaction&gt; transactions) {<br>        try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {<br>            <br>            // Submit all 500,000 as separate tasks<br>            List&lt;Future&lt;Void&gt;&gt; futures = transactions.stream()<br>                .map(txn -&gt; executor.submit(() -&gt; {<br>                    processTransaction(txn);<br>                    return null;<br>                }))<br>                .toList();<br>            <br>            // Wait for all to complete<br>            for (Future&lt;Void&gt; future : futures) {<br>                future.get();<br>            }<br>        }<br>    }<br>    <br>    private void processTransaction(Transaction txn) {<br>        Account account = accountRepo.findById(txn.getAccountId());<br>        FraudCheck fraud = fraudApi.check(txn);<br>        Customer customer = crmSystem.getCustomer(account.getCustomerId());<br>        dataWarehouse.save(new EnrichedTransaction(txn, account, fraud, customer));<br>    }<br>}</pre><p><strong>Performance Gains:</strong></p><ul><li>Processing time reduced from 6 hours to 45 minutes (8x improvement)</li><li>Memory usage for threads dropped from 1GB to 80MB</li><li>Database connection pool stayed under 50 connections (was hitting 500 limit before)</li><li>No increase in CPU usage (still I/O bound)</li></ul><p><strong>Key Insight</strong>: Spawning 500,000 virtual threads is trivial. Each thread blocks during API calls and database queries, efficiently multiplexing onto a small number of carrier threads.</p><h4>Use Case 3: Microservices Architecture</h4><p><strong>Company</strong>: Travel booking platform with microservices for flights, hotels, cars, and activities</p><p><strong>Problem Statement:</strong> The aggregation service needed to call 4 downstream services to build a complete travel package. Using platform threads:</p><ul><li>Sequential calls took 800ms total (200ms each)</li><li>Parallel calls with CompletableFuture required complex orchestration code</li><li>Reactive WebFlux was considered but rejected due to team expertise</li></ul><p><strong>Virtual Threads Implementation:</strong></p><pre>@Service<br>public class TravelPackageService {<br>    <br>    public TravelPackage buildPackage(SearchCriteria criteria) {<br>        <br>        try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {<br>            <br>            // Launch all searches in parallel - simple and readable<br>            Future&lt;List&lt;Flight&gt;&gt; flightsFuture = <br>                executor.submit(() -&gt; flightService.search(criteria));<br>            <br>            Future&lt;List&lt;Hotel&gt;&gt; hotelsFuture = <br>                executor.submit(() -&gt; hotelService.search(criteria));<br>            <br>            Future&lt;List&lt;Car&gt;&gt; carsFuture = <br>                executor.submit(() -&gt; carService.search(criteria));<br>            <br>            Future&lt;List&lt;Activity&gt;&gt; activitiesFuture = <br>                executor.submit(() -&gt; activityService.search(criteria));<br>            <br>            // Collect results - each blocks its virtual thread while waiting<br>            List&lt;Flight&gt; flights = flightsFuture.get();<br>            List&lt;Hotel&gt; hotels = hotelsFuture.get();<br>            List&lt;Car&gt; cars = carsFuture.get();<br>            List&lt;Activity&gt; activities = activitiesFuture.get();<br>            <br>            return new TravelPackage(flights, hotels, cars, activities);<br>        } catch (Exception e) {<br>            throw new PackageSearchException(&quot;Failed to build package&quot;, e);<br>        }<br>    }<br>}</pre><p><strong>Overall System Improvement:</strong></p><ul><li>Response time dropped from 800ms to 220ms (calls now parallel)</li><li>Code remained simple and imperative (no reactive complexity)</li><li>Error handling remained straightforward with try-catch</li><li>Easy to add timeout handling with future.get(timeout, unit)</li><li>Team velocity improved (no need to learn reactive programming)</li></ul><p><strong>Inter-Service Communication Benefits:</strong> Each microservice also adopted virtual threads, allowing them to handle more concurrent requests from other services without increasing instance counts.</p><p><strong>Key Insight</strong>: Virtual threads provided the scalability benefits of reactive programming while keeping code simple and maintainable. The team could focus on business logic rather than fighting with reactive operators.</p><h3>Best Practices</h3><h4>1. Let Virtual Threads Block Freely</h4><pre>Don’t try to avoid blocking when using virtual threads:<br>// GOOD: Simple and efficient with virtual threads<br>public User getUserWithOrders(Long userId) {<br>    User user = userRepository.findById(userId); // Blocks - fine!<br>    List&lt;Order&gt; orders = orderRepository.findByUserId(userId); // Blocks - fine!<br>    user.setOrders(orders);<br>    return user;<br>}<br><br>// BAD: Unnecessary complexity<br>public CompletableFuture&lt;User&gt; getUserWithOrders(Long userId) {<br>    // Don&#39;t use CompletableFuture just to avoid blocking<br>    return CompletableFuture.supplyAsync(...)<br>        .thenCompose(...);<br>}</pre><p>Virtual threads are designed to block. Embrace it.</p><h4>2. Avoid Unnecessary Thread Pools</h4><p>With virtual threads, you rarely need traditional thread pools:</p><pre>// OLD: Complex thread pool configuration<br>@Bean<br>public ThreadPoolTaskExecutor taskExecutor() {<br>    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();<br>    executor.setCorePoolSize(10);<br>    executor.setMaxPoolSize(50);<br>    executor.setQueueCapacity(100);<br>    executor.setThreadNamePrefix(&quot;async-&quot;);<br>    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());<br>    executor.initialize();<br>    return executor;<br>}<br><br>// NEW: Simple virtual thread executor<br>@Bean<br>public Executor taskExecutor() {<br>    return Executors.newVirtualThreadPerTaskExecutor();<br>}</pre><h4>3. Use Structured Concurrency Patterns</h4><p>Java 21 introduces structured concurrency (preview feature) for better task management:</p><pre>// Traditional approach<br>public OrderSummary getOrderSummary(Long orderId) {<br>    try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {<br>        Future&lt;Order&gt; orderFuture = executor.submit(() -&gt; orderService.getOrder(orderId));<br>        Future&lt;Payment&gt; paymentFuture = executor.submit(() -&gt; paymentService.getPayment(orderId));<br>        <br>        return new OrderSummary(orderFuture.get(), paymentFuture.get());<br>    }<br>}<br><br>// Structured concurrency (Java 21+)<br>public OrderSummary getOrderSummary(Long orderId) {<br>    try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {<br>        Future&lt;Order&gt; orderFuture = scope.fork(() -&gt; orderService.getOrder(orderId));<br>        Future&lt;Payment&gt; paymentFuture = scope.fork(() -&gt; paymentService.getPayment(orderId));<br>        <br>        scope.join();           // Wait for all tasks<br>        scope.throwIfFailed();  // Throw if any task failed<br>        <br>        return new OrderSummary(orderFuture.resultNow(), paymentFuture.resultNow());<br>    } catch (InterruptedException | ExecutionException e) {<br>        throw new ServiceException(&quot;Failed to get order summary&quot;, e);<br>    }<br>}</pre><p><strong>Benefits of structured concurrency:</strong></p><ul><li>Automatic cancellation of remaining tasks if one fails</li><li>Better error propagation</li><li>Guaranteed cleanup (try-with-resources)</li><li>Clearer task relationships</li></ul><h4>4. Review and Minimize Synchronized Blocks</h4><p>Replace synchronized with ReentrantLock for better virtual thread compatibility:</p><pre>// BAD: Causes pinning<br>public class Counter {<br>    private int count = 0;<br>    <br>    public synchronized void increment() {<br>        count++;<br>        // If this method does I/O, virtual thread is pinned<br>        database.save(count);<br>    }<br>}<br><br>// GOOD: Virtual thread can unmount<br>public class Counter {<br>    private int count = 0;<br>    private final ReentrantLock lock = new ReentrantLock();<br>    <br>    public void increment() {<br>        lock.lock();<br>        try {<br>            count++;<br>            database.save(count); // Virtual thread can unmount during I/O<br>        } finally {<br>            lock.unlock();<br>        }<br>    }<br>}</pre><p><strong>When synchronized is acceptable:</strong></p><ul><li>Very short critical sections (no I/O, just memory operations)</li><li>Sections that rarely execute</li></ul><h4>5. Monitor for Pinning in Production</h4><p>Enable pinning detection in production (with low overhead):</p><pre># Add to JVM args<br>-Djdk.tracePinnedThreads=short</pre><p>Set up alerts for pinning events and investigate:</p><pre>// Log pinning events<br>jvmMetrics.register(&quot;virtual.threads.pinned&quot;, () -&gt; <br>    Thread.getAllStackTraces().entrySet().stream()<br>        .filter(entry -&gt; entry.getKey().isVirtual())<br>        .filter(entry -&gt; /* check if pinned */)<br>        .count()<br>);</pre><h4>6. Keep ThreadLocal Usage Minimal</h4><p>Minimize ThreadLocal to reduce memory with millions of threads:</p><pre>// BAD: Heavy ThreadLocal usage<br>public class RequestProcessor {<br>    private static final ThreadLocal&lt;Map&lt;String, Object&gt;&gt; context = <br>        ThreadLocal.withInitial(HashMap::new);<br>    <br>    public void process() {<br>        context.get().put(&quot;data&quot;, largeObject); // Memory adds up!<br>    }<br>}<br><br>// GOOD: Pass context explicitly<br>public class RequestProcessor {<br>    public void process(RequestContext context) {<br>        context.setData(largeObject); // Only one instance<br>    }<br>}<br><br>// BETTER: Use ScopedValues (Java 21 preview)<br>public class RequestProcessor {<br>    private static final ScopedValue&lt;RequestContext&gt; CONTEXT = ScopedValue.newInstance();<br>    <br>    public void process(RequestContext context) {<br>        ScopedValue.where(CONTEXT, context).run(() -&gt; {<br>            // Context available in this scope<br>            doWork();<br>        });<br>    }<br>}</pre><h4>7. Test Under Realistic Load Conditions</h4><p>Always load test before production:</p><pre>// Load testing strategy<br>@SpringBootTest<br>public class LoadTest {<br>    <br>    @Test<br>    public void testHighConcurrency() {<br>        try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {<br>            <br>            List&lt;Future&lt;ResponseEntity&gt;&gt; futures = new ArrayList&lt;&gt;();<br>            <br>            // Simulate 10,000 concurrent requests<br>            for (int i = 0; i &lt; 10000; i++) {<br>                futures.add(executor.submit(() -&gt; <br>                    restTemplate.getForEntity(&quot;http://localhost:8080/api/orders/1&quot;, Order.class)<br>                ));<br>            }<br>            <br>            // Verify all complete successfully<br>            for (Future&lt;ResponseEntity&gt; future : futures) {<br>                ResponseEntity response = future.get(10, TimeUnit.SECONDS);<br>                assertEquals(HttpStatus.OK, response.getStatusCode());<br>            }<br>        }<br>    }<br>}</pre><p><strong>Test scenarios:</strong></p><ul><li>Gradual load increase (0 to 10,000 concurrent users)</li><li>Sustained high load (10,000 users for 1 hour)</li><li>Spike tests (sudden jump to 20,000 users)</li><li>Database connection pool limits</li><li>External API rate limits</li></ul><h3>Key Takeaways</h3><p>Virtual threads represent one of the most significant improvements to Java’s concurrency model in decades. Here’s what you need to remember:</p><p><strong>1. Virtual threads solve the scalability problem without complexity</strong> You get reactive-level scalability with imperative code simplicity. This is a game-changer for I/O-heavy applications.</p><p><strong>2. Migration is often trivial</strong> In many cases, enabling virtual threads is a single configuration line. Your existing blocking code becomes massively scalable.</p><p><strong>3. Not a silver bullet</strong> Virtual threads excel at I/O-bound workloads but don’t help with CPU-intensive tasks. Know when to use them.</p><p><strong>4. Watch for pinning</strong> The main gotcha is thread pinning with synchronized blocks. Use ReentrantLock when doing I/O in critical sections.</p><p><strong>5. Simplicity is the superpower</strong> The real benefit isn’t just scalability; it’s achieving that scalability while keeping your code simple, readable, and maintainable.</p><h3>When to Adopt Virtual Threads</h3><p><strong>You should adopt virtual threads if:</strong></p><ul><li>Your application is I/O-bound (databases, REST APIs, files)</li><li>You need to handle high concurrency (10,000+ concurrent operations)</li><li>You can upgrade to Java 21+</li><li>You want simpler code than reactive programming</li><li>Your team prefers traditional programming models</li></ul><p><strong>You should wait if:</strong></p><ul><li>Your workload is primarily CPU-bound</li><li>You’re running Java 17 or earlier (and can’t upgrade)</li><li>Your application has low concurrency needs</li><li>You’ve already invested heavily in reactive programming, and it’s working well</li></ul><h4>The Simplicity Advantage</h4><p>Perhaps the most underrated benefit of virtual threads is how they preserve Java’s traditional programming model. You don’t need to:</p><ul><li>Learn reactive operators</li><li>Debug complex async call chains</li><li>Explain monads to your team</li><li>Retrofit your entire codebase</li></ul><p>Instead, you write simple, sequential code that’s easy to understand, easy to debug, and scales to millions of concurrent operations.</p><h4>Call to Action: Try It in Your Next Spring Boot Project</h4><p>Getting started is incredibly easy:</p><ol><li>Upgrade to Java 21+ and Spring Boot 3.2+</li><li>Add spring.threads.virtual.enabled=true to your configuration</li><li>Deploy and measure the results</li></ol><p>That’s it. Your existing code now scales massively.</p><p>Start with a non-critical service, measure the improvement, and gradually roll it out. The risk is low, the migration is simple, and the benefits can be substantial.</p><p>Virtual threads aren’t the future; they’re available now, production-ready, and they might just be the upgrade your Spring Boot application has been waiting for.</p><p>Happy coding 🚀!</p><h3>A message from our Founder</h3><p><strong>Hey, </strong><a href="https://linkedin.com/in/sunilsandhu"><strong>Sunil</strong></a><strong> here.</strong> I wanted to take a moment to thank you for reading until the end and for being a part of this community.</p><p>Did you know that our team run these publications as a volunteer effort to over 3.5m monthly readers? <strong>We don’t receive any funding, we do this to support the community. ❤️</strong></p><p>If you want to show some love, please take a moment to <strong>follow me on </strong><a href="https://linkedin.com/in/sunilsandhu"><strong>LinkedIn</strong></a><strong>, </strong><a href="https://tiktok.com/@messyfounder"><strong>TikTok</strong></a>, <a href="https://instagram.com/sunilsandhu"><strong>Instagram</strong></a>. You can also subscribe to our <a href="https://newsletter.plainenglish.io/"><strong>weekly newsletter</strong></a>.</p><p>And before you go, don’t forget to <strong>clap</strong> and <strong>follow</strong> the writer️!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=deb895f8d880" width="1" height="1" alt=""><hr><p><a href="https://blog.stackademic.com/unlocking-java-virtual-threads-a-spring-boot-perspective-deb895f8d880">Unlocking Java Virtual Threads: A Spring Boot Perspective</a> was originally published in <a href="https://blog.stackademic.com/">Stackademic</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building Real-Time Data Streaming with Spring Boot: A Complete Guide]]></title>
            <link>https://blog.stackademic.com/building-real-time-data-streaming-with-spring-boot-a-complete-guide-68a3c9aaa505?source=rss-9be9624c6d5a------2</link>
            <guid isPermaLink="false">https://medium.com/p/68a3c9aaa505</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Chanuka Dinuwan]]></dc:creator>
            <pubDate>Mon, 01 Sep 2025 14:04:32 GMT</pubDate>
            <atom:updated>2025-09-02T06:18:52.159Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Xwdw4Or8fOBaiD_O.png" /></figure><h3>Introduction</h3><p>In today’s data-driven world, real-time processing has become essential for modern applications. Whether you’re building a financial trading platform, IoT data pipeline, or live analytics dashboard, the ability to process and stream data in real-time can make or break user experience.</p><p>In this article, I’ll walk you through building a robust streaming solution using Spring Boot, demonstrating how to handle continuous data flows efficiently and reliably.</p><h3>Why Spring Boot for Streaming?</h3><p>Spring Boot provides excellent support for streaming through:</p><ul><li><strong>Spring WebFlux</strong>: Reactive programming model for non-blocking operations</li><li><strong>Server-Sent Events (SSE)</strong>: Built-in support for real-time browser communication</li><li><strong>Integration with message brokers</strong>: Seamless connection to Kafka, RabbitMQ, etc.</li><li><strong>Backpressure handling</strong>: Automatic flow control for sustainable streaming</li></ul><h3>Project Architecture Overview</h3><p>Our streaming solution consists of several key components:</p><h3>1. Data Source Layer</h3><ul><li><strong>Purpose</strong>: Generate or receive continuous data streams</li><li><strong>Technologies</strong>: REST endpoints, scheduled tasks, or external integrations</li><li><strong>Key Features</strong>: Configurable data generation rates and formats</li></ul><h3>2. Stream Processing Layer</h3><ul><li><strong>Purpose</strong>: Transform, filter, and enrich streaming data</li><li><strong>Technologies</strong>: Spring WebFlux, Reactor operators</li><li><strong>Key Features</strong>: Non-blocking processing, error handling, and backpressure management</li></ul><h3>3. Client Communication Layer</h3><ul><li><strong>Purpose</strong>: Deliver processed data to connected clients</li><li><strong>Technologies</strong>: Server-Sent Events, WebSocket, or REST streaming</li><li><strong>Key Features</strong>: Real-time updates, connection management, and graceful disconnection</li></ul><h3>Implementation Deep Dive</h3><h3>Setting Up the Reactive Stream Controller</h3><pre>@RestController<br>@RequestMapping(&quot;/api/stream&quot;)<br>public class StreamController {<br>    <br>    @GetMapping(value = &quot;/data&quot;, produces = MediaType.TEXT_EVENT_STREAM_VALUE)<br>    public Flux&lt;ServerSentEvent&lt;DataModel&gt;&gt; streamData() {<br>        return dataService.getDataStream()<br>                .map(data -&gt; ServerSentEvent.builder(data)<br>                        .id(String.valueOf(data.getId()))<br>                        .event(&quot;data-update&quot;)<br>                        .build())<br>                .doOnCancel(() -&gt; log.info(&quot;Client disconnected&quot;))<br>                .doOnError(error -&gt; log.error(&quot;Stream error: &quot;, error));<br>    }<br>}</pre><h3>Implementing the Data Service</h3><pre>@Service<br>public class DataService {<br>    <br>    public Flux&lt;DataModel&gt; getDataStream() {<br>        return Flux.interval(Duration.ofSeconds(1))<br>                .map(tick -&gt; generateData())<br>                .onBackpressureBuffer(100)<br>                .share(); <br>    }<br>    <br>    private DataModel generateData() {<br>        return DataModel.builder()<br>                .id(System.currentTimeMillis())<br>                .timestamp(Instant.now())<br>                .value(ThreadLocalRandom.current().nextDouble(100))<br>                .build();<br>    }<br>}</pre><h3>Handling Backpressure and Error Recovery</h3><p>One of the most critical aspects of streaming is handling situations where consumers can’t keep up with producers:</p><pre>public Flux&lt;DataModel&gt; resilientDataStream() {<br>    return Flux.interval(Duration.ofMillis(100))<br>            .map(this::processData)<br>            .onBackpressureBuffer(1000, BufferOverflowStrategy.DROP_OLDEST)<br>            .retry(3)<br>            .onErrorResume(error -&gt; {<br>                log.error(&quot;Stream failed, switching to fallback&quot;, error);<br>                return getFallbackStream();<br>            });<br>}</pre><h3>Frontend Integration</h3><h3>JavaScript Client Implementation</h3><pre>const eventSource = new EventSource(&#39;/api/stream/data&#39;);</pre><pre>eventSource.addEventListener(&#39;data-update&#39;, function(event) {<br>    const data = JSON.parse(event.data);<br>    updateChart(data);<br>});</pre><pre>eventSource.onerror = function(event) {<br>    console.error(&#39;Stream connection error:&#39;, event);<br>    // Implement reconnection logic<br>    setTimeout(() =&gt; {<br>        window.location.reload();<br>    }, 5000);<br>};</pre><h3>React Hook for Stream Management</h3><pre>function useDataStream() {<br>    const [data, setData] = useState([]);<br>    const [connected, setConnected] = useState(false);<br>    <br>    useEffect(() =&gt; {<br>        const eventSource = new EventSource(&#39;/api/stream/data&#39;);<br>        <br>        eventSource.onopen = () =&gt; setConnected(true);<br>        eventSource.addEventListener(&#39;data-update&#39;, (event) =&gt; {<br>            const newData = JSON.parse(event.data);<br>            setData(prev =&gt; [...prev.slice(-99), newData]);<br>        });<br>        <br>        return () =&gt; {<br>            eventSource.close();<br>            setConnected(false);<br>        };<br>    }, []);<br>    <br>    return { data, connected };<br>}</pre><h3>Performance Optimization Strategies</h3><h3>1. Connection Management</h3><ul><li>Implement connection pooling for database operations</li><li>Use reactive database drivers (R2DBC) for non-blocking I/O</li><li>Monitor active connections and implement cleanup strategies</li></ul><h3>2. Memory Management</h3><ul><li>Configure appropriate buffer sizes based on your use case</li><li>Implement data windowing to prevent memory leaks</li><li>Use weak references for temporary data structures</li></ul><h3>3. Network Optimization</h3><ul><li>Compress data when appropriate (gzip, custom compression)</li><li>Batch small updates to reduce network overhead</li><li>Implement intelligent reconnection strategies</li></ul><h3>Monitoring and Observability</h3><h3>Metrics That Matter</h3><pre>@Component<br>public class StreamMetrics {<br>    <br>    private final MeterRegistry meterRegistry;<br>    private final Counter streamConnections;<br>    private final Timer dataProcessingTime;<br>    <br>    public StreamMetrics(MeterRegistry meterRegistry) {<br>        this.meterRegistry = meterRegistry;<br>        this.streamConnections = Counter.builder(&quot;stream.connections&quot;)<br>                .description(&quot;Number of active stream connections&quot;)<br>                .register(meterRegistry);<br>        this.dataProcessingTime = Timer.builder(&quot;stream.processing.time&quot;)<br>                .description(&quot;Time spent processing stream data&quot;)<br>                .register(meterRegistry);<br>    }<br>}</pre><h3>Health Checks and Alerts</h3><ul><li>Monitor stream latency and throughput</li><li>Set up alerts for connection drops or processing delays</li><li>Implement circuit breakers for external dependencies</li></ul><h3>Common Pitfalls and How to Avoid Them</h3><h3>1. Memory Leaks in Long-Running Streams</h3><p><strong>Problem</strong>: Accumulating data without proper cleanup <strong>Solution</strong>: Implement sliding windows and explicit resource management</p><h3>2. Client Reconnection Issues</h3><p><strong>Problem</strong>: Clients losing connection without proper recovery <strong>Solution</strong>: Implement exponential backoff and connection state management</p><h3>3. Overwhelming Downstream Systems</h3><p><strong>Problem</strong>: Producing data faster than consumers can handle <strong>Solution</strong>: Implement proper backpressure strategies and rate limiting</p><h3>Testing Strategies</h3><h3>Unit Testing Reactive Streams</h3><pre>@Test<br>public void testDataStream() {<br>    StepVerifier.create(dataService.getDataStream().take(3))<br>            .expectNextCount(3)<br>            .verifyComplete();<br>}</pre><h3>Integration Testing with TestContainers</h3><pre>@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)<br>class StreamingIntegrationTest {<br>    <br>    @Test<br>    void testStreamEndpoint() {<br>        WebTestClient.bindToServer()<br>                .baseUrl(&quot;http://localhost:&quot; + port)<br>                .build()<br>                .get()<br>                .uri(&quot;/api/stream/data&quot;)<br>                .accept(MediaType.TEXT_EVENT_STREAM)<br>                .exchange()<br>                .expectStatus().isOk()<br>                .returnResult(String.class)<br>                .getResponseBody()<br>                .take(Duration.ofSeconds(5))<br>                .collectList()<br>                .block();<br>    }<br>}</pre><h3>Production Deployment Considerations</h3><h3>Scaling Strategies</h3><ul><li><strong>Horizontal scaling</strong>: Use load balancers with session affinity</li><li><strong>Vertical scaling</strong>: Optimize JVM settings for high-throughput scenarios</li><li><strong>Database scaling</strong>: Consider read replicas for data-heavy streams</li></ul><h3>Security Considerations</h3><ul><li>Implement proper authentication for stream endpoints</li><li>Use HTTPS for all streaming connections</li><li>Consider rate limiting to prevent abuse</li></ul><h3>Monitoring in Production</h3><ul><li>Set up comprehensive logging for stream lifecycle events</li><li>Monitor JVM metrics (heap usage, GC pressure)</li><li>Track business metrics (active users, data throughput)</li></ul><h3>Key Takeaways</h3><p>Building a production-ready streaming solution with Spring Boot requires careful consideration of:</p><ol><li><strong>Reactive Programming</strong>: Embrace non-blocking I/O for scalability</li><li><strong>Backpressure Management</strong>: Always handle flow control properly</li><li><strong>Error Recovery</strong>: Implement robust retry and fallback mechanisms</li><li><strong>Resource Management</strong>: Monitor and manage connections and memory usage</li><li><strong>Client Experience</strong>: Provide smooth reconnection and state management</li></ol><h3>What’s Next?</h3><p>This foundation gives you a solid starting point for building streaming applications. Consider exploring:</p><ul><li>Integration with Apache Kafka for distributed streaming</li><li>Advanced monitoring with Micrometer and Prometheus</li><li>Implementing stream analytics with Spring Cloud Data Flow</li><li>Building reactive microservices architectures</li></ul><p>The complete source code for this project is available on <a href="https://github.com/cdin8619-0/springboot-stream">GitHub</a>, where you can explore the full implementation and run the examples yourself.</p><p><em>Have you built streaming applications with Spring Boot? Share your experiences and challenges in the comments below! For more articles on reactive programming and Spring Boot, follow me on Medium. Happy Coding!</em></p><h3>A message from our Founder</h3><p><strong>Hey, </strong><a href="https://linkedin.com/in/sunilsandhu"><strong>Sunil</strong></a><strong> here.</strong> I wanted to take a moment to thank you for reading until the end and for being a part of this community.</p><p>Did you know that our team run these publications as a volunteer effort to over 3.5m monthly readers? <strong>We don’t receive any funding, we do this to support the community. ❤️</strong></p><p>If you want to show some love, please take a moment to <strong>follow me on </strong><a href="https://linkedin.com/in/sunilsandhu"><strong>LinkedIn</strong></a><strong>, </strong><a href="https://tiktok.com/@messyfounder"><strong>TikTok</strong></a>, <a href="https://instagram.com/sunilsandhu"><strong>Instagram</strong></a>. You can also subscribe to our <a href="https://newsletter.plainenglish.io/"><strong>weekly newsletter</strong></a>.</p><p>And before you go, don’t forget to <strong>clap</strong> and <strong>follow</strong> the writer️!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=68a3c9aaa505" width="1" height="1" alt=""><hr><p><a href="https://blog.stackademic.com/building-real-time-data-streaming-with-spring-boot-a-complete-guide-68a3c9aaa505">Building Real-Time Data Streaming with Spring Boot: A Complete Guide</a> was originally published in <a href="https://blog.stackademic.com/">Stackademic</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>