Working With Attachments in RavenDB 4.0
Working With Attachments in RavenDB 4.0
Dig into a new feature of RavenDB 4.0, attachments, and see an example of how they actually work.
Join the DZone community and get the full member experience.Join For Free
RavenDB vs MongoDB: Which is Better? This White Paper compares the two leading NoSQL Document Databases on 9 features to find out which is the best solution for your next project.
In my previous post, I talked about attachments, how they look in the studio, and how to work with them from code. In this post, I want to dig a little deeper into how they are actually working.
Attachments are basically blobs that can be attached to a document. A document can have any number of attachments attached to it, and the actual contents of the attachment are actually stored separately from the document. One of the advantages of this separate storage is that it also allows us to handle de-duplication.
The trivial example is needing to attach the same file to a lot of documents will result in just a single instance of that file being kept around. There are actually quite a lot of use cases that call for this (for example, imagine the default profile picture), but this really shines when you start working with revisions. Every time that document changes (which include modifications to attachments, of course), a new revision is created. Instead of having each revision clone all of the attachments, or not have attachments tracked by revisions, each revision will simply reference the same attachment data. That way, you can get a whole view of the document at a point in time, implement auditing and tracking, etc.
Another cool aspect of attachments being attached to documents is that they flow in the same manner over replication. So, if you modified a document and added an attachment, that modification will be replicated at the same time (and in the same transaction) as that attachment.
In terms of actually working with the attachments, we keep track of the references between documents and attachments internally and expose them via the document metadata.
This is done so you don’t need to make any additional server calls to get the attachments on specific documents. You just need to load it, and you have it all there.
This looks like this, note that you can get all the relevant information about the attachment directly from the document, without having to go elsewhere. This is also how you can compare attachment changes across revisions, and this allows you to write conflict resolution scripts that operate on documents and attachments seamlessly.
Putting the attachment information inside the document metadata is a design decision that was made because for the vast majority of the cases, the number of attachments per document is pretty small, and even in larger cases (dozens or hundreds of attachments) it works very well. If you have a case where a single document has many thousands or tens of thousands of attachments, that will likely be a very high load on the metadata, and you should consider splitting the attachments into multiple documents (sub-documents with some domain knowledge will work).
Let's consider a big customer, to whom we keep issuing invoices. A good problem to have is that eventually, we’ll issue enough invoices to that customer that we start suffering from very big metadata load just because of the attachment tracking. We can handle that by using (customer/1234/invoices/2017-04, customer/1234/invoices/2017-05 ) as the documents we’ll use to hang the attachments on.
This was done intentionally because it mimics the same way you’ll split a file that has an unbounded growth (keeping all invoicing data for a big customer in a single document is also not a good idea, and has the same solution).
Published at DZone with permission of Oren Eini, CEO RavenDB , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.