SlideShare ist ein Scribd-Unternehmen logo
1 von 38
Message Oriented Architecture
using NServiceBus
Lars-Erik Kindblad
Senior Consultant
Twitter: @kindblad
E-mail: le@kindblad.com
What is NServiceBus?
 Lightweight messaging framework for designing distributed systems in
.NET
 An Enterprise Service Bus
 Helps to make distributed systems more
 Reliable
 Scalable
 Extensible

 Founded in 2006 by UDI Dahan, an international renowned expert on
software architecture and design
 Services and support provided by Particular Software
 Open Source but not free
 Source code available at https://github.com/Particular/NServiceBus

 Binaries available as NuGet packages
 Remote Procedure Call (Request/Response)
Request: Order

Website

Response: Order ID

Order Service
PlaceOrder(order)

Synchronous – The website is blocked while calling the Order Service

 Messaged Oriented Architecture (One-way messaging)
Message: PlaceOrder

Website

Message Queue

Connect

Receive
PlaceOrder message

Windows Service
Process the PlaceOrder message
Asynchronous – The website is NOT blocked while the place order is processed
NServiceBus Terminology & Components
Sender
/Send-Only
Endpoint

Message,
Command
or Event
Message: PlaceOrder

Website

Message Queue

Connect

Receive
PlaceOrder message

Windows Service
Process the PlaceOrder message
Message Handler

Worker
Endpoint,
Host or
Receiver
Messages in NServiceBus
 Just simple .NET classes with properties

 2 types of messages
 Command
• Do something: PlaceOrder
• Sent from one or more senders, processed by one endpoint
• Implement ICommand

 Event
• Something has happened: OrderWasPlaced
• Published from one sender, processed by several endpoints/subscribers
• Implement IEvent
 Command
Strongly coupled to
the receiving
endpoint
Command: PlaceOrder

Website

Command: PlaceOrder

Message Queue

Endpoint

 Event (Publish/Subscribe)
Subscribing
Endpoint

Loosely coupled to
the receiving
endpoints
Event: OrderWasPlaced

Worker
NServiceBus will add 3
messages to the
queue, one for each
subscriber

Event: OrderWasPlaced

Message Queue

Subscribing
Endpoint

Subscribing
Endpoint
Queues
 First in-first out datastructure for storing and retrieving messages
PlaceOrder message 4
First-In
Queue
PlaceOrder message 3
PlaceOrder message 2
PlaceOrder message 1
First-Out

 Default queue in NServiceBus is Microsoft Message Queuing (MSMQ)
 Available on all Windows editions since NT 4 and Windows 95

 Also support for






ActiveMQ
RabbitMQ
SQL Server
Windows Azure Queues
Windows Azure ServiceBus

 One endpoint has one queue, Send-Only endspoints do not have queues
How to Send & Handle Messages
Command: PlaceOrder

Website

Send

Command: PlaceOrder

Message Queue

Endpoint

Handle
(Command or Event)
Summary
 Sender endpoint
 Sends a message
 Can be a website, desktop application or even a worker endpoint

 Command
 Message where we want to do something concrete
 ICommand interface

 Event
 Message where we notify about something that has happened
 IEvent interface

 Message handler
 Processes a given message
 IHandleMessages<Message> interface

 Worker endpoint
 Finds and executes the handler for the message
 Can be a Windows Service, console application, WCF, website
NServiceBus Requirements
 MSMQ (default) or any other supported queue






ActiveMQ
RabbitMQ
SQL Server
Windows Azure Queues
Windows Azure ServiceBus

 Distributed Transaction Cordinator (DTC)
 RavenDB (default) or a relational database
 Download the installer from NServiceBus from http://particular.net/ to
setup the required components
NuGet packages
 NServiceBus Interfaces
 Interfaces that we need to define messages

 NServiceBus
 Most of the code that drives NServiceBus except hosting
 Use for send-only endpoints

 NServiceBus Host
 Console application that can be installed as a Windows Service
 Used to process/handle messages

 NServiceBus Testing
 Framework for testing NServiceBus endpoints
The Case – The eBook Shop
1. Create the order in the
database
2. Auto-pay the order using a
previously stored credit
card
3. Send an order confirmation
by e-mail
4. Deliver the book to the
Windows Phone device
The Usual Way - Request/Response
Place order
1. Add order

Browser

OrderController
Confirmation page
with order ID

Database
2. Charge credit card

PaymentService
3. Send mail

SMTP-Server
4. Deliver ebook

DeviceService
Problem #1 – The order is lost during an error
Place order
1. Add order

Browser

OrderController

Database

Error page
Exception

2. Charge credit card

Error

PaymentService

3. Send mail

SMTP-Server
4. Deliver ebook

DeviceService

 Conseqence
 The database is rolled back
 User receives an error
 Order must be sent again
Problem #2 – Poor Transaction Management
Place order
1. Add order

Browser

OrderController

Database

Error page
2. Charge credit card

PaymentService
3. Send mail

SMTP-Server
Exception

4. Deliver ebook

Error

DeviceService

 Conseqence:
 The credit card is charged (no transaction support)
 A mail saying the order was successful was sent (no transaction support)
 The order do not exist in the database (supports transaction – rolled back)
 The user will receive an error
Let’s Fix This With NServiceBus
 Change to a message oriented architecture
Add PlaceOrder
message

Place order

Browser

Queue

OrderController
Confirmation page

Old

New
The Message Must Also Be Processed
MSMQ

1. Connect

2. Receive message PlaceOrder

Worker
(Windows Service)

PlaceOrder

3. PlaceOrder
Message Handler

4. Add order

Database
5. Charge credit card

PaymentService
6. Send mail

SMTP-Server
7. Deliver ebook

DeviceService
When an Error Occur

4. Rollback. Put the message
back on the queue
and retry

MSMQ

1. Connect

2. Receive message
PlaceOrder

Worker
(Windows Service)

PlaceOrder

