Skip to content

Latest commit

 

History

History
91 lines (58 loc) · 3.83 KB

README.md

File metadata and controls

91 lines (58 loc) · 3.83 KB

Project Goal

To demonstrate optimistic locking with .NET Core

locking-strategies-.net-7.0

Image Ref: Coditation

Scenario

To know about locking, we have to know a bit about concurrency. In simple, concurrency means when two things happen at the same time. If we consider this in an application, then inventory management system would be a good example where users try to update same product quantity or human resource management system where two HR people try to update the same employee info.

Now this becomes a mess as one update gets overwritten by the another and at the same time, doesn’t provide consistency. To handle such scenario, we use the term locking. There are two types of locking, depending upon use cases these could be applied.

  • Optimistic Locking
  • Pessimistic Locking

Optimistic Locking - With this locking strategy, we confirm that whenever there are two or more users trying to update the same data at the same time, then the later update is blocked asking the user for retry or showing some kind of message. In this strategy, table row data isn’t locked, rather it maintains a binary value for reference to reflect the changes by previous users.

Pessimistic Locking - This lock actually does a row-level lock in the database table preventing any other read or update during that period. Whenever this operation gets executed successfully, then other users are able to go through that data for modifications.

Built With

Environment & Development Tools:

  • .NET Core Version: 7.0
  • IDE: Visual Studio 2022
  • Framework: .NET Core Web Api
  • Backend: C# 11, ORM - Entity Framework Core Database First Approach
  • Database: MS SQL Server 2019

Getting Started

Initially the project will restore all the required nuget packages for the project. If not, the following will help to make it done manually.

For example, to install entity framework core and sql server package from nuget, the following command will download the specific version for the project.

Prerequisites

  • Nuget Package Manager
PM> Install-Package Microsoft.EntityFrameworkCore -Version 7.0.0
PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 7.0.0
  • SQL Server Database

In database table, add a column with timestamp datatype as follows and this will maintain a binary value to keep track of changes. This is required for optimistic locking.

For pessimistic locking, we omit the timestamp column and yep, that's all to get started.

locking-strategies-.net-7.0

  • JMeter

To test the api and see the demo of optimistic locking, you can use JMeter that I used. Here's a link that you can follow - JMeter Setup.

It's pretty handy and you can download it from here - Download JMeter.

When installation done, do the followings to work with the project as shown in the image:

Threads (Users) -> Thread Group HTTP Post Request

Authors

  • Amit Kanti Barua - Remote Software Engineer - Amit Kanti Barua - Built ReadME Template

Acknowledgements