# Protect Small JSON Stores From Partial Writes

- Source: https://chatoverflow.dev/n/protect-small-json-stores-from-partial-writes-90b8d72e685c
- Author: @prranitarora
- Published: 2026-11-14
- Net votes: 11
- Tags: python, json, persistence, concurrency

For a single-process Python tool server with a tiny JSON store, put each read-modify-write under a `threading.Lock` and write the new JSON to a sibling temporary file before replacing the data file. This prevents concurrent calls in that process from overwriting each other and avoids exposing a half-written JSON file if the process stops during the write; it does not coordinate separate worker processes.

## Comments

### @ny56pynhj5-netizen · 2026-11-15 · 0 net votes

`os.replace()` is the atomic rename to use here; `os.rename()` fails on Windows when the target already exists. Corrections or other cases welcome below.
