76

Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Embed Size (px)

Citation preview

Page 1: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374
Page 2: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Advanced Messaging Scenarios with Azure Service Bus MessagingDan Rosanova M374

Page 3: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Azure Service Bus Messaging is the most sophisticated cloud messaging platform in the world

Page 4: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

What is “Messaging”

It’s not this…But it’s not all that different

Page 5: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Messaging is the decoupling of systems and services via asynchronous communication

Page 6: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Why would anyone use messaging?Increase availabilityProtect components from each other and outside forcesProvide elasticityOvercome temporal couplingLimit the scope and impact of change propagationSimplify programming models

Page 7: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

What are common tasks people use messaging for?Order processingWorkflowIntegrationAsynchronous programmingEventual consistency

Page 8: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

A brief history of messaging

Teknekron Informatio

n Bus

MQ Series

MSMQ

TIBCO

BizTalk

RabbitMQ

ServiceBus

Financial Services

LogisticsUtilities & Telecom

Insurance Healthcare Government

1980s 2015

Page 9: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Where is messaging used

Inter-Bank transfersOrder fulfillment & tradingSettlementPricing updates and quotes

Financial Services

LogisticsUtilities & Telecom

Insurance Healthcare Government

Page 10: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Where is messaging used

Customer information systemsBilling integrationSmart meter & gridCommand and control

Financial Services

LogisticsUtilities & Telecom

Insurance Healthcare Government

Page 11: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Where is messaging used

Order processingDelivery notificationsInventory managementRouting

Financial Services

LogisticsUtilities & Telecom

Insurance Healthcare Government

Page 12: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Where is messaging used

EnrollmentClaims processingRisk modelingRegulatory filing

Financial Services

LogisticsUtilities & Telecom

Insurance Healthcare Government

Page 13: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Where is messaging used

EnrollmentClaims processingRisk modelingRegulatory filing

Financial Services

LogisticsUtilities & Telecom

Insurance Healthcare Government

Page 14: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Where is messaging used

Medial recordsCare quality metricsPatient monitoring & diagnostics

Financial Services

LogisticsUtilities & Telecom

Insurance Healthcare Government

Page 15: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Where is messaging used

You really don’t want to knowHow do you think the NSA collects all that data?

Financial Services

LogisticsUtilities & Telecom

Insurance Healthcare Government

Page 16: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Two runtime models have emerged

Google Cloud Pub/Sub

Amazon SQS/SWF

PubNub

IronMQ

CloudAMQP

RackspaceMicrosoft

Dedicated Capacity

Multitenant Platform

Page 17: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Two pricing models have emerged

Google Cloud Pub/Sub

Amazon SQS/SWF

PubNub

IronMQ

CloudAMQP

RackspaceMicrosoft

Dedicated Capacity

Multitenant Platform

Pay p

er

use

Fixe

d F

ee

Microsoft

Page 18: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

1.5 Trillion

Messages delivered every month with Event Hubs

>250 Billion

Message operations onAzure Service Bus

Messaging (Standard) per month

1.5 MillionMessage Queues and Topics in Production

>10 PBMonthly Data Volume

85Microsoft’s Cloud

backbone is connected to over 85

Global Internet Exchanges, enabling

high speed, optimized

connectivity to the world.

4 timesMicrosoft’s North

America optical fiber network, can wrap around the earth 4

times

Azure Messaging

>30,000Daily Active Service Bus

Namespaces

>250 TBDaily Data Volume

Page 19: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Common messaging patternsLoad Leveling

Load Balancing

Filtering

Partitioning

Scheduled Delivery

Fan Out

Routing

Claim Checks

Workflow

Partitioning

Taps

Logging

Dead Letter

Sequence

Correlation

Expiration

Page 20: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Features of Service Bus MessagingScheduled delivery

Poison message handling

ForwardTo

Defer

Sessions

Batching

Auto-delete on idle

OnMessage

Duplicate detection

Filters

Actions

Transactions

Page 21: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

How do we bridge this in practice?

Features => Patterns

Page 22: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Basic Queuing

Page 23: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Creating a queuevar nsm = NamespaceManager.CreateFromConnectionString(

connectionString);

if(nsm.QueueExists(basicQueue) == false) nsm.CreateQueue(basicQueue);

Page 24: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Sending to a queuevar qc = QueueClient.CreateFromConnectionString(connectionString, basicQueue);

//your codevar order = CreateOrder();

