Reputation: 161
I have a website and part of my website needs to read/write a plain text file on server. most of the time we have between 50 to 100 concurrent online users and their actions on our site result to read/write certain file in which that is a temporary place holder for some other pages.
So I want to know about concurrent read/write conflicts and what's the best practice to avoid that if that happened. How does my server manage these requests?
Upvotes: 0
Views: 398
Reputation: 43748
An option in *nix systems is creating a temporary file, writing to it, fsync()ing it, and then rename()ing to the target filename. Of course this only works for a single writer, or if you only want the latest writer.
Upvotes: 1
Reputation: 1273
Of more importance than the webserver is the underlying OS and filesystem.
You can (in supported circumstances) obtain an exclusive lock on a file handle in PHP using flock() causing all other attempts at an exclusive lock to block and stack behind it.
Upvotes: 1