3. PlaceOrder
Error
Message Handler
More about MSMQ
 Max 4MB message size
 Persists messages to files. Default location C:WINDOWSSystem32msmqstorage
 Guranteed once-and-only-once delivery
 The message will only be handled once

 Supports transactional queues and distributed transactions (using DTC)
 If an error occur both changes to the database and to the queue can be rolled back

 Supports store-and-forward
 MSMQ will store messages locally before forwarding it to the queue on another server
 MSMQ will confirm when the message has been written to the disk in the outgoing
queue, not when it has been delivered to the server
 Important feature for distributed systems with unrealiable networks
Server A

Sender

MSMQ
Outgoing
queue

Server B
Message

MSMQ
Input
queue

Worker
How NServiceBus Handles Failure
 Transaction management and rollback are automatically handled by
NServiceBus in message handlers
 When an error occur
1. The message is put back on the queue
2. Attempt to process the message 5 times, wait for 10 seconds
3. Attempt to process the message 5 times, wait for 20 seconds
4. Attempt to process the message 5 times, wait for 30 seconds
5. Attempt to process the message 5 times, send the message to an error queue
The time interval is configurable

 Messages can be moved back to the original queue for replay using
ReturnToSourceQueue.exe tool.
A challenge – The Order ID
Old

New

 The order ID has not been generated yet – it’s generated in the message
handler at the worker endpoint
Possible Solutions
 Change the UI to not show the order ID
 Use a GUID (Guid.NewGuid()) or find another way to generate the ID in the controller
instead of in the database
• message.OrderId = Guid.NewGuid();
Bus.Send(message);
ViewBag.OrderId = message.OrderId;
• 5773DD0E-0AB0-446B-8649-B2D43D7DA4AA is not a very user friendly ID

 Move OrderRepository.Add(order) to the Controller:
• var orderId = OrderRepository.Add(order);
Bus.Send(message);
• Less reliable and more error prone solution

 Use Send/Reply to simulate request/response
• The controller will wait for a PlaceOrderCompleted message that the PlaceOrder handler will send
• Too much overhead
• An Anti-pattern
We Still Have Poor Transaction Management
1. Add order

Handle PlaceOrder

Database
2. Charge credit card

PaymentService
3. Send mail

SMTP-Server
4. Deliver ebook
Exception

Error

DeviceService

 Conseqence:
 The credit card is charged (no transaction support)
 A mail saying the order was successful was sent (no transaction support)
 The order do not exist in the database (supports transaction – rolledback)
 The message is put back on the queue and will be retried = credit card will be charged
twice
Solution
 Split into man small messages - One message per transactional boundary
Place order

OrderController

Browser

1. Add PlaceOrder message
PlaceOrder message

2. Handle PlaceOrder

Add PayOrder message

If an error occur only
DeliverEbook is affected. The
message will be put back on the
queue and retried

SendMail message

4. Handle SendMail
DeliverEbook message

5. Handle DeliverEbook

PayOrder message

Queue

Add SendMail &
DeliverEbook message

3. Handle PayOrder
Code - The Messages
Code – Message Handlers #1
Code – Message Handlers #2
Yet another transactional issue
MSMQ

Processed
ok

Handle PayOrder message

Charge credit card

Request
Handle PayOrder

Request: Error

PaymentService

Payment OK

Network
error

External
Payment
Gateway

 The message will be put back on the queue and processed again
 Consequence: The credit card will be charged more than once
Solution
 Operations should be idempotent
 Idempotent operations can be run many times without changing the result

MSMQ

Handle PayOrder message
Charge credit card

Request
Handle PayOrder

Payment OK

PaymentService

Has the order
been paid?
Use OrderId

Database

Payment OK

External
Payment
Gateway

Has the
message
already been
processed?
Use order ID
or a GUID

Database
Another Challenge – Eventual Consistency

Users clicks on View
Order History

 The PlaceOrder message is processed async by another process
 If it has not gotten processed yet then the new order will not be in the list
 Solution
 Some UI trick?
 Move the OrderRepository.Add to the controller
 Make sure the messages are handled fast enough
• Define a SLA on the message to help monitoring
Publish/Subscribe
Place order

OrderController

Browser

1. Add PlaceOrder message
PlaceOrder command

2. Handle PlaceOrder

Add PayOrder command

With Publish/Subscribe the
Subscribers are loosely coupled
to the publisher
Subscribe to OrderWasPaid

4. Handle SendMail

Subscribes to OrderWasPaid

Queue

3. Handle PayOrder
Add OrderWasPaid event

OrderWasPaid

Subscribes to OrderWasPaid

4. Handle DeliverEbook

PayOrder command

OrderWasPaid

NServiceBus will add 2
messages to the
queue, one for each
subscriber
Summary
Request/Response

Command

Event

X

X

Automatically retries

X

X

Full transactional
support

X

X

Reliable
Consistency

X

Can return ID
generated by the
database to the
sender

X

Loosely coupled

X

 Reads is synchronous and should be request/response
 Using a queue for synchronous operations gives little value, only overhead

 Writes can be asynchronous one-way messaging or events
 There are no silver bullet – We are always trading one set of problems for
another set of problems
Scaling
 Scale up
 Easy to scale up if business processes are split into multiple messages
 Increase the number of threads in the config – NServiceBus will concurrently process
multiple messages

 Scale out
 Use the Distributor or the Master (Both a distributor and a worker)
Worker #1

Message

MSMQ

Distributor

Worker #2

Worker #3

 The distributor will forward the message to a worker that is ready to process the message
How the Distributor Works
1. Worker:
Send I’m ready
message

I’m ready message

Distributor:
Control Queue

I’m ready message

Distributor

Any messages in
the input queue?

PlaceOrder message

1. Website:
Send
PlaceOrder
message

Send PlaceOrder
message from input
queue

PlaceOrder message

Distributor
Input Queue

Yes

No

PlaceOrder message

Distributor

Find worker
through
message in
Storage
queue

