In Go,It's oneConcurrently safeMapping structure, specially used to process key-value pair data in high concurrency scenarios. Its concurrency safety is throughInternal implementation mechanismto ensure that, rather than relying on external lock mechanisms (such as
or
) to manually protect the operation.
The principle of concurrent security implementation
A more complex data structure and operation strategy are adopted to achieve concurrent security. Its core design can be divided into the following aspects:
1. Read and write separation mechanism
The internal structure is throughRead and write separationImplemented mainly consists of two parts:
- Read-only part (read map): Used to store stable data. The read operation is mainly carried out from this read-only part to avoid the use of locks.
- Dirty map: When the data is modified (write or deleted), it will be moved to the dirty data area, and locked while writing to ensure concurrency security.
2. Quickly read paths
-
Lockless reading: If the data already exists in
read map
(i.e. stable data), read operation does not require locking, which makesThe read operation is very efficient.
-
Copy on writing: When the data is
read map
When it does not exist, it may exist indirty map
middle. At this time, you need to upgrade the lock and get it fromdirty map
Read or write data.
3. Lock protection during writing
- When writing to (
Store
orDelete
)hour,Will be here
dirty map
Perform the operation. Write operations are locked to ensure security during concurrent writes. - Each time you write,
All check
read map
anddirty map
Whether the data between them needs to be synchronized (such as when the amount of data exceeds a certain threshold), and clean and migrate the dirty data parts.
4. Lazy Synchronization
When read operations are frequent,Some dirty data will be gradually migrated to
read map
, thereby reducing the dependence of read operations on locks. ThisDelay synchronizationThe strategy ensures that read operations can avoid lock competition as much as possible, thereby improving read performance.
5. Atomic operation
Partial operations (such as
LoadOrStore
、LoadAndDelete
etc.) Atomic operations are adopted. Their implementation uses the underlying atomicity check and assignment operations to ensure that these operations can remain consistent in a concurrent environment.
Key Operation Instructions
-
Read operation (
Load
):- First from
read map
Read in , if found, return directly. - If
read map
Not found in , then try todirty map
Read in and may trigger a lock operation.
- First from
-
Write operation (
Store
):- The write operation will be locked
to ensure that the
dirty map
Safe writing. - If dirty data becomes more or is written frequently, it may trigger
read map
Synchronization, migrate some dirty data toread map
。
- The write operation will be locked
-
Delete operation (
Delete
):- The deletion operation will also be locked and deleted
dirty map
data in .
- The deletion operation will also be locked and deleted
-
Batch operation (
Range
):-
Range
Operation traversalAll data in it ensures that there is no concurrent conflict during the traversal.
-
Code Example
package main import ( "fmt" "sync" ) func main() { var m // Write data ("foo", 42) ("bar", 100) // Read data value, ok := ("foo") if ok { ("foo:", value) } // Delete data ("foo") // Use Range to traverse all elements (func(key, value interface{}) bool { (key, value) return true }) }
Advantages
-
High reading performance: Performance is very excellent in scenarios where more reads and less writes, because
read map
There is no need to add locks when reading, reducing lock competition. -
Automatic concurrency control:
There is no need to manually manage lock mechanisms, which reduces the complexity of writing concurrent security code.
-
Suitable for high concurrency scenarios: Especially in the case of large amounts of reading,
The performance is better than that of traditional
map
+The plan.
When to use
-
Read more and write less scenarios: When concurrent access is mainly read operation and write operation is small,
The read and write separation mechanism makes it highly performant.
-
Requires simple concurrent access: When concurrent access is required
map
and do not want to manually manage locks,It is a very convenient tool.
When not to use
-
Very frequent writing operations:
Locking is required in writing operations. If the write operations account for a high proportion, it may not be as good as the traditional way of manually locking.
map
The solution is efficient.
This is the end of this article about the detailed explanation and usage scenarios of Go. For more relevant Go language content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!