34
NServiceBus introduction to a message based distributed architecture

NServiceBus - introduction to a message based distributed architecture

Embed Size (px)

DESCRIPTION

NServiceBus - introduction to a message based distributed architecture

Citation preview

Page 1: NServiceBus - introduction to a message based distributed architecture

NServiceBusintroduction to a message based distributed

architecture

Page 2: NServiceBus - introduction to a message based distributed architecture

Mauro ServientiCTO @ Mastreeno, LTD

[email protected]@mauroservienti

//milestone.topics.it//github.com/mauroservienti

NServiceBus trainer/supportRavenDB trainer

Microsoft MVP – Visual C#

Page 3: NServiceBus - introduction to a message based distributed architecture

Resources

• Slides on SlideShare://slideshare.net/mauroservienti

• Samples on GitHub://github.com/mauroservienti

– Repository: Conferences– Branch: 2014/DDDEastAnglia

Page 4: NServiceBus - introduction to a message based distributed architecture

SO…YOU’D LIKE TO SCALE…

Page 5: NServiceBus - introduction to a message based distributed architecture

Scale out and HA

• Basically means: horizontal scale & distribute;• It can also mean:

– Async;– Non coupled:

• we need to escape from the monolith;

– Non temporally coupled:• Where makes sense we should not rely on two portion

of the system to be live at the same time to work as expected

Page 6: NServiceBus - introduction to a message based distributed architecture

Distribution & RPC #1

Given that we have occasionally connected moving parts:

Web Frontend ShippingWCF Service

Standard RPC call to WCF

If the «Shipping» service fails, because of an hardware failure:• We can lose information;• We need to handle failures and retries manually;

Page 7: NServiceBus - introduction to a message based distributed architecture

Distribution & RPC #2

Given that we have network with medium to high latency:

If the connection with the «Shipping» service fails, is slow or timeouts:• We can lose information;• We slow down the whole system;

Web Frontend ShippingWCF Service

Standard RPC call to WCF

Page 8: NServiceBus - introduction to a message based distributed architecture

Messages

A message can be shipped to a remote service via a queue

Web Frontend #1 Shipping Endpoint #1

queueWeb Frontend #2 Shipping Endpoint #2

com

petin

g co

nsum

ers

• If one endpoint fails messages will be picked by the other(s);• On slow connections or slow endpoints the entire system is not slowed;

Page 9: NServiceBus - introduction to a message based distributed architecture

issues and hiccups

• “Distributed”:– We cannot rely on subsequent calls;– We cannot rely on ordering to drive the system;– We cannot rely on transactions;

• “Async”:– Async errors;– Auditing;– Retries;– Timeouts;

Page 10: NServiceBus - introduction to a message based distributed architecture

NServiceBusPlease welcome:

Page 11: NServiceBus - introduction to a message based distributed architecture

NServiceBus?

• A toolkit:– reduce the friction of async and distributed

development based on messages on top of queues;

– Transport independent;– And much more, much :-)

• A Platform:– A set of tools to designs, administer and monitor

production solutions;

Page 12: NServiceBus - introduction to a message based distributed architecture

Concepts

• Message– An atomic piece of information that has a semantic meaning in

the business;• Component

– Something that can handle a message;• Service

– A set of components grouped by context;• Endpoint

– A set of services grouped by:• SLA(s);• Infrastructure concerns;• Etc..;

Page 13: NServiceBus - introduction to a message based distributed architecture

DEMO

Page 14: NServiceBus - introduction to a message based distributed architecture

Recap

• Request/reply pattern;• “Easy peasy” setup with the "Host";• “Unubotrusive” mode:

– reduce versioning problems across endpoints;• Receiver doesn’t know anything of the sender:

– no coupling in the configuration;• The sender is configured to send messages

– Based on mappings in the configuration;

Page 15: NServiceBus - introduction to a message based distributed architecture

Concepts #2

• Command: A message that semantically identifies something to be done (imperative):– "CreateNewUser";

• Event: A message that semantically identifies something happened and immutable (past):– "NewUserCreated";

• Subscription: The notion that an endpoint is interested in an event;

Page 16: NServiceBus - introduction to a message based distributed architecture

DEMO

