Buffered Writes and Flushing

A writer can hold bytes in a buffer before delivering them to the next layer. A flush asks that layer to receive the buffered output.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Observe delayed visibility
  2. Understand persistence separately
  3. Follow data through user-space and the filesystem
  4. Test observation and durability as distinct goals

Observe delayed visibility

A program writing a short progress line may not make it visible immediately when output is buffered. A flush can make it available to downstream readers sooner. Exit status and command output are still separate observations.

Understand persistence separately

Flushing a language-level buffer can leave data cached by the operating system. Durable storage needs the applicable sync semantics and recovery discipline. See file replacement and durability for why finishing a write has several layers.[1]

Follow data through user-space and the filesystem

A text writer can buffer a line inside the process before sending it onward. Flushing transfers that buffered data toward the underlying stream. On a filesystem, ordinary flush alone does not guarantee the bytes survive a sudden power loss. See atomic replacement.

format record → output object buffer
flush → underlying stream or OS
durability sync, where required and supported → storage guarantee under the chosen model

Test observation and durability as distinct goals

A progress display may need prompt visibility only. A completed export can require stronger durability according to its filesystem and contract. Test a reader observing data before and after flushing, then use environment-appropriate durability checks if the workflow depends on them.

References

  1. Python Software Foundation, io: Core Tools for Working with Streams ↩