<?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"><channel><title><![CDATA[Rithwik Kumar Nagulapati — Data Engineering Real-Time Experiences and Stories]]></title><description><![CDATA[Hello, This is Rithwik, a Data Engineer, helping teams build scalable and reliable data platforms on GCP, AWS and Azure. My focus has always been on creating sy]]></description><link>https://rithwikn.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sat, 28 Nov 2026 02:21:43 GMT</lastBuildDate><atom:link href="https://rithwikn.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Beating the 7-Day Cluster TTL: How I Built a Graceful Shutdown Watchdog for a Kafka Streaming Pipeline]]></title><description><![CDATA[A production story about migrating a streaming pipeline off VMware, surviving a hard infrastructure TTL limit, and keeping services running continuously…]]></description><link>https://rithwikn.hashnode.dev/beating-the-7-day-cluster-ttl-how-i-built-a-graceful-shutdown-watchdog-for-a-kafka-streaming-pipeline</link><guid isPermaLink="true">https://rithwikn.hashnode.dev/beating-the-7-day-cluster-ttl-how-i-built-a-graceful-shutdown-watchdog-for-a-kafka-streaming-pipeline</guid><category><![CDATA[kafka]]></category><category><![CDATA[#apache-spark]]></category><category><![CDATA[Java]]></category><category><![CDATA[data-engineering]]></category><category><![CDATA[Cloud]]></category><dc:creator><![CDATA[Rithwik Kumar Nagulapati]]></dc:creator><pubDate>Sun, 05 Jul 2026 23:43:32 GMT</pubDate><content:encoded><![CDATA[<p><strong><mark class="bg-yellow-200 dark:bg-yellow-500/30">It started with a cost conversation</mark></strong></p>
<p>My manager flagged the VMware costs on our streaming pipeline. Two machines running 24/7 — two Kafka consumers and a Bootstrap application, always on, whether traffic was heavy or quiet. The bill was climbing and the ask was direct: find a cheaper way to run this.</p>
<p>The obvious move was to migrate to an ephemeral cluster. Pay only for active compute, tear it down when not needed. Straightforward enough — except our infrastructure team had a hard rule I hadn't fully accounted for: every cluster in our environment auto-deletes after 7 days. No override, no extension.</p>
<p>For a batch job that runs once a day and exits cleanly, a 7-day TTL is no problem. For a streaming pipeline that needs to run continuously around the clock — two Kafka consumers and a Bootstrap app processing messages all day every day — a cluster that disappears mid-stream is a serious problem. Kafka consumers don't just pause gracefully when the ground disappears beneath them.</p>
<p>So I had to figure out how to run a continuous streaming pipeline on a cluster with a hard expiry date. This is what I built.</p>
<p><strong>What the old setup looked like</strong></p>
<p>The pipeline was straightforward. Two Kafka consumers pulled messages from topics and ingested records into a database. A Bootstrap application polled the database for records in specific statuses, processed them, published a response back to Kafka, and wrote results back to the DB. All three services ran on VMware machines — two machines, always on.</p>
<p>It worked reliably. But two machines running 24/7 just to keep a streaming pipeline alive is expensive, and as the VMware costs climbed, it became hard to justify.</p>
<p><strong>The challenge: a 7-day hard TTL on every cluster</strong></p>
<p>Moving the three services to an ephemeral cluster was the easy part. The hard part was what our infrastructure team told us next: every cluster in our environment had a maximum TTL of 7 days. After 7 days, the cluster is automatically deleted — no override, no extension.</p>
<p>For our streaming pipeline, that meant one of two things. Either the cluster gets force-deleted mid-stream on day 7 — Kafka consumers killed without warning, in-flight messages potentially lost, database writes incomplete — or we figure out a way to shut down gracefully before the TTL fires and immediately bring everything back up on a fresh cluster.</p>
<p>The second option is obviously the right answer. But it's not trivial. You need something that knows when the TTL is approaching, can signal three separate services to shut down cleanly, wait for them to finish, and then coordinate the cluster recycle, all without dropping/ losing a log.</p>
<p><em><strong>The goal wasn't just to survive the 7-day TTL. It was to make the recycle completely invisible to the pipeline — no data loss, no gaps, no manual intervention.</strong></em></p>
<p><strong>The solution: two shell scripts working together</strong></p>
<p>I built two shell scripts an install shutdown script and a graceful shutdown script — that work together as a TTL watchdog. Here's how they work.</p>
<p>The watchdog continuously monitors the cluster's TTL status. When it detects that the cluster is 15 minutes away from the 7-day expiry, it kicks off the shutdown sequence.</p>
<p><strong>Step 1 — detecting the TTL window</strong></p>
<p>The watchdog script runs in the background from the moment the cluster starts. It checks the cluster's remaining TTL on a regular interval. When it detects the TTL is within 15 minutes of expiry, it triggers the shutdown sequence.</p>
<p>Fifteen minutes is the window I chose based on how long the three services need to drain. It's enough time to let in-flight Kafka messages finish processing and for the Bootstrap app to complete any open DB writes — with time to spare.</p>
<p><code>#Simplified watchdog logic</code></p>
<p><code>while true; do</code></p>
<p><code>remaining=$(get_cluster_ttl_remaining_seconds)</code></p>
<p><code>if [ "$remaining" -le 900 ]; then # 15 minutes = 900 seconds trigger_graceful_shutdown</code></p>
<p><code>break</code></p>
<p><code>fi</code></p>
<p><code>sleep 60</code></p>
<p><code>done</code></p>
<p><strong>Step 2 — graceful shutdown of all three services</strong></p>
<p>Once the watchdog fires, it sends a shutdown signal to all three services — the two Kafka consumers and the Bootstrap app. Each service is designed to handle this signal cleanly: it stops picking up new work, finishes what it's currently processing, flushes any buffered data, and exits.</p>
<p>The whole drain takes 30 to 60 seconds in practice. The services don't need to be killed — they stop themselves once they've handled the signal and cleared their in-flight work.</p>
<p>Crucially, we wait for confirmation that each service has actually stopped before proceeding. Not just that the signal was sent — that the process has exited. This is what guarantees no in-flight messages are dropped.</p>
<p><strong>Step 3 — log backup running throughout</strong></p>
<p>From the moment the services start on the cluster, a separate log backup script runs in parallel. Every five minutes, it copies the latest logs to a GCS bucket. This runs continuously throughout the cluster's lifetime — not just at shutdown.</p>
<p>#<code>Runs in background from service start until cluster teardown</code></p>
<p><code>while ps -p $SERVICE_PID &gt; /dev/null; do</code></p>
<p><code>gsutil -m cp -r /var/log/services/ gs://your-bucket/pipeline-logs/$(date +%Y-%m-%d-%H)/</code></p>
<p><code>sleep 300 # every 5 minutes</code></p>
<p><code>done</code></p>
<p>By the time the cluster is deleted, every log from the entire run is already safely in GCS. There's nothing left on the cluster that we care about.</p>
<p><strong>Step 4 — delete the cluster, spin up a new one</strong></p>
<p>Once all three services have stopped cleanly and logs are backed up, the watchdog deletes the cluster. This happens before the 7-day TTL fires — we're in control of the deletion, not the infrastructure team's auto-delete mechanism.</p>
<p>Immediately after deletion, a new cluster is provisioned and the three services are started on it. The new cluster has a fresh 7-day TTL. The whole recycle takes a few minutes. From that point, the pipeline is running again and the watchdog starts its countdown for the next cycle.</p>
<p><em>By deleting the cluster ourselves, we reset the TTL clock. The infrastructure team's 7-day rule is never violated — we just recycle before it can fire.</em></p>
<p><strong>What this achieved in production</strong></p>
<p>No forced cluster terminations. The 7-day TTL has never fired on a live cluster since this went live. We always get there first. No data loss during recycles. Every in-flight Kafka message is processed before the shutdown completes. Every DB write finishes. The 30–60 second drain window is enough. Infrastructure cost down significantly. Two always-on VMware machines replaced by an ephemeral cluster we control and recycle ourselves. Zero manual intervention. The watchdog runs automatically. Full log coverage. Because backup runs every 5 minutes throughout the cluster's life, there are no gaps in the log history even across recycles.</p>
<p><strong>Things to get right if you build something like this</strong></p>
<p>Give yourself enough TTL buffer. Fifteen minutes worked for our drain time. If your services take longer to flush, increase it. Getting this wrong means your cluster gets force-deleted mid-drain. Wait for actual process exit, not just signal delivery. Sending the shutdown signal and checking that the process has actually stopped are two different things. Only the second one guarantees clean shutdown. Run log backup throughout, not just at shutdown. If the cluster dies unexpectedly before you drain cleanly, you want logs from the whole run — not just the last few minutes. Make the cluster deletion and re-creation automatic. Manual steps in a recycle that happens every 7 days will get missed eventually. Test the drain under load. Verify your services actually stop cleanly when the signal arrives mid-message, not just when the queue is empty.</p>
<p><strong>Final thought</strong></p>
<p><mark class="bg-yellow-200 dark:bg-yellow-500/30">The thing I found interesting about this problem is that it wasn't really a technical limitation — it was an infrastructure policy limitation. The 7-day TTL exists for a reason. The solution wasn't to fight it or work around it. It was to work with it by making the recycle fast, clean, and automatic.</mark></p>
<p><mark class="bg-yellow-200 dark:bg-yellow-500/30">If you're dealing with a similar constraint — hard cluster TTLs, spot instance interruptions, ephemeral environments with forced expiry — the pattern is the same. Know when the clock runs out, drain cleanly before it does, and restart fast. The pipeline doesn't care about the recycle as long as it's done right.</mark></p>
<p><mark class="bg-yellow-200 dark:bg-yellow-500/30">Happy to discuss the drain implementation in more detail in the comments particularly around Kafka consumer offset handling during the shutdown sequence, which deserves its own conversation.</mark></p>
]]></content:encoded></item><item><title><![CDATA[How I Cut Infrastructure Costs by Building an Auto-Shutdown Mechanism for a Long-Running Java Batch Pipeline on an Ephemeral Cluster]]></title><description><![CDATA[A production story about cost reduction, Quartz scheduler design, and gracefully shutting down a Java application that was never built to stop itself.]]></description><link>https://rithwikn.hashnode.dev/how-i-cut-infrastructure-costs-by-building-an-auto-shutdown-mechanism-for-a-long-running-java-batch-pipeline-on-an-ephemeral-cluster</link><guid isPermaLink="true">https://rithwikn.hashnode.dev/how-i-cut-infrastructure-costs-by-building-an-auto-shutdown-mechanism-for-a-long-running-java-batch-pipeline-on-an-ephemeral-cluster</guid><category><![CDATA[Cloud]]></category><category><![CDATA[Java]]></category><category><![CDATA[airflow]]></category><category><![CDATA[data-engineering]]></category><category><![CDATA[spark]]></category><category><![CDATA[kafka]]></category><category><![CDATA[Spark]]></category><category><![CDATA[GCP]]></category><category><![CDATA[Azure]]></category><category><![CDATA[Python]]></category><category><![CDATA[Dataproc]]></category><dc:creator><![CDATA[Rithwik Kumar Nagulapati]]></dc:creator><pubDate>Sun, 05 Jul 2026 23:25:26 GMT</pubDate><content:encoded><![CDATA[<h3><mark class="bg-yellow-200 dark:bg-yellow-500/30">It started with a cost conversation</mark></h3>
<p>So my manager pulled me aside one day. Pretty direct conversation, infrastructure costs for our batch pipeline were too high, and he needed me to do something about it.</p>
<p>The job itself wasn't complicated. A Java batch application, runs once a day, processes a large volume of data using Spark, finishes in one to three hours. Solid, reliable. The problem was what happened after it finished which was nothing. The cluster just sat there. Two edge nodes, always on, burning compute cost for 21+ hours of complete idleness every single day.</p>
<p><strong><mark class="bg-yellow-200 dark:bg-yellow-500/30">The ask</mark>:</strong> run the job once, then make the infrastructure disappear. Spin it back up tomorrow. Only pay for what we are actually using.</p>
<p>Simple enough in theory. But here's where it got interesting, our Java application had no way to shut itself down. It was a Quartz scheduler daemon, built to run forever. In a persistent cluster world, nobody cared. In an ephemeral model, that's the first thing we have to solve.</p>
<p><strong><mark class="bg-yellow-200 dark:bg-yellow-500/30">What we had before and why nobody questioned it</mark></strong></p>
<p>The old setup made sense when it was built. Persistent two-node cluster, Spark job fires once a day, runs a couple of hours, finishes. Simple, predictable, works every time.</p>
<p>The issue is that 'it works' and 'it's cheap' are two very different things. We were paying full cluster cost for a system that was actually doing useful work maybe 10% of the day. Once my manager put the cost in front of me, that was hard for me to argue with.</p>
<p><strong><mark class="bg-yellow-200 dark:bg-yellow-500/30">The solution- three layers</mark></strong></p>
<p><strong>The architecture I landed on has three pieces working together. Here's the full picture:</strong></p>
<img src="https://cdn.hashnode.com/uploads/covers/6a4adf54d86bf20ea95eb8b4/668ae7d1-76ca-4ee3-9dc8-ce396d223b9e.jpg" alt="" style="display:block;margin:0 auto" />

