title: Memory-Mapped MappedByteBuffer or Direct ByteBuffer for DB Implementation?
body: <p><em>This looks like a long question because of all the context. There are 2 questions inside the novel below. Thank you for taking the time to read this and provide assistance.</em></p>
  
  <p><strong>Situation</strong></p>
  
  <p>I am working on a scalable datastore implementation that can support working with data files from a few KB to a TB or more in size on a 32-bit or 64-bit system.</p>
  
  <p>The datastore utilizes a Copy-on-Write design; always appending new or modified data to the end of the data file and never doing in-place edits to existing data.</p>
  
  <p>The system can host 1 or more database; each represented by a file on-disk.</p>
  
  <p>The details of the implementation are not important; the only important detail being that I need to constantly append to the file and grow it from KB, to MB, to GB to TB while at the same time randomly skipping around the file for read operations to answer client requests.</p>
  
  <p><strong>First-Thoughts</strong></p>
  
  <p>At first glance I knew I wanted to use memory-mapped files so I could push the burden of efficiently managing the in-memory state of the data onto the host OS and out of my code. </p>
  
  <p>Then all my code needs to worry about is serializing the append-to-file operations on-write, and allowing any number of simultaneous readers to seek in the file to answer requests.</p>
  
  <p><strong>Design</strong></p>
  
  <p>Because the individual data-files can grow beyond the 2GB limit of a MappedByteBuffer, I expect that my design will have to include an abstraction layer that takes a write offset and converts it into an offset inside of a specific 2GB segment.</p>
  
  <p>So far so good...</p>
  
  <p><strong>Problems</strong></p>
  
  <p>This is where I started to get hung up and think that going with a different design (proposed below) might be the better way to do this.</p>
  
  <p>From reading through 20 or so "memory mapped" related questions here on SO, it seems mmap calls are sensitive to wanting contiguous runs of memory when allocated. So, for example, on a 32-bit host OS if I tried to mmap a 2GB file, due to memory fragmentation, my chances are slim that mapping will succeed and instead I should use something like a series of 128MB mappings to pull an entire file in.</p>
  
  <p>When I think of that design, even say using 1024MB mmap sizes, for a DBMS hosting up a few huge databases all represented by say 1TB files, I now have <em>thousands</em> of memory-mapped regions in memory and in my own testing on Windows 7 trying to create a few hundred mmaps across a multi-GB file, I didn't just run into exceptions, I actually got the JVM to segfault every time I tried to allocate too much and in one case got the video in my Windows 7 machine to cut out and re-initialize with a OS-error-popup I've never seen before.</p>
  
  <p>Regardless of the argument of "you'll never likely handle files that large" or "this is a contrived example", the fact that I could code something up like that with those type of side effects put my internal alarm on high-alert and made consider an alternative impl (below).</p>
  
  <p>BESIDES that issue, my understanding of memory-mapped files is that I have to re-create the mapping every time the file is grown, so in the case of this file that is append-only in design, it literally constantly growing.</p>
  
  <p>I can combat this to some extent by growing the file in chunks (say 8MB at a time) and only re-create the mapping every 8MB, but the need to constantly be re-creating these mappings has me nervous especially with no explicit <a href="http://bugs.sun.com/view_bug.do?bug_id=4724038" rel="nofollow noreferrer">unmap feature supported in Java</a>.</p>
  
  <p><strong>Question #1 of 2</strong></p>
  
  <p>Given all of my findings up to this point, I would dismiss memory-mapped files as a good solution for primarily read-heavy solutions or read-only solutions, but not write-heavy solutions given the need to re-create the mapping constantly.</p>
  
  <p>But then I look around at the landscape around me with solutions like MongoDB embracing memory-mapped files all over the place and I feel like I a missing some core component here (I do know it allocs in something like 2GB extents at a time, so I imagine they are working around the re-map cost with this logic AND helping to maintain sequential runs on-disk).</p>
  
  <p>At this point I don't know if the problem is Java's lack of an unmap operation that makes this so much more dangerous and unsuitable for my uses or if my understanding is incorrect and someone can point me North.</p>
  
  <p><strong>Alternative Design</strong></p>
  
  <p>An alternative design to the memory-mapped one proposed above that I will go with if my understanding of mmap is correct is as follows:</p>
  
  <p>Define <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html#allocateDirect%28int%29" rel="nofollow noreferrer">a direct ByteBuffer</a> of a reasonable configurable size (2, 4, 8, 16, 32, 64, 128KB roughly) making it easily compatible with any host platform (don't need to worry about the DBMS itself causing thrashing scenarios) and using the original FileChannel, perform <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html#read%28java.nio.ByteBuffer,%20long%29" rel="nofollow noreferrer">specific-offset reads</a> of the file 1 buffer-capacity-chunk at a time, completely forgoing memory-mapped files at all.</p>
  
  <p>The downside being that now my code has to worry about things like "did I read enough from the file to load the complete record?"</p>
  
  <p>Another down-side is that I don't get to make use of the OS's virtual memory logic, letting it keep more "hot" data in-memory for me automatically; instead I just have to hope the file cache logic employed by the OS is big enough to do something helpful for me here.</p>
  
  <p><strong>Question #2 of 2</strong></p>
  
  <p>I was hoping to get a confirmation of my understanding of all of this. </p>
  
  <p>For example, maybe the file cache is fantastic, that in both cases (memory mapped or direct reads), the host OS will keep as much of my hot data available as possible and the performance difference for large files is negligible.</p>
  
  <p>Or maybe my understanding of the sensitive requirements for memory-mapped files (contiguous memory) are incorrect and I can ignore all that.</p>