HackerPost

ENGINEER

Are you a programmer? Then you should know about Optimistic Concurrency Control

Time Spent- 13m
177 Visitors

Optimistic Concurrency, also known as Optimistic Locking, emerges as a crucial mechanism for maintaining data consistency amid concurrent changes. As applications witness a surge in traffic and interactivity, a profound understanding and effective implementation of this concept become more critical than ever.


Why Programmers Should Care


Optimistic concurrency serves as a method for performing concurrency control, a crucial aspect in modern applications. In the upcoming sections, we delve into two prevalent concurrency control mechanisms, with a primary focus on optimistic concurrency. The aim is to provide insights catering to junior and senior developers.


Understanding Concurrency


Rob Pike, co-creator of the Plan 9 operating system and Golang, in his noteworthy "Concurrency is not parallelism" speech, defined concurrency as dealing with numerous tasks simultaneously. In essence, it involves handling multiple operations concurrently, such as responding to user input while simultaneously writing to a database.


The Concurrency Problem (Understanding with an example)


Consider a scenario where you live in a house with 10 rooms, and your mother locks one of these rooms, holding the key while she's away. This situation parallels the challenges faced in applications with concurrent data updates. If only one entity can modify a piece of data, there's no issue. However, when multiple entities can update simultaneously, ensuring consistent data becomes paramount.


Addressing the Problem


Imagine two individuals retrieving the same record from a server almost simultaneously, making changes, and assuming their updates were successful. The challenge arises when one person's changes overwrite the other, leading to potential data inconsistency. This is a classic race condition, where the last update wins, creating a need for concurrency control mechanisms.


Pessimistic Concurrency Control


Pessimistic concurrency control tackles the issue by preventing simultaneous updates through a locking mechanism. Drawing an analogy to a room locked by your mother, only one transaction can access the data at a time, ensuring consistency but potentially leading to bottlenecks or deadlocks.


Disadvantages of Pessimistic Concurrency


While effective, pessimistic concurrency has drawbacks akin to your mother constantly carrying the room key. Performance concerns, scalability challenges, and high resource consumption may arise due to the locking mechanism.


Optimistic Concurrency Control


Contrasting with pessimistic concurrency, optimistic concurrency control assumes conflicts are rare and can be resolved later. Instead of locking data during read or update, it checks for changes before saving. If another user has modified the data since it was read, an exception is thrown, requiring the current user to decide how to handle it.


Pessimistic vs. Optimistic Concurrency Control


Pessimistic control is likened to having a single key for a room, preventing simultaneous access for consistency but potentially causing bottlenecks. On the other hand, optimistic control allows more open access, resolving conflicts after they occur, similar to trusting family members not to disturb a room always.


Implementing Optimistic Concurrency with EF Core


Implementing optimistic concurrency involves strategic choices for developers using Entity Framework Core (EF Core). Here are two solutions:


Solution #1: Native Database-Generated Concurrency Tokens


If using SQL Server, a straightforward solution involves adding a `RowVersion` property to the entity, mapped to a `rowversion` column. This allows automatic changes in the database every time the row is modified.


Solution #2: Application-Managed Concurrency Tokens


For precise control over the concurrency token, developers can manage it in the application's code. This method is beneficial for databases like SQLite that lack auto-updating features. By configuring a property, such as `Version`, as a concurrency token, developers gain control over which column changes regenerate the token.


Conclusion


In this article, we get to know about optimistic concurrency, exploring its significance, advantages, and implementation strategies with EF Core. Understanding the nuances of concurrency control mechanisms is crucial for developers navigating the challenges posed by high-traffic, interactive applications. As the programming landscape evolves, embracing optimistic concurrency becomes not just a choice but a necessity for crafting robust and consistent systems.