<p><em><strong>Figure 1 - The Airflow DAG sequences three tasks. Exit code 0 means success, exit code 1 means failure. The cluster is deleted after Task 2 either way.</strong></em></p>
<p><strong>Layer 1 - Airflow owns the cluster lifecycle</strong></p>
<p>The Airflow DAG runs on a daily schedule and handles three things in sequence:</p>
<ul>
<li><p>Task 1 creates the ephemeral cluster.</p>
</li>
<li><p>Task 2 is the PIG task which runs a shell script that launches the Java daemon and waits for it to exit.</p>
</li>
<li><p>Task 3 deletes the cluster.</p>
</li>
</ul>
<p>Exit code 0 means the job completed successfully, exit code 1 means it failed. Task 3 runs either way and the cluster is deleted, we are not leaving infrastructure running regardless of outcome. The exit code is simply the signal Airflow uses to mark the task as succeeded or failed.</p>
<p><strong>Layer 2 - What the Quartz workers actually do</strong></p>
<p>What is Quartz job doing here, it matters for under-standing the shutdown design.</p>
<p>The six workers are not threads in a while loop polling some queue. They're Quartz jobs annotated with @DisallowConcurrentExecution. Quartz wakes each one up every five minutes, runs its execute() method once, and puts it back to sleep. That's it.</p>
<p>Inside execute(), the worker calls .fire() on the coordinator. That acquires a distributed database lock so multiple workers don't grab the same task queries the task table for pending work, submits a Spark job if there's something there, waits for it to finish, and returns. No loop. No idle tracking. No self-shutdown logic whatsoever.</p>
<p>Workers fire, do their thing if work exists, and return. They have no idea whether the task table is empty or not. Whether to shut down is completely outside their scope that's the monitor's job.</p>
<p><strong>Layer 2b - The IdleMonitorThread</strong></p>
<p>This is the piece I added. One background thread whose entire job is to watch the Quartz scheduler and decide when it's safe to exit.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6a4adf54d86bf20ea95eb8b4/0ac56a01-1258-40bf-945a-a8fbaca9a66f.jpg" alt="" style="display:block;margin:0 auto" />

