Refactor write worker

This pull request introduces some tools (eg. monotonic timers and serialization) and uses them to refactor write_worker function.