<p>In k8s <a href="https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/#cron-job-limitations" rel="noreferrer">Cron Job Limitations</a> mentioned that there is no guarantee that a job will executed exactly once:</p>

<blockquote>
  <p>A cron job creates a job object about once per execution time of its
  schedule. We say “about” because there are certain circumstances where
  two jobs might be created, or no job might be created. We attempt to
  make these rare, but do not completely prevent them. Therefore, jobs
  should be idempotent</p>
</blockquote>

<p>Could anyone explain:</p>

<ul>
<li>why this could happen?</li>
<li>what are the probabilities/statistic this could happen?</li>
<li>will it be fixed in some reasonable future in k8s?</li>
<li>are there any workarounds to prevent such a behavior (if the running job can't be implemented as idempotent)?</li>
<li>do other <em>cron related</em> services suffer with the same issue? Maybe it is a core cron problem?</li>
</ul>

<p>The controller:</p>

<p><a href="https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/cronjob/cronjob_controller.go" rel="noreferrer">https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/cronjob/cronjob_controller.go</a></p>

<p>starts with a comment that lays the groundwork for an explanation:</p>

<pre><code>I did not use watch or expectations. Those add a lot of corner cases, and we aren't expecting a large volume of jobs or scheduledJobs. (We are favoring correctness over scalability.)  

If we find a single controller thread is too slow because there are a lot of Jobs or CronJobs, we we can parallelize by Namespace. If we find the load on the API server is too high, we can use a watch and UndeltaStore.) 

Just periodically list jobs and SJs, and then reconcile them.
</code></pre>

<p>Periodically means every 10 seconds:</p>

<p><a href="https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/cronjob/cronjob_controller.go#L105" rel="noreferrer">https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/cronjob/cronjob_controller.go#L105</a></p>

<p>The documentation following the quoted limitations also has some useful color on some of the circumstances under which 2 jobs or no jobs may be launched on a particular schedule:</p>

<pre><code>If startingDeadlineSeconds is set to a large value or left unset (the default) and if concurrentPolicy is set to AllowConcurrent, the jobs will always run at least once.

Jobs may fail to run if the CronJob controller is not running or broken for a span of time from before the start time of the CronJob to start time plus startingDeadlineSeconds, or if the span covers multiple start times and concurrencyPolicy does not allow concurrency. For example, suppose a cron job is set to start at exactly 08:30:00 and its startingDeadlineSeconds is set to 10, if the CronJob controller happens to be down from 08:29:00 to 08:42:00, the job will not start. Set a longer startingDeadlineSeconds if starting later is better than not starting at all.
</code></pre>

<p>Higher level, solving for only-once in a distributed system is hard:</p>

<p><a href="https://bravenewgeek.com/you-cannot-have-exactly-once-delivery/" rel="noreferrer">https://bravenewgeek.com/you-cannot-have-exactly-once-delivery/</a></p>

<p>Clocks and time synchronization in a distributed system is also hard:</p>

<p><a href="https://8thlight.com/blog/rylan-dirksen/2013/10/04/synchronization-in-a-distributed-system.html" rel="noreferrer">https://8thlight.com/blog/rylan-dirksen/2013/10/04/synchronization-in-a-distributed-system.html</a></p>

<p>To the questions:</p>

<ul>
<li><p>why this could happen?</p>

<p>For instance- the node hosting the CronJobController fails at the time a job is supposed to run.  </p></li>
<li><p>what are the probabilities/statistic this could happen?</p>

<p>Very unlikely for any given run. For a large enough number of runs, very unlikely to escape having to face this issue. </p></li>
<li><p>will it be fixed in some reasonable future in k8s?</p>

<p>There are no idemopotency-related issues under the area/batch label in the k8s repo, so one would guess not.   </p>

<p><a href="https://github.com/kubernetes/kubernetes/issues?q=is%3Aopen+is%3Aissue+label%3Aarea%2Fbatch" rel="noreferrer">https://github.com/kubernetes/kubernetes/issues?q=is%3Aopen+is%3Aissue+label%3Aarea%2Fbatch</a></p></li>
<li><p>are there any workarounds to prevent such a behavior (if the running job can't be implemented as idempotent)?</p>

<p>Think more about the specific definition of idempotent, and the particular points in the job where there are commits. For instance, jobs can be made to support more-than-once execution if they save state to staging areas, and then there is an election process to determine whose work wins.</p></li>
<li><p>do other cron related services suffer with the same issue? Maybe it is a core cron problem?</p>

<p>Yes, it's a core distributed systems problem. </p>

<p>For most users, the k8s documentation gives perhaps a more precise and nuanced answer than is necessary. If your scheduled job is controlling some critical medical procedure, it's really important to plan for failure cases. If it's just doing some system cleanup, missing a scheduled run doesn't much matter. By definition, nearly all users of k8s CronJobs fall into the latter category. </p></li>
</ul>
