Concurrency is a complex problem of the IT field. I'll try to explain some issues related to the concurrency and how to solve those issues.
Concurrency Issues:
- 🍎🍊 inconsistent reads - when the data is modified by a different actors. Depending on when you transaction starts reading the data and how log this transaction spans, you can get (read) different values.
- 💥🍎 lost updates - when 2 transactions are spanned different time and modifying the same entity. Last ended transaction will override first transaction result.
Solutions: Optimistic Lock:
🔓Optimistic Lock - concurrency control mechanism, which allows to write data simultaneously from the different transactions. It is called optimistic, because the assumption is that the collisions (conflict) will not be frequent.
- ✔️ better performance duo to improved concurrency in the system
- ❌ need to handle the collisions (conflicts)
Solutions: Pessimistic Lock:
🔒Pessimistic Lock - concurrency control mechanism, which locks the resources, processed by a transaction.
- ✔️ improves application durability and data constancy
- ❌ degrades the application performance and availability
- ❌ can create a Dead Lock
Avoiding Deadlocks:
☠️🔒 Is a situation, when locks are applied on resources, which are required in other transactions. If you have 2 transactions:
- acquires lock on a "Book" entity and later want to acquire lock on "Author" entity
- acquires lock on a "Author" entity and later want to acquire lock on "Book" entity In order to finish one transaction you need to acquire lock, on a resource, which is locked by other transaction. When none of transactions cannot be completed, this situation is called a Dead Lock.
Patterns of Enterprise Application Architecture: Martin Fowler, with Dave Rice, Matthew Foemmel, Edward Hieatt, Robert Mee, and Randy Stafford
Note: The content in this article is a summary/interpretation inspired by them. Proper attribution is given to the respective authors.