28. BUG in Double-Checked Locking of Singleton Pattern & its Fix | Low Level System Design Question

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

КОМЕНТАРІ • 59

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

    I have 4 years of experience, and came accross this volatile keyword n number of times..but this knowledge..i never saw..not even heard of ..❤
    You are. GEM... eagerly waiting for your next lecture on singleton.. please upload as soon as possible.

  • @sharathchandra157
    @sharathchandra157 10 місяців тому +3

    @ConceptandCoding
    Synchronized blocks also guarantee that all variables accessed inside the synchronized block will be read in from main memory, and when the thread exits the synchronized block, all updated variables will be flushed back to main memory again, regardless of whether the variable is declared volatile or not.
    so i belive synchronized itself should be fine even if you dont use volatile keyword.

    • @abrahamlincoln5724
      @abrahamlincoln5724 10 місяців тому +1

      Yep, that's what I was thinking.

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

      Yes, the L1 cache issue should not be a problem for the mentioned reason. However, the reordering can still be an issue because the 1st null check is outside synchronous block(8:12). To avoid reordering, volatile is still needed. Please correct me, if I am wrong.

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

    Great explanation!
    Please bring the next video on how to eliminate synchronised keywords.

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

    Hi @concept&&coding , i find the arguments misleading and wrong at multiple places
    1. Its not the CPU which does reordering of instructions its the compiler dueing compile time and assembler when generating asm code. CPU only executes them.
    2. Also in a multi code cpu architecture, L1 cache is local to a core and multiple cores do not share / sync data as u have mentioned, that is not what L1 cache is meant for, please check this.
    3. Also both harvard arch , modified harvard arch which is what we have in modern cpus , the data flows from the memory to cache and not vice versa , from a cpu perspective caches are transparent and its MMU which transfer hot data to cahce for speedup , so the argument that memeory gets allocated in cache and then thread 2 accessing it in L1 cache of core 2 is wrong as the allocation happens in memory and both cache will have cache miss for first time and then it will be fetched from memeory

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

      Agreed 💯

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

      I think CPU can reorder the instructions. For example if CPU figures has a set of instructions like a=b+c; d=e+f; it can choose to execute d=e+f as there is no dependency on a=b+c; This can't be controlled by the JIT compiler. compiler only compiles code and probably optimizes it for space but cant take dynamic decisions at run time. JIT compiler. Probably JIT compiler might reorder the compiled code if it feels there is scope for optimization for less resource consumption. I don't think it can take runtime decisions. Locality Of Reference is decided by the CPU and not the JIT compiler I suppose

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

      But I agree, object creation always allocates memory in heap and is no way related to cache. So, even if object is being created, since the lock is held, T2 executing in parallel on core2 still waits for the lock to be released by T1. It cant execute the new Instance() code until T1 releases the lock

  • @ManishKumar-zt5sk
    @ManishKumar-zt5sk 6 місяців тому +2

    Alien knowledge, just top class...

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

    Thank you, Shrayansh. Awesome As always.

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

    I used volatile many times without knowing why 😂. It was a good explanation can you also cover how to handle concurrency if it is a distributed system.

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

      Sure. Already planned it. Will cover soon

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

      use zooleeper or hazelcust or something else for it

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

    please more such lectures requested

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

    @ConceptandCoding
    With the synchronized block, visibility into memory is ensured right? Before reading anything synchronized makes sure thread fetches data from memory and after writers ensures that it is flushed to memory.
    The instruction reordering will be an issue but the second one is not.

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

    Thank you for the nice explanation!!

  • @manojkumar-dv8pf
    @manojkumar-dv8pf Рік тому +1

    Hey Shreyansh, I have a doubt -
    even if we have added 'volatile' keyword before conObject, what is the gurantee that this `conObject = memoryPointer;`
    will be the last line. I agree that all the instructions written before `conObject = memoryPointer;` will be executed first
    but what if the actual order of instructions is like this -
    if(conObject == null){
    memoryPointer = allocateMemory();
    conObject = memoryPointer;
    memoryPointer.memberVariable = initlizeVariable();
    }

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

      read jmm, if volatile reordering is not allowed

  • @AkashShukla-ny9wu
    @AkashShukla-ny9wu 3 місяці тому

    how can thread 2 enter in a synchronised block, it will enter once thread t1 cross "}" closing of synchronized block, right?

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

    Isn't the compiler responsible for reordering of instructions instead of CPU?

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

      No its CPU, compiler will generate you the bytecode, ultimately instructions are going to run in Cores. Their reordering happens.

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

    Serializable Singleton and Enum singleton ,and using Inner class also we can do I guess.

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

    Hi Shreyansh ,Checked your video on how to ask for referral through linkedin .But ,for that to work we need to be connected with that person as note can be sent with less than 300 alphabets only. Or will I require Linkedin Premium for this?

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

      Hi Nik, you don't need premium. Send the connection request and then send the msg
      Try to connect to those people, who do not have too many followers or connections. Else it's possible that they will not accept the request as so many requests are already in pending.
      So target managers or engineers who have less connections and followers

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

    I am more of frontend developer but also worked on node. I am not sure how to answer these concepts in node js way. Almost everywhere for system design they use java. Can you clear me on this

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

      Hi, since i am not at all familiar with node. I can not say much about it.
      If anyone looking at this msg and have some information, pls share your inputs team

  • @RahulPatel-lq6qp
    @RahulPatel-lq6qp 6 місяців тому

    how does it know it doesn't has to reorder - at compile time?

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

    can you please help me understand how singleton behaves in distributed env? suppose 100 vm's are there does each vm will have the singleton instance ?

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

      yes there would be 100 instances. to avoid that, we can store the singleton in a distributed cache like redis.

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

    im a fresher ,so at what level in the industry do I need to be aware of such concepts ?

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

      LLD is imp, what level i would say if your fundamentals are good for classes, objects and it's relationship you are good.
      Else there is no limit on questions

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

    Hi shrayansh, can't understand the caching issue alone. When we create object aren't we allocating memory for it as per instructions? Then how we don't have it in memory? Or are we allocating space for it in cache here?

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

      Please refer my comment I have explained, I am glad someone else also gave a thought..

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

      @@ash_engineering I guess there are 2 aspects. One is the variable. Second is the actual memory address of the allocated object. Shreyansh probably meant the local variable "instance" in Thread stack (which is in cache connected to CPU Core1 assuming T1 is part of CPU core1) . There are two things. 1)Object creation(which happens in RAM/main Memory). 2)Assignment of created object address to local variable "instance" . T1 has its own copy of "instance" whereas T2 executing on CPU core2 has its own copy of "instance" . Now Shreyansh meant that assignment of Object address has happened to "instance" variable in T1 but hasnt happened to "instance" variable of T2.

  • @GauravKumar-by1yt
    @GauravKumar-by1yt Рік тому +1

    Great explanation, thankyou

  • @aravintht.k9276
    @aravintht.k9276 Рік тому

    Great explanation.

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

    Awesome video as always 💯❤

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

    I observed that from udemy your courses are in Hindi, if it's would be in English I would have enrolled those courses. Language barrier

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

    how is it done in python, any idea?

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

      sorry i am not aware of the python language, anyone else can help it here?

  • @ANUJ-we3su
    @ANUJ-we3su Рік тому

    Sir can you please share telegram link ,it is not working which is present in about section of your channel ,actually i am following your lld series but since i code in c++ ,i want to get c++ code ,maybe someone have shared it on your telegram group.

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

    Can you add English subs to your udemy courses

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

    proofs?

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

    This is incorrect. synchronized keyword insures both happens before and memory visibility guarantee.

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

    I got a qn? Like y does everyone these days working in big tech leave their job and sell courses or giv advice in LinkedIn, is the job that bad and that hectic that its difficult to continue for long yrs to come.?

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

      No sure Debojeet. I might be wrong person to answer this.
      Already working and it's been 8yrs and many more years to come, so to be honest don't know why leave and sell courses :)

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

      I don't understand the intent behind your question. Why don't you use your own wisdom and consume the good content while it's available? Everyone needs money. Free stuff isn't sustainable.

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

    show example of code how to see your theoretical explanation