Skip to content

Marten as Document Database

Jeremy Miller27th July 2023

Marten is an open source library that allows .NET developers to utilize the rock solid PostgreSQL database engine as a full fledged document database and event store. In this post let’s just concentrate on how to get started with the document database features in Marten and why you might want to do that.

Why a Document Database? Why PostgreSQL? Why Marten?

First though, let’s simply think about the basics of server side system development with a backing database. We commonly use an in memory model of our business domain or just the system state in code, but then persist that information in a potentially different structure in the actual database. That is frequently solved by using a relational database (RDBMS) for data storage, then an object-relational mapper (ORM) framework of some sort to translate back and forth between the two separate models. As many of us know from experience, that can be laborious and sometimes error prone.

Fortunately, the software industry has more recently supplemented the tried and true RDBMS database model with various NoSQL database models. In the document-oriented model, business entity data is stored as structured data, usually as JSON or some sort of binary representation of JSON. When appropriate for your application, document database provide much more development efficiency by eliminating explicit mapping and by allowing for much more rapid iteration of your business model and backing database state. That’s been absolutely fantastic for the development experience, and even aids quite a bit in test automation scenarios. At the time Marten was first conceived though, those fancy new NoSQL databases also lacked strong transactional models.

This is where PostgreSQL and Marten comes in. At the time (2015/16), the other original founders of Marten and I wanted the NoSQL document database model that we liked, but with an ACID transactional model for strong consistency — and do so with a level of reliability that we desperately needed at the time for an important application using a NoSQL database that was struggling under peak loads. At that time, PostgreSQL 9.4 had just released a new version with their efficient JSONB storage model and a whole host of JSON related operators. Marten was launched as an effort to leverage the very robust PostgreSQL database engine, but leverage its new JSONB support as a full fledged document database while also getting a strongly consistent transactional model.

You can read more about Marten and its document database features in Fast Application Persistence with Marten from CoDe Magazine.

And also check out how Marten works with PostgreSQL with this presentation from Jeremy from CitusCon 2023:

All Rights Reserved.