<?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 Teachable on Medium]]></title>
        <description><![CDATA[Stories by Teachable on Medium]]></description>
        <link>https://medium.com/@teachable?source=rss-3e9c4d1c6a3b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ak1coaNtqWv4fuX4kt6TKA.png</url>
            <title>Stories by Teachable on Medium</title>
            <link>https://medium.com/@teachable?source=rss-3e9c4d1c6a3b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 04:23:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@teachable/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[Journey of Tuning a Sidekiq Job]]></title>
            <link>https://medium.com/teachable/journey-of-tuning-a-sidekiq-job-07e29b9ce78b?source=rss-3e9c4d1c6a3b------2</link>
            <guid isPermaLink="false">https://medium.com/p/07e29b9ce78b</guid>
            <category><![CDATA[sidekiq]]></category>
            <category><![CDATA[platform-engineering]]></category>
            <dc:creator><![CDATA[Teachable]]></dc:creator>
            <pubDate>Wed, 18 Sep 2024 15:46:52 GMT</pubDate>
            <atom:updated>2024-09-18T15:46:52.473Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YiwljM98AZM2f9pmeuG3lA.png" /></figure><p>Hear from Sue, Sr. Software Engineer, on our Engineering Enablement team!</p><h3>Problems</h3><p>In the Engineering Enablement team, we proactively monitor the health of the whole Teachable platform. Recently, a sidekiq job entered our radar for a performance tuneup.</p><p>Sidekiq is a background job framework for Ruby on Rails platform. Namely, Sidekiq enables developers to maintain fast and responsive web applications by moving time-consuming tasks into the background. However, sidekiq is not a superman butler that you can throw whatever tasks to it. It shares some common resources with the web applications, such as database connections. When sidekiq experiences latency, the main application also suffers. I documented the journey of tuning up this sidekiq job.</p><h3>Tuning Journey:</h3><ol><li>Break Down Large Jobs</li></ol><p>Ideal Sidekiq background jobs should be small and rapid. Breaking down a complex or long-running job into smaller tasks has multiple benefits:</p><ul><li>Failure of one job does not affect others, allowing retries from the point of failure instead of restarting the entire job.</li><li>Smaller jobs use less memory, releasing it once the job is complete.</li></ul><p>The old job involves roughly following steps: search for data, export the data into a text file, then email the text file link to the user. Therefore, the steps were broken into smaller jobs: search job, text writer job, and email job. Utilizing Sidekiq Pro’s batch jobs feature, these jobs were integrated into a group. With the change, tested in an isolated quarantine queue, we saw a 36% improvement over the old job.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Gi-6tqlnVLCr5QWvAvVX1Q.png" /></figure><p>2. Database Query Optimization</p><p>In RoR, N+1 query is a very common mistake. It is a query pattern where an initial query is made to retrieve a collection of objects, and then spawn additional queries for each object in that collection.</p><p>The old job first retrieving IDs from Elasticsearch and then making multiple database queries for every ID. This resulted in 8 database queries for every record. By optimizing the query process, the number of queries was reduced to 3 queries per record, a 63% reduction. For a file with 1000 records, it will be 3000 queries instead of 8000.</p><p>Benchmark for these two different way of querying:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r9g5Tv8mRzQx24YQvC1BpA.png" /></figure><p>3. Optimizing Elasticsearch Query</p><p>In additional database query, an issue in elasticsearch querying was identified: Elasticsearch’s scroll returned all 91 million IDs for every search, filtering only in result processing.</p><p>Elasticsearch is very efficient but also very different from RDBMs. For example, its index is more like a database in RDBM. It also has its own query language. By including a domain ID in the initial search, the elasticsearch result is greatly reduced, and the memory usage of processing the scroll object was significantly reduced therefore.</p><p>Memory usage of scroll object (tested with two domain in local machine):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ihwomGzADYiORJhLu_EtKg.png" /></figure><p>For production benchmarking, a job was selected that had run the same day with identical parameters (domain id, filters, sort criteria). The new job was run in test mode.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LTgKNaGy07U9pZ2yfLyCEQ.png" /></figure><h3>Minimize Risks In Production:</h3><p>During the tuning journey, several techniques were implemented to minimize user and system impact, control accuracy, and ensure smooth migration. These included separating queues to manage workflow efficiently, introducing feature flags to carefully roll out new jobs, separating methods if existing files required updates, building test modes for production benchmarking, and performing file comparison on output files to guarantee no surprises to users.</p><h3>Lesson Learned:</h3><p>In summary, three key takeaways stand out as particularly valuable to learn: first, benchmark every step to ensure accurate and reliable results; secondly, don’t overlook cautious steps like test modes, which are crucial for establishing real-world production benchmarks that can be trusted; and finally, always keep an eye out for opportunities to fine-tune performance at every level.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=07e29b9ce78b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/teachable/journey-of-tuning-a-sidekiq-job-07e29b9ce78b">Journey of Tuning a Sidekiq Job</a> was originally published in <a href="https://medium.com/teachable">Teachable</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>