on inserting to the map, why locking is needed if the key is different for each request? is locking necessary even if the insertion is to different keys?
It's a good question. I didn't know the answer. This is what I found Why Locking is Necessary, Even for Different Keys: While it might seem counterintuitive that locking is necessary when inserting into a map with different keys, the underlying implementation of Go maps can cause issues in concurrent scenarios: * Hash Collisions: * Multiple keys can hash to the same index within the map's internal hash table. * When multiple goroutines try to insert into the same bucket, data races can occur, potentially leading to incorrect values or even map corruption. * Resize Operations: * If the map's capacity is exceeded, a resize operation is triggered. * During a resize, the map's internal structure is reallocated and rehashed. * Concurrent access to the map during a resize can lead to data loss or incorrect behavior.
@@practicego thank you for sharing! I kind of see why but I would prefer some visual explanation of how things happen in memory. will look more into this when I have some time. thank you.
Excellent example and clear didactics about the topic
Please do the complete video on system design. Thank you.
Sure 😃
Please do the complete video on systems design
Sure. I will
on inserting to the map, why locking is needed if the key is different for each request? is locking necessary even if the insertion is to different keys?
It's a good question. I didn't know the answer.
This is what I found
Why Locking is Necessary, Even for Different Keys:
While it might seem counterintuitive that locking is necessary when inserting into a map with different keys, the underlying implementation of Go maps can cause issues in concurrent scenarios:
* Hash Collisions:
* Multiple keys can hash to the same index within the map's internal hash table.
* When multiple goroutines try to insert into the same bucket, data races can occur, potentially leading to incorrect values or even map corruption.
* Resize Operations:
* If the map's capacity is exceeded, a resize operation is triggered.
* During a resize, the map's internal structure is reallocated and rehashed.
* Concurrent access to the map during a resize can lead to data loss or incorrect behavior.
@@practicego thank you for sharing! I kind of see why but I would prefer some visual explanation of how things happen in memory. will look more into this when I have some time. thank you.
Thanks.