Page 17: NServiceBus - introduction to a message based distributed architecture

Recap

• Event based pattern;• Events are “copied” per subscriber;• Subscriptions defined in the mappings config:• Subscriptions stored in RavenDB, by default:

– Other options supported out-of-the-box• Events are, can be, .net interfaces:

– Evolution and versioning is easier: interfaces can be composed;

Page 18: NServiceBus - introduction to a message based distributed architecture

TRANSPORT

Page 19: NServiceBus - introduction to a message based distributed architecture

Transports?

• The “wire” that connects endpoints;• Supported transports (OOTB):

• MSMQ;• RabbitMQ;• Sql Server;• Azure ServiceBus & Table Queues;

• Transport independent;• In v4.x both parties must agree on the serialization format;

Page 20: NServiceBus - introduction to a message based distributed architecture

under the hood

• At the Sender (or publisher):– Message serialization;– Push on the transport;

• At the receiver– Message deserialization;– Component (Handler) lookup & invoke;

• Wrapped in a “transaction”:– v4 requires the DTC, v5 won’t :-);

• I hate the DTC :-P

Page 21: NServiceBus - introduction to a message based distributed architecture

HANDLING FAILURESShit happens…

Page 22: NServiceBus - introduction to a message based distributed architecture

Async failures

• First Level Retries:– Message handling is wrapped in a transaction;– By default is retried in sequence for 5 times;

• Second Level Retries:– If FLR fails all the attempts, SLR kicks in;– 5s delay -> FLR, 10s delay -> FLR, 15s delay -> FLR;

• Do not rely on hope: there is the error queue;

Page 23: NServiceBus - introduction to a message based distributed architecture

What are retries for and not for?

• Transient failures:– E.g. Mail server down or third party services not

available;• Bugs:

– We deployed a bug, I did :-), and we do not lose anything

• Not there to handle business exceptions:– they can, obviously, but their main target are the

DevOps guys;

Page 24: NServiceBus - introduction to a message based distributed architecture

DEMO

Page 25: NServiceBus - introduction to a message based distributed architecture

Recap

• “Easy peasy”;• Do not wrap your code in a try/catch block;• Keep your handlers small and idempotent:

– They can be retried;– The more complex they are the harder will be to

be idempotent;• The Particular Platform:

– Tool chain to handle failures;

Page 26: NServiceBus - introduction to a message based distributed architecture

SCALE-OUTDidn’t we start speaking about scalability?

Page 27: NServiceBus - introduction to a message based distributed architecture

DEMO

Page 28: NServiceBus - introduction to a message based distributed architecture

Recap

• Messages are an atomic piece of information:– They can be handled by one of the endpoints;

• Multiple endpoints means:– More power to the business;– Much easier to be highly available;– Upgrade without shutting down the “system”;

• If we have a storage:– We need to scale it out too;– Will it be complex? Can be, but we can;

Page 29: NServiceBus - introduction to a message based distributed architecture

SAGALong running processes

Page 30: NServiceBus - introduction to a message based distributed architecture

Long and complex

Check-out

Order ItemsPick up

Ensure ItemsIn Stock

Bill customer credit card

3rd partybilling service

Setup Order cancellation

Timeout

Start item collection

Order ready to be

shipped

Customer credit card

billed

cancellation Timeout Elapsed

OrderCancelled? yes

No

Cancel & CompleteThe order

Ship & CompleteThe order

Use

r R

eque

st

Page 31: NServiceBus - introduction to a message based distributed architecture

DEMO

Page 32: NServiceBus - introduction to a message based distributed architecture

Recap

• A long running process with a state;• Can be scaled-out;• Are concurrency safe;• Stored by default on RavenDB:

– Other options supported out-of-the-box;• Timeouts are the way to go to handle the

decision process based on a missing “answer”;

Page 33: NServiceBus - introduction to a message based distributed architecture

Mind…

• Keep you handling code as small as possible;• If an handler does more than one single action

it is a smell:– Exactly like a too long method;– Move to a Saga if you need a “shared” state;

• The more an handler is simple the more it will be easy to make it idempotent;

Page 34: NServiceBus - introduction to a message based distributed architecture

THANK YOU, WE ARE ALL SET :-)Q&A