Post on 24-Jun-2015
description
NServiceBus On Azure
Yves GoelevenFounder of MessageHandler.net
• Developer on NServiceBus
• Windows Azure MVP
• @YvesGoeleven
AgendaNServiceBus on Azure
• Tour d’Azure• Hosting options• Transports • Persistence • Tips & tricks || Q&A
Tour d’Azure
Why people are interested in Azure? Various reasons
• Automated, • Scalability (scale out)• Elasticity (scale in again)• Cost• Globally available
NServiceBus & Azure: Perfect matchAll this goodness comes at a cost though
• Serious learning curve• Different development paradigm• Need to understand infrastructure
• NServiceBus helps reduce that learning curve, • makes it easy to develop for Azure
What is Azure?Global network of huge data centers operated by Microsoft
Some numbersJust to illustrate how huge it is
• 13 regions• 321 IP ranges• 250.000+ customers• 2.000.000+ VM’s• 25+ trillion objects stored
200 services running on top
Storage Big data
Caching CDN
Database
Identity
Media Networking
Traffic
Messaging
Cloud ServicesWeb Sites
Connectivity
MobileVirtual Machines
ImplicationsOf such a huge network
• Latency is normal• Machine failure is normal• Network partitioning is normal• No distributed transactions!
ImplicationsOf ‘as a service’ model
• Microsoft doesn’t trust you!• Individual resources are limited• Throttling• Your resources are moved around• Unpredictable resource performance• Transient errors• No locks or very short locks• No local transactions!
• 1 exception: Sql as it is build into the protocol
How to deal with itNServiceBus helps a lot, but you need to code to it as well
• Retry, retry, retry!• Pick a transport that retries instead of relying on transactions• Enable First Level Retry• Enable Second Level Retry
How to deal with itNServiceBus helps a lot, but you need to code to it as well
• Take care of idempotency• Atomic messagehandler implementations• Saga’s too! Update saga state & nothing else!• Use saga’s to coordinate compensation logic• Check for retries• Check side effects
See, http://docs.particular.net/nservicebus/understanding-transactions-in-windows-azure for more options
How to deal with itDo not trust your disk!
• Do not put anything on disk!• The machine will fail, the disk will be gone!• Anyone noticed there is no SLA for individual VM’s?
• Put your stuff in storage• 99.99% SLA• Local Redundant & Geo Redunant
Hosting options
3 hosting optionsSame underlying infrastructure, built on top of each other
Cloud services Web sites Virtual Machines
Cloud services
Cloud servicesMany identical vm’s managed by azure
• Stateless• Based on a template (role)• Includes agents
• Health reporting• Diagnostics collection• Runtime & configuration environment
ServicePackage
ServicePackage
ServicePackage
Server Rack 1 Server Rack 2
Virtual machine
Virtual machine
Provision Role InstancesDeploy App CodeConfigure Network
Virtual machine
Virtual machine
Windows Azure Datacenter
ServicePackage
Provision Role InstancesDeploy App CodeConfigure Network
Windows Azure Datacenter
ServicePackage
Provision Role InstancesDeploy App CodeConfigure Network
Network Load Balancer
Windows Azure Datacenter
Network load-balancer configured for traffic
Provision Role InstancesDeploy App CodeConfigure Network
Network Load Balancer
Windows Azure Datacenter
Cloud servicesSpecific NServiceBus host
• Make use of agents present on the vm• Runtime & configuration environment• Diagnostics
• Found in NServiceBus.Hosting.Azure
Cloud servicesUse NServiceBusRoleEntrypoint
• Just call start & stop
public class WorkerRole : RoleEntryPoint{ private readonly NServiceBusRoleEntrypoint nsb = new NServiceBusRoleEntrypoint();
public override bool OnStart() { nsb.Start(); return base.OnStart(); }
public override void OnStop() { nsb.Stop(); base.OnStop(); }}
Cloud servicesAnd Configure your endpoint as a worker
• AsA_Worker: Specific profile, similar to AsA_Publisher on-premises but different defaults
• Configured for local emulator by default• Don’t forget to change config settings before deploying to azure itself
public class EndpointConfig : IConfigureThisEndpoint, AsA_Worker, UsingTransport<AzureServiceBus>{}
Cloud servicesIs designed for massive scale, what if you don’t need that
• AsA_Host: Specific profile for colocation
• Reference nservicebus.hosting.azure.hostprocess from your AsA_Worker endpoint• Zip your endpoint & put in blob storage container
• AsA_Host endpoint will • Download them on startup• Run them• Manage them (including updates)
public class EndpointConfig : IConfigureThisEndpoint, AsA_Host{}
Virtual Machines
Virtual MachinesJust like regular VM’s, except
• Built on cloud services, yet single VM• No SLA unless you run multiple (stateless) copies
• You manage the os• You are responsible for failover & backups!
• Disk persisted remotely• In azure storage services• No local write cache by default, subject to throttling• Impacts IOPS• May loose local data during geo disaster
• 1 minute local replication (lease held by original node)• 5 minute geo-replication SLA• Happened 3 times in past 5 years, no data loss yet
Virtual MachinesRegular NServiceBus host can be used, but
• Do not setup DTC
• Do not rely on transports that rely on DTC • no msmq
• If you use other transports that rely on local disk• Make sure they are configured for failover or clustered
• Do not rely on persistance that rely on DTC • share connection to prevent auto promotion
• If you use persistence that relies on local disk• Make sure it’s configured for failover or clustered
Websites
WebsitesShared website hosting
• IIS as a service
• With Continuous Integration built in• Deploy straight from github/bitbucket/tfs/...
• You only controle your code• Nothing else
WebsitesNServiceBus self hosting can be used
• Only public remote transports can be used• Only public remote persistance can be used
• Remote: No option to install on the machine
• Public: No option to setup virtual networking with cloudservices or virtual machines
Transports
Best transportsBecause they retry and are built for failover
Azure ServiceBus
Azure Storage
Optional transportsIf you manage it correctly (Failover, clustering, sharding, etc…)
Sql Database
DB DB
Sql Server Rabbit MQ Active MQ
Azure Storage QueuesQueue construct in Azure Storage Services
• Highly reliable• Very cheap• 200TB/500TB capacity limit• HTTP(S) based• Queue Peek Lock for retries• Max 7 days TTL!
Azure Storage QueuesConfigure NServiceBus to use azure storage queues
• Found in NServiceBus.Azure.Transports.WindowsAzureStorageQueues
• Connection string
• Detailed configuration
public class EndpointConfig : IConfigureThisEndpoint, AsA_Worker, UsingTransport<AzureStorageQueues>{}
DefaultEndpointsProtocol=https;AccountName=myAccount;AccountKey=myKey;
http://docs.particular.net/nservicebus/using-azure-storage-queues-as-transport-in-nservicebus
Azure ServiceBusBroker service in azure
• Reliable• Supports queues, topics & subscriptions• 5GB capacity limit• No limit on TTL• TCP based, lower latency• Queue Peek Lock for retries
• Emulates local transactions
• Loads of additional features• Dedupe, sessions, partitioning, ...
• Relatively expensive
Azure ServiceBusConfigure NServiceBus to use azure servicebus
• Found in NServiceBus.Azure.Transports.WindowsAzureServiceBus
• Connection string
• Detailed configuration
public class EndpointConfig : IConfigureThisEndpoint, AsA_Worker, UsingTransport<AzureServiceBus>{}
http://docs.particular.net/nservicebus/using-azure-servicebus-as-transport-in-nservicebus
Endpoint=sb://{yournamespace}.servicebus.windows.net/;SharedSecretIssuer=owner;SharedSecretValue={yourkey}
Azure ServiceBusAdditional features & applicability
• Applicable• Duplicate detection: time window• Partitioning: Bundle of queues/topics• Message ordering• Deadlettering• Batched operations
• Not applicable:• Sessions: instance affinity for message set, used for large messages, use databus
instead
Persistence
Best persistenceBecause it is built for failover
Azure Storage
Optional persistenceIf you manage it correctly (Failover, clustering, sharding, etc…)
Sql Database
DB DB
Sql ServerRaven Db
Azure StorageFound in NServiceBus.Azure
• Uses Azure Table Storage & Azure Blob Storage• Supports Subscriptions, Saga’s, Timeouts & Databus
• Configured for local emulator by default, detailed configuration;
public class EnableStorage : INeedInitialization{ public void Init() { Configure.Instance .AzureSubscriptionStorage() .AzureSagaPersister() .UseAzureTimeoutPersister() .AzureDatabus(); }}
http://docs.particular.net/nservicebus/using-azure-servicebus-as-transport-in-nservicebus
Tips & tricks || Q&A
Tips & tricksLessons learned the hard way
• Do not develop with the emulator (Compute nor storage)• Slow & behaves differently• Use nservicebus.hosting.azure.hostprocess for your workers• Use IISExpress for your websites• Use azure storage or azure servicebus online
Tips & tricksLessons learned the hard way
• Deploy asap• Your machine != huge cloud• Test as if ‘in production’
Tips & tricksLessons learned the hard way
• Tune the machine for lot’s of small requests
System.Net.ServicePointManager.DefaultConnectionLimit = 5000;System.Net.ServicePointManager.UseNagleAlgorithm = false;System.Net.ServicePointManager.Expect100Continue = false;
Tips & tricksLessons learned the hard way
• Intellitrace is your best friend for remote debugging
Tips & tricksLessons learned the hard way
• Log file shipping instead of default trace logs• Ship eventlog & log files via diagnostics service• Configured in .wadcfg file• Trace logs in table storage is to unfriendly
Wrapup
Want to know more?
• Overview: http://docs.particular.net/nservicebus/windows-azure-transport• Hosting: http://docs.particular.net/nservicebus/hosting-nservicebus-in-windows-azure• Cloud services: http://docs.particular.net/nservicebus/hosting-nservicebus-in-windows-azure-cloud-services• Shared host: http://docs.particular.net/nservicebus/shared-hosting-nservicebus-in-windows-azure-cloud-services• Azure servicebus: http://docs.particular.net/nservicebus/using-azure-servicebus-as-transport-in-nservicebus• Azure storage queues: http://
docs.particular.net/nservicebus/using-azure-storage-queues-as-transport-in-nservicebus• Storage persistence: http://docs.particular.net/nservicebus/using-azure-storage-persistence-in-nservicebus• Transactions: http://docs.particular.net/nservicebus/understanding-transactions-in-windows-azure
Resources
Or get your hands dirty?
• Samples: https://github.com/particular/nservicebus.azure.samples
Resources
Thanks