Discussing two popular data locking methods: Record Locking and Optimistic Locking

Discussing two popular data locking methods: Record Locking and Optimistic Locking

Daily short news for you
  • For over a week now, I haven't posted anything, not because I have nothing to write about, but because I'm looking for ways to distribute more valuable content in this rapidly exploding AI era.

    As I shared earlier this year, the number of visitors to my blog is gradually declining. When I looked at the statistics, the number of users in the first six months of 2025 has dropped by 30% compared to the same period last year, and by 15% compared to the last six months of 2024. This indicates a reality that users are gradually leaving. What is the reason for this?

    I think the biggest reason is that user habits have changed. They primarily discover the blog through search engines, with Google being the largest. Almost half of the users return to the blog without going through the search step. This is a positive signal, but it's still not enough to increase the number of new users. Not to mention that now, Google has launched the AI Search Labs feature, which means AI displays summarized content when users search, further reducing the likelihood of users accessing the website. Interestingly, when Search Labs was introduced, English articles have taken over the rankings for the most accessed content.

    My articles are usually very long, sometimes reaching up to 2000 words. Writing such an article takes a lot of time. It's normal for many articles to go unread. I know and accept this because not everyone encounters the issues being discussed. For me, writing is a way to cultivate patience and thoughtfulness. Being able to help someone through my writing is a wonderful thing.

    Therefore, I am thinking of focusing on shorter and medium-length content to be able to write more. Long content will only be used when I want to write in detail or delve deeply into a particular topic. So, I am looking for ways to redesign the blog. Everyone, please stay tuned! 😄

    » Read more
  • CloudFlare has introduced the pay per crawl feature to charge for each time AI "crawls" data from your website. What does that mean 🤔?

    The purpose of SEO is to help search engines see the website. When users search for relevant content, your website appears in the search results. This is almost a win-win situation where Google helps more people discover your site, and in return, Google gets more users.

    Now, the game with AI Agents is different. AI Agents have to actively seek out information sources and conveniently "crawl" your data, then mix it up or do something with it that we can't even know. So this is almost a game that benefits only one side 🤔!?

    CloudFlare's move is to make AI Agents pay for each time they retrieve data from your website. If they don’t pay, then I won’t let them read my data. Something like that. Let’s wait a bit longer and see 🤓.

    » Read more
  • Continuing to update on the lawsuit between the Deno group and Oracle over the name JavaScript: It seems that Deno is at a disadvantage as the court has dismissed the Deno group's complaint. However, in August, they (Oracle) must be held accountable for each reason, acknowledging or denying the allegations presented by the Deno group in the lawsuit.

    JavaScript™ Trademark Update

    » Read more

Problem

Locking records while reading or updating data is a relatively common occurrence. A classic example for this case is a money transfer problem between A and B: While A is transferring an amount x to B, we need to check the balance of A, subtract x dollars from their account, and then add x dollars to B's account. It is essential to ensure that all these debit and credit processes are successful for the transaction to be completed. If an error occurs while deducting money from A without adding it to B or vice versa, it would result in a significant accuracy issue for the program.

The typical order to execute the queries in this case with PostgreSQL would be:

BEGIN TRANSACTION;

SELECT balance FROM accounts WHERE user = 'A';

// if a > x then...  

UPDATE accounts SET balance = balance - x WHERE user = 'A';
UPDATE accounts SET balance = balance + x WHERE user = 'B';

COMMIT;

Most of us know how to solve this problem using transactions. It involves initiating a transaction and ensuring that both the debit and credit processes are successful before considering the money transfer as a successful transaction and saving it in the database. If, unfortunately, one of the processes fails, nobody would be deducted or credited with money illogically.

However, in cases where A can perform multiple money transfer operations almost simultaneously, another issue arises. This issue relates to the first SELECT statement used to check the balance. It is highly possible that multiple SELECT statements return the balance of A before checking it. In this case, all those SELECT statements would satisfy the condition a > x and what if they all proceed with the subsequent UPDATE statements?

