<?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 Sahil Gupta on Medium]]></title>
        <description><![CDATA[Stories by Sahil Gupta on Medium]]></description>
        <link>https://medium.com/@sahilgupta_33409?source=rss-3db08a9fcea3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Sahil Gupta on Medium</title>
            <link>https://medium.com/@sahilgupta_33409?source=rss-3db08a9fcea3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 19:09:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sahilgupta_33409/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[The Server That Was Busy Doing Nothing]]></title>
            <link>https://medium.com/@sahilgupta_33409/the-server-that-was-busy-doing-nothing-ba4f3f4ae107?source=rss-3db08a9fcea3------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba4f3f4ae107</guid>
            <category><![CDATA[cpu-utilization]]></category>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[cache]]></category>
            <category><![CDATA[production-issue]]></category>
            <dc:creator><![CDATA[Sahil Gupta]]></dc:creator>
            <pubDate>Thu, 03 Sep 2026 19:58:09 GMT</pubDate>
            <atom:updated>2026-09-03T19:58:09.662Z</atom:updated>
            <content:encoded><![CDATA[<h3>How a simple UI/UX improvement quietly filled a 6GB cache and pinned our CPU at 100% — while real traffic stayed almost zero</h3><p>It started with a number that didn’t make sense.</p><p>Our Next.js site is small, nothing exotic, deployed with PM2 on a Ubuntu. Traffic is low. It’s the kind of app that should idle quietly most of the day.</p><p>Instead, htop showed this:</p><pre>0[||||||||||||||||||||||||||||||||||||||||||||] 100.0%<br>1[||||||||||||||||||||||||||                  ]  42.4%<br>2[||||||||||||||||||||||||||||||||||||        ]  63.1%<br>3[||||||||||||||||||||||||||||||||            ]  46.6%<br>Mem[||||||||||||||||||||||||||||||||||||       ] 4.06G/29.2G<br>...<br>PID     USER CPU%  MEM%  TIME+     Command<br>3623063 root 90.4  9.0  8h49:50  next-server (v15.5.12)</pre><p>One core, pinned. Memory fine. Disk fine. And every 2–3 days, the app got so overloaded it had to be restarted just to keep the site alive. No traffic spike ever explained it. That mismatch — high CPU, low traffic — is the whole story. Everything below is us chasing that contradiction down to a single missing line of code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/804/1*4jsYrGZ4g7abqMbI-UERyw.png" /></figure><h3>Ruling out the easy answers</h3><p>The first instinct with “server is slow” is usually wrong, so we didn’t trust it. We checked the boring stuff first, because boring stuff is cheap to check and expensive to skip:</p><ul><li>Memory: fine.</li><li>Disk: fine, no space pressure.</li><li>Traffic: genuinely low.</li><li>Process list: one single next-server process was the entire story. Its siblings sat at 0.7% CPU. Whatever was happening was isolated to one worker, not systemic load.</li></ul><p>That last point mattered a lot. If it were real request volume, load would spread across workers. One process quietly cooking on its own, independent of traffic, meant something <em>inside</em> that process was looping or churning — not something <em>arriving</em> at it.</p><h3>Looking inside the process, not around it</h3><p>This is where most “restart it and hope” debugging stops. We didn’t want a band-aid, we wanted the actual mechanism. Two tools, both non-destructive, both safe to run on a live production process:</p><pre>perf record -p &lt;pid&gt; -g -- sleep 30 &amp;&amp; perf report<br>strace -p &lt;pid&gt; -c -f</pre><p>perf showed almost the entire CPU budget inside Node&#39;s own event loop and V8&#39;s JIT-compiled JavaScript — Builtins_ArrayMap, deep call stacks through node::MakeCallback, uv_run, CheckImmediate. In plain terms: this wasn&#39;t an idle process waiting on I/O — it was actively executing JavaScript, over and over, at high frequency.</p><p>strace -c backed that up with numbers:</p><pre>% time     seconds  usecs/call     calls    errors syscall<br>------ ----------- ----------- --------- --------- ----------------<br>96.23    5.960977         505     11783       976 futex<br>1.32    0.081693          21      3724           epoll_pwait<br>0.55    0.034335          15      2191           write<br>...<br>0.14    0.014000          31       441       165 openat<br>0.01    0.000732          29        25        14 mkdir</pre><p>96% of syscall time in futex — thread contention, consistent with heavy concurrent work, not a quiet process politely waiting for a request. And notice those error counts: 165 of 441 openat calls failed, 14 of 25 mkdir calls failed. Something was repeatedly trying to open or create files that didn&#39;t behave the way the code expected.</p><h3>The cache that ate itself</h3><p>Next.js keeps a cache on disk for pages it has already built — a folder called .next/cache. For a small app, that folder should be small. Ours wasn&#39;t:</p><pre>$ du -sh /var/www/html/app-platform/.next/cache<br>5.9G    /var/www/html/app-platform/.next/cache/</pre><p>5.9 gigabytes. For five to ten pages. That’s the moment the investigation stopped being vague and started being a hunt for a specific, findable bug — nothing about “low traffic, small app” explains a cache that size on its own. Something was writing to that folder far more than it should.</p><h3>The logs were shouting the answer the whole time</h3><p>We just hadn’t looked yet.</p><pre>$ pm2 logs app --lines 300 --nostream<br>Blog API failed | slug=australia-states | status=404 Not Found<br>Blog API failed | slug=blogs | status=404 Not Found<br>Blog API failed | slug=blogs | status=404 Not Found<br>Blog API failed | slug=blogs | status=404 Not Found<br>Blog API failed | slug=.jpg%2C.jpeg%2C.webp%2C.png | status=404 Not Found<br>Blog API failed | slug=blogs | status=404 Not Found<br>...</pre><p>Read that second-to-last line again: slug=.jpg,.jpeg,.webp,.png. Nobody types that into a URL bar. That&#39;s a config value — a list of allowed image extensions — that has clearly leaked into a link somewhere and is being followed like a real address, over and over, by something automated. Combine that with slug=blogs repeating dozens of times in a row, and a picture starts forming: something is repeatedly hitting the same handful of broken URLs, and every single hit is a fresh, failed API call.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/338/1*n2o_nb34YkSNiS0EHF9sjQ.png" /></figure><h3>Chasing it through the code (including a wrong turn)</h3><p>Naturally, we grepped for the exact log message:</p><pre>grep -rn &quot;Blog API failed&quot; /var/www/html/app-platform/src</pre><p>Nothing. Zero matches. For a moment that felt like a dead end — until we realized our <em>first</em> attempt at this search had actually matched something, just in the wrong place: .next/server/.../page.js, a minified, webpack-bundled build file, not source code. One matching line was over 45,000 characters long. Lesson learned the hard way: always exclude .next and node_modules before you start reading grep output like it&#39;s prose.</p><p>Back in the real source tree, the trail led through a small chain of files:</p><ul><li>src/lib/fetchBlogs.ts — a list-fetcher for the sitemap. Wrong log format, wrong shape. Not it.</li><li>src/lib/fetchUniversitiesAndCourses.ts — a genuinely heavy batch-fetcher (parallel pagination, verbose per-page logging) that turned out to be a separate, smaller concern, not the main fire.</li><li>src/app/(main)/course/[name]/[hashid]/page.tsx — closer in shape, but its error format didn&#39;t match either. Interestingly, this file <em>had already been hardened</em> at some point: wrapped in React&#39;s cache() to avoid duplicate fetches, with a 15-second timeout via AbortController. Good patterns — just not the smoking gun.</li><li>Finally: src/services/blog.server.ts and src/app/(main)/blogs/[slug]/page.tsx. Here it was.</li></ul><pre>export const getBlogData = cache(async (slug: string) =&gt; {<br>  // ... fetch with timeout ...<br>  if (!res.ok) {<br>    console.error(`[BLOG API ERROR] ${res.status} | slug=${slug}`);<br>    return null;<br>  }<br>  // ...<br>});</pre><pre>export default async function BlogDetailPage({ params }: PageProps) {<br>  const { slug } = await params;<br>  const blog = await getBlogData(slug);<br><br>  if (!blog) {<br>    // notFound()   &lt;-- commented out<br>    return (<br>      &lt;div className=&quot;min-h-screen ...&quot;&gt;<br>        &lt;h1&gt;Blog Not Found&lt;/h1&gt;<br>        &lt;p&gt;Sorry, we couldn&#39;t find the blog post you&#39;re looking for.&lt;/p&gt;<br>        &lt;Link href=&quot;/blogs&quot;&gt;&lt;Button&gt;Back to Blogs&lt;/Button&gt;&lt;/Link&gt;<br>      &lt;/div&gt;<br>    );<br>  }<br><br>  return &lt;BlogDetailClient blog={blog} /&gt;;<br>}</pre><p>There it is, sitting right there in a code comment: // notFound(). Commented out. Someone, at some point, deliberately chose to render a friendly &quot;Blog Not Found&quot; screen instead of calling Next.js&#39;s real notFound() function — probably to make the not-found page look nicer. It&#39;s a completely understandable instinct. It&#39;s also the entire root cause.</p><h3>Why one commented-out line was enough to break everything</h3><p>Here’s the part that made this bug so satisfying to finally understand: rendering a “not found” component and returning it as a normal page reply means the server responds with HTTP 200 — “OK, here’s your page.” Not 404. Not “this doesn’t exist.” Just, “sure, here you go.”</p><p>The page route was also configured like this:</p><pre>export const revalidate = 900       // ISR: refresh every 15 minutes<br>export const dynamic = &quot;force-static&quot;</pre><p>That’s Next.js’s Incremental Static Regeneration — it treats successfully-rendered pages as static assets worth caching to disk, so it doesn’t have to rebuild them on every request. Completely reasonable, <em>for real pages</em>. But Next.js has no way to know the difference between “this is a real blog post” and “this is a friendly apology screen” — both are just JSX that rendered successfully and got a 200. So both get cached. Forever, or at least until the next rebuild.</p><p>And search engine crawlers read HTTP status codes literally. A 200 means “valid, indexable page.” A crawler hitting /blogs/blogs or /blogs/.jpg,.jpeg,.webp,.png and getting a 200 back has no reason to ever stop checking it — as far as it knows, that page is real and might be updated later. So it comes back. Again. And discovers more dead links along the way, probing more URLs, generating more 200s, generating more permanently cached &quot;not found&quot; pages.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/672/1*0EStkMgH98YCe0jtG9LPeA.png" /></figure><p>That loop, running quietly in the background for weeks, is a complete explanation for everything we’d measured:</p><ul><li>The 5.9GB cache — one permanent file per unique bad slug a crawler ever tried.</li><li>The CPU load — every one of those first-time hits meant a full render, a fresh backend fetch, and a cache write, all CPU work, all for a page nobody would ever actually read.</li><li>The failing API log spam — every crawl of a dead slug meant another doomed request to the backend.</li><li>The connection pile-up we found later with ss -tnp | grep &#39;127.0.0.1:3000&#39; — dozens of SYN-SENT connections from Nginx that never completed their handshake, because the Next.js process was too busy grinding through cache work to accept them promptly. Only two connections in the whole list were actually ESTABlished. That&#39;s the site-feels-slow symptom, explained at the TCP level.</li></ul><h3>The fix: tell the truth</h3><pre>import { notFound } from &quot;next/navigation&quot;<br><br>export default async function BlogDetailPage({ params }: PageProps) {<br>  const { slug } = await params;<br>  const blog = await getBlogData(slug);<br><br>  if (!blog) {<br>    notFound();<br>  }<br><br>  return &lt;BlogDetailClient blog={blog} /&gt;;<br>}</pre><p>The nice “Blog Not Found” screen didn’t disappear — it moved to where Next.js expects it, a not-found.tsx file in the same route folder, which renders automatically whenever notFound() is called. Visually, nothing changes for a human visitor. But now the HTTP response, it is a real, honest 404. Crawlers stop treating dead links as living pages. Next.js stops caching them as if they were real. Same change, mirrored, in the course detail page next to it.</p><p>One function call. That was the entire fix.</p><h3>Did it actually work?</h3><p>We didn’t take that on faith — we measured, before and after.</p><p>Restart behavior. A watchdog script had been quietly auto-restarting the app every time CPU sat above 80% for five straight minutes:</p><pre>CPU_LIMIT=80<br>CHECK_INTERVAL=5<br>MAX_HIGH_COUNT=60   # 60 × 5s = 5 minutes sustained</pre><p>That’s what “needs a restart every 2–3 days” actually was — an automated safety net catching the fire, not the fire going out on its own. After the fix shipped:</p><pre>id  name        uptime  ↺   status   cpu    mem<br>1   app         21h     56  online   0%     161.2mb<br>0   appindia    18h     84  online   0%     77.3mb</pre><p>18 hours, uninterrupted, 0% CPU. The restart counter hadn&#39;t moved since the deploy — meaning the watchdog hadn&#39;t needed to intervene once in that window, something that used to happen every 2–3 days without fail.</p><p>The cache. The deploy required a rebuild, which reset .next/cache from scratch — so the honest comparison isn&#39;t &quot;5.9GB vs. 0,&quot; it&#39;s the growth <em>rate</em> going forward:</p><pre>$ du -sh .next/cache &amp;&amp; find .next/cache -type f | wc -l<br>2.4G<br>25601</pre><p>Down from 5.9GB, and now being watched over the following days to confirm it plateaus at the size of real content, instead of climbing without end again.</p><h3>What this was really about</h3><p>Nothing here was exotic. No memory corruption, no kernel bug, no infrastructure failure. A well-meaning UX decision — “let’s show a nicer message instead of a bare 404” — combined with a caching strategy that trusted HTTP status codes at face value, quietly turned ordinary web-crawler traffic into a slow-motion resource leak that took weeks to notice and about an hour of perf, strace, and grep to actually explain.</p><p>The lesson that’s easy to forget in a good UI: a 404 page is still supposed to say 404. The moment it doesn’t, every system downstream that trusts HTTP status codes — caches, crawlers, monitoring — starts making decisions based on a lie, and the consequences show up somewhere you’d never think to look first: a CPU graph, at 3am, with barely any traffic in sight.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba4f3f4ae107" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[50 Threads Missed the Same Cache Key. I Expected 1 Database Query. I Got 50.]]></title>
            <link>https://medium.com/@sahilgupta_33409/50-threads-missed-the-same-cache-key-i-expected-1-database-query-i-got-50-30a01d9a97b6?source=rss-3db08a9fcea3------2</link>
            <guid isPermaLink="false">https://medium.com/p/30a01d9a97b6</guid>
            <category><![CDATA[dogpile-problem]]></category>
            <category><![CDATA[hotkeys]]></category>
            <category><![CDATA[thundering-herd-problem]]></category>
            <category><![CDATA[design-systems]]></category>
            <dc:creator><![CDATA[Sahil Gupta]]></dc:creator>
            <pubDate>Tue, 18 Aug 2026 07:41:52 GMT</pubDate>
            <atom:updated>2026-08-18T07:41:52.600Z</atom:updated>
            <content:encoded><![CDATA[<p><em>A small, measured experiment in why “add TTL jitter” and “add single-flight” are not the same fix.</em></p><p>I had a simple question: when a <strong>hot</strong> cache key expires under load, which one actually protects the database — TTL jitter, or single-flight ?</p><p>This failure mode goes by a few names — <strong>cache stampede</strong>, <strong>thundering herd</strong>, <strong>dogpile effect</strong> — and they all describe the same thing.</p><p>HLD checklists usually list TTL jitter and single-flight both under “cache stampede mitigations,” in the same bullet list, like they’re interchangeable. I wanted numbers, not a checklist. So I built a small Java lab, held the key count at exactly one, and changed only the miss handler.</p><h3>The setup</h3><ul><li>Java 25, Jedis 5.2.0, redis:7-alpine</li><li>One Redis key, product:42</li><li>A fake origin (FakeDb) that sleeps 50ms per query — so the origin cost is exact and I&#39;m not fighting real network jitter</li><li>50ms client threads hammering get(key) in a tight loop, 50 threads, 8 seconds per run</li><li>Redis TTL: 2000ms</li><li>The only thing that changes between runs is the <strong>miss handler</strong>:</li><li><strong>A — naive:</strong> on miss, load DB, then SET</li><li><strong>B — jitter:</strong> same, but SET with TTL = 2000ms ± 400ms</li><li><strong>C — single-flight:</strong> on miss, one thread wins a local race and rebuilds; the other 49 join() its CompletableFuture instead of touching the DB</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/724/1*1jDFOFPTfrw1nA66J42Gcg.png" /></figure><p>Cache-aside itself never changes. Redis GET/SET stays intentionally racy in all three runs — that race is the whole point.</p><h3>Why cache-aside isn’t enough for a hot key</h3><p>Cache-aside looks correct on a whiteboard: GET, on miss load from DB, then SET. That&#39;s fine for a key nobody&#39;s fighting over. The problem is that &quot;check&quot; and &quot;load&quot; are not one atomic step.</p><pre>t = 0.0 ms   Redis: product:42 is GONE<br>t = 0.1 ms   T1 GET → null   (decides: miss, will load DB)<br>             T2 GET → null<br>             T3 GET → null<br>             Nobody has called SET yet. Al l three already passed the miss check.<br>t = 0.1–50ms T1, T2, T3 are ALL inside FakeDb.query() — not waiting at Redis<br>t = 50.1 ms  T1 SET. Redis now has the value. Too late for T2 and T3 —<br>             they already left Redis and committed to the DB call.</pre><p>A thread that has already heard “miss” is not waiting on your SET. It&#39;s walking to the database. There is no lock between &quot;Redis said no&quot; and &quot;I will now go load it,&quot; so however many threads clear that check before the first SET lands, that&#39;s how many identical queries the origin sees.</p><h3>What actually happened</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/605/1*0N3BCP9MihjnrbniaiEFXw.png" /></figure><p>Same fixture. Same three expiries (~t=2.0s, 4.0s, 6.1s). Only the miss handler changed.</p><p><strong>A — naive.</strong> Three TTL expiries, three waves of exactly 50 identical DB queries. No coalescing at all: 50 threads × 3 waves = 150 misses = 150 DB queries.</p><p><strong>B — jitter.</strong> The middle wave moved from t=4.0s to t=3.8s — the random ±400ms on SET actually took effect. But there&#39;s still one key, so there&#39;s still one TTL, so there&#39;s still exactly one expiry moment. Peak stayed 50. Total stayed 150. Jitter changed <em>when</em> the stampede happened, not <em>how wide</em> it was.</p><p><strong>C — single-flight.</strong> Same 150 Redis misses — jitter and single-flight both still see the key expire and 50 threads notice. But only 3 threads (one per wave) ever reach FakeDb. The other 147 join the winner&#39;s future and get the answer for free.</p><h3>The part that should worry you: the dashboards looked fine</h3><p>Hit rate in run A: <strong>99.9700%</strong>. p99 in run A: <strong>16ms</strong>. Those are the numbers on-call would see on a normal dashboard, and they’re honest — almost every one of the ~500,000 requests really was a healthy cache hit. The disaster was 150 calls out of 500,000, or about 0.03% of traffic. That’s far too small a slice to move a blended p99, and hit rate is dominated by the same healthy majority.</p><p>The one number that told the truth was <strong>database queries per 100ms</strong>. Watched at that resolution, run A shows a spike from ~0 to 50 exactly at each TTL boundary. Watched at a one-minute resolution, or through Redis hit rate, or through blended latency percentiles, it’s invisible.</p><p>If I only had those two dashboards, I would have shipped the naive version and called it healthy.</p><h3>Three names people mash together</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*N0MyXifi8GpYx3d1gk45FQ.png" /></figure><p>This experiment never created a second key. It hammered one key with 50 threads. There was nothing for jitter to spread. Don’t say “add jitter and single-flight” as if they’re the same mitigation — jitter is the fix for a different failure mode.</p><h3>Single-flight is a waiting room, not a second cache</h3><p>The in-process fix is small: a ConcurrentHashMap&lt;String, CompletableFuture&lt;String&gt;&gt;. On a miss, a thread does putIfAbsent — whoever gets null back is the winner and calls the database; everyone else gets the existing future back and calls .join() on it.</p><p>The map is <em>not</em> a cache, on purpose:</p><ol><li>Redis is still checked first. Hits never touch the map.</li><li>An entry exists only while a rebuild is in flight.</li><li>finally { inflight.remove(key, mine) } deletes the entry the instant the winner finishes.</li></ol><p>Waiters still wait roughly the origin’s latency — single-flight doesn’t make a miss <em>faster</em>, it makes the origin see one query instead of fifty. (Making misses fast too is a separate technique — stale-while-revalidate — that I didn’t build here.)</p><h3>The one JVM caveat</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/722/1*yIs10UapC35cjhTV9DIiUA.png" /></figure><p>This single-flight is a field on one Java object living in one heap. One process, one map, one winner. Run the same code on two pods, and each pod elects its own local winner — Redis still ends up seeing two identical queries instead of one, because pod B has no way to join() pod A&#39;s future.</p><p>For a real fleet, the lock has to live somewhere all the pods can see it — Redis itself:</p><pre>SET lock:product:42 1 NX EX 5</pre><ul><li>NX — only the first caller in the whole fleet gets the lock.</li><li>EX 5 — the lock expires on its own if the winner crashes, instead of staying stuck forever.</li></ul><p>Every other pod’s SET NX comes back nil; those pods don&#39;t touch the database — they retry the GET, wait, or serve something stale. It&#39;s still worth keeping the local, in-JVM single-flight in front of this: you don&#39;t want 50 threads inside one pod each independently attempting the Redis lock. Local single-flight to pick one attempt per pod, then a Redis lock to pick one winner across all pods.</p><p>I didn’t run the two-JVM version in this session — it’s the natural next experiment, and I’d expect in-process-only single-flight to leak exactly one extra query per additional pod.</p><h3>What I’d actually say in an interview</h3><p>A cache stampede — also called thundering herd, or the dogpile problem — is a hot key expiring while every in-flight request misses it at once, so the database gets the same query many times over. Cache-aside’s GET-then-load-then-SET is a race, not a lock — threads that already got a miss are already headed to the database before the first SET can help them. TTL jitter fixes a different problem (many keys expiring together); for one hot key, you need single-flight, and for more than one process, that single-flight has to be backed by a lock that lives in Redis, not in a single JVM’s heap. And don’t trust hit rate or blended p99 to catch this — watch origin queries-per-second in short time buckets instead.</p><h3>What’s next</h3><p>Not done in this session, on purpose — each is its own experiment:</p><ol><li>SET lock:key NX EX 5 across two real JVMs, to actually measure the &quot;N pods, N leaked queries&quot; claim instead of asserting it.</li><li>Cache avalanche with 1,000 keys and aligned TTLs vs. jittered TTLs — where I’d expect jitter to actually win.</li><li>Stale-while-revalidate, to see whether miss-path p99 can stay cache-fast instead of paying the full origin latency.</li><li>Wiring FakeDb behind a size-2 connection pool, so a stampede turns into pool-wait instead of raw query count.</li></ol><p><em>Setup: Java 25, Jedis 5.2.0, </em><em>redis:7-alpine, 50 threads, one key, 50ms origin, 2s TTL, 8s runs. All numbers above are from the actual runs — nothing in this post is simulated or estimated.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=30a01d9a97b6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The CloudWatch Bill Came From an RDS Instance That describe-db-instances Said Did Not Exist]]></title>
            <link>https://medium.com/@sahilgupta_33409/the-cloudwatch-bill-came-from-an-rds-instance-that-describe-db-instances-said-did-not-exist-ab5c00da9ce1?source=rss-3db08a9fcea3------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab5c00da9ce1</guid>
            <category><![CDATA[production-issue]]></category>
            <category><![CDATA[cloudwatch-logs]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[aws-billing]]></category>
            <category><![CDATA[aws-rds]]></category>
            <dc:creator><![CDATA[Sahil Gupta]]></dc:creator>
            <pubDate>Sun, 16 Aug 2026 05:18:56 GMT</pubDate>
            <atom:updated>2026-08-16T05:18:56.159Z</atom:updated>
            <content:encoded><![CDATA[<h3>Hook</h3><p>I launched a small EC2 box, installed Docker by hand, and did <strong>not</strong> turn on CloudWatch Agent or EC2 Detailed Monitoring.</p><p>The bill still had a CloudWatch line I did not recognize:</p><p>USE2-CW:MetricMonitorUsage</p><p>Ohio. Metric storage. Roughly <strong>$0.30 per metric-month</strong> after the first ten free metrics.</p><p>The numbers were not a rounding error:</p><p>PeriodMetric-months (as billed)CloudWatch MetricMonitorUsageLate June 2026~30.73<strong>$6.22</strong>July 2026116.00<strong>$31.80</strong>August 1–16 202654.26<strong>~$13.28</strong></p><p>July is internally consistent with AWS’s published custom-metric price: the first 10 metric-months are free, then (116 - 10) × $0.30 = $31.80.</p><p>I had not published custom metrics. Cost Explorer grouped the entire charge under one operation: <strong>MetricStorage</strong>. It did not name a namespace, a resource, or a feature.</p><p>This article is the autopsy of that bill: why the usual CloudWatch debugging checklist can honestly return “nothing is wrong,” why CloudTrail and billing can disagree about PutMetricData, and how a <strong>second RDS control plane</strong> — RDS Preview — kept a PostgreSQL instance alive while the normal RDS API reported an empty account.</p><h3>The system (what was actually running)</h3><p>The intended setup was small:</p><ul><li>One EC2 instance in us-east-2</li><li>Docker installed manually (not ECS/EKS)</li><li>A learning app that needed PostgreSQL</li><li>No CloudWatch Agent</li><li>No EC2 Detailed Monitoring</li><li>No instance IAM profile</li></ul><p>There had also been RDS activity earlier in the month. Billing showed db.t4g.micro instance-hours and GP2 storage in Ohio. The RDS console and the normal CLI later showed <strong>zero</strong> instances.</p><p>That combination is what made the CloudWatch line feel like a ghost: compute I recognized, monitoring I thought I had never enabled, and a database I could no longer see.</p><h3>The real problem: CloudWatch billing names the storage, not the publisher</h3><p>CloudWatch custom metrics are billed as <strong>metric-months</strong>, not as “I opened the CloudWatch console.”</p><p>A few distinctions that matter:</p><p><strong>Basic EC2 metrics are not this charge.</strong> CPU, network, status checks on a default EC2 instance are not what MetricMonitorUsage is for.</p><p><strong>list-metrics is a catalog, not a bill.</strong> This account had ~1,245 metric <em>definitions</em> in Ohio. Most of them were AWS service namespaces (AWS/Usage, AWS/Config, AWS/EC2, …). AWS exposes those namespaces in the console. Seeing 497 AWS/Config definitions does not mean Config is running, and it does not mean those definitions are what you pay for.</p><p><strong>Cost Explorer stops at </strong><strong>MetricStorage.</strong> Grouping CloudWatch cost by API operation produced:</p><p>OperationUnblended cost (July window in the investigation)MetricStorage~$26.70 and rising with the monthPutMetricData$0.00ListMetrics$0.00PutDashboard$0.00</p><p>So the bill said: <em>you are storing billable metrics.</em> It did not say <em>which ones.</em></p><p><strong>Metric-months survive deletion of the resource.</strong> 84.172 metric-months is not “84 metrics exist right now.” It is usage over time. Delete the publisher mid-month and you still pay for the fraction of the month the metrics existed.</p><p>Headline lesson:</p><blockquote><em>CloudWatch cost RCA is not “find the CloudWatch setting and turn it off.” It is “find the publisher of billable metric series.” The console you are staring at may not be the control plane that owns that publisher.</em></blockquote><h3>Naive approach: debug CloudWatch as if it were an EC2 monitoring feature</h3><p>The first checklist is the one every AWS cost post recommends. I ran it. It was not wrong. It was incomplete.</p><p>CheckResultEC2 Detailed MonitoringDisabledamazon-cloudwatch-agentUnit not foundECS / EKS / Container InsightsNot usedCloudWatch Logs metric filtersNot the storyAWS Config configuration recorderEmptyCustom namespaces (anything not AWS/…)NoneEC2 instance profileNone — the box could not call PutMetricData via instance roleaws rds describe-db-instances[]aws rds describe-db-clusters[]aws rds describe-db-snapshots[]CloudTrail PutMetricData event historyEvents: []</p><p>At that point a reasonable engineer concludes: leftover metric-months from a deleted RDS, wait for next month, open a support case if it continues.</p><p>That conclusion would have been wrong. The charge was still growing in August. Something was still publishing.</p><h3>Failure: two observability systems told two different truths</h3><p>Billing analysis (from AWS Support) said PutMetricData was active:</p><ul><li>June: 12,011 calls</li><li>July: 48,944 calls</li><li>August 1–16: 25,720 calls</li></ul><p>CloudTrail event history in the same region, filtered to PutMetricData, returned nothing.</p><p>Both can be true at once:</p><ul><li>CloudTrail <strong>management event</strong> history is not a complete meter of every CloudWatch data-plane write.</li><li>AWS services can publish metrics without leaving a console-shaped PutMetricData breadcrumb for the account owner.</li><li>Cost Explorer’s PutMetricData <strong>cost</strong> being $0.00 also does not prove the API is idle. Request volume can be free while <strong>storage of unique metric series</strong> is not.</li></ul><p>The failure mode is methodological:</p><blockquote><em>If your RCA tool cannot see service-side metric publication, an empty CloudTrail result is not evidence that publication is not happening. It is evidence that </em>that tool<em> did not record it.</em></blockquote><p>Meanwhile list-metrics <em>did</em> show something unusual for a personal EC2-and-Docker account:</p><ul><li>AWS/RDS-Preview — 112 metric definitions</li><li>IAM role AWSServiceRoleForRDSPreview created <strong>2026-06-23</strong></li><li>Same role LastUsedDate <strong>2026-08-16</strong>, region us-east-2</li><li>CloudWatch MetricMonitorUsage started around <strong>June 23</strong></li></ul><p>The normal RDS API still said there were no databases.</p><h3>Investigation: follow the role, not the console</h3><p>The useful signal was not a CloudWatch dashboard. It was IAM.</p><p>Roles that existed in the account included service-linked roles for Config, DevOps Guru, Observability Admin, RDS, <strong>RDS Preview</strong>, and an rds-monitoring-role trusted by monitoring.rds.amazonaws.com.</p><p>rds-monitoring-role proved Enhanced Monitoring had been configured at some point (created June 7, attached AmazonRDSEnhancedMonitoringRole). That was a clue, not the cause. The live Preview instance later showed MonitoringInterval: 0 — Enhanced Monitoring was <strong>not</strong> on.</p><p>The role that matched the bill’s start date was AWSServiceRoleForRDSPreview:</p><ul><li>Created within one second of a CloudTrail CreateDBInstance for a database identifier</li><li>Still used in August</li><li>Trusted by rds-preview.amazonaws.com</li></ul><p>CloudTrail for that database identifier showed CreateDBInstance on June 23. It did <strong>not</strong> show DeleteDBInstance in the returned window.</p><p>ARN prefix on the created resource was arn:aws:rds-preview:…, not arn:aws:rds:….</p><p>That is a different service partition of RDS, not a stale metric from a deleted standard instance.</p><p>The instance was visible only after using the Preview control plane:</p><ul><li>Console: RDS Preview home for us-east-2 (separate from the standard RDS console)</li><li>CLI: aws rds describe-db-instances with --endpoint-url pointed at the <strong>RDS Preview</strong> endpoint</li></ul><p>Then the “missing” database was simply available: PostgreSQL, db.t4g.micro, created June 23, publicly reachable on the Preview hostname, Database Insights mode standard.</p><p>Standard CLI without that endpoint continued to return empty. That is not a lag. It is two APIs.</p><pre>Billing:  USE2-CW:MetricMonitorUsage  (MetricStorage)<br>                │<br>                ▼<br>CloudWatch namespace:  AWS/RDS-Preview<br>                │<br>                ▼<br>IAM:  AWSServiceRoleForRDSPreview  (created Jun 23, last used Aug 16)<br>                │<br>                ▼<br>CloudTrail:  CreateDBInstance  on rds-preview ARN<br>                │<br>                ▼<br>Normal RDS API:  DBInstances = []     ← wrong control plane<br>Preview RDS API: database still available</pre><h3>Root cause</h3><p><strong>An RDS Preview PostgreSQL instance kept running in </strong><strong>us-east-2 after I stopped looking at it in the normal RDS console.</strong></p><p>Consequences that are supported by the evidence:</p><ol><li>The Preview instance is not returned by aws rds describe-db-instances against the default RDS endpoint.</li><li>CloudWatch still had an AWS/RDS-Preview namespace with on the order of 100 metric series — the same order of magnitude as July&#39;s 116 billed metric-months.</li><li>USE2-CW:MetricMonitorUsage started when the Preview instance and its service-linked role were created (June 23).</li><li>After the Preview instance was deleted, list-metrics --namespace AWS/RDS-Preview returned no metrics.</li></ol><p>What is <strong>not</strong> claimed:</p><ul><li>That RDS Enhanced Monitoring was the billing mechanism. The instance had MonitoringInterval: 0.</li><li>That every one of the 1,245 catalogued metrics was billable. Most were not.</li><li>That CloudTrail is broken. It recorded the create. It did not record the CloudWatch data plane the way the billing meter did.</li><li>That standard RDS metrics on a normal aws rds instance would have produced the same MetricMonitorUsage line. AWS documents standard RDS metrics as included; this incident is specifically the <strong>Preview</strong> resource plus billable CloudWatch metric storage. The precise AWS-internal classification of AWS/RDS-Preview series as &quot;custom&quot; vs &quot;Preview-specific&quot; was not independently proven beyond Support tying the usage to that resource and the metrics disappearing after delete.</li></ul><p>The engineering root cause is simpler than the billing SKU:</p><blockquote><em>I queried the wrong control plane, trusted an empty list as “resource gone,” and paid CloudWatch to store metrics for a database that was still up.</em></blockquote><h3>Solution</h3><p>Do not disable CloudWatch. Do not delete service-linked roles because they look unused. Delete or stop the <strong>publisher</strong>.</p><p>In this case that meant terminating the Preview DB instance through the Preview endpoint/console, not through standard RDS.</p><p>After delete:</p><ul><li>Preview describe-db-instances eventually empty</li><li>AWS/RDS-Preview metric catalog empty</li><li>Already-incurred June/July/August metric-months do not refund</li><li>Future MetricMonitorUsage should stop growing once no new billable series are stored</li></ul><p>Leave AWSServiceRoleForRDSPreview alone until AWS stops using it. Service-linked role deletion is not the cleanup; resource deletion is.</p><h3>Trade-offs</h3><p><strong>RDS Preview vs normal RDS.</strong> Preview exists to try newer engine versions. It is a separate environment, separate console, separate endpoint, and AWS may auto-delete Preview instances after a documented window. It is the wrong default for “I need Postgres for a side project.” Normal RDS has instance + storage cost. It should not, by itself, recreate this CloudWatch MetricMonitorUsage pattern for standard metrics.</p><p><strong>Postgres on the EC2 Docker host vs managed RDS.</strong> For a throwaway learning app, local Docker Postgres has no second control plane to forget. You lose managed backups, Multi-AZ, and the RDS operational lessons. That is a valid trade for cost control. It is a bad trade if the point of the exercise was to learn RDS.</p><p><strong>Enhanced Monitoring / Performance Insights / Database Insights.</strong> These are the knobs that <em>can</em> create extra monitoring cost on <strong>normal</strong> RDS. They were red herrings here. Checking them is still correct. Treating a matching IAM role as proof of current billing is not.</p><p><strong>Asking Support early vs finishing CLI RCA.</strong> Support could see metric ownership that Cost Explorer will not show. The CLI work still mattered: it produced the Preview ARN, the role timeline, and the exact question to ask. “Why is CloudWatch charging me?” is a weak ticket. “Trace USE2-CW:MetricMonitorUsage to namespace/resource; Preview role last used today; standard RDS API empty&quot; is a strong one.</p><h3>What the numbers actually taught</h3><p>The bill was internally consistent with <strong>~100 extra metric-months above the 10 free</strong>, not with “CloudWatch is expensive” as a vibe.</p><p>That is why AWS/RDS-Preview with 112 definitions was the right suspect once EC2, agent, Config, and custom namespaces were gone. The catalog size finally lined up with the meter.</p><p>AWS/Config at 504 definitions and AWS/Usage at 471 did not line up with the dollar amount in the same way, and Config had no recorder. Those namespaces are how you waste a day if you sort list-metrics by count instead of by &quot;which of these are billable series from a resource I still own?&quot;</p><h3>Lessons</h3><ol><li><strong>Empty </strong><strong>describe-* means &quot;not in this API,&quot; not &quot;does not exist.&quot;</strong> Preview, Outposts, another region, another account, and a deleted-but-still-metered resource are different failures. This one was Preview.</li><li><strong>Billing SKUs are RCA handles.</strong> USE2-CW:MetricMonitorUsage + operation MetricStorage means stored billable metric series in Ohio. It does not mean &quot;CloudWatch is enabled on the EC2.&quot;</li><li><strong>Service-linked </strong><strong>LastUsedDate is evidence.</strong> A role used today while the matching describe is empty is a control-plane mismatch, not a reason to delete-role.</li><li><strong>CloudTrail absence is not a meter.</strong> Use it to reconstruct creates/deletes. Do not use management-event history as proof that PutMetricData is idle.</li><li><strong>Do not disable the observer.</strong> Disable or delete the publisher. Deleting metric definitions while the resource is still available does not stop the next metric-month.</li><li><strong>Region in the bill is the region to debug.</strong> USE2 is Ohio. Debugging Mumbai because that is where you usually click is a separate, very common way to get a false &quot;I can&#39;t find any metrics.&quot;</li></ol><h3>Further questions</h3><ul><li>After Preview deletion, does MetricMonitorUsage go to ~$0 in the next full billing month, or do leftover series linger as metric-months?</li><li>Are AWS/RDS-Preview series billed because they sit outside the standard AWS/RDS included-metrics policy, or because Preview publishes additional dimension cardinality?</li><li>Would CloudTrail <strong>data events</strong> for CloudWatch have shown the publisher that management events missed?</li><li>What is the smallest Cost and Usage Report (CUR) query that attributes MetricMonitorUsage to a resource ID when Cost Explorer will not?</li></ul><p>Those are the next experiments. This incident already paid for the first lesson: <strong>if the bill names a resource family you cannot see, you are probably on the wrong endpoint.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab5c00da9ce1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Changing a Kafka group.id Is Not a Rename — How an Unconditional seek() Replayed Our Delay Queue]]></title>
            <link>https://medium.com/@sahilgupta_33409/changing-a-kafka-group-id-is-not-a-rename-how-an-unconditional-seek-replayed-our-delay-queue-682810e4984d?source=rss-3db08a9fcea3------2</link>
            <guid isPermaLink="false">https://medium.com/p/682810e4984d</guid>
            <category><![CDATA[system-design-concepts]]></category>
            <category><![CDATA[production-issue]]></category>
            <category><![CDATA[kafka]]></category>
            <dc:creator><![CDATA[Sahil Gupta]]></dc:creator>
            <pubDate>Wed, 12 Aug 2026 18:44:07 GMT</pubDate>
            <atom:updated>2026-08-12T18:46:02.799Z</atom:updated>
            <content:encoded><![CDATA[<h3>The Incident</h3><p>While migrating a Java 8 service to Java 25, I noticed a Kafka issue: all consumers were using the same group ID. When one consumer failed, Kafka triggered a rebalance for the entire consumer group, affecting all consumers.<br>So planing to move Kafka consumers from one shared legacy consumer group onto <strong>per-topic</strong> groups — without losing the backlog the old group had not finished.</p><p>The dangerous part was not the backlog itself.</p><p>It was that a <strong>brand-new</strong> group.id has <strong>no committed-offset history</strong>. Left to the Kafka Java client default, auto.offset.reset=latest, the new group can <strong>silently skip</strong> everything still in the topic. No exception. Just missing work.</p><p>So we bridged progress manually: read the old group’s committed offsets, then seek() the new group to that point inside ConsumerRebalanceListener.onPartitionsAssigned.</p><p>That bridge worked — once.</p><p>Then a routine rebalance between two replicas ran the same seek() again, reset the fetch position to the original seed, and a small batch of already-forwarded messages was forwarded a second time.</p><p><strong>This article explains what went wrong with the Kafka bug, what Kafka really guarantees when different group IDs are used, and why putting “run once” code in rebalance listeners can cause problems. It also explains how to fix it by checking saved group state, and how we tested the fix using multiple processes running at the same time — not just by restarting one process</strong></p><h3>The system (what was migrating)</h3><p>The service is a <strong>delay-queue </strong>:</p><ol><li>Producers publish into <strong>time-bucket delay topics</strong> .</li><li>The consumer holds work until the delay elapses.</li><li>It then <strong>forwards</strong> the message to a destination topic.</li></ol><p>Design constraints that matter to this story:</p><ul><li>Each delay topic was provisioned with <strong>exactly one partition</strong>, deliberately — so Kafka’s per-partition ordering matches the delay queue’s FIFO expectation.</li><li>Production ran <strong>two replicas(Pod)</strong> of the consumer process, sharing one consumer group <strong>per topic</strong> after the migration.</li><li>Before the migration, a <strong>single legacy shared consumer group</strong> spanned the delay topics.</li></ul><p>The confirmed goal was to <strong>migrate without losing unprocessed messages</strong>. Using separate topic groups may also help isolate failures, but we don’t have proof that this was the reason for the change.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IX_dTDKSVSCwngrHS5llcw.png" /></figure><h3>The real problem: Kafka does not continue progress across group.id</h3><p>Committed offsets are keyed by (group.id, topic, partition) in __consumer_offsets topic.</p><p>Kafka does not know that “this group is the continuation of that other group.”</p><p>If you create a <strong>new group ID</strong>, Kafka treats it like a <strong>new consumer starting fresh</strong>, even if the code and application are the same.</p><p>So the decision sequence for “will the new group see the old backlog?” is not one rule. It is gates:</p><p><strong>Gate 0 — Does the data still physically exist?</strong><br>Retention (retention.ms / retention.bytes, or compaction) may already have deleted bytes. If the log segment is gone, no group id recovers it. Topic retention for the affected topics in the original incident was <strong>not confirmed</strong> in our reconstruction — so this article does not claim the backlog was or was not retention-safe; it only states that Gate 0 is always real.</p><p><strong>Gate 1 — Does this exact </strong><strong>group.id have a valid committed offset for this </strong><strong>(topic, partition)?</strong><br>A brand-new group fails this gate.</p><p><strong>Gate 2 — If Gate 1 fails, what does </strong><strong>auto.offset.reset say?</strong></p><p>ValueEffect when no valid commitlatest (client default)Seek to log end → <strong>skip current backlog</strong>, silentlyearliestConsume from oldest retained offset → often <strong>reprocess far more</strong> than the unprocessed gapnoneFail closed — force explicit handling</p><p><strong>Override — application </strong><strong>seek() wins</strong> over both Gate 1 and Gate 2 for the in-memory fetch position.</p><p>Headline lesson:</p><blockquote><em>Changing </em><em>group.id is not a configuration rename. It is operationally equivalent to onboarding a </em><strong><em>new</em></strong><em> consumer against the same log. Continuity of progress must be built deliberately.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n1q6rrt0_3nhozcAhHMerw.png" /></figure><h3>Naive approach: seed once via seek() on partition assignment</h3><p>The approach we implemented:</p><ol><li>Before cutover, inspect the <strong>old</strong> group’s committed offset per topic.</li><li>Store a small map: topic → starting offset.</li><li>In onPartitionsAssigned, call seek() to that stored offset so the <strong>new</strong> group starts at the gap — not after it (latest) and not at the entire retained history (earliest).</li></ol><p>Why this looked attractive:</p><ul><li>Precise: start at the unprocessed tail.</li><li>Ships with the code deploy.</li><li>Avoids relying on a silent latest skip.</li></ul><p>The map was a <strong>single scalar per topic</strong>. That is only safe because these topics had <strong>one partition</strong>. Multi-partition topics would need per-partition seeds. That limitation was identified in the incident record; it is a design constraint, not a hypothetical.</p><h3>Failure: the seed was unconditional</h3><p>The seek() ran <strong>every time</strong> onPartitionsAssigned ran — not only the first time the new group needed a bootstrap.</p><p>With two replicas sharing a group, membership changes are ordinary: deploys, restarts, scaling events. Under an <strong>eager</strong> rebalance protocol (stop-the-world revoke and reassign — as opposed to cooperative sticky assignment), a membership change causes partitions to be revoked and reassigned. onPartitionsAssigned fires again.</p><p>Causal chain (important precision):</p><ol><li>seek(seed) moves the consumer’s <strong>in-memory fetch position</strong> backward.</li><li>The consumer re-fetches and the application <strong>re-forwards</strong> records it had already handled.</li><li>As processing continues, commits advance again — overwriting the progress that had been real.</li></ol><p>The rebalance itself did <strong>not</strong> delete committed offsets. The defect was <strong>application logic</strong> reacting to rebalance by repositioning as if cutover were happening again.</p><p><strong>What we observed:</strong></p><ul><li>The same “seeking to offset N” log line appeared <strong>twice</strong>, roughly <strong>24 seconds</strong> apart — once associated with each replica’s assignment path — with the consumer group <strong>generation</strong> incrementing by one between the events. Generation bumps are strong evidence a rebalance completed.</li><li>Downstream: on the order of a <strong>dozen</strong> already-forwarded messages were forwarded to the destination topic a second time.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jRgt1y6fF8N-yWJsfUjFwg.png" /></figure><p>That scale matters for honesty: this was a real correctness incident, not a theoretical race, and it was small enough to notice in destination traffic — not framed here as a multi-hour outage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*xLw-OOEWub8bhouDUq5B1w.png" /></figure><h3>Investigation standard: single-process restart is the wrong test</h3><p>Reasoning through the code was not treated as sufficient.</p><p>We stood up a <strong>real local Kafka broker</strong> (Dockerized ZooKeeper + Kafka images for that lab) and raced <strong>two genuinely separate OS processes</strong> into the same consumer group to force an actual rebalance.</p><p>Deliberately <strong>not</strong> used as a stand-in: restarting a single process. That does not reproduce the multi-member rebalance behavior that made the bug routine in production.</p><p>Verification was against:</p><ul><li>consumer-group offset inspection, and</li><li><strong>real destination topic contents</strong></li></ul><p>— not “the log line looked right.”</p><p>This failure mode produces <strong>no consumer error</strong>. Quietly wrong behavior is the signature. Lag charts and destination duplicates matter more than green health checks.</p><h3>Root cause (one sentence)</h3><p><strong>One-time bootstrap was implemented as unconditional </strong><strong>seek() on every partition assignment, so ordinary multi-replica rebalances replayed from a fixed seed.</strong></p><p>Underlying misconceptions this exposed:</p><ol><li>“New group id” somehow inherits old progress — it does not.</li><li>“onPartitionsAssigned runs once at startup” — it runs on the rebalance lifecycle.</li><li>“Rebalances are rare edge cases with two replicas” — they are normal.</li><li>“Duplicates mean Kafka at-least-once misbehaved” — here duplicates were <strong>logic-driven replay</strong>, a separate mechanism from crash-after-process-before-commit.</li></ol><h3>The fix that shipped</h3><p>Guard the seek():</p><p>Only seek if there is <strong>no existing committed offset</strong> for that partition — checked via the consumer’s committed(...) API against the group’s real offset state.</p><p>API precision (from our research pass): committed(Set&lt;TopicPartition&gt;) returns a map whose <strong>per-partition entries</strong> may be null when nothing is committed. The check is “is this partition’s committed entry absent?”, not “did the whole call return null?”</p><p>Why this works across replicas without extra coordination:</p><ul><li>Committed offsets are <strong>group-level</strong> state (Kafka’s internal offset-tracking topic), not per-process memory.</li><li>Whichever replica evaluates the guard next gets the same answer.</li><li>After the first successful bootstrap + commit path establishes a real commit, later rebalances see non-null committed state and skip seek().</li></ul><p>The bootstrap is intended to be <strong>temporary</strong>: once the new group has its own committed history, a follow-up change removes the rebalance-driven positioning logic entirely and returns to a plain subscription.</p><p>We are not claiming production rollback runbooks or staged-rollout step lists beyond what the record confirms: the guard was shipped as a fix to the existing bootstrap; removal of the bootstrap later was planned. Detailed staged sequences discussed in internal strategy notes that are marked retrospective/[VERIFY] are not repeated here as historical fact.</p><h3>Trade-offs: what we did vs what we could have done</h3><p>These alternatives are useful for readers. Only Option 3 (unguarded, then guarded) is confirmed as the path pursued end-to-end in the original migration. Others include natural alternatives and retrospective analysis — not claimed as contemporaneous debate unless noted.</p><p>OptionBehaviorUpsideDownside1. Do nothing (latest)Skip current backlogZero workSilent data loss relative to requirement2. earliestReplay retained historySimple configLarge duplicate blast radius; not “just the gap”3. App seek() bootstrap (what we did)Precise gap startShips with codeMust be idempotent under rebalance — we learned this the hard way4. Admin/out-of-band seed before first member joinsNew group already has a valid commitEntire seek-in-listener bug class becomes structurally harderHuman/runbook step separate from deploy; easy to forget under pressure</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*izXvcVYlTXl02iczOkHFUA.png" /></figure><p>Option 4 is especially interesting as a <strong>structural</strong> alternative: if the new group already has a committed offset before any application listener runs, you never need cutover seek() in onPartitionsAssigned. Whether that option was considered at the time is <strong>not confirmed</strong> in the source record; it is included here as engineering comparison, not as rewritten history.</p><p>Also in scope for trade-offs: the committed() check runs in the listener on the consumer poll/rebalance path. It is a coordinator call. Timeouts or exceptions in that path need explicit thought — the original record does not confirm how exceptions were handled, so this article flags the risk rather than inventing a handler.</p><h3>What this fix does not solve</h3><p>Even with the guard:</p><ul><li>A consumer that <strong>forwards successfully and then crashes before commit</strong> can still re-forward on restart. That is ordinary <strong>at-least-once</strong> behavior.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=682810e4984d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hello world : Low Level Design]]></title>
            <link>https://medium.com/@sahilgupta_33409/hello-world-low-level-design-bce7591043ce?source=rss-3db08a9fcea3------2</link>
            <guid isPermaLink="false">https://medium.com/p/bce7591043ce</guid>
            <category><![CDATA[interview-questions]]></category>
            <category><![CDATA[low-level-design]]></category>
            <dc:creator><![CDATA[Sahil Gupta]]></dc:creator>
            <pubDate>Tue, 08 Oct 2024 17:22:13 GMT</pubDate>
            <atom:updated>2024-10-08T17:22:13.716Z</atom:updated>
            <content:encoded><![CDATA[<h3>Hello world : Low Level Design</h3><blockquote>Low-Level Design (LLD) refers to the phase in software development where the high-level system architecture and specifications from the High-Level Design (HLD) phase are transformed into a more detailed and comprehensive design. It is the next step after the architectural design phase and before the actual implementation (coding) of the system.</blockquote><blockquote>LLD explains class daigram using methods, relationship between classes, and program specification. In order for the programmer to create the program directly from document, it describes the module. Due to the fact that various character sets, it gives us the structure and behaviour of a class.</blockquote><h3>Introduction to Low-Level Design (LLD)</h3><p>Low-Level Design (LLD) is the process of elaborating and specifying the system architecture and components in a more detailed manner.</p><ol><li><strong>Importance of LLD in software development</strong></li></ol><ul><li>LLD serves as a blueprint or roadmap for developers to follow during the coding phase.</li><li>It reduces ambiguity by providing clear specifications, thereby minimizing errors and rework during the implementation.</li></ul><p>2. <strong>Significance of LLD interviews for software engineers</strong></p><ul><li>A Software developer using low level design as lld focuses on how to build software from set of requirements</li><li>how different components interact with each other</li><li>what responsibilty each component has etc</li></ul><h3>Getting Started with LLD Interviews</h3><ol><li>Understanding Core Principles</li><li>Fundamentals: UML, OOP, and Design Patterns</li><li>Step-by-Step Approach to LLD Interviews</li><li>Solving a Sample LLD Question</li></ol><h3>Understanding Core Principles</h3><h4>YAGNI Principle</h4><blockquote>YAGNI: <strong>You Are Not Gonna Need it<em>.</em></strong></blockquote><ul><li>It is a principle in software deveopment which state that features should only be added when required.</li><li>it trims away excess and inefficiency in development to faciliate the desired increase frequency of releases.</li></ul><h4>DRY Principle</h4><blockquote>DRY : <strong>Dont Repeat Yourself</strong></blockquote><ul><li>it is a principle of software development aimed at reducing repetition of information which is likely to change, replacing it with abstractions that are less likely to change, or using data normalization which avoids redundancy in the first place.</li></ul><h4>SOLID Principles</h4><blockquote><strong>SOLID Stand for</strong></blockquote><ul><li>Single-responsibility Principle (SRP)</li><li>Open-closed Principle (OCP)</li><li>Liskov’s Substitution Principle (LSP)</li><li>Interface Segregation Principle (ISP)</li><li>Dependency Inversion Principle (DIP)</li></ul><p>These are sets of 5 principles(rules) that are strictly followed as per requirements for optimal designing. In order to write scalable, flexible, maintainable and resuseable code.</p><h3>Fundamentals: UML, OOP, and Design Patterns</h3><blockquote><strong>Unified Modeling Language (UML)</strong></blockquote><ul><li>Importance of UML in visualizing and communicating designs</li><li>Explanation of commonly used diagrams in LLD (class diagrams, sequence diagrams, etc.)</li></ul><blockquote><strong>Object-Oriented Programming (OOP) Concepts</strong></blockquote><ul><li>Introduction to essential OOP concepts and their relevance in LLD</li><li>Introduction to Design Patterns</li></ul><blockquote><strong>Design Pattern</strong></blockquote><ol><li><strong>Creational Design Pattern</strong>: (<a href="https://medium.com/@sahilgupta_33409/singleton-design-pattern-9174ada3c862">Signleton</a>, Builder, <a href="https://medium.com/@sahilgupta_33409/prototype-design-pattern-f6d0c5bb501c">Prototype</a>, Factory, Abstract Factory)</li><li><strong>Structural Design Pattern</strong>: (Adaptor, Bridge, Composite, Decorator, Facade, Flyweight, Proxy)</li><li><strong>Behaviour Design Pattern</strong>: (Chain of Responsibility, Command, Itrator, Mediator, Memento, Observer, State, Strategy, Template, Visitor)</li></ol><h3>Step-by-Step Approach to LLD Interviews</h3><blockquote><strong>Requirement Collection</strong></blockquote><ul><li>Strategies for gathering and understanding project requirements</li></ul><blockquote><strong>Creating Class Diagrams</strong></blockquote><ul><li>Process of translating requirements into class diagrams</li><li>Consideration of relationships and dependencies between classes</li></ul><blockquote><strong>Data Store Design</strong></blockquote><ul><li>Decisions regarding data storage, database schema design, and data access layers</li></ul><blockquote><strong>Coding in LLD Interviews</strong></blockquote><ul><li>Tips for implementing designs efficiently, considering best practices</li></ul><h3>Complete step to Solving a Simple LLD Question</h3><blockquote><em>one of the best approach to solve any low level problem is</em></blockquote><ol><li>Requiremtent Gadring</li><li>Identify Class/Module</li><li>Define class Interface</li><li>Design UML</li><li>Handle Data</li><li>Define Relationship</li><li>Consider Design Pattern</li><li>Code</li></ol><p>In upcoming blogs we are going to solve top 50 famous interview low level design problems with approach and steps discussed above.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bce7591043ce" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Prototype Design Pattern]]></title>
            <link>https://medium.com/@sahilgupta_33409/prototype-design-pattern-f6d0c5bb501c?source=rss-3db08a9fcea3------2</link>
            <guid isPermaLink="false">https://medium.com/p/f6d0c5bb501c</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[low-level-design]]></category>
            <category><![CDATA[prototype-design-pattern]]></category>
            <dc:creator><![CDATA[Sahil Gupta]]></dc:creator>
            <pubDate>Fri, 08 Mar 2024 03:44:43 GMT</pubDate>
            <atom:updated>2024-03-08T03:49:39.836Z</atom:updated>
            <content:encoded><![CDATA[<p>In <strong><em>this article we are going to look into object creation in java and how Creational design Pattern help to create object in more efficient way. Also will deep dive into Prototype design pattern, also differentiate between singleton and prototype pattern when to use which pattern.</em></strong></p><p><strong><em>Interviewer: What are different ways of object creation in Java?</em></strong><br><strong><em>Me:</em></strong> In Java their is a lot of way to create object</p><ul><li>Using new operator: new demoClass()</li><li>Using factory methods: demoFactory.getInstance( )</li><li>Using newInstance: (Class.forName(demoClass))emp.newInstance( )</li><li>By cloning an already available object: (demoClass)obj1.clone( )</li></ul><p><strong><em>Interview: Difference between shallow copy and object cloning?<br>Me:</em></strong></p><p>Shallow Copy:</p><ul><li>In shallow copy, we only clone the parent object and not its containing objects.</li><li>In shallow copy, we copy references of the Original object, and hence if we make any changes in one of the objects, those changes get reflected in other objects as well.</li></ul><p>Deep Copy:</p><ul><li>In a deep copy, we clone the parent object as well as its containing objects.</li><li>In a deep copy, we copy the original object with its values so after copying, both the objects are independent of each other, and hence, if we make any changes in one object, that change does not get reflected in the other object.</li></ul><p><strong><em>Interviewer: What if we have to create same object multiple times with slightly change? Is we are going to create whole object every time ?</em></strong><br><strong><em>Me:</em></strong> Yes you can create object every time with the help of new keyword or other way mention above, but i agree this is not a good approach to create same object every time. if their is only some changes in that object then we don’t need to create whole new object every time. It also violates Design Principles like <strong>DRY(don’t repeat yourself )</strong>. To resolve this issue we can follow Prototype design pattern.</p><p><strong><em>Interviewer: Nice I wanted to listen this, can you please elaborate about prototype design pattern ?</em><br><em>Me:</em></strong> It is a type of creational pattern which enable the creation of object by coping existing object. It’s focus on concept of copying existing object rather than creating new instance form scratch.</p><p>Lets take a real time example as we have resume format/cover-letter format, applying for different company we modify same resume content every time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nligBbg6BXY5GV_881Xx8Q.png" /></figure><p>Step 1: first we create Document interface with fillContent, display, clone</p><pre>public interface Document {<br> public void filledContent(String content);<br> public void displayContent();<br> public Document clone();<br>}</pre><pre>Step 2: Make Resume class with default format</pre><pre>public class Resume implements Document{<br> private String content;<br> <br> public Resume() {<br>  this.content = &quot;Default content is set for resume&quot;;<br> }<br> <br> @Override<br> public void filledContent(String content) {<br>  this.content = content;<br> }<br><br> @Override<br> public void displayContent() {<br>  System.out.println(this.content);<br> }<br> <br> @Override<br> public Document clone() {<br>  return new Resume();<br> }</pre><pre>Step 3: Make Cover Letter class with default format</pre><pre>public class CoverLetter implements Document{<br> private String content;<br> <br> public CoverLetter() {<br>  this.content = &quot;Default content is set for cover letter&quot;;<br> }<br> <br> @Override<br> public void filledContent(String content) {<br>  this.content = content;<br> }<br><br> @Override<br> public void displayContent() {<br>  System.out.println(this.content);<br> }<br> <br> @Override<br> public Document clone() {<br>  return new CoverLetter();<br> }</pre><pre>Step 4: Make a Document Manager</pre><pre>public class DocumentManager {<br> public static HashMap&lt;String, Document&gt; documentMap = new HashMap&lt;&gt;();<br> static {<br>  documentMap.put(&quot;Resume&quot;, new Resume());<br>  documentMap.put(&quot;CoverLetter&quot;, new CoverLetter());<br> }<br> <br> public static Document getDocument(String documentType) {<br>  Document document = documentMap.get(documentType);<br>  return document != null ? document.clone(): null;<br> }</pre><pre>Step 5: client create resume object  and cover-letter and update content for google company after and day they also get a referral from amazon client update same resume for   amazon.</pre><pre>public class Main {<br><br> public static void main(String[] args) {<br>  Document clonedResume = DocumentManager.getDocument(&quot;Resume&quot;);<br>  Document clonedCoverLetter = DocumentManager.getDocument(&quot;CoverLetter&quot;);<br>  <br>  //prepare for google<br>  clonedResume.filledContent(&quot;This resume is prepare for google&quot;);<br>  clonedCoverLetter.filledContent(&quot;This cover-letter is prepare for google&quot;);<br>  <br>  clonedResume.displayContent();<br>  clonedCoverLetter.displayContent();<br>  <br>  System.out.println(&quot;#######################&quot;);<br>  <br>  //prepare for amazon<br>  clonedResume.filledContent(&quot;This resume is prepare for amazon&quot;);<br>  clonedCoverLetter.filledContent(&quot;This cover-letter is prepare for amazon&quot;);<br>  <br>  clonedResume.displayContent();<br>  clonedCoverLetter.displayContent();<br>}</pre><pre>By doing this we are updating a created instance multiple time for multiple use, we have a instance and we just cloning that instance multiple time for different use.</pre><p><strong><em>Interviewer: Ok in Pervious interview we had discuss about singleton pattern but i have a little confusion can you clear how prototype pattern is different from singleton pattern and when to use prototype when to use singleton?</em></strong><br><strong><em>Me:</em></strong> Ok as a name suggest singleton (single instance) means you have a only instance and you are using that only, but in prototype design pattern you have one instance but you can copy that instance according to their use. The prototype pattern involves copying something that already exists.</p><p><strong><em>Interviewer: Does it make sense to clone an object which is supposed to be immutable?</em></strong><br><strong><em>Me:</em></strong> It make sense only if we intend to modify either of the object and would like to preserve original state in other. Otherwise we can reuse the original object by making it singleton.</p><p><strong><em>Interviewer: What is the main advantage of using a prototype design pattern over object creation using a new keyword?</em></strong><br><strong><em>Me:</em></strong> When you create new object using “new” keyword, it involves executing of constructor and initializing all attribute. but in prototype design pattern new object is created by cloning existing object, skipping constructor calls and initialization process. So instead of creating a costly object, an existing object is copied, hence reduce the creation cost.</p><p><strong><em>Conclusion</em></strong><br>In this article, we understood the Prototype Design Pattern, need of this, when to use singleton and when prototype.</p><p>Complete implementations can be found over <a href="https://github.com/skg1998/low-level-design/tree/main/prototype_pattern/">Github</a></p><p><strong><em>Next Up<br></em></strong>We are going to look at Factory Design Pattern next</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f6d0c5bb501c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Singleton Design Pattern]]></title>
            <link>https://medium.com/@sahilgupta_33409/singleton-design-pattern-9174ada3c862?source=rss-3db08a9fcea3------2</link>
            <guid isPermaLink="false">https://medium.com/p/9174ada3c862</guid>
            <category><![CDATA[low-level-design]]></category>
            <category><![CDATA[interview-questions]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[design-pattern-in-java]]></category>
            <category><![CDATA[creational-design-pattern]]></category>
            <dc:creator><![CDATA[Sahil Gupta]]></dc:creator>
            <pubDate>Mon, 12 Feb 2024 20:31:42 GMT</pubDate>
            <atom:updated>2024-02-12T20:31:42.435Z</atom:updated>
            <content:encoded><![CDATA[<p><strong><em>One of the most favorite design pattern ask in interview and use in our programming life is singleton pattern. i am going to start my blog as a interview conversation and try to cover different way of creation(eager, lazy, syncronized, double checked, volatile), bug while creating that design. After creation part try to discussion of destory that singleton pattern and how to prevent from destory.</em></strong></p><p><strong>Interviewer</strong>: Do you know about singleton pattern <br><strong>Me</strong>: Yes (fully confident 😎😎😎)</p><p><strong>Interviewer</strong>: define it <br><strong>Me</strong>: Singleton Design Pattern is a type of creational design pattern which is use to create only one instance no matter how many times you calls. It ensures only one instance of a class is created by making the constructor private.</p><ul><li>Make the constructor private which prevents other classes from instantiating it.</li><li>Make one static getter method to access that instance without constructor call provides global access to that instance.</li><li>Make one static variable of object type who hold the instance.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/721/1*SAvYiUgXXeXi2VqjA1UeLg.png" /></figure><p><strong>Interviewer</strong> : ok nice, can you code it <br><strong>Me</strong> : sure (still high confidence)</p><pre>public class EagerSingletonPattern {<br> private static EagerSingletonPattern instance = new EagerSingletonPattern();<br> private EagerSingletonPattern() {<br>  <br> }<br> <br> public static EagerSingletonPattern getInstance() {<br>  return instance;<br> }<br>}</pre><p><strong>Interview</strong>: Ok nice but in eager type implementation object is created in starting, but i actually want instance singleton instance is created only when needed, can you modify your code <br><strong>Me</strong>: sure you are saying about Lazy approach (still confidence😊😊😊 ), once you call getInstace method, only then instance will created and on other calls it will return same instance.</p><pre>public class LazySingletonPattern {<br> private static LazySingletonPattern instance;<br> private LazySingletonPattern() { }<br> <br> public static LazySingletonPattern getInstance() {<br>  if(instance == null) {<br>   instance = new LazySingletonPattern();<br>  }<br>  return instance;<br> }<br>}</pre><p><strong>Interviewer</strong> : Nice is singleton pattern is thread safe ??<br><strong>Me</strong>: no, singleton is just a way to write/design your code, not actually solving a problem. To make it thread safe we need syncronized our getInstance method so at a time only one thread can access (now confidence decrease 😢😢😢).</p><pre>public class SyncronizedSingletonPattern {<br> private static SyncronizedSingletonPattern instance;<br> private SyncronizedSingletonPattern() { }<br> <br> public static synchronized SyncronizedSingletonPattern getInstance() {<br>  if(instance == null) {<br>   instance = new SyncronizedSingletonPattern();<br>  }<br>  return instance;<br> }<br>}</pre><p><strong>Interviewer</strong>: nice, but what do you think it is optimize??<br><strong>Me</strong> : Re-visit my code and try to understand what going on and realized that this approach is thread-safe and behavior of the Singleton is predictable in multi-threaded environment, but there are performance issues with this approach:</p><ul><li>once the singleton object is initialized completely, acquiring and releasing the locks would appear unnecessary every time.</li></ul><p>then i wrote double check method so when instance is null then only it will acquire a lock not all time.</p><pre>public class DoubleCheckSingletonPattern {<br> private static DoubleCheckSingletonPattern instance;<br> private DoubleCheckSingletonPattern() {<br>  <br> }<br> <br> public static DoubleCheckSingletonPattern getInstance() {<br>  if(instance == null) {<br>   synchronized(DoubleCheckSingletonPattern.class) {<br>    if(instance == null) {<br>     instance = new DoubleCheckSingletonPattern();<br>    }<br>   }<br>  }<br>  <br>  return instance;<br> }<br>}</pre><p>At this point my confidence is decrease a lot and now i am afraid what will be next question (bas kro yaar bas singleton puchooge kya 😰😰😰)</p><p><strong>Interviewer</strong>: Nice, looking like perfect, but their is nothing like a perfect in the world. do you think this is a best solution? is their is any bug over here.<br><strong>Me</strong>: low confidence but high voice, looking good working fine on all aspect.</p><p><strong>Interviewer</strong>: Do you know about the concept that their is a L1 caching before main memory and storing data to cache before updating main memory, it will take some time to update main memory or synchronized with other cache. just think you have two thread one is updating in cache but not yet sync with other core cache and also not update main memory. In that case if 2nd thread check it will find instance as a null and so their would be a chance of creating 2 different instance. can you solve that bug</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F-FEjNybffvxcZhZba3wIg.png" /></figure><p><strong>Me</strong>: in mind bas kar bhai ab bacche ki jaan lega kya 😭😭😭. but one things come in mind what you i directly update main memory and come up with volatile key approach.</p><pre>public class VolatileDoubleCheckSingletonPattern {<br> private static volatile VolatileDoubleCheckSingletonPattern instance;<br> private VolatileDoubleCheckSingletonPattern() {<br>  <br> }<br> <br> public static VolatileDoubleCheckSingletonPattern getInstnace() {<br>  if(instance == null) {<br>   synchronized(VolatileDoubleCheckSingletonPattern.class) {<br>    if(instance == null) {<br>     instance = new VolatileDoubleCheckSingletonPattern();<br>    }<br>   }<br>  }<br>  <br>  return instance;<br> }<br>}</pre><p><strong>Volatile keyword</strong> give a variable super power to directly update in main memory. The volatile keyword ensures that changes made by one thread are immediately visible to others.</p><p><strong>Interviewer</strong> : nice, but you know we can destroy these singleton pattern, do you have idea.<br><strong>Me</strong>: yes we can destroy with the help of <strong>Cloneable interface, Reflection and Serializable</strong>.</p><p><strong>Interviewer</strong>: ok can you explain with code <br><strong>Me</strong>: In below code, extract all constructor using reflection and then change the accessibility private to public and call getInstance() method. whenever we calling constructor we will get new instance.</p><pre>public class ReflectionDestory {<br> public static void main(String[] args) {<br>  LazySingletonPattern instance = LazySingletonPattern.getInstance();<br>  LazySingletonPattern instance2 = null;<br>  try {<br>   Constructor[] constructors = LazySingletonPattern.class.getDeclaredConstructors();<br>   for(Constructor constructor: constructors) {<br>    constructor.setAccessible(true);<br>    instance2 = (LazySingletonPattern)constructor.newInstance();<br>    break;<br>   }<br>  }catch(Exception e) {<br>   e.printStackTrace();<br>  }<br>  <br>  System.out.println(instance.hashCode());<br>  System.out.println(instance2.hashCode());<br> }<br>}</pre><p><strong>Interviewer</strong> : can be overcome and resolve this problem to make our singleton non destroyable.<br><strong>Me</strong>: yes we can, if we modify our private constructor we can stop to call constructor. in below code if some one try to invoke the constructor directly it will throw Exception.</p><pre>class Singleton {<br> private static Singleton instance;<br><br> private Singleton() {<br>   if (instance != null) {<br>        throw new IllegalStateException(&quot;instance already created.&quot;);<br>      }<br> }<br> <br> public static Singleton getInstance() {<br>  if(instance == null) {<br>   instance = new Singleton();<br>  }<br>  <br>  return instance;<br> }<br>}<br><br>public class OvercomeReflectionDestory {<br> public static void main(String[] args) {<br>  Singleton instance = Singleton.getInstance();<br>  Singleton instance2 = null;<br>  try {<br>   Constructor[] constructors = Singleton.class.getDeclaredConstructors();<br>   for(Constructor constructor: constructors) {<br>    constructor.setAccessible(true);<br>    instance2 = (Singleton)constructor.newInstance();<br>    break;<br>   }<br>  }catch(Exception e) {<br>   e.printStackTrace();<br>  }<br>  <br>  System.out.println(instance.hashCode());<br>  System.out.println(instance2.hashCode());<br> }<br>}</pre><p><strong>Interviewer</strong>: Nice, can you also explain Cloneable<br><strong>Me</strong>: ok, sure</p><pre>class LazySingletonPattern implements Cloneable{<br>   private static LazySingletonPattern instance;<br>  <br>   private LazySingletonPattern() {<br>    <br>   }<br>   <br>   public static LazySingletonPattern getInstance() {<br>      if(instance == null) {<br>         instance = new LazySingletonPattern();<br>      }<br>    return instance;<br>   }<br><br>    @Override<br>    protected Object clone() throws CloneNotSupportedException{<br>        return super.clone();<br>    }<br><br>}<br><br>public class CloneDestory {<br><br> public static void main(String[] args) throws CloneNotSupportedException {<br>  // TODO Auto-generated method stub<br>    LazySingletonPattern instance = LazySingletonPattern.getInstance();<br>    LazySingletonPattern instance2 = (LazySingletonPattern)instance.clone();<br>    <br>    System.out.println(instance.hashCode());<br>    System.out.println(instance2.hashCode());<br>   }<br><br>}</pre><p><strong>Interviewer</strong>: can you resolve this issue<br><strong>Me</strong>: ok i can try , Override clone() method and throw CloneNotSupportedException to prevent this.</p><pre><br>class LazySingletonPattern implements Cloneable{<br> private static LazySingletonPattern instance;<br><br> private LazySingletonPattern() {<br>  <br> }<br> <br> public static LazySingletonPattern getInstance() {<br>  if(instance == null) {<br>   instance = new LazySingletonPattern();<br>  }<br>  <br>  return instance;<br> }<br> <br> @Override<br>    protected Object clone() throws CloneNotSupportedException {<br>        throw new CloneNotSupportedException(&quot;Cloning of this object is not allowed&quot;);<br>    }<br>}<br><br>public class OvercomeCloneDestory {<br> public static void main(String[] args) throws CloneNotSupportedException {<br>  // TODO Auto-generated method stub<br>  LazySingletonPattern instance = LazySingletonPattern.getInstance();<br>  LazySingletonPattern instance2 = (LazySingletonPattern)instance.clone();<br>  <br>  System.out.println(instance.hashCode());<br>  System.out.println(instance2.hashCode());<br> }<br>}</pre><p><strong>Interviewer</strong> : ok nice, you have a good idea of singleton pattern but, do you know where to use this design pattern.<br><strong>Me</strong>: we can use in <strong>Logging system, Database Connection</strong></p><p><strong>Thanks…</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9174ada3c862" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>