Content Developer II at Microsoft, working remotely in PA, TechBash conference organizer, former Microsoft MVP, Husband, Dad and Geek.
124031 stories
·
29 followers

Multi-Tenancy: Mixed Modes with Marten

1 Share

This is continuing a series about multi-tenancy with MartenWolverine, and ASP.Net Core:

  1. What is it and why do you care?
  2. Marten’s “Conjoined” Model
  3. Database per Tenant with Marten
  4. Mixed Modes with Marten (this post)

In previous posts I’ve described first why multi-tenancy might be useful, how to do it with Marten’s “conjoined” tenancy model, and lastly how to do it with separate databases for each tenant.

Marten is more flexible than that though, and it’s sometimes valid to mix and match the two tenancy modes. Offhand, I can think of a couple reasons that JasperFx clients or other Marten users have hit:

  1. Multi-level tenancy. In one case I’m aware of, a Marten system was using a separate database for each country where the system was operational, but used conjoined tenancy by company doing business within that country. That was beneficial for scaling, but also to be able to easily query across all businesses within that country and to keep reference data centralized by country database as well
  2. Being able to reduce hosting costs by centralizing one larger customer on their own database while keeping other customers on a second database. Or simply spreading customer data around to more equitably spread database load rather than incurring the hosting cost of having so many separate databases

In regards to the second point, I’d be curious to hear how that equation might change for something like the Neon serverless hosted PostgreSQL model.

The simplest model for this is the static multi-tenancy recipe in Marten that simply makes you specify the tenant ids and databases upfront like so:

        var builder = Host.CreateApplicationBuilder();
        builder.Services.AddMarten(opts =>
        {
            opts.MultiTenantedDatabases(tenancy =>
            {
                // Put data for these three tenants in the "main" database
                // The 2nd argument is just an identifier for logs and diagnostics
                tenancy.AddMultipleTenantDatabase(builder.Configuration.GetConnectionString("main"), "main")
                    .ForTenants("company1", "company2", "company3");

                // Put the single "big" tenant in the "group2" database
                tenancy.AddSingleTenantDatabase(builder.Configuration.GetConnectionString("group2"), "big");
            });
        });

        await builder.Build().StartAsync();

When fetching data, you still use the same mechanism to create a session for the tenant id like:

var session = store.LightweightSession("company1");

Marten will select the proper database for the “company1” tenant id above, and also set default filters on tenant_id = 'company1' on that session for all LINQ queries unless you explicitly tell Marten to query against all tenants or a different, specific tenant id.

Summary and What’s Next

You’ve got options with Marten! I think for the next post in this series I’ll move to Wolverine instead and how it can track work for a tenant across asynchronous message handling.



Read the whole story
alvinashcraft
2 hours ago
reply
West Grove, PA
Share this story
Delete

Polypane-demonium

1 Share

Polypane purveyor Kilian Valkhof joins Nick & Jerod to tell us all about his efforts building a web browser just for web development. We cover it all: from the business concerns, to the technical details, to his excellent choice not to use TypeScript! We even sneak in a feature request that already made its way into this excellent dev tool for ambitious web developers.

Leave us a comment

Changelog++ members save 10 minutes on this episode because they made the ads disappear. Join today!

Sponsors:

  • Socket – Secure your supply chain and ship with confidence. Install the GitHub app, book a demo or learn more
  • Neon – Fleets of Postgres! Enterprises use Neon to operate hundreds of thousands of Postgres databases: Automated, instant provisioning of the world’s most popular database.

Featuring:

Show Notes:

Something missing or broken? PRs welcome!





Download audio: https://op3.dev/e/https://cdn.changelog.com/uploads/jsparty/327/js-party-327.mp3
Read the whole story
alvinashcraft
2 hours ago
reply
West Grove, PA
Share this story
Delete

How to Become a Technical Writer – A Guide for Developers

1 Share
Being a top-notch developer was my dream – until I realized I could combine my passion for writing with my technical knowledge to earn a living. That's right: I'm talking about becoming a technical writer. And if you're a developer, you may be in a good position to get into

Read the whole story
alvinashcraft
2 hours ago
reply
West Grove, PA
Share this story
Delete

Microsoft’s embarrassing Recall

1 Share
Illustration of Windows Recall
Image: Microsoft

Last week, I revealed that Microsoft’s Windows engineers were scrambling to secure its controversial AI-powered Recall feature in time for its planned debut on June 18th. Hours later, Microsoft announced that the feature that screenshots nearly everything you do on new Qualcomm-powered laptops is being recalled.

Copilot Plus PCs shipped earlier this week without Recall, and Microsoft has modified the setup process to remove it from Windows 11 — it’s now a feature that’s “coming soon” to these devices. Recall is still prominently featured in Microsoft’s marketing materials, which suggests the company is confident it will return very soon.

After developing Recall largely in secret away from the usual Windows Insider public testing,...

Continue reading…

Read the whole story
alvinashcraft
4 hours ago
reply
West Grove, PA
Share this story
Delete

New .NET 9 Templates for Blazor Hybrid, .NET MAUI

1 Share
Microsoft's fifth preview of .NET 9 nods at AI development while also introducing new templates for some of the more popular project types, including Blazor Hybrid and .NET MAUI.
Read the whole story
alvinashcraft
4 hours ago
reply
West Grove, PA
Share this story
Delete

Vivaldi 6.8 Arrives with Vivaldi Mail 2.0

1 Share

Vivaldi 6.8 for desktop is now available, and it arrives with a major new release of its integrated mail app, Vivaldi Mail 2.0.

The post Vivaldi 6.8 Arrives with Vivaldi Mail 2.0 appeared first on Thurrott.com.

Read the whole story
alvinashcraft
4 hours ago
reply
West Grove, PA
Share this story
Delete
Next Page of Stories