I suppose this works more than well enough in practice but I can't help suggesting...

Why not open and truncate after preparing the data (marshalled json) and just before writing it, instead of truncating before taking some topic locks, and then filling in afterwards?

The most robust option, which is more work instead of just moving lines, is to create a new tmp file next to the existing one, and if fully successful, fsync and rename over the existing file.

just suggestions :)