Store I’m ready
message
Storage Queue
Other Features
 Sagas
 Long running workflow like processes
 State is shared between multiple message handlers

 Scheduling
 Send a message every x minute. A handler will handle the message

 Unobtrusive mode
 Use your own ICommand, IEvent etc. to reduce coupling to NServiceBus

 Supports
 Various dependency injection containers
 Logging frameworks
 Etc.

 ++
NServiceBus License & Pricing
 Open source
 Used to be free, but not anymore
 Indefinitely license with 1 year maintenance updates
• $500 USD per processing core
• $250 USD per sending core

 Monthly subscription
• $35 USD per processing core
• $17 USD per sending core

 Development and testing environments + disaster recovery and passive
backups are free
 Developers need a license (free) that must be renewed every 3rd month
through particular.net
Demo
About Capgemini
With more than 120,000 people in 40 countries, Capgemini is one
of the world's foremost providers of consulting, technology and
outsourcing services. The Group reported 2011 global revenues
of EUR 9.7 billion.
Together with its clients, Capgemini creates and delivers
business and technology solutions that fit their needs and drive
the results they want. A deeply multicultural organization,
Capgemini has developed its own way of working, the
Collaborative Business ExperienceTM, and draws on Rightshore ®,
its worldwide delivery model.
Rightshore® is a trademark belonging to Capgemini

www.capgemini.com

The information contained in this presentation is proprietary.
© 2012 Capgemini. All rights reserved.

Weitere ähnliche Inhalte

Was ist angesagt?

Windows Registered I/O (RIO) vs IOCP
Windows Registered I/O (RIO) vs IOCPWindows Registered I/O (RIO) vs IOCP
Windows Registered I/O (RIO) vs IOCPSeungmo Koo
 
〈야생의 땅: 듀랑고〉 서버 아키텍처 Vol. 3
〈야생의 땅: 듀랑고〉 서버 아키텍처 Vol. 3〈야생의 땅: 듀랑고〉 서버 아키텍처 Vol. 3
〈야생의 땅: 듀랑고〉 서버 아키텍처 Vol. 3Heungsub Lee
 
Async Messaging in CQRS: Part 1 - Masstransit + DDD Intro
Async Messaging in CQRS: Part 1 - Masstransit + DDD IntroAsync Messaging in CQRS: Part 1 - Masstransit + DDD Intro
Async Messaging in CQRS: Part 1 - Masstransit + DDD IntroGeorge Tourkas
 
홍성우, 게임 서버의 목차 - 시작부터 출시까지, NDC2019
홍성우, 게임 서버의 목차 - 시작부터 출시까지, NDC2019홍성우, 게임 서버의 목차 - 시작부터 출시까지, NDC2019
홍성우, 게임 서버의 목차 - 시작부터 출시까지, NDC2019devCAT Studio, NEXON
 
왕초보를 위한 도커 사용법
왕초보를 위한 도커 사용법왕초보를 위한 도커 사용법
왕초보를 위한 도커 사용법GeunCheolYeom
 
NDC12_Lockless게임서버설계와구현
NDC12_Lockless게임서버설계와구현NDC12_Lockless게임서버설계와구현
NDC12_Lockless게임서버설계와구현noerror
 
Python 게임서버 안녕하십니까 : RPC framework 편
Python 게임서버 안녕하십니까 : RPC framework 편Python 게임서버 안녕하십니까 : RPC framework 편
Python 게임서버 안녕하십니까 : RPC framework 편준철 박
 
오딘: 발할라 라이징 MMORPG의 성능 최적화 사례 공유 [카카오게임즈 - 레벨 300] - 발표자: 김문권, 팀장, 라이온하트 스튜디오...
오딘: 발할라 라이징 MMORPG의 성능 최적화 사례 공유 [카카오게임즈 - 레벨 300] - 발표자: 김문권, 팀장, 라이온하트 스튜디오...오딘: 발할라 라이징 MMORPG의 성능 최적화 사례 공유 [카카오게임즈 - 레벨 300] - 발표자: 김문권, 팀장, 라이온하트 스튜디오...
오딘: 발할라 라이징 MMORPG의 성능 최적화 사례 공유 [카카오게임즈 - 레벨 300] - 발표자: 김문권, 팀장, 라이온하트 스튜디오...Amazon Web Services Korea
 
PyQGIS 개발자 쿡북(PyQGIS Developer Cookbook) 한국어 판
PyQGIS 개발자 쿡북(PyQGIS Developer Cookbook) 한국어 판 PyQGIS 개발자 쿡북(PyQGIS Developer Cookbook) 한국어 판
PyQGIS 개발자 쿡북(PyQGIS Developer Cookbook) 한국어 판 SANGHEE SHIN
 
Introduction to CI/CD
Introduction to CI/CDIntroduction to CI/CD
Introduction to CI/CDHoang Le
 
[야생의 땅: 듀랑고] 서버 아키텍처 Vol. 2 (자막)
[야생의 땅: 듀랑고] 서버 아키텍처 Vol. 2 (자막)[야생의 땅: 듀랑고] 서버 아키텍처 Vol. 2 (자막)
[야생의 땅: 듀랑고] 서버 아키텍처 Vol. 2 (자막)Heungsub Lee
 
Building .NET Microservices
Building .NET MicroservicesBuilding .NET Microservices
Building .NET MicroservicesVMware Tanzu
 
[야생의 땅: 듀랑고] 서버 아키텍처 - SPOF 없는 분산 MMORPG 서버
[야생의 땅: 듀랑고] 서버 아키텍처 - SPOF 없는 분산 MMORPG 서버[야생의 땅: 듀랑고] 서버 아키텍처 - SPOF 없는 분산 MMORPG 서버
[야생의 땅: 듀랑고] 서버 아키텍처 - SPOF 없는 분산 MMORPG 서버Heungsub Lee
 
containerd the universal container runtime
containerd the universal container runtimecontainerd the universal container runtime
containerd the universal container runtimeDocker, Inc.
 
