File store and concurrency
File store and concurrency
The store keeps an in-memory map for fast access and persists the complete collection after each change. A mutex protects the map and prevents two writes from replacing the file at the same time.
What you will learn
- return copies instead of exposing mutable internal state;
- use
sync.RWMutexaround shared data; - write a temporary file and rename it into place;
- preserve the last good snapshot when encoding fails;
- describe the limits of a single-process file store.
Store shape
type FileStore struct {
mu sync.RWMutex
path string
notes map[string]note.Note
}
Load and validate the file in a constructor. If the file does not exist, start with an empty map. Other errors—including invalid JSON—should be returned rather than silently discarding data.
Return a snapshot
func (s *FileStore) List(_ context.Context) ([]note.Note, error) {
s.mu.RLock()
defer s.mu.RUnlock()
result := make([]note.Note, 0, len(s.notes))
for _, n := range s.notes {
result = append(result, n)
}
slices.SortFunc(result, func(a, b note.Note) int {
return a.CreatedAt.Compare(b.CreatedAt)
})
return result, nil
}
The returned slice is independent of the map. Because every Note field here is a value or immutable string, copying each struct is enough. A model containing maps, slices, or pointers would need deliberate deeper copying.
Persist without truncating the good file first
func writeSnapshot(path string, notes []note.Note) error {
directory := filepath.Dir(path)
temporary, err := os.CreateTemp(directory, ".notes-*.json")
if err != nil {
return fmt.Errorf("create temporary snapshot: %w", err)
}
temporaryName := temporary.Name()
defer os.Remove(temporaryName)
encoder := json.NewEncoder(temporary)
encoder.SetIndent("", " ")
if err := encoder.Encode(notes); err != nil {
temporary.Close()
return fmt.Errorf("encode snapshot: %w", err)
}
if err := temporary.Sync(); err != nil {
temporary.Close()
return fmt.Errorf("sync snapshot: %w", err)
}
if err := temporary.Close(); err != nil {
return fmt.Errorf("close snapshot: %w", err)
}
if err := os.Rename(temporaryName, path); err != nil {
return fmt.Errorf("replace snapshot: %w", err)
}
return nil
}
Creating the temporary file in the destination directory makes a same-filesystem rename possible. Rename behaviour and durability details vary by operating system and filesystem. For stronger crash guarantees, directory syncing and platform-specific handling may be required; a database is usually the better next step.
Mutate only when persistence succeeds
A simple safe approach is:
- take the write lock;
- copy the map;
- apply the change to the copy;
- persist the copy;
- replace the in-memory map only after persistence succeeds;
- release the lock.
Holding the lock across disk I/O limits throughput, but makes the correctness rule easy to reason about. Optimise only after measurement and a design that preserves ordering.
Important limitation
sync.RWMutex coordinates goroutines in one process only. Starting two copies of this API against the same file can lose updates. This project must be documented as single-process. Multi-process writers need file locking with platform caveats or, preferably, a transactional database.
Practice
Why is “update the live map, unlock, then write the file” unsafe even if every individual map access uses a mutex?
Summary
- Protect shared maps and return independent snapshots.
- Preserve the old file by writing a temporary snapshot first.
- Publish the in-memory change only after persistence succeeds.
- State clearly that an in-process mutex does not coordinate multiple processes.
Ready to wrap up?
Mark this complete when you can explain the main idea and finish the practice task.