<p>Mongock looks very promising. We want to use it inside a kubernetes service that has multiple replicas that run in parallel.</p>
<p>We are hoping that when our service is deployed, the first replica will acquire the mongockLock and all of its ChangeLogs/ChangeSets will be completed before the other replicas attempt to run them.</p>
<p>We have a single instance of mongodb running in our kubernetes environment, and we want the mongock ChangeLogs/ChangeSets to execute only once.</p>
<p>Will the mongockLock guarantee that only one replica will run the ChangeLogs/ChangeSets to completion?</p>
<p>Or do I need to enable transactions (or some other configuration)?</p>

paul-pauses-to-wonder
<p>I am going to provide the short answer first and then the long one. I suggest you to read the long one too in order to understand it properly.</p>
<h2>Short answer</h2>
<p>By default, Mongock guarantees that the ChangeLogs/changeSets will be run only by one pod at a time. The one owning the lock.</p>
<h2>Long answer</h2>
<p>What really happens behind the scenes(if it's not configured otherwise) is that when a pod takes the lock, the others will try to acquire it too, but they can't, so they are forced to wait for a while(configurable, but 4 mins by default)as many times as the lock is configured(3 times by default). After this, if i's not able to acquire it and there is still pending changes to apply, Mongock will throw an MongockException, which should mean the JVM startup fail(what happens by default in Spring).</p>
<p>This is fine in Kubernetes, because it ensures it will restart the pods.
So now, assuming the pods start again and changeLogs/changeSets are already applied, the pods start successfully because they don't even need to acquire the lock as there aren't pending changes to apply.</p>
<h2>Potential problem with MongoDB without transaction support and Frameworks like Spring</h2>
<p>Now, assuming the lock and the mutual exclusion is clear, I'd like to point out a potential issue that needs to be mitigated by the the changeLog/changeSet design.</p>
<p>This issue applies if you are in an environment such as Kubernetes, which has a pod initialisation time, your migration take longer than that initialisation time an the Mongock process is executed before the pod becomes ready/health(and it's a condition for it). This last condition is highly desired as it ensures the application runs with the right version of the data.</p>
<p>In this situation imagine the Pod starts the Mongock process. After the Kubernetes initialisation time, the process is still not finished, but Kubernetes stops the JVM abruptly. This means that some changeSets were successfully executed, some other not even started(no problem, they will be processed in the next attempt), but one changeSet was partially executed and marked as not done. This is the potential issue. The next time Mongock runs, it will see the changeSet as pending and it will execute it from the beginning. If you haven't designed your changeLogs/changeSets accordingly, you may experience some unexpected results because some part of the data process covered by that changeSet has already taken place and it will happen again.</p>
<p>This, somehow needs to be mitigated. Either with the help of mechanisms like transactions, with a changeLog/changeSet design that takes this into account or both.</p>
<p>Mongock currently provides transactions with “all or nothing”, but it doesn’t really help much as it will retry every time from scratch and will probably end up in an infinite loop. The next  version 5 will provide transactions per ChangeLogs and changeSets, which together with good organisation, is the right solution for this.</p>
<p>Meanwhile this issue can be addressed by following <a href="https://github.com/cloudyrock/mongock/blob/master/community/README_V3.md#mongo-transaction-limitations" rel="noreferrer">this design suggestions</a>.</p>
