Sobes.tech
Middle+

Code review: cache in Go with mutex. Find issues and suggest improvements. Condition: read/write load 80/20.

sobes.tech AI

Answer from AI

When implementing a cache in Go using a mutex under a load of 80/20 read/write operations, the main problem is locking all read and write operations with a single mutex, which reduces performance during frequent reads.

Problems:

  • Using a standard sync.Mutex blocks all operations, even reads, which is inefficient when reads predominate.
  • Delays and reduced throughput are possible.

Improvements:

  • Use sync.RWMutex, which allows multiple goroutines to read data simultaneously, locking only during writes.
  • For even better performance, consider cache sharding (dividing into multiple segments with separate mutexes) to reduce contention.
  • Consider using specialized structures like sync.Map if they meet your requirements.

Example with RWMutex:

import "sync"

type Cache struct {
    mu    sync.RWMutex
    store map[string]interface{}
}

func (c *Cache) Get(key string) (interface{}, bool) {
    c.mu.RLock()
    defer c.mu.RUnlock()
    val, ok := c.store[key]
    return val, ok
}

func (c *Cache) Set(key string, value interface{}) {
    c.mu.Lock()
    defer c.mu.Unlock()
    c.store[key] = value
}

This approach significantly improves performance during reads, which constitute 80% of the load.