GitLab Auto DevOps 大解析—CI/CD 原來可以這樣做
GitLab Auto DevOps 大解析—CI/CD 原來可以這樣做GitLab Auto DevOps 大解析—CI/CD 原來可以這樣做
GitLab Auto DevOps 大解析—CI/CD 原來可以這樣做Chen Cheng-Wei
 
잘 알려지지 않은 숨은 진주, Winsock API - WSAPoll, Fast Loopback
잘 알려지지 않은 숨은 진주, Winsock API - WSAPoll, Fast Loopback잘 알려지지 않은 숨은 진주, Winsock API - WSAPoll, Fast Loopback
잘 알려지지 않은 숨은 진주, Winsock API - WSAPoll, Fast Loopback흥배 최
 
[KGC 2012]Boost.asio를 이용한 네트웍 프로그래밍
[KGC 2012]Boost.asio를 이용한 네트웍 프로그래밍[KGC 2012]Boost.asio를 이용한 네트웍 프로그래밍
[KGC 2012]Boost.asio를 이용한 네트웍 프로그래밍흥배 최
 

Was ist angesagt? (20)

Windows Registered I/O (RIO) vs IOCP
Windows Registered I/O (RIO) vs IOCPWindows Registered I/O (RIO) vs IOCP
Windows Registered I/O (RIO) vs IOCP
 
〈야생의 땅: 듀랑고〉 서버 아키텍처 Vol. 3
〈야생의 땅: 듀랑고〉 서버 아키텍처 Vol. 3〈야생의 땅: 듀랑고〉 서버 아키텍처 Vol. 3
〈야생의 땅: 듀랑고〉 서버 아키텍처 Vol. 3
 
Async Messaging in CQRS: Part 1 - Masstransit + DDD Intro
Async Messaging in CQRS: Part 1 - Masstransit + DDD IntroAsync Messaging in CQRS: Part 1 - Masstransit + DDD Intro
Async Messaging in CQRS: Part 1 - Masstransit + DDD Intro
 
홍성우, 게임 서버의 목차 - 시작부터 출시까지, NDC2019
홍성우, 게임 서버의 목차 - 시작부터 출시까지, NDC2019홍성우, 게임 서버의 목차 - 시작부터 출시까지, NDC2019
홍성우, 게임 서버의 목차 - 시작부터 출시까지, NDC2019
 
왕초보를 위한 도커 사용법
왕초보를 위한 도커 사용법왕초보를 위한 도커 사용법
왕초보를 위한 도커 사용법
 
NDC12_Lockless게임서버설계와구현
NDC12_Lockless게임서버설계와구현NDC12_Lockless게임서버설계와구현
NDC12_Lockless게임서버설계와구현
 
Python 게임서버 안녕하십니까 : RPC framework 편
Python 게임서버 안녕하십니까 : RPC framework 편Python 게임서버 안녕하십니까 : RPC framework 편
Python 게임서버 안녕하십니까 : RPC framework 편
 
오딘: 발할라 라이징 MMORPG의 성능 최적화 사례 공유 [카카오게임즈 - 레벨 300] - 발표자: 김문권, 팀장, 라이온하트 스튜디오...
오딘: 발할라 라이징 MMORPG의 성능 최적화 사례 공유 [카카오게임즈 - 레벨 300] - 발표자: 김문권, 팀장, 라이온하트 스튜디오...오딘: 발할라 라이징 MMORPG의 성능 최적화 사례 공유 [카카오게임즈 - 레벨 300] - 발표자: 김문권, 팀장, 라이온하트 스튜디오...
오딘: 발할라 라이징 MMORPG의 성능 최적화 사례 공유 [카카오게임즈 - 레벨 300] - 발표자: 김문권, 팀장, 라이온하트 스튜디오...
 
PyQGIS 개발자 쿡북(PyQGIS Developer Cookbook) 한국어 판
PyQGIS 개발자 쿡북(PyQGIS Developer Cookbook) 한국어 판 PyQGIS 개발자 쿡북(PyQGIS Developer Cookbook) 한국어 판
PyQGIS 개발자 쿡북(PyQGIS Developer Cookbook) 한국어 판
 
Introduction to CI/CD
Introduction to CI/CDIntroduction to CI/CD
Introduction to CI/CD
 
[야생의 땅: 듀랑고] 서버 아키텍처 Vol. 2 (자막)
[야생의 땅: 듀랑고] 서버 아키텍처 Vol. 2 (자막)[야생의 땅: 듀랑고] 서버 아키텍처 Vol. 2 (자막)
[야생의 땅: 듀랑고] 서버 아키텍처 Vol. 2 (자막)
 
Building .NET Microservices
Building .NET MicroservicesBuilding .NET Microservices
Building .NET Microservices
 
CI-CD WITH GITLAB WORKFLOW
CI-CD WITH GITLAB WORKFLOWCI-CD WITH GITLAB WORKFLOW
CI-CD WITH GITLAB WORKFLOW
 
[야생의 땅: 듀랑고] 서버 아키텍처 - SPOF 없는 분산 MMORPG 서버
[야생의 땅: 듀랑고] 서버 아키텍처 - SPOF 없는 분산 MMORPG 서버[야생의 땅: 듀랑고] 서버 아키텍처 - SPOF 없는 분산 MMORPG 서버
[야생의 땅: 듀랑고] 서버 아키텍처 - SPOF 없는 분산 MMORPG 서버
 
containerd the universal container runtime
containerd the universal container runtimecontainerd the universal container runtime
containerd the universal container runtime
 
Introduction to docker
Introduction to dockerIntroduction to docker
Introduction to docker
 
GitLab Auto DevOps 大解析—CI/CD 原來可以這樣做
GitLab Auto DevOps 大解析—CI/CD 原來可以這樣做GitLab Auto DevOps 大解析—CI/CD 原來可以這樣做
GitLab Auto DevOps 大解析—CI/CD 原來可以這樣做
 
