# Better way for map update concurrent control?

**URL:** <https://forum.golangbridge.org/t/better-way-for-map-update-concurrent-control/38679>\
**Category:** Technical Discussion\
**Created:** [May 3, 2025, 1:10am UTC](https://forum.golangbridge.org/t/better-way-for-map-update-concurrent-control/38679 "2025-05-03T01:10:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Klose6](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/klose6/32/16444_2.png) [@Klose6](https://forum.golangbridge.org/u/Klose6)\
**Post date:** [May 3, 2025, 1:10am UTC](https://forum.golangbridge.org/t/better-way-for-map-update-concurrent-control/38679/1 "2025-05-03T01:10:31Z")

</div>

Hi, if there is a map with key-value pair like (`IP string, info struct{data string}`) and I need to frequently update the `data` for each IP and from time to time I also need to add/remove IPs, so what is the better way for the concurrent control for the update of that Map except the RWLock? because for RWLock it will lock the whole map for each IP data update, thanks!

---

<div class="post-metadata">

**Author:** ![Yamil\_Bracho](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/yamil_bracho/32/5141_2.png) [@Yamil\_Bracho](https://forum.golangbridge.org/u/Yamil_Bracho)\
**Post date:** [May 3, 2025, 2:46pm UTC](https://forum.golangbridge.org/t/better-way-for-map-update-concurrent-control/38679/2 "2025-05-03T14:46:11Z")

</div>

Maybe use sync.Map([sync package - sync - Go Packages](https://pkg.go.dev/sync)).  
sync.Map takes care of all that locking (or atomic operations) for you—so no manual locking needed

---

<div class="post-metadata">

**Author:** ![Modestying](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/modestying/32/15157_2.png) [@Modestying](https://forum.golangbridge.org/u/Modestying)\
**Post date:** [May 7, 2025, 1:28am UTC](https://forum.golangbridge.org/t/better-way-for-map-update-concurrent-control/38679/3 "2025-05-07T01:28:41Z")

</div>

maybe you can try redis

---

<div class="post-metadata">

**Author:** ![Dean\_Davidson](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/dean_davidson/32/9685_2.png) [@Dean\_Davidson](https://forum.golangbridge.org/u/Dean_Davidson)\
**Post date:** [May 7, 2025, 3:23pm UTC](https://forum.golangbridge.org/t/better-way-for-map-update-concurrent-control/38679/4 "2025-05-07T15:23:18Z")

</div>

It might be overkill, but you could use an in-memory caching solution such as:

> **[GitHub - allegro/bigcache: Efficient cache for gigabytes of data written in...](https://github.com/allegro/bigcache)**
>
> Efficient cache for gigabytes of data written in Go.

You could also maybe take inspiration from their blog post about building it:

> **[Writing a very fast cache service with millions of entries in Go](https://blog.allegro.tech/2016/03/writing-fast-cache-service-in-go.html)**
>
> Recently our team has been tasked to write a very fast cache service. The goal was pretty clear but possible to achieve in many ways. Finally we decided to try something new and implement the service in Go. We have described how we did it and what...

---

<div class="post-metadata">

**Author:** ![falco467](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/falco467/32/13340_2.png) [@falco467](https://forum.golangbridge.org/u/falco467)\
**Post date:** [May 8, 2025, 1:54pm UTC](https://forum.golangbridge.org/t/better-way-for-map-update-concurrent-control/38679/5 "2025-05-08T13:54:36Z")

</div>

From the docs:

> The Map type is specialized. Most code should use a plain Go map instead, with separate locking or coordination, for better type safety and to make it easier to maintain other invariants along with the map content.
> 
> The Map type is optimized for two common use cases: (1) when the entry for a given key is only ever written once but read many times, as in caches that only grow, or (2) when multiple goroutines read, write, and overwrite entries for disjoint sets of keys. In these two cases, use of a Map may significantly reduce lock contention compared to a Go map paired with a separate [Mutex](https://pkg.go.dev/sync#Mutex) or [RWMutex](https://pkg.go.dev/sync#RWMutex).

So in most cases using a Map with an RWMutex is probably the most straightforward and simplest solution. And only if the performance of this is not enough for you, you should search for a solution which fits your special case (and it might even be an in memory database)

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [August 6, 2025, 1:55pm UTC](https://forum.golangbridge.org/t/better-way-for-map-update-concurrent-control/38679/6 "2025-08-06T13:55:34Z")

</div>

Closed on August 6, 2025, 1:55pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