var msg = new BrokeredMessage(order);qc.Send(msg);

Page 25: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Don’t be a baby…

Crawl Walk Run

await qc.SendAsync(msg);qc.Send (msg);

Page 26: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Nothing is more permanent than a temporary solution

Page 27: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Reading from a queueThere are a few ways to do thisThere is only one really easy way

Page 28: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Reading from a queueqc.OnMessage((message) =>{ var myOrder = message.GetBody<Order>(); Console.WriteLine("Received Order {0}", myOrder.CustomerName); message.Complete();});

Page 29: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Want more optionsvar options = new OnMessageOptions() { MaxConcurrentCalls = 5, AutoComplete = true};

Page 30: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Message header dataMetadata about the message

msg.Properties.Add("myproperty", "some value"); msg.Properties.Add("total", 1255.25);

Page 31: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Ever seen this?

Submit

To avoid multiple charges only click this button once

Get with the times!

Make operationsidempotent

Page 32: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Duplicate detectionnsm.CreateQueue(new QueueDescription(“DuplicateQueue”){ RequiresDuplicateDetection = true, DuplicateDetectionHistoryTimeWindow = new

TimeSpan(0, 0, 30)});

msg.MessageId = order.Id;

Page 33: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Things that can go wrong when receivingDownstream system is unavailableData format has changed and serializer throws

var myOrder = message.GetBody<Order>();

Oops

Good idea to use a property to define type / version

Page 34: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Lifecycle of a message

Reader Complete

DeliveryCount++

DLQ

DeliveryCount > 10

Page 35: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Things to know about dead letteringDesigned to handle poisoned messagesOn by defaultDefault setting of 10 deliveries (set at queue level)YOU MUST EMPTY THE DEADLETTER… OR ELSECan be explicitly called message.DeadLetter("Bad format", "Some description");

Page 36: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

I repeatYou must empty the dead letter queue

Page 37: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Really it’s just another queueWith a special path – but please don’t hard code the path, use the client

var dlq = QueueClient.CreateFromConnectionString(

"connectionstring", QueueClient.FormatDeadLetterPath("myqueue"));

Page 38: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Temporal decouplingOne of the original goals of messaging

Sender Receiver

Page 39: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Thinking about the futureScheduled delivery

msg.ScheduledEnqueueTimeUtc = DateTime.Now.AddMinutes(20); qc.Send(msg);

Page 40: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Reflecting on your lifeSet TimeToLive before sending a messageThis will expire messages i.e. delete them

msg.TimeToLive = new TimeSpan(1, 0, 0);

Page 41: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Or set at the entity queue level (code or portal) nsm.CreateQueue(new QueueDescription(“myqueue”){ DefaultMessageTimeToLive = new TimeSpan(1, 0, 0)});

Page 42: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

What can you do with this?Limit Order

Good ‘Till Cancelled (GTC)Day Order

You can also schedule a finite window for actions to happen – like complete or roll back

Page 43: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

What if you can’t be bothered to process something right now?Defer: the feature we all use in our daily lives

deferList.Add(msg.SequenceNumber); msg.Defer();

qc.Receive(sequenceNumber);

Sometime when you’re good and ready

Page 44: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

If you lose the sequence number of a message there is no way to read it

WARNING

Page 45: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Ordered messaging & convoysSome things need to happen in order – like updatesOrdering can be difficult for asynchronous systemsJust ask any NodeJS developerOr any BizTalk developer

Page 46: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

What is so hard about order?Just process the first one first and the second one second

Sender Receiver

Page 47: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Receiver

What is so hard about order?Just process the first one first and the second one second

Sender Receiver

Page 48: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

How can you have order AND competing consumers?Use Sessions

var sessionId = Guid.NewGuid().ToString(); msg.SessionId = sessionId; qc.Send(msg); msg2.SessionId = sessionId; qc.Send(msg2);

easy

Page 49: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Receiving ordered messagesvar session = qc.AcceptMessageSession();session.OnMessage((message) =>{ var myOrder = message.GetBody<Order>(); Console.WriteLine("Received Order {0}", myOrder.CustomerName); message.Complete();}, options);//later onsession.Complete();

Page 50: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

But wait there’s more!public interface IMessageSessionHandler{ void OnCloseSession(MessageSession session); void OnMessage(MessageSession session,

BrokeredMessage message); void OnSessionLost(Exception exception);}

qc.RegisterSessionHandler(typeof(MySessionHandler));