잘 알려지지 않은 숨은 진주, Winsock API - WSAPoll, Fast Loopback
잘 알려지지 않은 숨은 진주, Winsock API - WSAPoll, Fast Loopback잘 알려지지 않은 숨은 진주, Winsock API - WSAPoll, Fast Loopback
잘 알려지지 않은 숨은 진주, Winsock API - WSAPoll, Fast Loopback
 
Presentation-DEVOPS-par-GDG
Presentation-DEVOPS-par-GDGPresentation-DEVOPS-par-GDG
Presentation-DEVOPS-par-GDG
 
[KGC 2012]Boost.asio를 이용한 네트웍 프로그래밍
[KGC 2012]Boost.asio를 이용한 네트웍 프로그래밍[KGC 2012]Boost.asio를 이용한 네트웍 프로그래밍
[KGC 2012]Boost.asio를 이용한 네트웍 프로그래밍
 

Ähnlich wie Message Oriented Architecture using NServiceBus

How to build more reliable, robust and scalable distributed systems
How to build more reliable, robust and scalable distributed systemsHow to build more reliable, robust and scalable distributed systems
How to build more reliable, robust and scalable distributed systemsLars-Erik Kindblad
 
Making communication across boundaries simple with Azure Service Bus
Making communication across boundaries simple with Azure Service BusMaking communication across boundaries simple with Azure Service Bus
Making communication across boundaries simple with Azure Service BusParticular Software
 
24. Advanced Transaction Processing in DBMS
24. Advanced Transaction Processing in DBMS24. Advanced Transaction Processing in DBMS
24. Advanced Transaction Processing in DBMSkoolkampus
 
High volume real time contiguous etl and audit
High volume real time contiguous etl and auditHigh volume real time contiguous etl and audit
High volume real time contiguous etl and auditRemus Rusanu
 
8 application servers_v2
8 application servers_v28 application servers_v2
8 application servers_v2ashish61_scs
 
CS9222 ADVANCED OPERATING SYSTEMS
CS9222 ADVANCED OPERATING SYSTEMSCS9222 ADVANCED OPERATING SYSTEMS
CS9222 ADVANCED OPERATING SYSTEMSKathirvel Ayyaswamy
 
Mq presentation
Mq presentationMq presentation
Mq presentationxddu
 
Event Driven Architectures - Phoenix Java Users Group 2013
Event Driven Architectures - Phoenix Java Users Group 2013Event Driven Architectures - Phoenix Java Users Group 2013
Event Driven Architectures - Phoenix Java Users Group 2013clairvoyantllc
 
Event Driven Architectures
Event Driven ArchitecturesEvent Driven Architectures
Event Driven ArchitecturesAvinash Ramineni
 
Moving "Something Simple" To The Cloud - What It Really Takes
Moving "Something Simple" To The Cloud - What It Really TakesMoving "Something Simple" To The Cloud - What It Really Takes
Moving "Something Simple" To The Cloud - What It Really TakesCloverDX
 
10135 a 05
10135 a 0510135 a 05
10135 a 05Bố Su
 
540slidesofnodejsbackendhopeitworkforu.pdf
540slidesofnodejsbackendhopeitworkforu.pdf540slidesofnodejsbackendhopeitworkforu.pdf
540slidesofnodejsbackendhopeitworkforu.pdfhamzadamani7
 
Mail server using Linux(Ubuntu)
Mail server using Linux(Ubuntu)Mail server using Linux(Ubuntu)
Mail server using Linux(Ubuntu)Navjot Navi
 
The Art of Message Queues
The Art of Message QueuesThe Art of Message Queues
The Art of Message QueuesMike Willbanks
 
Realtime Communication Techniques with PHP
Realtime Communication Techniques with PHPRealtime Communication Techniques with PHP
Realtime Communication Techniques with PHPWaterSpout
 

Ähnlich wie Message Oriented Architecture using NServiceBus (20)

How to build more reliable, robust and scalable distributed systems
How to build more reliable, robust and scalable distributed systemsHow to build more reliable, robust and scalable distributed systems
How to build more reliable, robust and scalable distributed systems
 
Making communication across boundaries simple with Azure Service Bus
Making communication across boundaries simple with Azure Service BusMaking communication across boundaries simple with Azure Service Bus
Making communication across boundaries simple with Azure Service Bus
 
24. Advanced Transaction Processing in DBMS
24. Advanced Transaction Processing in DBMS24. Advanced Transaction Processing in DBMS
24. Advanced Transaction Processing in DBMS
 
Csc concepts
Csc conceptsCsc concepts
Csc concepts
 
High volume real time contiguous etl and audit
High volume real time contiguous etl and auditHigh volume real time contiguous etl and audit
High volume real time contiguous etl and audit
 
Rpc
RpcRpc
Rpc
 
8 application servers_v2
8 application servers_v28 application servers_v2
8 application servers_v2
 
CS9222 ADVANCED OPERATING SYSTEMS
CS9222 ADVANCED OPERATING SYSTEMSCS9222 ADVANCED OPERATING SYSTEMS
CS9222 ADVANCED OPERATING SYSTEMS
 
Mq presentation
Mq presentationMq presentation
Mq presentation
 
Event Driven Architectures - Phoenix Java Users Group 2013
Event Driven Architectures - Phoenix Java Users Group 2013Event Driven Architectures - Phoenix Java Users Group 2013
Event Driven Architectures - Phoenix Java Users Group 2013
 
Event Driven Architectures
Event Driven ArchitecturesEvent Driven Architectures
Event Driven Architectures
 
Moving "Something Simple" To The Cloud - What It Really Takes
Moving "Something Simple" To The Cloud - What It Really TakesMoving "Something Simple" To The Cloud - What It Really Takes
Moving "Something Simple" To The Cloud - What It Really Takes
 
MQPresentation.ppt
MQPresentation.pptMQPresentation.ppt
MQPresentation.ppt
 
