DocumentDB Vacuum Locks
Tags: documentdb, mvcc, aws, database
Published: 2024-03-23 12:25:41

## Beware possible locks on large updates/deletions

Historically, traditional databases dealt with writes with pessimistic locks on records during writes to avoid inconsistency, which had the obvious drawback of being unable to handle concurrency properly, as transactions could fail.

This is solved by [MVCC](https://en.wikipedia.org/wiki/Multiversion_concurrency_control) (Multiversion Concurrency Control), by creating a new version of a record on every update, circumventing the need to lock records, and allowing concurrency (see [this](https://www.youtube.com/watch?v=iM71d2krbS4) video from Cameron McKenzie for a nice and simple illustrated explanation).

However, to clean up the old versions, a vacuum process must run in the background, which may cause locks in your complete collection, bottlenecks, and possibly unexpected downtimes in your application.

There is no permanent fix for this at the moment, but if you need to perform such updates, you may contact support and ask them to disable the process that reclaims unused storage space. This will not negatively impact your workload, and space reclaimed by the garbage collector will continue to be recycled. However, the size of your collections will never decrease, even if a significant amount of data has been deleted.

## **The good news**

AWS is currently working on a fix for it, which may be available at any time, so you should keep an eye on the [DocumentDB release notes](https://docs.aws.amazon.com/documentdb/latest/developerguide/release-notes.html).

## This is how we experienced it

On a lovely Tuesday morning, we reset one of our Kafka topics (~71 GB) to re-consume all our data for a particular domain to aggregate it with new fields in our database. All messages were successfully consumed and written in the primary DB instance in a few minutes as expected:

![](https://cdn-images-1.medium.com/max/2000/1*fiPdi6OEk4nb2aO6tF39Qg.png)

What we did not expect, though, are those waves of latency increase in our workload hours after the records were consumed and initially without much of a pattern until approx. 5 pm:

![](https://cdn-images-1.medium.com/max/2948/1*LxNFDKkmQ9hZSSm4E4TnWQ.png)

Those were all caused by locks on a particular collection in the read replicas as shown by the pink bars in the [Document performance insights](https://docs.aws.amazon.com/documentdb/latest/developerguide/performance-insights.html) metrics below:

![](https://cdn-images-1.medium.com/max/6504/1*CqNo7oVd6SCRw-KUs5UzCQ.png)

As you see, the locks were gone after around 11 pm, matching the end of the DocumentDB freeable memory metrics changes below:

![](https://cdn-images-1.medium.com/max/2000/1*mEzDP4eWYU-zFr3BCvyhKg.png)

Reaching out to AWS support, they investigated the issue. They confirmed it was caused by the process of reclaiming unused space during the garbage collection on the vacuum process. This process must be synchronized between the writer and readers because the readers might still have in-flight transactions that can see the deleted data. In some rare circumstances and the presence of a large amount of reclaimable data, this synchronization can adversely affect the workload on the replicas.

## Conclusion

Be aware of the MVCC strategy and check how it may affect your database (not only DocumentDB) in case of large updates as described above, and probably most importantly, always test it in staging first ;)

Also, be aware of the known issue with the DocumentDB vacuum process and watch the [release notes](https://docs.aws.amazon.com/documentdb/latest/developerguide/release-notes.html).