<?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 Lewis Franklin on Medium]]></title>
        <description><![CDATA[Stories by Lewis Franklin on Medium]]></description>
        <link>https://medium.com/@brolewis?source=rss-40b4641299a9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*tXBKd4feQLwOwZu3.jpeg</url>
            <title>Stories by Lewis Franklin on Medium</title>
            <link>https://medium.com/@brolewis?source=rss-40b4641299a9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 16:25:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@brolewis/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[Celery Distributed Job Scheduler]]></title>
            <link>https://medium.com/@brolewis/celery-distributed-job-scheduler-c9ba5c748f81?source=rss-40b4641299a9------2</link>
            <guid isPermaLink="false">https://medium.com/p/c9ba5c748f81</guid>
            <dc:creator><![CDATA[Lewis Franklin]]></dc:creator>
            <pubDate>Fri, 28 Jun 2013 19:34:31 GMT</pubDate>
            <atom:updated>2013-06-28T19:34:31.117Z</atom:updated>
            <content:encoded><![CDATA[<h4>Using ZooKeeper to prevent a single point of failure</h4><p>Most of my work is spent converting and managing an <a href="http://en.wikipedia.org/wiki/Extract,_transform,_load">ETL</a> system where we get data from a number of different clients and then import them into our database for our analytic tools.</p><p>For some time now I have been using <a href="http://celeryproject.org/">Celery</a> for distributed computing and loved that I no longer was dependent on the uptime of a single machine since I have a cluster of machines that handle the data processing and so one going down didn’t mean I had to drop everything and fix it; it could wait.</p><p>Over time I realized that there was a better need for coordination between celery processes and so I found and started using <a href="http://zookeeper.apache.org/">ZooKeeper</a> (via <a href="http://kazoo.readthedocs.org/en/latest/">Kazoo</a>) and life was getting even better.</p><p>However, in spite of all this distributed computing goodness, I still had one problem that bothered me: I was dependent on one machine. A number of our jobs were started via a cron job that existed on one computer and so if that computer failed, I needed that machine back. Sure, I could have a dormant cron on a second machine and activate it when the first failed, but (a) it still required action on my part and (b) there would still be (most likely) a noticeable lag between the first server failing and me getting the second one started (because, inevitably, it happens in the middle of the night and I’m too asleep to hear my phone notify me).</p><p>I looked on the web to find a better solution but was unsatisfied with anything that was out there. The closest I came was <a href="http://nerds.airbnb.com/introducing-chronos">Chronos</a> but it had a ton of overhead and it didn’t install quickly (or at all) for me so it wasn’t the solution I wanted.</p><p>I knew that Celery had a built-in job scheduler, but according to the <a href="http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html?highlight=beat#introduction">documentation</a>, you could only run one instance at a time. As I thought more and more about wanting to have a distributed job scheduler, I felt that the best place to start was with the Celery job scheduler (beat). Using the experience I had from using ZooKeeper, I set out to write a Celery beat scheduler that could be run across multiple computers and they would use ZooKeeper to ensure that only one at a time ran and also to recover from failure.</p><p>The end result can be found <a href="https://gist.github.com/brolewis/5799103"><strong>HERE</strong></a>.</p><p>This replacement for the Celery schedulers serve two primary functions that don’t come with the default scheduler. First, it allows for Celery beat to be run on multiple computers with only one creating tasks at a time. This is accomplished by using a distributed lock on ZooKeeper. The process that obtains the lock is the one that actually processes the Celery beat <a href="http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html#beat-entries">entries</a>. The others will sleep for <a href="http://docs.celeryproject.org/en/latest/configuration.html#std:setting-CELERYBEAT_MAX_LOOP_INTERVAL">CELERYBEAT_MAX_LOOP_INTERVAL</a> and attempt to acquire the lock. If unsuccessful, it assumes that the lock holder is still processing entries and goes back to sleep.</p><p>But what happens when an instance dies? If a second instance wakes up after the interval (lets say 5 minutes) and is able to acquire the lock, should it just start processing jobs without any regard to the past? If the first instance died one second after the second one went to sleep, any jobs scheduled over that nearly 5 minute window would essentially be lost if the past is ignore and this is unacceptable because we have some jobs that only run once or twice a day.</p><p>To that end my Celery scheduler also will rewind back in time (no further than the max loop interval) and run all the jobs that should have been run between the last run and when the second instance acquired the lock and then, once caught up, revert to running as normal (no more traveling through time!).</p><p>The process for writing this scheduler has been interesting and rewarding, with a mix of frustrating and challenging. I hope that there are others out there that find it to be a useful tool. I also know that the code probably isn’t perfect and so would love any comments or suggestions so that this can be the best tool for distributed job scheduling in Celery.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c9ba5c748f81" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>