Using Distributed Locking To Solve Race Conditions (and why a simple solution works better)

  Рет қаралды 16,949

Milan Jovanović

Milan Jovanović

Күн бұрын

Пікірлер: 77
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
Simplify your development process - download my free Clean Architecture template: bit.ly/3Andaly
@coding-in
@coding-in 5 ай бұрын
Thanks Milan, how to address if we need call validation without its command? I have downloaded the template, is there a docs guidelines?
@iteospace
@iteospace 4 ай бұрын
Hey, Milan how about distributed databases like Cassandra, ScyllaDb, YugabyteDb based on LSM Tree
@hybridloo
@hybridloo 5 ай бұрын
Milan, I love how you tackle core production use cases and scenarios that we all face every day, and how you solve them easily. Keep up the awesome work!
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
Thanks a lot! :)
@MTimothyWallace
@MTimothyWallace 4 ай бұрын
Here is a key combo you should start using. In Visual Studio Ctrl-K,S will bring up a context menu that allows you to select the structure with which you will surround the selected text.
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
Oh, that'll come in handy. Appreciate the tip!
@nouchance
@nouchance 5 ай бұрын
Dear Milan I like your coding style:)
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
Thanks, much appreciated! :)
@fieryscorpion
@fieryscorpion 3 ай бұрын
Could you please create a video that shows how to handle race conditions when multiple instances of database are involved? For eg: multiple instances of application and multiple instances of database.
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
A distributed transaction? No, screw that 😂
@ALOKSHARMAMD
@ALOKSHARMAMD 5 ай бұрын
nice, i didnt knew we could use when statements like these in c#. i have been using unique indexes similar to your solution but i threw whole exception without specifics , thanks.
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
Yeah, this is one of these useful C# features that not many people know of
@dhruvmathur5315
@dhruvmathur5315 5 ай бұрын
Thanks for the videos amazing content
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
Most welcome!
@JonathanGabrielSilva
@JonathanGabrielSilva 5 ай бұрын
Hey guys... someone can tell witch is de milan theme for visual studio? i love it, but didnt figure out...
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
@@JonathanGabrielSilva it's ReShaper, plus its syntax highlighting :)
@hailuong9295
@hailuong9295 5 ай бұрын
he use VS2022 default dark theme, but he also use Resharper so code color is little bit different than the stock one in VS2022
@JonathanGabrielSilva
@JonathanGabrielSilva 5 ай бұрын
thank you! now my code is almost beautful like milan's code... xP
@mdmoniruzzaman2922
@mdmoniruzzaman2922 5 ай бұрын
what do you think about optimistic lock for same last product ordering by two or more users in a distributed system?
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
If we already have an entity than optimistic concurrency should be fine
@baebcbcae
@baebcbcae 3 ай бұрын
so, what to do using mssql without unique index and redis in this case ?
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Distributed lock of some sorts
@jimmyahmed5619
@jimmyahmed5619 3 ай бұрын
What if you're using a database that doesn't support uniqu index like Azure Table Storage or Cosmos Db?
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Find a new database 😂
@ahmedameen2
@ahmedameen2 5 ай бұрын
Great video, can you tell me how to handle a use case where new item should have unique auto increment counter?
@poneciak57
@poneciak57 5 ай бұрын
Database should handle that depending on your database u can check for example SERIAL for postgres
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
Autoincrementing PK?
@ahmedameen2
@ahmedameen2 5 ай бұрын
@@MilanJovanovicTech it's auto increment field not primary+ this will be applied per tenant not global counter. So, there could be itemRefNum=5 with tenant x and itemRefNum=5 with tanant y
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
@@ahmedameen2 Is it important for it to be a counter? Each tenant has to start with 1, then 2, etc.?
@ahmedameen2
@ahmedameen2 5 ай бұрын
Yes unfortunately it's a business need ​@@MilanJovanovicTech
@kattamanchiarjun
@kattamanchiarjun 5 ай бұрын
Isn't the problem you stated can be sloved by eTag concept? Is distributed lock the right approach to solve this concurrency or race condition issue?
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
How would ETag solve this?
@jaimelannister141
@jaimelannister141 5 ай бұрын
I liked your style of presentation but I think you didn't really address the title of your video. It's about distributed locking and you assumed we had to stick to Postgres and after that resorted to a uniqueness index. Well, not every case of distributed locking happens when dealing with a relational database. I was looking for an in-depth explanation of something like the Redlock algorithm and an attempt to implement it.
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
I'll cover Redlock in a future (well-named) video!
@jaimelannister141
@jaimelannister141 5 ай бұрын
@@MilanJovanovicTech Thank you. I'm definitely looking forward to it
@sunzhang-d9v
@sunzhang-d9v 5 ай бұрын
Is this one better than Redis lock? Then what should I do if there is no unique field for inventory increase or decrease?
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
I wouldn't call it better, but it's definitely a simpler option if you're already using Postgres. No additional infrastructure needed. If there's no unique field, like in your case, use a lock.
@sunzhang-d9v
@sunzhang-d9v 5 ай бұрын
@@MilanJovanovicTech 👍
@betopolione.laura.gil.1
@betopolione.laura.gil.1 5 ай бұрын
Thanks for your content. I really like it. I have quetion regarding UseCase concept. I dont know if you are basing on clean arch (uncle Bob) cocept. If so, dont you think you are leaking tech/infra details into your usecase, with this implementation?
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
A use case is a pretty generic concept. One use case = one piece of functionality.
@betopolione.laura.gil.1
@betopolione.laura.gil.1 5 ай бұрын
@@MilanJovanovicTech yeah, thats is the reason I specified if you was baseing on Uncle Bob concept of usecase. From Uncle Bob words: "We do not expect this layer [usecases] to be affected by changes to externalities such as the [database], the UI, or any of the common frameworks. This layer is isolated from such concerns."
@cazorla82
@cazorla82 5 ай бұрын
How to use distributed lock in Sql Server instead of Postgre?
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
There are: - sp_getapplock (learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-getapplock-transact-sql) - sp_releaseapplock (learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-releaseapplock-transact-sql)
@aminmemar416
@aminmemar416 5 ай бұрын
so we can remove first email check if right?
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
Yes
@alexlo5655
@alexlo5655 5 ай бұрын
Hi Milan, thank you for the video. Can you present the same but with the Redis lock?
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
Yes!
@luizadolfo1
@luizadolfo1 5 ай бұрын
Considering your previous videos on using soft delete, this approach can generate errors when a user is deleted and a new user is registered with the same email.
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
But we're not using soft deletes here?
@hafizabdullah7488
@hafizabdullah7488 5 ай бұрын
Try a combination of email and the soft delete flag when creating the index
@akashrokade12
@akashrokade12 5 ай бұрын
In line 28 why have you used add method… when you are trying to update
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
Am I not creating a new user?
@atomicazure
@atomicazure 4 ай бұрын
🤣🤣🤣
@Maxim.Shiryaev
@Maxim.Shiryaev 5 ай бұрын
And the Users.Exists() check is redundant. By the way, ef core doesn't support unique constraints that makes it impossible to use deferred constraints when needed. Only unique indexes that can not be deferred.
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
I left it for fail fast principle, but yeah, it's redundant if we handle the concurrency exception
@androidsavior
@androidsavior 5 ай бұрын
Summary: use db unique index
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
TL;DW
@androidsavior
@androidsavior 5 ай бұрын
​@@MilanJovanovicTech 😅😅 I enjoyed it, i'm gonna try the redlock. Thank you
@TheScriptPunk
@TheScriptPunk 4 ай бұрын
I think....if you use an email verification process, you dont need to lock as it wouldnt be possible to register a duplicate email. You would only have the ability to verify a single email.
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
Race condition still? Who verifies first?
@KingOfBlades27
@KingOfBlades27 5 ай бұрын
That is indeed picky but true for sure 😅
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
Which approach would you use?
@Meryovi
@Meryovi 5 ай бұрын
... I was about to say that a unique index was probably a better option in this particular scenario, then you went ahead and did just that. Haha.
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
There we go 😁
@teothe
@teothe 5 ай бұрын
Sorry but isn't the purpose of a lock to ensure that whatever you have beneath it will succeed and not throw exceptions and/or have an invalid state in your data? You check if the email exists but then you depend on the infrustructure to enforce it? this is not simple it's, at best, inefficient, if not prone to ddos attacks use locks and if the app is in many instances just put the lock somewhere accessable for all
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
Also factor in the likelihood of a race condition here. It's pretty low. Locks are an overkill for this. Use a unique constraint.
@teothe
@teothe 5 ай бұрын
@@MilanJovanovicTech Depending on database exceptions is also an overkill
@herve952
@herve952 5 ай бұрын
Why would you add this extra complexity for a scenario that is really not likely to happen. I'd say near as likely as losing the connection to the db, which will throw also. The goal is not to prevent the error from happening, rather it is to enforce uniqueness. And what better place than the actual place the live in to do it.
@joga_bonito_aro
@joga_bonito_aro 5 ай бұрын
@@teothe I don't think this is meant for the gigazillion user base application. Rather for a really low user count that is easy to comprehend.
@teothe
@teothe 5 ай бұрын
@herve952 @joga_bonito_aro @MilanJovanovicTech just to clarify, i am not objecting to having a unique constrain on the db but it is one thing to have something to safeguard your persistant state and another to use this in the app the email uniqueness comes as a business requirement, and as such you must treat it and as early as possible if for the business is a very serious matter, you do a proper lock or a distributed one, if it is something that they don't really care, you just go with the db constrain but in any case you should never let exceptions define the behaviour of your app, an exception raised from classA and an exception raised from classB should always result in the exact same behaviour
@davearkley7014
@davearkley7014 5 ай бұрын
I know this is picky, but please consider calling an EmailAddress just that, not Email. An Email has a sender, recipient, subject. body etc.
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
The Email on the User entity?
@joga_bonito_aro
@joga_bonito_aro 5 ай бұрын
I hate nitpick commits.
@cheeseburger1884
@cheeseburger1884 5 ай бұрын
never thought about it that way :o
Are You Accidentally Crippling Your EF Core Queries?
17:18
Milan Jovanović
Рет қаралды 32 М.
#behindthescenes @CrissaJackson
0:11
Happy Kelli
Рет қаралды 27 МЛН
JISOO - ‘꽃(FLOWER)’ M/V
3:05
BLACKPINK
Рет қаралды 137 МЛН
Caleb Pressley Shows TSA How It’s Done
0:28
Barstool Sports
Рет қаралды 60 МЛН
Why I Don't Like Singletons
29:05
The Cherno
Рет қаралды 101 М.
How Distributed Lock works | ft Redis | System Design
10:24
ByteMonk
Рет қаралды 13 М.
Exceptions Are Extremely Expensive… Do This Instead
17:15
Milan Jovanović
Рет қаралды 52 М.
10 Signs Your Software Project Is Heading For FAILURE
17:59
Continuous Delivery
Рет қаралды 38 М.
Microservices with Databases can be challenging...
20:52
Software Developer Diaries
Рет қаралды 128 М.
The Problem With UUIDs… And How To Solve It
18:04
Milan Jovanović
Рет қаралды 15 М.
Completely Get Rid of Exceptions Using This Technique
19:24
Milan Jovanović
Рет қаралды 28 М.
#behindthescenes @CrissaJackson
0:11
Happy Kelli
Рет қаралды 27 МЛН