Reading and writing are less symmetric than you (probably) think

(fgiesen.wordpress.com)

71 points | by luu 3 days ago

9 comments

  • Pannoniae 5 hours ago
    Great article :D The rabbithole actually goes even deeper because buffering on the read side is almost always good, but reads are often way more latency-sensitive than writes. Which leads to the paradox that increasing the buffer size / prefetching more with mmapping can lead to worse performance due to the added latency. And decreasing the buffer size can have the same effect due to issuing more calls, so in order to squeeze in the optimal throughput/latency ratio on reads you have to tune it per workload.... or just try a few different buffer sizes and see which one works better.

    Writes are the opposite, they're usually not RAW (read-after-write), they're "fire and forget", so you don't really care about latency, but you do care a whole a lot about your writes actually arriving... again two competing parameters because the more you batch the writes, the faster they are but the bigger chance of a torn write or data loss. And unlike reads, writes are not idempotent on the application level so ideally you'd want some logic to atomically write a meaningful amount of data so it won't get torn, even if it crashes.

    Now in practice just about no one actually does any of this but it's a fun thought :D

    • Insimwytim 4 hours ago
      I have committed a sin of starting with the comments without reading an article, assumed it was about reading and writing text by humans and was increasingly confused by your first paragraph.

      I tried to translate it as metaphor at first, but it didn't end up being coherent.

      • psdndown 3 hours ago
        As did I. My happy impression that I was to be commended for reading slowly was turned upside down by the same realization.
    • plasticchris 2 hours ago
      Makes sense. If you are reading something you need it to make progress, if you are writing you are finished.
  • zokier 23 minutes ago
    There is plenty of fun to be had with write errors and fsync. One example was so called "fsyncgate" where postgresql stumbled upon fsync issues: https://news.ycombinator.com/item?id=20491965

    I think there were some changes in kernel since that but I can't remember what exactly

  • 1317 7 hours ago
    oh i thought this would be about language and how the brain works, that article sounds more interesting to be honest sorry
    • prmph 6 hours ago
      Indeed, that's what I also thought this would be about; What a bummer.

      Someone should post that article instead, lol. I'd love to discuss why I am able to comprehend and read a language almost fully but cannot speak it.

      • chrisweekly 6 hours ago
        Search for "Broca and Wernicke" to learn more about the distinct areas of the brain responsible for producing vs processing language.
      • todowhatexactly 6 hours ago
        Production is different from reception.

        Compare the ease with which you recognize a bicycle and the difficulty of drawing one.

        It’s not the mechanics of the pencil alone although that plays a role a well. But even if you could somehow imprint perfect lines like you imagine them, the bicycle and the lightening etc will not be realistic unless you are a trained artist.

    • tbrownaw 6 hours ago
      Or even something about how writing usefully on a topic requires higher expertise than reading and understanding what was written.
      • verdverm 5 hours ago
        or how if you want to write better, read more
    • ziiinq 5 hours ago
      [dead]
  • cryptonector 1 hour ago
    I was hoping for something about human reading and writing, but it was about POSIX I/O APIs. Not bad, and I'd go further and point out that `mmap()` is good for reading but not for writing, and why.
  • vatsachak 7 hours ago
    This is true even at the CPU cache level so it's probably going to trickle down all the way.

    For example Jon G gave a talk at Jane Street about how he had a mysterious performance bug that was later attributed to cores fighting over the same cache line

    https://youtu.be/tND-wBBZ8RY?is=624w3YoDN2QYBWuD

    • rwmj 5 hours ago
      False sharing is pretty well known isn't it? I learned about it at university. It's the whole reason why the Linux "dwarves" tools were written, so you can check struct field alignment / grouping against cache line size and make sure you don't accidentally have two adjacent fields used by different cores.
  • rwmj 5 hours ago
    In any discussion of read, write & sync, I'd want to talk about write amplification (read/modify/write cycles), and also in Linux the sync_file_range syscall which is a useful way to sync part of a file.
  • mightyham 5 hours ago
    > the data may have made it to storage, but that doesn’t necessarily mean it’s actually visible, because the metadata necessary to reach it might not have been written yet.

    The author should clarify here that this fsync wrinkle only affects newly created files, not updates to existing ones.

  • tbrownaw 6 hours ago
    Yep, reading something doesn't invalidate your caches. Or everyone else's caches.
    • adrian_b 4 hours ago
      I am not sure if you mean what you have written, or you mean the opposite, but reading a big file when there are no available free memory pages will flush memory pages from the disk cache, which previously were caching data from files accessed by your user, or by other users.

      In high performance applications, you must not let the kernel choose randomly what pages should be invalidated, but you must use madvise for memory_mapped files or posix_fadvise for files accessed with read/write, with appropriate arguments so that cache memory pages that are not needed any more will be freed with enough time in advance before reading more data.

      • tbrownaw 2 hours ago
        It can evict other things for space reasons, sure. But it doesn't make their contents invalid.
  • chrchang523 3 hours ago
    (2015)