low-end persistence strategies?

pyguy2 at gmail.com pyguy2 at gmail.com
Wed Feb 16 11:30:46 EST 2005


People sometimes run to complicated systems, when right before you
there is a solution. In this case, it is with the filesystem itself.

It turns out mkdir is an atomic event (at least on filesystems I've
encountered).  And, from that simple thing, you can build something
reasonable as long as you do not need high performance. and space isn't
an issue.

You need a 2 layer lock (make 2 directories) and you need to keep 2
data files around plus a 3rd temporary file.

The reader reads from the newest of the 2 data files.

The writer makes the locks, deletes the oldest data file and renames
it's temporary file to be the new data file.                 You could
have the locks expire after 10 minutes, to take care of failure to
clean up.  Ultimately, the writer is responsible for keeping the locks
alive. The writer knows it is his lock because it has his timestamp.
If the writer dies, no big deal, since it only affected a temporary
file and the locks will expire.

Rename the temporary file  takes advantage of the fact that  a rename
is essentially immediate. Since, whatever does the reading, only reads
from the newest of the 2 files (if both are available).  Once, the
rename of the temporary file done by the writer is complete, any future
reads will now hit the newest data. And, deleting the oldest file
doesn't matter since the reader never looks at it.

If you want more specifics let me know.

john




More information about the Python-list mailing list