10135 a 05
10135 a 0510135 a 05
10135 a 05
 
540slidesofnodejsbackendhopeitworkforu.pdf
540slidesofnodejsbackendhopeitworkforu.pdf540slidesofnodejsbackendhopeitworkforu.pdf
540slidesofnodejsbackendhopeitworkforu.pdf
 
Rpc
RpcRpc
Rpc
 
Mail server using Linux(Ubuntu)
Mail server using Linux(Ubuntu)Mail server using Linux(Ubuntu)
Mail server using Linux(Ubuntu)
 
The Art of Message Queues
The Art of Message QueuesThe Art of Message Queues
The Art of Message Queues
 
message passing
 message passing message passing
message passing
 
Realtime Communication Techniques with PHP
Realtime Communication Techniques with PHPRealtime Communication Techniques with PHP
Realtime Communication Techniques with PHP
 

Mehr von Lars-Erik Kindblad

Application Architecture April 2014
Application Architecture April 2014Application Architecture April 2014
Application Architecture April 2014Lars-Erik Kindblad
 
Unit Tests are Overrated (NDCOslo 2013)
Unit Tests are Overrated (NDCOslo 2013)Unit Tests are Overrated (NDCOslo 2013)
Unit Tests are Overrated (NDCOslo 2013)Lars-Erik Kindblad
 
Publish & Subscribe to events using an Event Aggregator
Publish & Subscribe to events using an Event AggregatorPublish & Subscribe to events using an Event Aggregator
Publish & Subscribe to events using an Event AggregatorLars-Erik Kindblad
 
The Single Responsibility Principle
The Single Responsibility PrincipleThe Single Responsibility Principle
The Single Responsibility PrincipleLars-Erik Kindblad
 
Avoid code duplication! Principles & Patterns
Avoid code duplication! Principles & PatternsAvoid code duplication! Principles & Patterns
Avoid code duplication! Principles & PatternsLars-Erik Kindblad
 
Application Architecture by Lars-Erik Kindblad, Capgemini
Application Architecture by Lars-Erik Kindblad, CapgeminiApplication Architecture by Lars-Erik Kindblad, Capgemini
Application Architecture by Lars-Erik Kindblad, CapgeminiLars-Erik Kindblad
 
Inversion of Control - Introduction and Best Practice
Inversion of Control - Introduction and Best PracticeInversion of Control - Introduction and Best Practice
Inversion of Control - Introduction and Best PracticeLars-Erik Kindblad
 
Introduction to FluentData - The Micro ORM
Introduction to FluentData - The Micro ORMIntroduction to FluentData - The Micro ORM
Introduction to FluentData - The Micro ORMLars-Erik Kindblad
 
Dependency Injection vs Service Locator - Best Practice
Dependency Injection vs Service Locator - Best PracticeDependency Injection vs Service Locator - Best Practice
Dependency Injection vs Service Locator - Best PracticeLars-Erik Kindblad
 

Mehr von Lars-Erik Kindblad (13)

Application Architecture April 2014
Application Architecture April 2014Application Architecture April 2014
Application Architecture April 2014
 
Application Architecture
Application ArchitectureApplication Architecture
Application Architecture
 
Unit Tests are Overrated (NDCOslo 2013)
Unit Tests are Overrated (NDCOslo 2013)Unit Tests are Overrated (NDCOslo 2013)
Unit Tests are Overrated (NDCOslo 2013)
 
Publish & Subscribe to events using an Event Aggregator
Publish & Subscribe to events using an Event AggregatorPublish & Subscribe to events using an Event Aggregator
Publish & Subscribe to events using an Event Aggregator
 
The Single Responsibility Principle
The Single Responsibility PrincipleThe Single Responsibility Principle
The Single Responsibility Principle
 
Avoid code duplication! Principles & Patterns
Avoid code duplication! Principles & PatternsAvoid code duplication! Principles & Patterns
Avoid code duplication! Principles & Patterns
 
Application Architecture by Lars-Erik Kindblad, Capgemini
Application Architecture by Lars-Erik Kindblad, CapgeminiApplication Architecture by Lars-Erik Kindblad, Capgemini
Application Architecture by Lars-Erik Kindblad, Capgemini
 
The Fluent Interface Pattern
The Fluent Interface PatternThe Fluent Interface Pattern
The Fluent Interface Pattern
 
Inversion of Control - Introduction and Best Practice
Inversion of Control - Introduction and Best PracticeInversion of Control - Introduction and Best Practice
Inversion of Control - Introduction and Best Practice
 
Layered Software Architecture
Layered Software ArchitectureLayered Software Architecture
Layered Software Architecture
 
Introduction to FluentData - The Micro ORM
Introduction to FluentData - The Micro ORMIntroduction to FluentData - The Micro ORM
Introduction to FluentData - The Micro ORM
 
Dependency Injection vs Service Locator - Best Practice
Dependency Injection vs Service Locator - Best PracticeDependency Injection vs Service Locator - Best Practice
Dependency Injection vs Service Locator - Best Practice
 
Data Access - Best Practice
Data Access - Best PracticeData Access - Best Practice
Data Access - Best Practice
 

Kürzlich hochgeladen

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 

Kürzlich hochgeladen (20)

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 

