Hacker News new | ask | show | jobs
by koolba 453 days ago
You missed a number of steps. The transactions are independent so they signal completion (to trigger the commit fsync) independently.

You can have the first transaction wait a bit to see if any other commits can be batched in the same fsync. However that’s off by default as the assumption is you want the transaction to complete as fast as possible.

At least that’s how PostgreSQL implements it.

1 comments

The clever way to do this is to immediately commit the first transaction when the storage engine is idle.

While it is waiting for the fsync to finish it should batch up any incoming WAL writes and then issue the next fsync immediately after the first one finishes, committing the entire batch at once. Then, and only then, it can reply to clients with “transaction complete”.

Some modern database engines now do this, by many older ones don’t because too much of their code assumes one transaction per fsync.

You still have to wait for the final fsync which is only requested after the transaction work has completed. So not sure you’re gaining much if at all from this.

There’s also concurrency issues with writing and fsyncing the same fd: http://oldblog.antirez.com/post/fsync-different-thread-usele...

The key limit is the rate of fsyncs, which is constrained by the user mode to kernel mode transition and physical characteristics of the storage device. In the good old days, it was about a millisecond due to spinning disk latencies, but even on the best SSDs it's about 200 microseconds. This is only about 5K transactions per second, maximum, no matter how trivial the transactions are!

With automatic batching, trivial transactions can be grouped together so that the bottleneck becomes bandwidth, not an absolute rate.

You get to have your cake and eat it too: There's no additional latency added using automatic batching of transactions because when the I/O queue is empty, the next transaction commits immediately, same as normal. If the disk is already in the middle of an fsync, the next one will have to queue up behind it in the storage subsystem anyway, so the DB engine may as well accumulate more transactions in-memory while it is waiting.