System Design: Concurrency Control in Distributed System | Optimistic & Pessimistic Concurrency Lock

Поділитися
Вставка
  • Опубліковано 27 вер 2024

КОМЕНТАРІ • 269

  • @ConceptandCoding
    @ConceptandCoding  Рік тому +51

    In Isolation Level Table: I meant Concurrency but instead i wrote Consistency.
    Read Uncommitted has high Concurrency and Serializable has Least Concurrency.

    • @akramwasim805
      @akramwasim805 Рік тому +1

      I was about to post it 😀

    • @panmenia
      @panmenia Рік тому

      hi Shreyansh. Great video. When is the 2 phase locking video coming? And do you mean 2 phase commit by 2 phase locking?

    • @ConceptandCoding
      @ConceptandCoding  Рік тому

      @@panmenia no two phase commit is different and two phase locking is different

    • @tarunkundhiya5196
      @tarunkundhiya5196 Рік тому +3

      @shreyansh you can consider adding an on-video note.

    • @basantrawat4279
      @basantrawat4279 Рік тому +3

      @@ConceptandCoding Hi sir because of you I was able to crack lld ,hld rounds in companies like amex,paytm payments bank, cars24,clearTax and all and joiner paytm payments bank.Thank you so much sir for all your videos and content

  • @Shivam-wk6sq
    @Shivam-wk6sq 2 місяці тому +6

    This is one of the finest indepth tutorials out there.. Trust me no-one teaches these things with this much level of depth and practical knowledge. Even I have 5 years of experience working at Flipkart but still this cleared my concepts. I feel the premium is so much useful of your channel Shrayansh.. Keeping doing this.

  • @basantrawat4279
    @basantrawat4279 Рік тому +35

    Hi sir because of you I was able to crack lld ,hld rounds in companies like amex,paytm payments bank, cars24,clearTax and all and joiner paytm payments bank.Thank you so much sir for all your videos and content

    • @car_holic1995
      @car_holic1995 Рік тому +2

      Congratulations bhai...can I get your mail for some guidance bhai.

    • @tusharbaheti1648
      @tusharbaheti1648 3 місяці тому

      how did u get the interview calls ?

  • @moatasim7
    @moatasim7 2 місяці тому +1

    this is probably one of the most comprehensive tech videos, hands down

  • @SurajSingh-tz1wr
    @SurajSingh-tz1wr 2 місяці тому +2

    This video cleared all my problems on how to handle concurrency in production as well as interviews :)

  • @hakariRonaldo
    @hakariRonaldo 3 дні тому +1

    Superb job done in imparting the concepts. You have an amazing talent in simplifying and detailing things.

  • @tejass5060
    @tejass5060 Рік тому +9

    Hey Shreyansh,
    Thanks for discussing the important topic it'll definitely help a lot of backend engineers.
    I just wanted to point something on consistency, I think consistency is low for "Read Uncommitted" isolation level and High for "Serializable"

    • @ConceptandCoding
      @ConceptandCoding  Рік тому +2

      Nope it's opposite. Serializable has the least concurrency as it use range lock.

    • @vivekjha6990
      @vivekjha6990 Рік тому +2

      Actually it is written "consistency" in the notes, but what Shrayansh meant is "concurrency"..

    • @tejass5060
      @tejass5060 Рік тому +1

      @@ConceptandCoding Yes, in notes it is mentioned as "consistency" next to the table but it should have been "concurrency".
      Anyways, mentioning it here so that people can get clarity on that.

    • @ConceptandCoding
      @ConceptandCoding  Рік тому +3

      Oops my bad. Just now saw it again. Sorry for this

    • @tejass5060
      @tejass5060 Рік тому +2

      No need to be sorry you have done a good job discussing this topic we appreciate your efforts and corrections can be done in the comments section.

  • @saurabhtiwari9614
    @saurabhtiwari9614 Рік тому +2

    i'm really blessed for having u as my teacher and thanks for clearing all my doubts. thank u soo soo much bhayiyaaa

  • @ydayanandareddy7283
    @ydayanandareddy7283 2 місяці тому

    I have watched your concurrancy control mechanisms , Book my Show design and Cap theorem . Due to lack of time before my interview , i just sticked to watch limited videos . As expected they asked me about concurrancy control , and book my show follow up question and syncronized block . Interviewer is very happy about my answers . Finally i have cleared my dream designation . Thank you soo much . Felt like its worth to buy memebership.🎉🎉🎉

  • @shrutikamboj4607
    @shrutikamboj4607 9 місяців тому +1

    Extremely underrated playlist!! So happy to stumble upon it.

  • @theunusual4566
    @theunusual4566 10 днів тому +4

    The IT Community is literally blessed by Having People like, who are having quest for knowledge and understanding and more intense quest of Sharing that knowledge.
    Thanks a ton Shreyans.

  • @albertoalvarez2028
    @albertoalvarez2028 Місяць тому +1

    Thanks a lot for sharing your knowledge, you´re the best professor I ever had.

  • @ANSHULGUPTA880
    @ANSHULGUPTA880 Рік тому +4

    Thanks bro for this video.
    1 Question, isolation level is working fine multiple application instances. But what if DB is also distributed, with active-active connection (2 primary DB)?

  • @bangbang86
    @bangbang86 Рік тому +3

    I appreciate you sharing knowledge.
    I think optimistic locking is essentially lock free, decision to commit or rollback depends on version values
    In your video you have mentioned that optimistic locking uses SELECT FOR UPDATE, this is inaccurate, taking a lock means pessimistic locking

    • @ConceptandCoding
      @ConceptandCoding  Рік тому

      I am not inline with it buddy.
      While comparing the version, that time you have to take lock else how you will handle the scenario when 2 request read and compare the version with DB at same time too?
      Both will go ahead and update the DB and "Lost Update" issue will arise.
      But glad you raised the point.

    • @bangbang86
      @bangbang86 Рік тому +1

      @@ConceptandCoding the way it is done is let say we have 2 sessions trying to update a row with primary key R1 and with Version V10
      Both sessions will read V10 version and will try to update the row using following UPDATE SQL pattern
      UPDATE table SET someCol=someVal
      WHERE primaryKey=R1 AND version=V10
      Since both sessions are firing the SQL, there will be a race condition and one will succeed and update one row while the other will update 0 rows, the session which updated 0 rows now knows that its update failed and it throws an exception(ORM such as Hibernate throw OptimisticLockException)
      As a programmer we can choose to retry this request being OPTIMISTIC that there will not a race condition again and its request will succeed this time.

    • @ConceptandCoding
      @ConceptandCoding  Рік тому

      @@bangbang86 make sense, as DB for each insert/update puts lock and during race condition one will fail.
      Generally handling this at application is faster than at DB.
      As you know that, in real code, application never directly connects with DB, there is one mediator which do lot of stuff like grouping of queries, diverting the query so that one DB server do not get overload etc.
      I am totally agree with your point, but i still feel handling this at application layer is much better.
      What do you think?

    • @bangbang86
      @bangbang86 Рік тому

      @@ConceptandCoding in any real world application there will be at least two application servers for fault tolerance and in such a scenario if the two requests working on same entity for e.g. user bank balance row, land on different app servers then it is impossible to do concurrency handling on app side since the conflicting requests lie outside the scope of each app server. This is why concurrency handling on common/converging part such as DB or any datastore is a better option as it has context of every request and is the place which stores the state of the entity. Hope I was able to explain my thought.

    • @ConceptandCoding
      @ConceptandCoding  Рік тому

      Agree, and that's why we are discussing distributed locking mechanism (lock on DB rather than distributed synchronised which app server do).
      But we both agree here now, optimistic is not actually Lock free. It does put a lock during update/insert :)

  • @kumarashutosh229
    @kumarashutosh229 Рік тому

    Nice stuff!
    To add to it, the `Serializable` isolation level has the lowest efficiency/concurrency because it processes the tasks submitted to it in a sequential or serial fashion. It picks the tasks in the `ORDER OF SUBMISION` from the queue, and that's why it guarantees correctness.

  • @anshulkatare
    @anshulkatare 27 днів тому

    Hi, Can you elaborate on Snapshot isolation level, what is the locking strategy used in this isolation level.
    And some more anamolies like read skew, write skew, lost update as well.

  • @deepikachaudhary1022
    @deepikachaudhary1022 2 місяці тому +1

    Amazing explanation,Thanks Shrayansh.

  • @SaiPhaniRam
    @SaiPhaniRam 3 місяці тому

    Hi sir, A quick question on RANGE LOCKING.
    "In case of Serializable Isolation Level - What is the purpose of locking neighboring rows via Range Locking?"
    Scenario: SERIALIZABLE ISOLATION LEVEL
    - In a banking setup, customer IDs with 1 - 10 have been queried/ Read in a transaction say "Txn A". ----- SHARED LOCK (S) IS APPLIED.
    - As per RANGE LOCKING, even the neighboring rows (customer's record with ID = 11) also gets locked. [LOCKED 1 - 11 RECORDS]
    - In a different transaction "Txn B", customer ID = 11 wants to transfer some money to customer X.
    - But Customer 11 is unable to send money as his record is in SHARED LOCKING & Once there is a SHARED LOCK, we cannot have an EXCLUSIVE LOCK on top of it. (As transferring money requires UPDATING Customer 11's record)
    To summarize, a query operation which does not involve Customer 11 is affecting his ability to transfer money.
    Could you please help me understand this (or) correct me if I am wrong?
    Thank you.

  • @vishnuvadhandevandla4110
    @vishnuvadhandevandla4110 Рік тому +1

    really you way of explanation awesome 👌now i got very much clarity how transactions happening because i saw so many videos this much explanation i never seen , really thanks so much once again , one request pls provide English version of lld

    • @ConceptandCoding
      @ConceptandCoding  Рік тому

      Thanks, all latest videos of LLD are in English only and few initial which are in hindi, i have explained in English in LLD live playlist

  • @CodeWithSahilBatra
    @CodeWithSahilBatra 6 днів тому

    There is a big mistake in the video, when you have explained optimistic locking. You have taken two transaction and on each write you are changing the version. Version should only be changed after commit and not write operation. Ideally all these changes are happening in memory and both the transactions are independent before the final commit.

  • @abhinavsinghal1530
    @abhinavsinghal1530 7 місяців тому +1

    For read uncommitted, why does it have highest consistency?? Shouldn't it be the lowest?

  • @vineettalashi
    @vineettalashi 8 місяців тому +1

    Thank you for this very informative and knowledgeable session.

  • @Newbie789
    @Newbie789 8 місяців тому +1

    Shaandar jabardust jindabaad very clear explanation thank you 🙏🙏

  • @abhishekdubey9554
    @abhishekdubey9554 9 днів тому

    For range lock, can we say that the entire table is getting locked for a single transaction and as a result concurrency is lowest as other transactions have to wait ? Please correct me if I am wrong.

  • @AtharvaRao0104
    @AtharvaRao0104 4 місяці тому

    Also at the end, when you show optimistic concurrency control can prevent deadlocks -
    If the isolation level was SSI instead of read committed:
    In transaction 2, The read of B will be outdated because of the commit by T1 as there is a new version of B.
    If a stale value of B was used to make some decision and that decision involves writing to A, then the decision becomes invalid.
    I think, the algorithm of optimistic concurrency control atleast in SSI will detect that the read of B has become stale and T2 will be aborted and T2 would have to be retried.

    • @aastha6911
      @aastha6911 Місяць тому

      @Shrayansh Jain, I also have the same doubt. Please help clarify.

  • @baburaomulaparthi4531
    @baburaomulaparthi4531 2 місяці тому

    Bro can you please add programatic example on Pessimaric and Opstimatic locking.

  • @sashibhushanarajput1194
    @sashibhushanarajput1194 20 днів тому +1

    Hey bro, you do a nice job but the thing is at a lot of places you say things which are inconsistent with what you yourself tell earlier. Please try to self review your videos before publishing it. Just a suggestion I understand this might take more effort but when you are taking responsibility for sharing knowledge then misinformation and causing confusion should be avoided.

  • @ManishChauhan1095cs
    @ManishChauhan1095cs Місяць тому

    Hi Shreyansh , I wanted to understand , how a trasaction can read a uncommited change done by another transaction , is this the case of nested transactions? since if these 2 transactions are being performed by different threads , the read should be same until the change is commited by any one transaction. Please clarify

  • @rahulsaini202
    @rahulsaini202 Рік тому

    @ConceptandCoding informative Video, depth explained. Please share the notes. It will be handy during the preparation of interviews. 🙏

  • @av21015
    @av21015 Рік тому

    You have such an in-depth knowledge, great video. I have this doubt when you said no chance of deadlock in OCC when TA has to write on row 1,2 and TB has to write on 2,1 after first step if TA takes exclusive lock on 1 and TB takes exclusive lock on 2 and for second step both cant proceed as the shared resources needed are locked then in that case we get a deadlock even in OCC.

    • @ConceptandCoding
      @ConceptandCoding  Рік тому +2

      Yes, i think the best way to say is OOC reduce the probability of deadlock, rather than it fully removes it.

  • @balushuprakash-qu9pm
    @balushuprakash-qu9pm 2 місяці тому

    I am trying to implement this project as a part of my resume. I am a fresher with 0 experience. Which type of locking should i add in my spring boot Application.Optimistic or Pessimistic.?

  • @saanikagupta1508
    @saanikagupta1508 Місяць тому

    At 5:30 you mentioned how synchronized will not work in distributed scenario with multiple processes (because no shared memory between processes). And then you mentioned that for Distributed environment we've Distributed Concurrency Control. But you missed telling how exactly this can help in distributed environment as no shared memory will still be a concern here no matter what locks we use.
    So as per my understanding, in Optimistic Concurrency Control, systems use versioning or timestamps to track changes and ensure consistency *across different nodes* before committing the transaction.
    In Pessimistic Concurrency Control, a transaction might acquire a lock via *Zookeeper*, ensuring that no other transaction across any node can access the locked data until the lock is released.

  • @ManishKumar-zt5sk
    @ManishKumar-zt5sk Місяць тому

    In one of the interview when I explained about distributed concurrency using OCC, the interviewer said these all stuffs are theoretical and for interview preparation theories and later got rejected.

  • @dimplamen
    @dimplamen 6 місяців тому

    Well done, a really well structured and clear explanation!
    PS: A little bit confused by the "consistency" scale decreasing while the isolation level is increasing. Perhaps you really meant "concurrency" there?

  • @manishajhunjhunwala2326
    @manishajhunjhunwala2326 2 місяці тому

    Hi @ConceptandCoding
    I have one question, so for read committed you mentioned that dirty read is prevented as a write lock is acquired by one transaction and no other transaction can read it during then
    But wanted to understand what happens when 2 transactions at the sane time try to read and write, so which transaction would get the lock first and how is this decided?

  • @pavankumar-cy6mg
    @pavankumar-cy6mg 8 місяців тому +2

    hi, @conceptandcoding, in OCC you have said that no deadlock is possible, take this case ID:1 and ID:2 ,Trans-A has acquired ex-lock on ID1 and trans-B has acquired ex-lock on ID2 after that Trans-A wants to put a shared lock on the ID2 and Trans-B wants to put a lock on ID1, in this case it is a deadlock right?

    • @ConceptandCoding
      @ConceptandCoding  8 місяців тому

      hi Pavan, pls check in comment section my discussion with @bangbang86 member. That will clarify your doubts fully. let me know if you able to find and got your answer.

    • @pavankumar-cy6mg
      @pavankumar-cy6mg 8 місяців тому

      @@ConceptandCoding i gone through all the conversation, it means that OCC never acquires the lock at all and it only checks while writing the data has been modified or not? am i right

    • @ConceptandCoding
      @ConceptandCoding  8 місяців тому

      @@pavankumar-cy6mg yes lock is done at DB level but application do not put the lock, application just add the row version in the query

    • @pavankumar-cy6mg
      @pavankumar-cy6mg 8 місяців тому

      @@ConceptandCoding and we could say then the Read committed does not comes under OCC, there is mistake in video

    • @ConceptandCoding
      @ConceptandCoding  8 місяців тому

      @@pavankumar-cy6mg no OCC does help to achieve the isolation level below repeatable read.
      Only part is, application do not explicitly put lock, it adds the db row version in the query. thats it.

  • @smitchaudhari9783
    @smitchaudhari9783 3 місяці тому

    Hello Shrayansh I think you inverted the consistency in seralisable we get high consistency or you can use availability instead.

  • @shubhampatidar6116
    @shubhampatidar6116 5 місяців тому +1

    Thanks Shrayanah Bhai

  • @akramwasim805
    @akramwasim805 Рік тому +1

    Nice Explanation, Keep up the momentum.

  • @Vinod-zs4dj
    @Vinod-zs4dj 5 місяців тому

    how can you say that consistency is high on read uncommitted isolation ?? let say transaction t1 and t2 is there, and t2 made some change and didn't commit and rolled that back so dirty read would be there by t1 which an inconsistency case because latest changes are not there in DB.

  • @anusuyaganguly6009
    @anusuyaganguly6009 Рік тому

    Very useful content .Waiting for your 2-phase locking session

  • @durgeshr3681
    @durgeshr3681 2 місяці тому +1

    Nice explanation thanks!!

  • @сойка-и8й
    @сойка-и8й Рік тому +1

    Outstanding video Sir, and the way you explain in depth about the problem and solution is exceptional❤❤

  • @UtkarshSingh-cb8fq
    @UtkarshSingh-cb8fq Рік тому

    Where are we applying these Locks? On Db-Level or Code-Level( duty of AppDev or DBDev) ? How does the code look like ?
    2 more requests:
    - Can you tell What Common Concurrency-questions, that are asked in interviews ?
    - Pls explain Class-level & Object-level lock.

    • @ConceptandCoding
      @ConceptandCoding  Рік тому +1

      Transaction and isolation level we have to define at code level, locks is at DB level.
      This is the most frequently asked interview question in concurrency buddy.

    • @UtkarshSingh-cb8fq
      @UtkarshSingh-cb8fq Рік тому

      @@ConceptandCoding for isolation we will be writing code to get the shared / exclusive lock ? How does that look in Java?
      What are good books to refer concurrency ?

    • @ConceptandCoding
      @ConceptandCoding  Рік тому +2

      @@UtkarshSingh-cb8fq any SQL book is okay for it.
      When you do Select query it put shared lock.
      When you use update, delete, select for update it put exclusive lock

    • @UtkarshSingh-cb8fq
      @UtkarshSingh-cb8fq Рік тому

      @@ConceptandCoding so when you say put shared lock at start of Read and hold it till transaction completes , then it will be DB property/responsibility to hold shared lock until transaction completed? And how DB knows about the transaction , like how many statements/operations are present in a transaction

    • @ConceptandCoding
      @ConceptandCoding  Рік тому

      @@UtkarshSingh-cb8fq DB does not know about how Many statements, DB know about Transaction start or aborted or committed

  • @rkreddy2699
    @rkreddy2699 Місяць тому

    Hey why is deadlock not possible in OCC, in if it used read committed, it still acquires X lock when write and if another transaction acquires lock for another row deadlock is still possible right?
    Also can u share the notes for sys des as well?

    • @ConceptandCoding
      @ConceptandCoding  Місяць тому

      Read Phase: The application reads the data along with a version number or timestamp.
      Compute Phase: The application processes the data without holding any locks.
      Validation Phase: Before committing the transaction, the application checks if the data has been modified by another transaction using the version number or timestamp. If the data has changed, the transaction is aborted and retried.
      Why Deadlocks are Unlikely in OCC
      Short-Lived Locks: In OCC, locks are typically held only during the brief period of the commit phase. This minimizes the window in which a deadlock can occur.
      Conflict Detection: Instead of waiting for locks to be released (which is a primary cause of deadlocks), transactions in OCC check for conflicts and either proceed or abort based on version checks.
      Scenario Where Deadlocks Can Occur
      Even in OCC, deadlocks can occur If two transactions try to acquire locks on multiple resources simultaneously during the commit phase, a deadlock can occur. But very unlikely because of extra validation phase.

  • @aeshwer
    @aeshwer Місяць тому

    How is phantom read issue occurring in Repeatable isolation when the read lock is for the transaction? can anyone answer this

    • @ConceptandCoding
      @ConceptandCoding  Місяць тому

      it do not put range lock. if any new row is inserted in between.

  • @Harsh-Choudhary451
    @Harsh-Choudhary451 Місяць тому

    Can anyone tell... Where are the notes

  • @manoharmanu9240
    @manoharmanu9240 6 місяців тому +1

    Great explanation👏👏

  • @fooballers7883
    @fooballers7883 8 місяців тому

    Thanks for a great tutorial... I can feel the passion in your teaching....Any one interested in forming a group to learn... as I am just starting out would be a great help

  • @AbhishekJha-sx2yv
    @AbhishekJha-sx2yv 3 місяці тому

    can we control the updation or versioning?
    since there's a possibility that the column which were update by txn A could be irrelevant for txn-B
    txn-B was trying to update some other column against the same row, but if txn - A changes the version, txn-B rolled back which is incorrect right.

  • @jayak3768
    @jayak3768 11 місяців тому

    Next to your table for transaction Isolation, it should concurrency going from high to low. Consistency goes from low to high, in your table from top to bottom.

    • @ConceptandCoding
      @ConceptandCoding  11 місяців тому

      Your are right, i have pinned that in comment too.

    • @harshagarwal_net
      @harshagarwal_net 8 місяців тому

      ​@@ConceptandCoding If possible, could you please add asterisks (*) to the videos? The absence of asterisks is causing confusion.

    • @ConceptandCoding
      @ConceptandCoding  8 місяців тому

      @@harshagarwal_net * sorry did not bot get it, for what reason could you pls elaborate

  • @Latestnewwzzzz
    @Latestnewwzzzz 5 місяців тому +1

    Awesome explanation

  • @pradnyeshaglawe351
    @pradnyeshaglawe351 7 місяців тому +1

    Non-repeatable read❌
    Non-repetetable read✅

  • @AdityaKumar-is9tk
    @AdityaKumar-is9tk 9 місяців тому

    Hey nice tutorial, but there are some of wrong things taught in this tutorial. Optimistic locking doesn't use any lock. And what you mean db put share lock and after reading it releases the lock? Whats point of putting shared lock, it can just read it

    • @ConceptandCoding
      @ConceptandCoding  9 місяців тому

      right, and same has been discussed in the comment section. Do check it out, hope that will clarify your doubt.

  • @mohammadkaif8143
    @mohammadkaif8143 8 місяців тому

    Hi Shreyanh @ConceptandCoding, You skipped one very important scenario i.e how repeatable read does not solves phanton read. Can you give the same example using lock strategy?

    • @ConceptandCoding
      @ConceptandCoding  8 місяців тому

      let me check

    • @mohammadkaif8143
      @mohammadkaif8143 7 місяців тому

      Can you please explain by taking scenario of two transactions T1 and T2 that how Repeatable Read is not able to solve phantom problem@@ConceptandCoding

  • @sujayghosh5128
    @sujayghosh5128 5 місяців тому

    How do you decide when to use which isolation level?

    • @ConceptandCoding
      @ConceptandCoding  5 місяців тому

      based on the trade off between consistency, concurrency and performance your application requires

    • @sujayghosh5128
      @sujayghosh5128 5 місяців тому

      @@ConceptandCoding Thanks for your reply. To help me understand better, can you give me some examples?

  • @rabindradocument8934
    @rabindradocument8934 Рік тому

    Concurrency is distributed system: how we will handle Concurrency between many microservices. How do we rollback complete transaction if one of the service fails.

    • @ConceptandCoding
      @ConceptandCoding  Рік тому

      Already explained this in: ua-cam.com/video/ET_DnJgfplY/v-deo.html

  • @Aakashkumar-re7gk
    @Aakashkumar-re7gk 10 місяців тому

    Hey Shreyansh,
    Great video, just one minor clarification related to optimistic concurrency control
    both user will read that seat is free with version 1
    now in your example you said first user will take the exclusive lock till the end of the transaction and if other user tries to update he will check first the version if version is different rollback and try again.
    one doubt here like both tries to read the row parallely both got the shared lock now if both tries to take exclusive lock parallely will database will handle this thing to give one transaction exclusive lock and other will be in waiting state am I right here it is database responsibility to give exclusive lock to a particular transaction if many are coming parllely

    • @ConceptandCoding
      @ConceptandCoding  10 місяців тому

      Yes it is database responsibility to provide only 1 exclusive lock to 1 txn at a time.
      And just one correction buddy, in optimistic concurrency control there is no lock required, (so that select for update, is generally select only, no lock is required )
      In comment section, there is one long discussion happened, see if you can get that, that will clarify you, if not let me know, we will connect and try to clarify buddy.

  • @bhaskarsharan4280
    @bhaskarsharan4280 Рік тому

    A little more into repeatable read , phantom read is possible in repeatable read because the shared lock can only be acquired on rows which are already present in table, not on rows which are new to table. Hence on multiple query we may see new rows. My question is will these new rows get shared locked during any read after they are inserted?

    • @ConceptandCoding
      @ConceptandCoding  Рік тому

      Could you please elaborate, i did not understand the question well.
      Repeatable read do not put range lock.

    • @bhaskarsharan4280
      @bhaskarsharan4280 Рік тому

      @@ConceptandCoding at time t0, transaction with repeatable read isolation level got some N rows on executing a query, it put shared lock on all of them, at time t2 some new M records satisfying the same condition were inserted in table, at time t4 same query was executed again with shared locks resulting in phantom read situation - N+M records will be fetched with shared lock on all N rows and no lock on all M rows, will these new M rows be given a lock after 2nd time read?

    • @ConceptandCoding
      @ConceptandCoding  Рік тому +1

      @@bhaskarsharan4280thanks for explaining.
      Yes, shared lock will be put on these new rows fetched

  • @prajyotlawande193
    @prajyotlawande193 Рік тому

    Hi Shrayansh, the default isolation level in MySQL is REPEATABLE_READ. So, if we have to use optimistic locking in MySQL, is it required to change the isolation level to READ_COMMITTED first?

    • @ConceptandCoding
      @ConceptandCoding  Рік тому

      Optimistic runs below Repeatable read.
      We can define the isolation level while creating the Txn.

  • @kent.johnson
    @kent.johnson 10 місяців тому +1

    Great content

  • @akshaymahajan9626
    @akshaymahajan9626 7 місяців тому

    Very well explained !

  • @nitinvadadoriya8280
    @nitinvadadoriya8280 3 місяці тому

    in OPTIMISTIC CONCURRENCY CONTROL DEAD LOCK POSSIBLE. 1. T-a take Exclusive lock(row-1), 2. T-b take Exclusive lock(row-2), 3. T-a req. Exclusive lock(row-2) it block, 4. T-b req. Exclusive lock(row-1) it block.

  • @nirutgupta5894
    @nirutgupta5894 Рік тому

    I think in optimistic also deadlock possible?
    T1
    Write A
    Write B
    T2
    Write B
    Write A
    Both will wait for each other at time t2 to release exclusive lock.

  • @suranjanachattopadhyay4979
    @suranjanachattopadhyay4979 Рік тому

    If one transaction has obtained shared lock on a db row, then how many more transactions can get the lock for reading the row?

    • @ConceptandCoding
      @ConceptandCoding  Рік тому

      Share lock can be taken by many txns at same time buddy.
      I don't think there is as such limit

  • @NeverGiveUp186
    @NeverGiveUp186 Рік тому

    Great video as always Shrayansh!! Thanks a lot!
    I had one doubt though. How do we go about acquiring locks in case of replicated dbs ? For example, lets say I acquired an exclusive lock on row1 in db1, but some other server gets redirected to db2, and the user is able to update row1 in that replica. Can you please explain in brief about this ?
    Thanks!

    • @ConceptandCoding
      @ConceptandCoding  Рік тому +1

      There are distributed lock mechanism like usage of Zookeeper, which will send the msg to all the active dbs that put a lock on this data.
      There are other mechanism too , will cover that may be in separate video buddy

    • @NeverGiveUp186
      @NeverGiveUp186 Рік тому

      @@ConceptandCoding Ohh..ok. Thanks for the response!

    • @vyshnavramesh9305
      @vyshnavramesh9305 8 місяців тому

      ​@@ConceptandCodingIsn't Zookeper to hold locks for multiple nodes of the microservice? I don't think Zookeeper has any relation with database or its replicas.

  • @aneksingh4496
    @aneksingh4496 Рік тому

    Very nicely explained....

  • @rahuljha8038
    @rahuljha8038 Рік тому +1

    very helpful video

    • @ConceptandCoding
      @ConceptandCoding  Рік тому

      Thanks

    • @manishajhunjhunwala2326
      @manishajhunjhunwala2326 2 місяці тому

      Hi @ConceptandCoding
      I have one question, so for read committed you mentioned that dirty read is prevented as a write lock is acquired by one transaction and no other transaction can read it during then
      But wanted to understand what happens when 2 transactions at the sane time try to read and write, so which transaction would get the lock first and how is this decided?

  • @nitishprasadkushwaha
    @nitishprasadkushwaha Рік тому

    Concureeency is low for serialiser and high for red uncommited right ?

  • @himanshubhandari7222
    @himanshubhandari7222 9 місяців тому

    In pcc, If some transaction has started (say T1) and then we are reading a row (say row A) then T1 itself won’t be able to update row A, as there is shared lock already on it..
    Am I correct..?

    • @jatingupta5187
      @jatingupta5187 Місяць тому

      It will update because lock is happened by same transaction only

  • @callmepolice100
    @callmepolice100 Рік тому

    Seems to be both phantom and non repeatable are the same in both the cases we are reading the value multiple times, can anyone tell me the difference

    • @AtharvaRao0104
      @AtharvaRao0104 Рік тому

      Phantom is seen when you do a query for multiple rows - the number of rows returned are different when you do that query multiple times in the same transaction.
      "One transaction changing the result of another transaction's search query"
      Non repeatable read - when you read same object multiple times within a transaction - you see different values . IT happens in READ COMMITTED isolation level because another transaction which started later than the first transaction can update and commit a new value. The first transaction which is long running can see old value as well as new value in different reads.

  • @nishantkumar6116
    @nishantkumar6116 Рік тому

    This version checking and updating the version is this done by db or is it to handled by application developers? Can someone help on this

    • @ConceptandCoding
      @ConceptandCoding  Рік тому

      In optimistic locking, it is handled by application.
      For example Oracle DB, they do not support row version concept, so it's on application to manage the version column by itself

  • @Shefalisingh-m1m
    @Shefalisingh-m1m Рік тому

    When is the 2PL session coming up ? :)

    • @ConceptandCoding
      @ConceptandCoding  Рік тому

      It's already live, pls check the HLD playlist. Today I have made it live buddy

  • @sheikhmuhammedshoaib1124
    @sheikhmuhammedshoaib1124 Рік тому

    Hi can you suggest me some Db books where i can get lot of practice question on isolation, transaction and concurrency.

  • @KKGTravel
    @KKGTravel Рік тому

    Awesome video

  • @Shefalisingh-m1m
    @Shefalisingh-m1m Рік тому

    WHo writes these transactions ? DB developers ?

    • @ConceptandCoding
      @ConceptandCoding  Рік тому

      No, transactions (begin, rollback, commit) we app developer write it.

  • @engineersCode
    @engineersCode 4 місяці тому +1

    Nice video "-)

  • @piyushshukla5125
    @piyushshukla5125 Рік тому

    most of the imp low level design video are available only to join member. can you please make it public

    • @ConceptandCoding
      @ConceptandCoding  Рік тому

      Only interview questions i have kept for the members buddy.
      But all the fundamentals and concepts are available public

    • @bishtsaurabh5
      @bishtsaurabh5 Рік тому

      Yes but those videos were initially free and now become paid which is not a good thing in my opinion . Although the content is great 👍

  • @garimadhanania1853
    @garimadhanania1853 5 місяців тому

    thank you!!

  • @ShivangiSingh-wc3gk
    @ShivangiSingh-wc3gk 20 днів тому

    I believe your consistency is off- read uncommitted have the least consistency

  • @tejass5060
    @tejass5060 Рік тому

    I think the DB locks that are used on processing the transaction are not same among all the DB's.
    In postgres, if you try to execute a deadlock scenario using repeatable read isolation level. It will commit without any issues.
    Any comments on this @Shreyansh?

    • @ConceptandCoding
      @ConceptandCoding  Рік тому

      In postgres too, definitely One of the txn should get aborted (as postgres automatically detect deadlock and abort only 1 txn).
      Have you tried testing this usecase?

    • @tejass5060
      @tejass5060 Рік тому +1

      @@ConceptandCoding Yes, I had a table called test with two rows.
      Tried executing 2 different transactions according to the deadlock scenario but it is not giving any error.
      I tried the same with serializable and it gives error

    • @ConceptandCoding
      @ConceptandCoding  Рік тому

      @@tejass5060 can you notice and tell one thing buddy:
      In Postgres for repeatable read deadlock scenario, does one txn getting rolled back or not?

    • @tejass5060
      @tejass5060 Рік тому

      @@ConceptandCoding Not rolling back.

    • @tejass5060
      @tejass5060 Рік тому

      Both the rows are getting updated.

  • @asktostranger8296
    @asktostranger8296 Рік тому

    Next video 🥺

  • @AmareshwarBhat800
    @AmareshwarBhat800 7 місяців тому

    Waste of time. Not recommended video.

  • @sarvagyaagarwal2823
    @sarvagyaagarwal2823 11 місяців тому

    RIP English

    • @fooballers7883
      @fooballers7883 8 місяців тому

      LOL India would return back to the 5 century without English

  • @logicalrisi
    @logicalrisi 7 місяців тому

    hello

  • @vivekjha6990
    @vivekjha6990 Рік тому +4

    Can you provide a brief overview on the transaction isolation levels that is employed in systems like BookMyShow and Tatkal booking on IRCTC to handle concurrency during their respective booking processes?
    I think strict isolation level like SERIALIZABLE suits best for BookMyShow to avoid phantom reads by applying range lock, because here the requirement is - user should get the ticket either Booked or Not Booked, there is no concept of Waiting ticket.
    But in terms of IRCTC Tatkal booking, there is short window of time with lot of concurrent requests and Waiting ticket is also a thing, So To trade of System performance, do you think they apply less strict Isolation Level??
    ( Not sure on this- but they allow some level of incorrect reads, if we check ticket availability status on 2 laptops, one shows available, but when you book with other at the same time, it can give Waiting ticket),

    • @ConceptandCoding
      @ConceptandCoding  Рік тому +7

      In my view, BookMyShow might be using optimistic concurrency Control (with Read Committed) isolation level.
      Couple of reasons:
      1. As you and me can select same seat at the same time but at the time of checkout one of us will see the issue.
      2. I can select/ unselect multiple seats, i can not put locks on all seats, as you know, in pessimistic if lock is put it will be released only after end of txn. So optimistic is the best option.
      For IRCTC, i will think and get back to you (but seems very similar to BookMyshow usecase only)

    • @shivashankarm78
      @shivashankarm78 Рік тому

      Hi @Shreyansh,
      First of all a very big Thanks for all your efforts for educating the community.
      My doubt is platforms like BookmyShow and IRCTC, we can also book multiple seats in single transaction right. Shouldnt we use Serailizable isolation level as it involves range of seats to be booked.

  • @swatisuman1990
    @swatisuman1990 Рік тому +2

    Hi Shreyansh,
    Can you please make a video on Kubernetes?

  • @consistentprani5034
    @consistentprani5034 3 місяці тому

    in read committed, lets say tA comes and reads a value and releases the shared lock, but then tB comes acquire exclusive lock and update that value. So tA has done a dirty read here

  • @nishantkumar6116
    @nishantkumar6116 Рік тому +2

    This video is a gem, got to know lot of concept in this 1 hr video. Thank you @Shreyans for making such videos. These contents need to be reached to larger groups surprised to see only 13k views on it

  • @sudhirsingh9149
    @sudhirsingh9149 23 дні тому

    If the database also duplicates and scales, then will this work ?

  • @swarnenduhazra6094
    @swarnenduhazra6094 5 місяців тому

    41:14 why lock 4 ? when the query clearly makes the range and inclusive both the limits ..
    locking the range 1-3 makes sense though..
    someone please clarify this

  • @brawnybytes
    @brawnybytes Місяць тому

    beautifully explained all the concepts :)

  • @khushgandhi6638
    @khushgandhi6638 9 місяців тому

    Why optimistic locking can only work with READ_COMITTED isolation level , can’t we have REPEATABLE_READ with optimistic locking…

  • @meharwaheed4742
    @meharwaheed4742 6 місяців тому +1

    Very interesting. I've learned a lot. Thank You

  • @sumurthdixit8482
    @sumurthdixit8482 Місяць тому

    Great playlist, but I am confused that at some places it is written that in OCC, no locks are used to perform Reads, not even shared locks to maximize concurrency in reads. Only (E) locks are used to perform updates on row. Can someone please clarify ?

    • @ConceptandCoding
      @ConceptandCoding  Місяць тому

      @@sumurthdixit8482 in OCC when we read the row, we do not put into any transaction, so no locks placed.
      and after its work done only at the end when it want to update the data in DB, it uses the transaction because we want rollback feature right.
      And thats where exclusive lock is taken, and before update it also does the version check that no other transaction has updated the db

  • @hinata4661
    @hinata4661 Місяць тому

    Video and notes not present in udemy. No point in purchasing udemy if some are exclusively present and youtube and made members only.

    • @ConceptandCoding
      @ConceptandCoding  Місяць тому

      @@hinata4661 just an fyi: on udemy video is present

  • @rahulagrawal9236
    @rahulagrawal9236 2 місяці тому

    Hi Shrayansh,
    In case of optimistic locking, if transaction A gets the read lock and after that transaction B also gets the read lock, no one will be able to get the write lock right?
    that is also a dead lock state, with just one row right?

    • @jatingupta5187
      @jatingupta5187 Місяць тому

      Transaction A will get because lock is happen by Ta and it will apply exclusive lock but another transaction cant

  • @bluesteel1
    @bluesteel1 6 місяців тому +1

    Good video but too many ads

  • @LootLege-xc7zn
    @LootLege-xc7zn 3 місяці тому

    i love you shrayansh

  • @techtransformers7464
    @techtransformers7464 3 місяці тому

    if i have multiple read transaction and also a write transaction present at same time, will DB going to priortize write transaction over read transactions ?