Skip to content

Idempotent Aggregates

Sponsor: Do you build complex software systems? See how NServiceBus makes it easier to design, build, and manage software systems that use message queues to achieve loose coupling. Get started for free.

Learn more about Software Architecture & Design.
Join thousands of developers getting weekly updates to increase your understanding of software architecture and design concepts.


Idempotent AggregatesThis blog post will cover creating Idempotent Aggregates and is apart of a series of posts related. Please take a look any previous posts as they may give more context to the code samples below.

Idempotence
Idempotence is the property of certain operations in mathematics and computer science, that can be applied multiple times without changing the result beyond the initial application.
In my previous post Idempotent Commands, I looked at creating idempotency at the persistence level if you are using an ACID compliance database. But what happens if you aren’t?  What other options do you have? Well if you are building a domain model and are creating Aggregates, you could create idempotent aggregates.

Aggregates

If there is enough value and complexity, I will create a domain model with Aggregates that will contain domain behavior and business logic. This is example is very trivial and I wouldn’t be creating an aggregate for basic CRUD. However, if we did have a complex domain and aggregate, we can pass in an OperationId to our Aggregate, prior to calling any behavioral methods in order to check if we have already executed that operation.

Handlers

Now in our Command Handler, we can call SetOperation and pass our MessageId from our Envelope<T>.
And as our previous example, here is our NancyFX Module (similar to ASP.NET Controller) which is sending our Command via MediatR.

Concurrency

I have not brought up concurrency as this is something that will be determined based on your data storage solution.

How do you?

How do you handle idempotent commands? I’d love to hear about other implementations or uses.  Comment below or on twitter.

Other Posts