To address this issue, there are several approaches. One of them is to lock the record being selected using the SELECT FOR UPDATE query. If a subsequent query encounters the SELECT on user A, it has to wait in a queue until the first query completes. This technique is called Record Locking. Alternatively, we also have another approach called Optimistic Locking. In today's article, I will discuss these two data locking methods to see how they work, their pros and cons, and when to use them.

Record Locking

Record Locking is a concurrency control technique in databases, where records are locked to ensure consistency and prevent multiple transactions from accessing the same record simultaneously.

When a transaction wants to update or read data from a record, it requests the database to lock that record to prevent other transactions from accessing it. After the transaction completes, it releases the lock, allowing other transactions to access it.

For example, in PostgreSQL, SELECT FOR UPDATE is a way to lock the records being queried. By replacing:

SELECT balance FROM accounts WHERE user = 'A';

With:

SELECT balance FROM accounts WHERE user = 'A' FOR UPDATE;

The first transaction will immediately lock the record with user = 'A', and subsequent transactions cannot proceed with reading until a successful COMMIT or ROLLBACK command is executed.

Record Locking ensures data consistency, but it can lead to deadlock situations. Therefore, the use of Record Locking needs to be carefully considered to ensure database performance and data consistency. To understand more about locking types and deadlocks, I recommend reading the article Understanding Lock Types (Explicit Locking) in PostgreSQL (written in Vietnamese).

Optimistic Locking

Optimistic Locking is a method in which transactions do not lock any records, allowing them to perform normally. As the name suggests, the idea behind Optimistic Locking is to assume that transactions accessing the same record will not update it simultaneously, and only one of those transactions will complete the data update.

When a transaction wants to update data, it does not lock the record to prevent other transactions from accessing it. Instead, it checks the state of the record before the update. If the state of the record hasn't been changed by other transactions, it proceeds with the update. Otherwise, if the state of the record has been changed, it rolls back the update.

To implement Optimistic Locking, we need to add a field to mark the update, such as version, updated_at, or any other field to be updated alongside the record.

For example, in the accounts table, we can add an updated_at field representing the time when the record was successfully updated. We no longer need SELECT FOR UPDATE, and instead, we will select updated_at in addition to balance.

SELECT balance, updated_at FROM accounts WHERE user = 'A';

Then, we perform a "conditional" update on updated_at:

UPDATE accounts SET balance = balance - x, updated_at = now() WHERE user = 'A' AND updated_at = updated_at;

Here, updated_at is the result of the SELECT statement within the transaction.

To explain the principle simply, because the UPDATE locks the record during data update, only one transaction at a time is allowed to update it. Subsequent transactions trying to update the data with the old updated_at essentially find no matching records. At that point, we can handle this case as a failed transaction.

Optimistic Locking is simple and effective in scenarios where data update transactions do not occur too frequently. However, it does not guarantee data consistency or carry a higher risk of causing errors as it is often handled at the application layer through programming code.

When to apply?

As always, the choice between Record Locking and Optimistic Locking depends on specific use cases, as each method has its advantages, disadvantages, and specific contexts. However, you can rely on some guidelines below to increase your decision-making capability.

Use Record Locking when:

  • Data consistency is the highest priority.
  • Data update transactions occur frequently or there are multiple transactions updating the same record simultaneously.

Use Optimistic Locking when:

  • Data consistency is not a strict requirement, and the application needs to prioritize performance and transaction execution speed.
  • Data update transactions occur infrequently or there are no multiple transactions updating the same record simultaneously.

Furthermore, in some cases, it is possible to combine both techniques to achieve an optimal solution. For example, using Optimistic Locking for reading transactions and Record Locking for data update transactions. This helps optimize performance and data consistency in the database system.

Conclusion

In database systems, there are two popular data locking methods: Optimistic Locking and Record Locking. While Optimistic Locking assumes that transactions accessing the same record will not update data simultaneously, Record Locking "quickly" locks the records to ensure that the first transaction has the right to access and prevent other transactions from accessing. Each method has its own pros and cons, so its application should be tailored to the specific use case.

Premium
Hello

Me & the desire to "play with words"

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (0)

Leave a comment...