<?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 Pushtisonawala on Medium]]></title>
        <description><![CDATA[Stories by Pushtisonawala on Medium]]></description>
        <link>https://medium.com/@pushtisonawala786?source=rss-b8b0d517c926------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*KXvl13OAtYrU_qp4</url>
            <title>Stories by Pushtisonawala on Medium</title>
            <link>https://medium.com/@pushtisonawala786?source=rss-b8b0d517c926------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 15:42:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pushtisonawala786/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Designing a Production-Ready Background Job System in Golang]]></title>
            <link>https://medium.com/@pushtisonawala786/designing-a-production-ready-background-job-system-in-golang-25d69696dc22?source=rss-b8b0d517c926------2</link>
            <guid isPermaLink="false">https://medium.com/p/25d69696dc22</guid>
            <dc:creator><![CDATA[Pushtisonawala]]></dc:creator>
            <pubDate>Sun, 04 Jan 2026 20:16:50 GMT</pubDate>
            <atom:updated>2026-01-05T15:06:10.062Z</atom:updated>
            <content:encoded><![CDATA[<p>The reason why modern applications seem so fast is because they don’t try to do all of their tasks together.</p><p>Sending emails, processing payments, or generating reports ,all such operations take place in the background through the help of job queues.</p><p>I wanted to get an understanding on how things like Celery, Sidekiq, or BullMQ worked underneath the hood, so I implemented a job queue system myself using Redis and Go.</p><p>This blog entry takes a step-by-step look at why job queues are used, how mine works, and how errors are handled when that happens.</p><h3>Why Background Job Queues Matter:</h3><p>Let me start with a relevant problem.</p><p>The “Place Order” is clicked by a user.<br>On the back end, the system must save order,pay,send confirmation email,trigger notifications,update analytics</p><p>If all of this runs in one request:</p><p>- The application slows down<br>- Failures hit users directly<br>- Scaling becomes painful</p><p>Job queues solve this problem by offloading heavyweight work to the background.</p><h3>Why Didn’t I Use Celery or Sidekiq:</h3><p>Yes, tools like <strong>Celery</strong>, <strong>BullMQ</strong>, and <strong>Sidekiq</strong> already exist.But they hide too much. I wanted to understand-</p><ul><li>How jobs are safely picked up</li><li>How retries really work</li><li>How crashes don’t lose jobs</li><li>How shutdowns are handled in production</li></ul><p>So I decided to build a <strong>minimal but reliable job queue system</strong> myself.</p><h3>Technology Choices:</h3><p>I intentionally kept the stack simple:</p><p>- <strong>Redis </strong>→ queue storage<br>- <strong>MongoDB </strong>→ Database<br>- <strong>Go </strong>→ workers and API server<br>- <strong>Prometheus</strong> → metrics and monitoring</p><p>Redis was perfect because:</p><ul><li>A list can be used as a queue.</li><li>Delayed retries for the ZSET data.</li><li>Atomic Operations Prevent Duplicates.</li></ul><h3>High Level Architecture:</h3><p>At a high level, the system looks like this:<br>Client → API Server → Redis → Worker Pool → Completed / Fail</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5jgqjeepth7_Baj4SDGh_w.jpeg" /></figure><h3>Job Lifecycle: From Creation to Completion</h3><p>When a job is created via the /job endpoint, it gets added to a Redis list named job_queue first. This queue signifies the work that awaits processing. At this point, the job does not have anything to do yet; it merely waits in the queue.</p><p>When a worker wishes to process a job, it does not remove the job from the job_queue directly. Instead, it calls Redis’s <strong>BRPOPLPUSH </strong>operation to remove the job from the job_queue and add it to the processing_queue atomically. This step is important as it ensures that when a job has been chosen for processing, it will be monitored as a job ‘in progress’.</p><p>When an employee crashes after selecting a job but before its completion, it does not mean that the job gets lost. The job still gets represented in processing_queue, which ensures that it can be identified and retrieved. This particular approach increases the reliability of the system remarkably.</p><h3>Worker Model &amp; Concurrency:</h3><p>This is the point in which the system finally becomes active.</p><p>Once a job arrives in the Redis system, nothing happens on its own. It simply sits there.</p><p>Employees are the people who perform all the work.</p><p>To begin with, I attempted to process the jobs one by one. It was simple, easy to understand and think about, and honestly, felt “safe”. However, soon I understood the glaring issue with this which is the systems in the real world do not receive jobs individually. Jobs come in bunches. Emails, notifications, background jobs,everything arrives at once.</p><p>Hence, I introduced several workers. Every worker is always listening to the Redis server waiting for a job to arrive. As soon as a job becomes available, a worker grabs the job and begins processing it. This made the system immediately faster. However, being faster can be dangerous if it’s not controlled.</p><p>That’s when the second realization hit me: unlimited concurrency is just as bad as no concurrency.</p><p>When too many workers start processing their jobs simultaneously, that leads to overloading of the database, spamming of external APIs, or actually crashing itself. In order to prevent this from happening, I added a concurrency limit. Internally, it works like a gate: only a fixed number of workers are allowed to process jobs at the same time. The rest will wait patiently for a slot to free up.</p><p>It gave me one thing: <strong>controlled parallelism</strong>. Jobs are processed in parallel, but the system will always stay within the safe limits.</p><h3>How Jobs Are Safely Picked Up (And Never Lost)</h3><p>One of the biggest issues with background tasks is duplicate or lost work.</p><p>I did not want to find myself in a situation where: two workers process the same job, or a job will vanish if an employee has a crash in the middle</p><p>In this problem, the workers do not simply “pop” tasks from the queue.</p><p>Instead, they atomically move the job from the job_queue to the processing_queue. This action accomplishes two different things at the same time. This action takes the job out of the pending queue and indicates that the task is “in progress”.</p><p>It made all the difference.</p><p>So, even if a process has crashed halfway, the process is still present in the processing_queue. It has not vanished into thin air. It has not been copied either. The system precisely knows where the process died.</p><p>A successfully completed task is explicitly deleted from processing_queue, whereas, in case of a failed task, a decision is made on whether it is to be retryed or routed to dead letter queue. At this stage, the job life cycle finally felt… solid.</p><h3>Failure Isn’t an Exception — It’s a Feature</h3><p>In the beginning, I believed failures should not be there.<br>That’s not the mindset they maintained. “Failure,” rather than being an exception, “is a certainty,”</p><p>So rather than having a panic reaction to failure, the system takes it as a normal occurrence.</p><p>When a task fails, instead of being tried again, the task does not go ahead and retry immediately. This can cause more problems, especially when a failure might be fixed temporarily. The job will then go into a retry queue where a timestamp reads ‘try this one later’.</p><p>These failures increase the waiting time with the use of exponential backoff. In turn, this provides enough time to heal from the failure or the worker from hammering the same failing job.</p><p>In the background, a retry scheduler silently monitors the retry queue. As soon as the retry time of a job has come, that job gets resubmitted into the principal job queue. The loop would continue until either the job is successful or it meets the maximum retry limit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5MwUoSS-G2dgl0iPYlJU5w.png" /></figure><h3>Dead Letter Queue: Knowing When to Stop</h3><p>There are tasks that are simply flawed. It could be an invalid payload. It could be an error in logic. It could be corrupted data. Unlike others that retry infinitely, it terminates finally.</p><p>If there are too many attempts for an activity, it will be routed to the Dead Letter Queue. This is not an error for the system, it’s the system acting responsibly.</p><p>The DLQ acts as a quarantine zone. The failed jobs are moved here so that everything else in the system can run effectively. Developers can revisit these jobs at a later stage and identify issues, thereby correcting them without any impact on live traffic.</p><p>After incorporating this, the system is no longer fragile.</p><p>• Failures occurred in isolation.<br> • Still, the system continued</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ndhk4WGUnPOQmTg_DXGC-Q.png" /></figure><h3>Metrics &amp; Observability: When Logs Are Not Enough</h3><p>At some point while building this system, I realized something uncomfortable.Even if my workers were running, and even if jobs were getting processed, I had <strong>no real way to know if the system was healthy</strong>.</p><p>Logs were telling me <em>what happened</em>, but they were silent about <em>how bad things were getting</em>.</p><p>If retries suddenly spike, logs won’t scream.<br> If the DLQ slowly fills up, logs won’t warn me.<br> If the queue grows faster than workers can process, logs will just… exist.</p><p>So I added <strong>Prometheus </strong>metrics directly inside the worker lifecycle. Every important events like job picked, job success, job failure, retry scheduled, job pushed to DLQ ,increments a counter. Queue lengths are exposed as gauges. Now I don’t have to <em>guess</em> what’s happening. I can <strong>see pressure building before the system collapses</strong>.</p><p>Then I wired everything into <strong>Grafana</strong>.</p><p>Now I don’t have to guess what’s happening.<br> I can <strong>see pressure building</strong> before the system collapses.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bO6UhULQEnuMYmCEnm4sZw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8PkRmA1hscCv2Rem7f5xug.png" /></figure><h3>Graceful Shutdown: Leaving No Job Halfway</h3><p>What remained, in order to feel like it was production quality, was graceful shutdown.When the system receives a shutdown notification, it doesn’t go into a panic. It doesn’t abruptly kill the workers. It gradually unwinds.</p><p>It first stops any further job fetching from Redis. Next, it waits until all the tasks that are currently running get completed normally. The process quits only once each of the workers is finished with its tasks.</p><p>This guarantees that: no job is half-processed,no work is duplicated and no data is silently discarded.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZFUDMupgTEBjFYeWRn93GQ.png" /></figure><h3>Admin APIs:</h3><p>What if retries get out of control?</p><p>What will happen in the event that the service that uses synchronization data is unavailable?</p><p>What would happen if you needed to stop everything that you are doing right now?</p><p>You don’t SSH into servers and pray. That is why I included Admin APIs.</p><p>These APIs enable controlled interventions, such as pausing or resuming worker functions, peering into queues, and manually retry jobs if necessary. This turns a black box system into one that humans can understand and control during an emergency. Admin APIs are all about functionality</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*CgbHv3Z3HTc4mV3EDscbXg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*SidXoJgSda82pTEMaWH2bg.png" /></figure><h3>Conclusion:</h3><p>This particular project never aimed at competing either with Celery, Sidekiq, or BullMQ.</p><p>It had to do with understanding the purpose of the existence of these things and the problems that they solve every day.</p><p>Working on implementing a background job queueing system from scratch helped me shift away from viewing job queues simply as ‘Redis lists with workers’. I began to perceive them as living systems which need to survive crashes, handle failures, slow down if needed, yet always keep progressing.</p><p>I realized reliability is achieved, not by praying for nothing to fail, but by breaking things from day one. I realized observability is a non-negotiable thing, and without it, working in production is a complete insanity. But most importantly, I realized why simple systems turn complex, and it has nothing to do with features, but with corner cases.</p><p>So, if it’s the first time you’re working on backend systems, I’d highly recommend implementing an infrastructure like this once, not to run it in a production environment but to understand what the requirements in production are.</p><p>Because once you learn to build it by yourself, you will never again look at “background jobs” the same way.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=25d69696dc22" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>