Open-source library
SNS.EventChannel
Publish to Amazon SNS without making the request wait.
// Returns as soon as the event is queued.
await eventRaiser.RaiseEvent(new OrderCreated
{
OrderId = order.Id,
CreatedAt = DateTime.UtcNow
});
// A hosted worker drains the channel in the
// background: up to ten messages per SNS batch,
// with retries on throttling and server errors.The problem
Publishing an event inside a web request ties that request to a network call. The response waits on Amazon SNS, and a throttled or failed publish becomes the caller’s problem. Most code that raises an event only needs to hand it off and move on.
My contribution
I wrote a library that gives each event type its own in-memory channel. Application code raises an event through one small service; a hosted background worker reads the channel, groups messages into SNS batch requests, and retries transient failures. Channels and the worker register through dependency-injection extension methods.
The result
An MIT-licensed package on NuGet with unit and integration tests. Raising an event is one awaited call, and batching, retries, and logging stay out of application code.
A few decisions that shaped it
Bounded by default
Each channel has a capacity limit, and writers wait when it is full. A burst of events slows producers down rather than growing memory without limit. An unbounded channel is available as an explicit choice.
Batch without adding delay
The worker fills a batch up to the SNS limit of ten messages, but sends what it has as soon as the channel is empty. Busy services get fewer API calls; quiet ones do not wait for a batch to fill.
Retry only what can succeed
Throttling and server errors are retried with exponential backoff. The retry policy is a Polly policy, so an application can replace it with its own.
Under the hood
- Typed channels, one entry point
- Every event type gets its own Channel<T>. A single EventRaiser service looks up the channel for the type being raised, so calling code depends on one class however many event types an application defines.
- A worker that follows the host
- The worker is an IHostedService. It starts with the application, runs its read loop without blocking startup, and on shutdown cancels the loop and waits briefly for in-flight work.
Have a project in mind? Let’s talk