<p><em><strong>Figure 2 - The full architecture. Six Quartz workers query the task table via lock. The IdleMonitorThread watches the scheduler and fires System.exit(0) after 3 minutes of idle. The shell then copies logs to GCS in parallel while java application is running</strong></em></p>
<p>The first thing the monitor does is sleep for ten minutes. And I want to explain why, because skipping this is the mistake that would cause entire system not functional: if the monitor starts checking at t=0, it can see an empty scheduler in the first 20 seconds before Quartz has even fired a single worker and shut the whole thing down before any real work happens. The warmup period is a safety buffer.</p>
<p><code>new Thread(() -&gt; {</code></p>
<p><code>try {</code></p>
<p><code>Thread.sleep(600000); // 10-minute warmup</code></p>
<p><code>}</code></p>
<p>One Quartz API call: scheduler.getCurrentlyExecutingJobs(). If that comes back empty and stays empty for three minutes straight, we call scheduler.shutdown(true) which waits for any in-flight jobs to finish, then System.exit(0).</p>
<p>The monitor doesn't re-query the database. It doesn't need to. Our workers fire every five minutes via cron, so if work existed, a worker would have picked it up before the 3-minute idle window ever closed. Three minutes of silence from the scheduler is enough.</p>
<p><em>System.exit(0) is what the shell passes back to Airflow as task success. That's the only signal Airflow needs and it means the work is genuinely done, not just timed out.</em></p>
<p><strong>Layer 3 - Continuous log backup to GCS</strong></p>
<p>I wrote log backup script to kick off the moment the Java process starts. From that point it runs in the background continuously copying logs to GCS every five minutes, all the way until the Java process exits.</p>
<p>Every five minutes throughout the entire job run, logs are already safe in GCS. By the time the cluster is torn down, nothing is lost.</p>
<p><strong>What actually changed in production</strong></p>
<ul>
<li><p>Cluster runs 1–3 hours a day instead of 24. We're paying for active compute time only the idle cost is gone.</p>
</li>
<li><p>Airflow task status is accurate. Exit code 0 means the job completed. Exit code 1 means it failed. The cluster is deleted after either outcome.</p>
</li>
<li><p>Zero changes to the core application. No touching the Quartz job logic, the locking, the Spark submission code. The IdleMonitorThread and the parallel log backup are a wrapper around the existing system.</p>
</li>
</ul>
<p><strong>If I were doing this again things I'd make sure to get right</strong></p>
<ul>
<li><p>Give the monitor a warmup period. Ten minutes worked for us, don't skip it. Starting idle checks at t=0 is a bug waiting to happen.</p>
</li>
<li><p>Use the scheduler's own API, not Thread.State. scheduler.getCurrentlyExecutingJobs() is one call and tells you exactly what you need.</p>
</li>
<li><p>Make the idle threshold configurable. Three minutes worked for our job frequency. Adjust for your own workload.</p>
</li>
<li><p>Use exit codes correctly 0 for success, 1 for failure. The cluster gets deleted either way; the exit code just tells Airflow how to mark the task.</p>
</li>
<li><p>Keep cluster deletion as its own downstream Airflow task. Don't mix it with the job task failure handling gets messy fast.</p>
</li>
</ul>
<p><strong>Final thought</strong></p>
<blockquote>
<p>The thing I keep coming back to is the shutdown problem wasn't inside the application, it was around it. The application didn't need to change. It needed a lifecycle layer that could observe it from outside and act on what it saw.</p>
</blockquote>
<blockquote>
<p>If you're running long-running Java daemons on persistent clusters and wondering whether ephemeral is feasible yes, it is. But think through termination before you make the switch. The cost savings are real and show up fast. The shutdown design is what makes it safe to run in production.</p>
</blockquote>
<p><strong><mark class="bg-yellow-200 dark:bg-yellow-500/30">Curious what approaches others have taken. If you've built something similar or hit problems I didn't cover, drop it in the comments.</mark></strong></p>
]]></content:encoded></item></channel></rss>