Databases – Single-Object Writes

Atomicity in isolation also applies when a single object is being changed.

For example consider the problem you stumble upon when writing a 20kb JSON document to a database:

  • If the network connection is interrupted after the first 10kbs has been sent, does the database store that un-parsable fragment 10kb fragment of JSON? 🤔
  • Also, if the power fails, will the database in the middle of overwriting the previous value on disc, do you end up with the old and new value spliced together? 🤔
  • Or… if another client reads that document while the write is in progress, will it see a partially updated value? 🤔

These updates would be quite confusing 🤷‍♂️

Storage engines and single object operations

So storage engines almost universally aim to provide atomicity isolation on the level of a single object such as a key value pair on one node.

Atomicity can be implemented using a log for crash recovery and isolation can be implemented using a lock on each object, allowing only one thread to access an object at any one time.

Some databases also provide, more complex atomic operations:

  • Such as an increment operation, which removes the need for a read modify writes cycle
  • Similarly popular, is a compare and set operation… which allows a write to happen only if the value has not concurrently change by someone else

✅ These single objection operations are useful as they prevent lost updates, when several client try to write the same object concurrently!

❌ However, there not transactions in the usual sense of the word

Compare and set and other single object operations have been dubbed light-weight transactions or even ACID for marketing purposes…

But that terminology is miss leading… a transaction is usually understood as an mechanism for grouping multiple operations on multiple objects into one unit of execution.

📚 Further Reading & Related Topics

If you’re exploring single object writes in databases, these related articles will provide deeper insights:

• Mastering Transaction Management in Databases – Learn about the principles of managing transactions in databases and how single object writes fit into broader transaction processing strategies for consistency and performance.

• Exploring Multi-Object Operations in Databases – Dive into how database systems handle multi-object writes and how they compare to single object writes in terms of efficiency and concurrency control.

Leave a comment

I’m Sean

Welcome to the Scalable Human blog. Just a software engineer writing about algo trading, AI, and books. I learn in public, use AI tools extensively, and share what works. Educational purposes only – not financial advice.

Let’s connect