← All posts
~/blog/why-i-built-pulsesharp.md#open-source #dotnet

Why I built PulseSharp instead of using a bigger library

Many .NET apps need two things that sound simple.

  1. When something happens, do a few things about it, and never lose any of it.
  2. Do some work in the background, and tell me how it’s going.

I’ll call the first one events and the second one jobs. I kept writing them again and again, in slightly different shapes, in every project. So I finally wrote them once, properly. That’s PulseSharp.

Why not just use what exists

There are good tools for this. Hangfire is a solid choice for background jobs. MassTransit is a solid choice when you have a message broker. I’m not here to say they’re bad.

My problem was the shape of the app. A lot of the software I think about doesn’t live on a server. It’s a point-of-sale till, a desktop app in a clinic, a warehouse scanner. These devices need to keep working when the internet is down, and the cloud still needs to hear about everything that happened.

Most tools in this space assume a server, a broker or one specific database. A till with a SQLite file and a bad connection isn’t what they were built for. I wanted one small programming model that works in a desktop app on SQLite, in a web app on PostgreSQL, and in a test, in memory.

What it looks like

csharp
builder.Services.AddPulseSharp(pulse =>
{
    pulse.UseSqlite("app.db");
    pulse.On<SaleCompleted>("sales-log", (sale, context, ct) =>
    {
        Console.WriteLine($"Sold for {sale.Total}");
        return Task.CompletedTask;
    });
});

await pulse.Emit(new SaleCompleted(sale.Id, sale.Total));
await pulse.Enqueue(new SendReceipt(sale.Id));

Emit saves the event first, then delivers it to every handler, in order, with retries. Enqueue saves a job, runs it once and lets you report progress. That’s most of the API. The words are plain on purpose: Emit, On, Enqueue, Stream, Cursor.

The idea I’m happiest with

Events are facts. “Sale 1043 completed at 14:02” never gets edited later.

That makes syncing them easy. Two sides only need to swap the events the other side hasn’t seen yet. There’s nothing to merge and no argument about which version wins. Syncing state is hard. Syncing events is simple.

So a till saves its events locally and runs its handlers right away, even offline. When it’s back online, it sends what it hasn’t sent. The server skips anything it already has. It also works the other way, so a price change made in the cloud reaches every till.

What I left out on purpose

  • Exactly-once delivery. It’s at-least-once, and each handler sees each stream in order. The docs say this plainly. I’d rather promise less and keep it than promise more and lie. Your handlers should be safe to run twice.
  • A big feature list in the core. The contracts package has no dependencies, and the core has no ASP.NET Core in it. You add only the packages you use.
  • A web UI. A monitor is planned for after 1.0. It isn’t there now.

Numbers, with an honest label

On the in-memory store, on one machine, with a short BenchmarkDotNet run: emitting an event and delivering it to one handler takes about 2.2 microseconds. Writing to a SQLite file with full durability takes about 0.66 ms per event. PostgreSQL in local Docker takes about 2 ms. These are my numbers on my machine, so treat them as a rough guide and run the benchmarks yourself.

Where it’s still rough

It’s a preview, version 0.8. The API can still change before 1.0. SQLite and PostgreSQL are the only stores today. SQL Server and recurring jobs are on the roadmap, not in the box.

To try it:

terminal
dotnet add package PulseSharp.Sqlite --prerelease

The docs and source are on GitHub under the pulsesharp organisation. If something breaks or the API feels awkward, open an issue. Now is the cheapest time to change it.