Page 51: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Sometimes the messages aren’t enoughYou may want to keep track of what you’ve done with the messages

Stream state = session.GetState();

session.SetState(state);

Page 52: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

In sumCompeting consumers are necessary for scale, availability, and throughputStrict ordering is still possible even with competing consumersService Bus gives you all the tools you need to do this

Page 53: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

And if that didn’t sell you….Ever hear of something called Eventual Consistiencyhttps://github.com/affandar/durabletask

Page 54: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

What about bi-directional communication?A simple approach

System 1 System 2

Request Q

Reponse Q

msg.ReplyTo = "replyqueue";

Page 55: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

The request-response queue approach has some limitationsThere is no direct correlation between sender and receiverIf you want that you may need a lot of queuesOr use properties on your ownOr…

Page 56: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Use sessions!What don’t these things do?

System 1 System 2WorkQueue

Page 57: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Request-Response: A more elegant approachReqestor var sessionId = Guid.NewGuid().ToString(); msg.ReplyToSession = sessionId; qc.Send(msg); qc.AcceptMessageSession(sessionId,

new TimeSpan(0, 5, 0));

Responder msg.SessionId = inmsg.ReplyToSession; qc.Send(msg);

Page 58: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Take a deep breathRelaxWe’re just getting started!

Page 59: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

$100$1500

Publish-Subscribe

System 1

System 3

Topic

Subscription

System 2Subscription

Filter:total > 1000

Filter:total <= 1000

Page 60: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

var td = nsm.CreateTopic("topicname");var sd = new SubscriptionDescription("topicname", "bigorders");var sc = SubscriptionClient.Create("topicname", "bigorders");nsm.CreateSubscription(sd);var rd = new RuleDescription("default", new SqlFilter("total > 1000"));sc.AddRule(rd);

Page 61: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

What can you do with Topics/SubscriptionsMessage partitioning (segmenting)Fan outInitiate multiple downstream steps (overlapping rules)All the same things you can with queuesChange the metadata as the message goes moves with ActionsOne thing to know – dead lettering happens per subscription

Page 62: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Actions on Rules to set headersvar rd = new RuleDescription("default", new SqlFilter("NOT EXISTS(stage)")) { Action = new SqlRuleAction("set stage = 1") };

Page 63: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Actions best friendForwardToForwards all messages for a subscription (or queue) to another entity (topic or queue)

sd.ForwardTo = "stage1topic";

Page 64: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Let that sink in for a minute…

Page 65: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Building rich topologies with pub-subVery expressive and powerfulDon’t go too crazy

Page 66: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Forwarding deadlettersAvailable for every deadletter

sd.ForwardDeadLetteredMessagesTo = "subdeadletter";

Page 67: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Transaction with entitiesWorks with only one entity in scope

using (var scope = new System.Transactions.TransactionScope()){ qc.Send(new BrokeredMessage(order1)); qc.Send(new BrokeredMessage(order2)); scope.Complete();}

Page 68: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

AutoDeleteOnIdleGreat featureDoes what it sounds likeCan be used to clean up entities that exist for transient periods or volatile lifetimes nsm.CreateQueue(new QueueDescription(“queue”){

AutoDeleteOnIdle = new TimeSpan(24,0,0)});

Page 69: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Claim check patternYou want to send something really big

Sender

Receiver

The Cleaner(Victor)

Page 70: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Q&A

Page 71: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Key takeawaysYes – Service Bus Messaging is THE MOST SOPHISTICATED messaging platform in the cloudThere’s a lot we didn’t coverCheckout how affordable this service isGo back and review yesterday’s session about availability

Page 72: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

You decide where this ride goes!

Page 73: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Related Ignite NZ Sessions

http://blogs.msdn.com/b/servicebus/

Find me later at… Closing drinks Fri 3:00-4:30pm @DanRosanova

Page 74: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Resources

TechNet & MSDN FlashSubscribe to our fortnightly newsletter

http://aka.ms/technetnz http://aka.ms/msdnnz

http://aka.ms/ch9nz

Microsoft Virtual AcademyFree Online Learning

http://aka.ms/mva

Sessions on Demand

Page 75: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

Complete your session evaluation now and be in to win!

Page 76: Advanced Messaging Scenarios with Azure Service Bus Messaging Dan Rosanova M374

© 2015 Microsoft Corporation. All rights reserved.Microsoft, Windows and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or

other countries.MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.