Scaling Marten with PostgreSQL Read Replicas

JasperFx Software is open for business and offering consulting services (like helping you craft scalability strategies!) and support contracts for both Marten and Wolverine so you know you can feel secure taking a big technical bet on these tools and reap all the advantages they give for productive and maintainable server side .NET development.

First off, big thanks to Jaedyn Tonee for actually doing all the work I’m writing about here. JT recently accepted a long standing “offer” to be part of the official “Critter Stack Core Team.”

Marten 7 embraced several new-ish features in Npgsql, including the NpgsqlDataSource concept for connection management. This opened Marten up for a couple other capabilities like integrating Marten with .NET Aspire. It also enabled Marten to utilize PostgreSQL Read Replicas for read only query usage. Read Replicas are valuable both for high availability of database systems, and to offload heavy read intensive operations off of the main database server and onto read replicas.

To opt into read replicas with Marten, you need to utilize the new MultiHostNpgsqlDataSource in Npgsql and Marten as shown in this sample code:

// services is an IServiceCollection collection
services.AddMultiHostNpgsqlDataSource(ConnectionSource.ConnectionString);

services.AddMarten(x =>
    {
        // Will prefer standby nodes for querying.
        x.Advanced.MultiHostSettings.ReadSessionPreference = TargetSessionAttributes.PreferStandby;
    })
    .UseLightweightSessions()
    .UseNpgsqlDataSource();

Behind the scenes, if you are opting into this model, when you make a query with Marten like this:

       // theSession is an IDocumentSession 
       var users = await theSession
            .Query<User>()
            .Where(x => x.FirstName == "Sam")
            .ToListAsync();

Marten will be trying to connect to a PostgreSQL read replica to service that LINQ query.

Summary

I hope this is an important new tool for Marten users to achieve both high availability and scalability within systems with bigger data loads.

Leave a comment