Concurrency phenomena are anomalies that every software developer should know. Learn about these in this excerpt from the book MariaDB for Developers.Concurrency phenomena are anomalies that every software developer should know. Learn about these in this excerpt from the book MariaDB for Developers.

Database Transactions: Everything That Can Go Wrong When Using Them

2025/10/07 05:18
3분 읽기
이 콘텐츠에 대한 의견이나 우려 사항이 있으시면 crypto.news@mexc.com으로 연락주시기 바랍니다

Note: This is an excerpt from an unedited version of my book MariaDB for Developers.


To this point, we have understood the concept of atomicity—either all operations succeed or none do. What can go wrong? It seems like we are covered. And we are. Until we introduce concurrency in our system. MariaDB is one of the most highly performant database systems and tries to parallelize processing to increase throughput.

\ Parallelizing means that MariaDB can execute transactions from different sessions at the same time by interleaving operations from different transactions instead of waiting for one to finish before starting the next. Each transaction has its own sequence of operations, but MariaDB executes them in overlapping order. Figure 8-2 shows two transactions (A and B) and multiple database operations interleaved through time.

Figure 8-2: Interleaving database operations for parallelism.

This interleaving allows MariaDB to use CPU and I/O resources more efficiently than without parallelism. This, however, opens the door to subtle problems when the parallel transactions read and write overlapping data. Let's study some of these problems known as concurrency phenomena.

Dirty Reads

Friday afternoon, and we've got a winner! Our to-do application—which by chapter 6 became more of a project management tool than a to-do app—is so central to the business that prizes are given to users who excelled at reporting bugs or helping its development. Our to-do application allows the HR team to grant prizes to users, and this use case involves reducing the quantity of the awarded prize in the prizes table.

\ Janet and Moe, both from HR, are using our to-do app at the same time. Janet is about to grant today's prize (named "Bagelers" in our database), while Moe is viewing a dashboard that shows an overview of the prize inventory. Jane selects the winner and the prize, and clicks on "Grant prize." Our to-do app starts a new transaction that decreases the quantity for Bagelers from 8 to 7.

\ At that moment, Moe refreshes the dashboard and sees that there are 7 Bagelers. However, the system crashes, and since the transaction was never committed, the new quantity is not written to disk. Jane gets an error, but Moe doesn't. To him, there are 7 Bagelers. He is seeing incorrect data. This is called a dirty read. Figure 8-3 shows an example of the sequence of operations that lead to a dirty read at time t3.

Figure 8-3: Example of dirty read phenomena.

Non-Repeatable Reads

A similar situation can occur when a transaction reads a value twice, but such value is modified between the reads by another transaction. In this case, the second read would obtain a different value. This phenomenon is called a non-repeatable read and can lead to incorrect results if the values are used for other calculations in the same transaction. Figure 8-4 shows a non-repeatable read at time t5.

Figure 8-4: Example of non-repeatable read phenomena.

Phantom Reads

If the write operation in the previous example implies inserting rows, we get what's called a phantom read phenomenon. Figure 8-5 shows a phantom read at time t5.

Figure 8-5: Example of phantom read phenomena.

시장 기회
Brainedge 로고
Brainedge 가격(LEARN)
$0.006708
$0.006708$0.006708
+0.70%
USD
Brainedge (LEARN) 실시간 가격 차트
면책 조항: 본 사이트에 재게시된 글들은 공개 플랫폼에서 가져온 것으로 정보 제공 목적으로만 제공됩니다. 이는 반드시 MEXC의 견해를 반영하는 것은 아닙니다. 모든 권리는 원저자에게 있습니다. 제3자의 권리를 침해하는 콘텐츠가 있다고 판단될 경우, crypto.news@mexc.com으로 연락하여 삭제 요청을 해주시기 바랍니다. MEXC는 콘텐츠의 정확성, 완전성 또는 시의적절성에 대해 어떠한 보증도 하지 않으며, 제공된 정보에 기반하여 취해진 어떠한 조치에 대해서도 책임을 지지 않습니다. 본 콘텐츠는 금융, 법률 또는 기타 전문적인 조언을 구성하지 않으며, MEXC의 추천이나 보증으로 간주되어서는 안 됩니다.

USD1 Genesis: 0 Fees + 12% APR

USD1 Genesis: 0 Fees + 12% APRUSD1 Genesis: 0 Fees + 12% APR

New users: stake for up to 600% APR. Limited time!