Git Product home page Git Product logo

outboxpatterndemo's Introduction

Build All

Outbox Pattern Demo

This project contains a basic implementation of the Outbox Pattern in C#.

What is the Outbox Pattern?

The problem

Often when the state of a business entity changes, you want to publish an event stating that this has happened so that other microservices are in sync. To achieve this, you would do the following:

  1. Receive message
  2. Update business entity
  3. Publish 'EntityUpdated' event

However, if step (2) is not idempotent and only step (3) fails, we find ourselves in a scenario where the business entity has been updated an other microservices are not aware of this change.

One (of many) solutions

The Outbox Pattern solves this problem by moving the event publisher into an outbox processor. The command service then updates the entity and adds a record to the outbox messages in the same transaction. The outbox processor will poll this outbox messages for any unprocessed messages, and publish them as they arrive. This is illustrated in the diagram below:

Diagram

This project

This project demo's the Outbox Pattern using NServiceBus (In Memory) and EntityFramework (SQLite). It contains 3 apps:

App Purpose
OutboxPatternDemo.MedicalRecords ASP.NET Web API that allows you to update medical records, and uses the Outbox Pattern to guarantee that update events are published at least once
OutboxPatternDemo.Bookings Console App that subscribes to and de-duplicates messages, and uses a saga to book follow up appointments
OutboxPatternDemo.Monitoring Runs the Particular Service Platform, which allows you to monitor events within the system

Getting started

All of the code is in the src folder. It requires the dotnet 6 SDK to run.

NServiceBus

Get your free development license from: https://particular.net/license/nservicebus?t=0&p=servicepulse

Then, copy your license.xml file to %LocalAppData%/ParticularSoftware. You may need to restart VS (or potentially your PC) for the license file to be picked up. This will be required NSB monitoring.

Entity Framework

The underlying data source for outbox events uses SQL Server. Before you can run the OutboxPatternDemo.MedicalRecords/Bookings apps, you need to create a local database called OutboxPatternDemo.

First, verify that Entity Framework is installed:

# install + update Entity Framework tool
dotnet tool install --global dotnet-ef
dotnet tool update --global dotnet-ef

Then, run the following commands from a PowerShell terminal in the src/OutboxPatternDemo.MedicalRecords directory:

# create contexts
dotnet ef database update --context MedicalRecordContext
dotnet ef database update --context CustomOutboxContext

and the following commands from the src/OutboxPatternDemo.Bookings directory:

# create contexts
dotnet ef database update --context DuplicateKeyContext

Updating migrations

First, verify that Entity Framework is installed:

# install + update Entity Framework tool
dotnet tool install --global dotnet-ef
dotnet tool update --global dotnet-ef

src/OutboxPatternDemo.MedicalRecords:

# create contexts
dotnet ef migrations add <migrationname> --context MedicalRecordContext
dotnet ef migrations add <migrationname> --context CustomOutboxContext

src/OutboxPatternDemo.Bookings:

# create contexts
dotnet ef migrations add <migrationname> --context DuplicateKeyContext

outboxpatterndemo's People

Contributors

benchiverton avatar benchiverton-cal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

bionstt devbrsa

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.