Nginx NJS fs.writeFileSync is atomic writing and race condition prevention ?
Valentin V. Bartenev
vbart at nginx.com
Fri Oct 19 17:03:32 UTC 2018
On Friday, 19 October 2018 06:33:57 MSK HENG wrote:
> I am new to Nginx NJS, and I want to write a website with NJS.
> I want to write a simple JSON database with NJS fs.writeFileSync ,just like
> Node.js LowDB.
> but I have no idea . Does NJS fs.writeFileSync is atomic writing and race
> condition prevention ?
> If NJS fs.writeFileSync is NOT atomic writing, it can NOT be treate as a
> normal database file write.
> Thank you !
The fs.writeFileSync function calls a simple write(), which is atomic with
But at the first glance to work with file like a database using multiple
nginx worker processes it's not enough to have atomic writing function.
You need to have atomic the whole sequence of operations:
read file -> update data -> write file.
Since currently there's no locking functionality in njs, the only way to
achieve this is to use only one worker process.
wbr, Valentin V. Bartenev
More information about the nginx-devel