WittCode💻

High Traffic Sites and Corrupt Data

By

High traffic sites can have corrupt data caused by concurrency issues. Learn how to handle concurrency issues on high traffic sites.

Table of Contents 📖

Concurrency Access and Data Corruption

When multiple users access and modify a database simultaneously, there is a chance that the data becomes corrupt. For example, imagine two database transactions start at the same time and both run the SQL statement below.

UPDATE post_likes SET like_count = like_count + 1 WHERE post_id = 1;

If these statements run at the same time, the starting like_count would be the same in each case. This means that the final like count will be incorrect. This is known as concurrent access or database concurrency issues. Concurrency issues can occur in high-traffic applications and can lead to problems such as lost updates, dirty reads, and other inconsistencies.

Transactions

One way of handling concurrency issues is to use transactions. Transactions are essential for ensuring that a group of database operations follow the ACID properties: Atomicity, Consistency, Isolation, and Durability.

BEGIN;
UPDATE post_likes SET like_count = like_count + 1 WHERE post_id = 1;
COMMIT;
  • BEGIN - starts a transaction. Once the transaction is started, the changes made to the database are not visible until the transaction is committed.
  • COMMIT - finalizes the transaction, making the changes permanent and visible to others.

ERROR: Note that some databases, like Postgres, run in a transactional context by default. However, for complex operations, we can group multiple statements into explicit transactions with BEGIN and COMMIT.

High Traffic Sites and Corrupt Data