Message Oriented Architecture using NServiceBus

  • 1. Message Oriented Architecture using NServiceBus Lars-Erik Kindblad Senior Consultant Twitter: @kindblad E-mail: le@kindblad.com
  • 2. What is NServiceBus?  Lightweight messaging framework for designing distributed systems in .NET  An Enterprise Service Bus  Helps to make distributed systems more  Reliable  Scalable  Extensible  Founded in 2006 by UDI Dahan, an international renowned expert on software architecture and design  Services and support provided by Particular Software  Open Source but not free  Source code available at https://github.com/Particular/NServiceBus  Binaries available as NuGet packages
  • 3.  Remote Procedure Call (Request/Response) Request: Order Website Response: Order ID Order Service PlaceOrder(order) Synchronous – The website is blocked while calling the Order Service  Messaged Oriented Architecture (One-way messaging) Message: PlaceOrder Website Message Queue Connect Receive PlaceOrder message Windows Service Process the PlaceOrder message Asynchronous – The website is NOT blocked while the place order is processed
  • 4. NServiceBus Terminology & Components Sender /Send-Only Endpoint Message, Command or Event Message: PlaceOrder Website Message Queue Connect Receive PlaceOrder message Windows Service Process the PlaceOrder message Message Handler Worker Endpoint, Host or Receiver
  • 5. Messages in NServiceBus  Just simple .NET classes with properties  2 types of messages  Command • Do something: PlaceOrder • Sent from one or more senders, processed by one endpoint • Implement ICommand  Event • Something has happened: OrderWasPlaced • Published from one sender, processed by several endpoints/subscribers • Implement IEvent
  • 6.  Command Strongly coupled to the receiving endpoint Command: PlaceOrder Website Command: PlaceOrder Message Queue Endpoint  Event (Publish/Subscribe) Subscribing Endpoint Loosely coupled to the receiving endpoints Event: OrderWasPlaced Worker NServiceBus will add 3 messages to the queue, one for each subscriber Event: OrderWasPlaced Message Queue Subscribing Endpoint Subscribing Endpoint
  • 7. Queues  First in-first out datastructure for storing and retrieving messages PlaceOrder message 4 First-In Queue PlaceOrder message 3 PlaceOrder message 2 PlaceOrder message 1 First-Out  Default queue in NServiceBus is Microsoft Message Queuing (MSMQ)  Available on all Windows editions since NT 4 and Windows 95  Also support for      ActiveMQ RabbitMQ SQL Server Windows Azure Queues Windows Azure ServiceBus  One endpoint has one queue, Send-Only endspoints do not have queues
  • 8. How to Send & Handle Messages Command: PlaceOrder Website Send Command: PlaceOrder Message Queue Endpoint Handle (Command or Event)
  • 9. Summary  Sender endpoint  Sends a message  Can be a website, desktop application or even a worker endpoint  Command  Message where we want to do something concrete  ICommand interface  Event  Message where we notify about something that has happened  IEvent interface  Message handler  Processes a given message  IHandleMessages<Message> interface  Worker endpoint  Finds and executes the handler for the message  Can be a Windows Service, console application, WCF, website
  • 10. NServiceBus Requirements  MSMQ (default) or any other supported queue      ActiveMQ RabbitMQ SQL Server Windows Azure Queues Windows Azure ServiceBus  Distributed Transaction Cordinator (DTC)  RavenDB (default) or a relational database  Download the installer from NServiceBus from http://particular.net/ to setup the required components
  • 11. NuGet packages  NServiceBus Interfaces  Interfaces that we need to define messages  NServiceBus  Most of the code that drives NServiceBus except hosting  Use for send-only endpoints  NServiceBus Host  Console application that can be installed as a Windows Service  Used to process/handle messages  NServiceBus Testing  Framework for testing NServiceBus endpoints
  • 12. The Case – The eBook Shop 1. Create the order in the database 2. Auto-pay the order using a previously stored credit card 3. Send an order confirmation by e-mail 4. Deliver the book to the Windows Phone device
  • 13. The Usual Way - Request/Response Place order 1. Add order Browser OrderController Confirmation page with order ID Database 2. Charge credit card PaymentService 3. Send mail SMTP-Server 4. Deliver ebook DeviceService
  • 14. Problem #1 – The order is lost during an error Place order 1. Add order Browser OrderController Database Error page Exception 2. Charge credit card Error PaymentService 3. Send mail SMTP-Server 4. Deliver ebook DeviceService  Conseqence  The database is rolled back  User receives an error  Order must be sent again
  • 15. Problem #2 – Poor Transaction Management Place order 1. Add order Browser OrderController Database Error page 2. Charge credit card PaymentService 3. Send mail SMTP-Server Exception 4. Deliver ebook Error DeviceService  Conseqence:  The credit card is charged (no transaction support)  A mail saying the order was successful was sent (no transaction support)  The order do not exist in the database (supports transaction – rolled back)  The user will receive an error
  • 16. Let’s Fix This With NServiceBus  Change to a message oriented architecture Add PlaceOrder message Place order Browser Queue OrderController Confirmation page Old New
  • 17. The Message Must Also Be Processed MSMQ 1. Connect 2. Receive message PlaceOrder Worker (Windows Service) PlaceOrder 3. PlaceOrder Message Handler 4. Add order Database 5. Charge credit card PaymentService 6. Send mail SMTP-Server 7. Deliver ebook DeviceService
  • 18. When an Error Occur 4. Rollback. Put the message back on the queue and retry MSMQ 1. Connect 2. Receive message PlaceOrder Worker (Windows Service) PlaceOrder 3. PlaceOrder Error Message Handler
  • 19. More about MSMQ  Max 4MB message size  Persists messages to files. Default location C:WINDOWSSystem32msmqstorage  Guranteed once-and-only-once delivery  The message will only be handled once  Supports transactional queues and distributed transactions (using DTC)  If an error occur both changes to the database and to the queue can be rolled back  Supports store-and-forward  MSMQ will store messages locally before forwarding it to the queue on another server  MSMQ will confirm when the message has been written to the disk in the outgoing queue, not when it has been delivered to the server  Important feature for distributed systems with unrealiable networks Server A Sender MSMQ Outgoing queue Server B Message MSMQ Input queue Worker
  • 20. How NServiceBus Handles Failure  Transaction management and rollback are automatically handled by NServiceBus in message handlers  When an error occur 1. The message is put back on the queue 2. Attempt to process the message 5 times, wait for 10 seconds 3. Attempt to process the message 5 times, wait for 20 seconds 4. Attempt to process the message 5 times, wait for 30 seconds 5. Attempt to process the message 5 times, send the message to an error queue The time interval is configurable  Messages can be moved back to the original queue for replay using ReturnToSourceQueue.exe tool.
  • 21. A challenge – The Order ID Old New  The order ID has not been generated yet – it’s generated in the message handler at the worker endpoint
  • 22. Possible Solutions  Change the UI to not show the order ID  Use a GUID (Guid.NewGuid()) or find another way to generate the ID in the controller instead of in the database • message.OrderId = Guid.NewGuid(); Bus.Send(message); ViewBag.OrderId = message.OrderId; • 5773DD0E-0AB0-446B-8649-B2D43D7DA4AA is not a very user friendly ID  Move OrderRepository.Add(order) to the Controller: • var orderId = OrderRepository.Add(order); Bus.Send(message); • Less reliable and more error prone solution  Use Send/Reply to simulate request/response • The controller will wait for a PlaceOrderCompleted message that the PlaceOrder handler will send • Too much overhead • An Anti-pattern
  • 23. We Still Have Poor Transaction Management 1. Add order Handle PlaceOrder Database 2. Charge credit card PaymentService 3. Send mail SMTP-Server 4. Deliver ebook Exception Error DeviceService  Conseqence:  The credit card is charged (no transaction support)  A mail saying the order was successful was sent (no transaction support)  The order do not exist in the database (supports transaction – rolledback)  The message is put back on the queue and will be retried = credit card will be charged twice
  • 24. Solution  Split into man small messages - One message per transactional boundary Place order OrderController Browser 1. Add PlaceOrder message PlaceOrder message 2. Handle PlaceOrder Add PayOrder message If an error occur only DeliverEbook is affected. The message will be put back on the queue and retried SendMail message 4. Handle SendMail DeliverEbook message 5. Handle DeliverEbook PayOrder message Queue Add SendMail & DeliverEbook message 3. Handle PayOrder
  • 25. Code - The Messages
  • 26. Code – Message Handlers #1
  • 27. Code – Message Handlers #2
  • 28. Yet another transactional issue MSMQ Processed ok Handle PayOrder message Charge credit card Request Handle PayOrder Request: Error PaymentService Payment OK Network error External Payment Gateway  The message will be put back on the queue and processed again  Consequence: The credit card will be charged more than once
  • 29. Solution  Operations should be idempotent  Idempotent operations can be run many times without changing the result MSMQ Handle PayOrder message Charge credit card Request Handle PayOrder Payment OK PaymentService Has the order been paid? Use OrderId Database Payment OK External Payment Gateway Has the message already been processed? Use order ID or a GUID Database
  • 30. Another Challenge – Eventual Consistency Users clicks on View Order History  The PlaceOrder message is processed async by another process  If it has not gotten processed yet then the new order will not be in the list  Solution  Some UI trick?  Move the OrderRepository.Add to the controller  Make sure the messages are handled fast enough • Define a SLA on the message to help monitoring
  • 31. Publish/Subscribe Place order OrderController Browser 1. Add PlaceOrder message PlaceOrder command 2. Handle PlaceOrder Add PayOrder command With Publish/Subscribe the Subscribers are loosely coupled to the publisher Subscribe to OrderWasPaid 4. Handle SendMail Subscribes to OrderWasPaid Queue 3. Handle PayOrder Add OrderWasPaid event OrderWasPaid Subscribes to OrderWasPaid 4. Handle DeliverEbook PayOrder command OrderWasPaid NServiceBus will add 2 messages to the queue, one for each subscriber
  • 32. Summary Request/Response Command Event X X Automatically retries X X Full transactional support X X Reliable Consistency X Can return ID generated by the database to the sender X Loosely coupled X  Reads is synchronous and should be request/response  Using a queue for synchronous operations gives little value, only overhead  Writes can be asynchronous one-way messaging or events  There are no silver bullet – We are always trading one set of problems for another set of problems
  • 33. Scaling  Scale up  Easy to scale up if business processes are split into multiple messages  Increase the number of threads in the config – NServiceBus will concurrently process multiple messages  Scale out  Use the Distributor or the Master (Both a distributor and a worker) Worker #1 Message MSMQ Distributor Worker #2 Worker #3  The distributor will forward the message to a worker that is ready to process the message
  • 34. How the Distributor Works 1. Worker: Send I’m ready message I’m ready message Distributor: Control Queue I’m ready message Distributor Any messages in the input queue? PlaceOrder message 1. Website: Send PlaceOrder message Send PlaceOrder message from input queue PlaceOrder message Distributor Input Queue Yes No PlaceOrder message Distributor Find worker through message in Storage queue Store I’m ready message Storage Queue
  • 35. Other Features  Sagas  Long running workflow like processes  State is shared between multiple message handlers  Scheduling  Send a message every x minute. A handler will handle the message  Unobtrusive mode  Use your own ICommand, IEvent etc. to reduce coupling to NServiceBus  Supports  Various dependency injection containers  Logging frameworks  Etc.  ++
  • 36. NServiceBus License & Pricing  Open source  Used to be free, but not anymore  Indefinitely license with 1 year maintenance updates • $500 USD per processing core • $250 USD per sending core  Monthly subscription • $35 USD per processing core • $17 USD per sending core  Development and testing environments + disaster recovery and passive backups are free  Developers need a license (free) that must be renewed every 3rd month through particular.net
  • 37. Demo
  • 38. About Capgemini With more than 120,000 people in 40 countries, Capgemini is one of the world's foremost providers of consulting, technology and outsourcing services. The Group reported 2011 global revenues of EUR 9.7 billion. Together with its clients, Capgemini creates and delivers business and technology solutions that fit their needs and drive the results they want. A deeply multicultural organization, Capgemini has developed its own way of working, the Collaborative Business ExperienceTM, and draws on Rightshore ®, its worldwide delivery model. Rightshore® is a trademark belonging to Capgemini www.capgemini.com The information contained in this presentation is proprietary. © 2012 Capgemini. All rights reserved.