Building enterprise
                          applications with
                            NServiceBus
                              Andreas Öhlund




onsdag den 12 maj 2010
What is a distributed
                               system?
                         “A distributed system is one in which the
                         failure of a computer you didn't even
                         know existed can render your own
                         computer unusable”
                                                   -Leslie Lamport




onsdag den 12 maj 2010
The first rule of NServiceBus
onsdag den 12 maj 2010
There is NO Service Bus!


                The first rule of NServiceBus
onsdag den 12 maj 2010
Well, at least no physical one
onsdag den 12 maj 2010
The second rule of NServiceBus
onsdag den 12 maj 2010
You DO NOT use
              synchronous communication


               The second rule of NServiceBus
onsdag den 12 maj 2010
The key to robust communication




onsdag den 12 maj 2010
Store and forward




onsdag den 12 maj 2010
Request/Response the
                      NServiceBus way

                     Fire and forget X 2




onsdag den 12 maj 2010
The effect of
             tight coupling
                         Temporal
                         Behavioral




onsdag den 12 maj 2010
Communication and coupling
onsdag den 12 maj 2010
Coupling NServiceBus style
onsdag den 12 maj 2010
Command oriented
                          communication
                                         CompleteSaleCommand



                           OnlineSales                         OrderService




onsdag den 12 maj 2010
Sending Messages
                              IBus.Send()
                         bus.Send(new CompleteSaleCommand
                                                 CreateOrderCommand
                            {
                              ProductId = 123,
                                   OnlineSales

                              Quantity = 10
                              ....
                            });




onsdag den 12 maj 2010
Receiving and
                  processing of messages
                 public class CompleteSaleMessageHandler:
               CreateOrderCommand

                    IHandleMessages<CompleteSaleCommand>
                 {                  OrderService



                    public void Handle(CompleteSaleCommand c)
                    {
                      ...
                    }
                 }


onsdag den 12 maj 2010
Message Contracts

                   •     Versioned by the owner

                   •     Regular .Net Assembly

                   •     Bound to a input queue

                   •     class MyMessage :
                         IMessage




onsdag den 12 maj 2010
Throttling the load
                         • Manage traffic peaks
                         • Consumer controls the pace
                         • <MsmqTransportConfig
                           NumberOfWorkerThreads="3" />




onsdag den 12 maj 2010
What happens when the load
                becomes to heavy for one server?




onsdag den 12 maj 2010
Scaling with the Distributor




onsdag den 12 maj 2010
Building consistent systems

                   •     Do we produce
                         predictable results even
                         under failure conditions?

                   •     What about non
                         transactional sources?




onsdag den 12 maj 2010
Isolate non transactional sources as
                          separate endpoints

                public void Handle(CompleteSaleCommand cmd)
                {
                   var order = ....
                   orderRepository.Save(order)

                         smtpClient.Send(new MailMessage{...})
                }




onsdag den 12 maj 2010
Isolate non transactional sources as
                          separate endpoints
                 public void Handle(CompleteSaleCommand cmd)
                 {
                public void Handle(CompleteSaleCommand cmd)
                {   var order = ....
                    orderRepository.Save(order)
                   var order = ....
                   orderRepository.Save(order)
                    bus.Send(new NotifyCustomerRequest
                              {
                   smtpClient.Send(new MailMessage{...})
                }                 EmailAddress = customer.Email,
                                  Body = “Order confirmation”
                              });
                 }

onsdag den 12 maj 2010
Idempotent

“Idempotent operations are operations that can be applied
      multiple times without changing the result”




onsdag den 12 maj 2010
When things go wrong




onsdag den 12 maj 2010
Messaging gives you a
                         chance to do better

                   • No more showing the users a WSOD
                   • Messages are replayable
                   • Async communication opens up a crucial
                         windows of time for corrections




onsdag den 12 maj 2010
Event oriented communication
onsdag den 12 maj 2010
Publish and subscribe


                   •     Everyone get’s a copy

                   •     Removes behavioral
                         coupling




onsdag den 12 maj 2010
Billing

                                                             BillingService




                                        OrderAcceptedEvent
                                                                                  Shipping
                         SalesService




                                                                              ShippingService




                         Sales


                         Business events
onsdag den 12 maj 2010
Finally a chance to discuss non functional
                requirements that business people understand

onsdag den 12 maj 2010
The mechanics of pub/sub




onsdag den 12 maj 2010
The mechanics of pub/sub




onsdag den 12 maj 2010
Becoming a subscriber

               bus.Subcribe<IOrderAcceptedEvent>();




onsdag den 12 maj 2010
Publishing
                bus.Publish<IOrderAcceptedEvent>(x=>
                   {
                     ProductId = 123,
                     Quantity = 10
                     ....
                   });




onsdag den 12 maj 2010
It’s hard to get it right
                      the first time around




onsdag den 12 maj 2010
Versioning events using interfaces

                         public interface IOrderAcceptedEvent2 :
                                            IOrderAcceptedEvent
                         {
                            string SomeNewProperty{ get;set; }
                         }




onsdag den 12 maj 2010
Long running
                         transactions




onsdag den 12 maj 2010
Use sagas to model long
                   running transactions
                     public	
  class	
  MySaga	
  :	
  Saga<MySagaData>,
                     	
  	
  	
  	
  IAmStartedByMessages<Message1>,
                     	
  	
  	
  	
  IHandleMessages<Message2>
                     {
                     	
  	
  	
  	
  public	
  void	
  Handle(Message1	
  message)
                     	
  	
  	
  	
  {
                     	
  	
  	
  	
  	
  	
  	
  	
  //	
  code	
  to	
  handle	
  Message1
                     	
  	
  	
  	
  }

                     	
  	
  	
  	
  public	
  void	
  Handle(Message2	
  message)
                     	
  	
  	
  	
  {
                     	
  	
  	
  	
  	
  	
  	
  	
  //	
  code	
  to	
  handle	
  Message2
                     	
  	
  	
  	
  }
                     }




onsdag den 12 maj 2010
Storing state
               public	
  class	
  MySagaData	
  :	
  IContainSagaData
               {
               	
  	
  	
  	
  //	
  the	
  following	
  properties	
  are	
  mandatory
               	
  	
  	
  	
  public	
  virtual	
  Guid	
  Id	
  {	
  get;	
  set;	
  }
               	
  	
  	
  	
  public	
  virtual	
  string	
  Originator	
  {	
  get;	
  set;	
  }
               	
  	
  	
  	
  public	
  virtual	
  string	
  OriginalMessageId	
  {	
  get;	
  set;	
  }
               }




onsdag den 12 maj 2010
Timeouts
               public	
  void	
  Handle(Message1	
  message)
               {
               	
  	
  	
  	
  this.Data.SomeID	
  =	
  message.SomeID;

               	
  	
  	
  	
  RequestTimeout(TimeSpan.FromHours(1),	
  "some	
  state");
               }

               public	
  override	
  void	
  Timeout(object	
  state)
               {
               	
  	
  	
  //	
  some	
  business	
  action	
  like:
               	
  	
  	
  if	
  (!Data.Message2Arrived)
               	
  	
  	
  	
  	
  	
  ReplyToOriginator(new	
  TiredOfWaitingForMessage2());
               }




onsdag den 12 maj 2010
Configuration
       Developers:
                   use code
       Administrators:
                   use config files




onsdag den 12 maj 2010
Hosting options

                   • Custom hosting
                    • Website
                    • Smartclient
                    • Commandline
                   • Generic host

onsdag den 12 maj 2010
Using the Generic Host
                  public class EndpointConfig:
                                IConfigureThisEndpoint,
                                AsA_Publisher
                  {

                  }




onsdag den 12 maj 2010
Using profiles to adapt to different
                             environments
onsdag den 12 maj 2010
NServiceBus role in
                  CQRS Style applications




onsdag den 12 maj 2010
NServiceBus is
           opinionated




onsdag den 12 maj 2010
Why all these opinions?




onsdag den 12 maj 2010
Don’t use messaging
                             for queries
                   • Queries need to return relatively fast
                   • No benefit from the robustness that NSB
                         gives
                   • Use NSB to cache data close and use native
                         apis to get at that data



onsdag den 12 maj 2010
The end

                   • www.nservicebus.com
                   • www.udidahan.com/blog
                   • andreasohlund.blogspot.com



onsdag den 12 maj 2010

NServiceBus Alt.Net 20100511

  • 1.
    Building enterprise applications with NServiceBus Andreas Öhlund onsdag den 12 maj 2010
  • 2.
    What is adistributed system? “A distributed system is one in which the failure of a computer you didn't even know existed can render your own computer unusable” -Leslie Lamport onsdag den 12 maj 2010
  • 3.
    The first ruleof NServiceBus onsdag den 12 maj 2010
  • 4.
    There is NOService Bus! The first rule of NServiceBus onsdag den 12 maj 2010
  • 5.
    Well, at leastno physical one onsdag den 12 maj 2010
  • 6.
    The second ruleof NServiceBus onsdag den 12 maj 2010
  • 7.
    You DO NOTuse synchronous communication The second rule of NServiceBus onsdag den 12 maj 2010
  • 8.
    The key torobust communication onsdag den 12 maj 2010
  • 9.
    Store and forward onsdagden 12 maj 2010
  • 10.
    Request/Response the NServiceBus way Fire and forget X 2 onsdag den 12 maj 2010
  • 11.
    The effect of tight coupling Temporal Behavioral onsdag den 12 maj 2010
  • 12.
  • 13.
  • 14.
    Command oriented communication CompleteSaleCommand OnlineSales OrderService onsdag den 12 maj 2010
  • 15.
    Sending Messages IBus.Send() bus.Send(new CompleteSaleCommand CreateOrderCommand { ProductId = 123, OnlineSales Quantity = 10 .... }); onsdag den 12 maj 2010
  • 16.
    Receiving and processing of messages public class CompleteSaleMessageHandler: CreateOrderCommand IHandleMessages<CompleteSaleCommand> { OrderService public void Handle(CompleteSaleCommand c) { ... } } onsdag den 12 maj 2010
  • 17.
    Message Contracts • Versioned by the owner • Regular .Net Assembly • Bound to a input queue • class MyMessage : IMessage onsdag den 12 maj 2010
  • 18.
    Throttling the load • Manage traffic peaks • Consumer controls the pace • <MsmqTransportConfig NumberOfWorkerThreads="3" /> onsdag den 12 maj 2010
  • 19.
    What happens whenthe load becomes to heavy for one server? onsdag den 12 maj 2010
  • 20.
    Scaling with theDistributor onsdag den 12 maj 2010
  • 21.
    Building consistent systems • Do we produce predictable results even under failure conditions? • What about non transactional sources? onsdag den 12 maj 2010
  • 22.
    Isolate non transactionalsources as separate endpoints public void Handle(CompleteSaleCommand cmd) { var order = .... orderRepository.Save(order) smtpClient.Send(new MailMessage{...}) } onsdag den 12 maj 2010
  • 23.
    Isolate non transactionalsources as separate endpoints public void Handle(CompleteSaleCommand cmd) { public void Handle(CompleteSaleCommand cmd) { var order = .... orderRepository.Save(order) var order = .... orderRepository.Save(order) bus.Send(new NotifyCustomerRequest { smtpClient.Send(new MailMessage{...}) } EmailAddress = customer.Email, Body = “Order confirmation” }); } onsdag den 12 maj 2010
  • 24.
    Idempotent “Idempotent operations areoperations that can be applied multiple times without changing the result” onsdag den 12 maj 2010
  • 25.
    When things gowrong onsdag den 12 maj 2010
  • 26.
    Messaging gives youa chance to do better • No more showing the users a WSOD • Messages are replayable • Async communication opens up a crucial windows of time for corrections onsdag den 12 maj 2010
  • 27.
  • 28.
    Publish and subscribe • Everyone get’s a copy • Removes behavioral coupling onsdag den 12 maj 2010
  • 29.
    Billing BillingService OrderAcceptedEvent Shipping SalesService ShippingService Sales Business events onsdag den 12 maj 2010
  • 30.
    Finally a chanceto discuss non functional requirements that business people understand onsdag den 12 maj 2010
  • 31.
    The mechanics ofpub/sub onsdag den 12 maj 2010
  • 32.
    The mechanics ofpub/sub onsdag den 12 maj 2010
  • 33.
    Becoming a subscriber bus.Subcribe<IOrderAcceptedEvent>(); onsdag den 12 maj 2010
  • 34.
    Publishing bus.Publish<IOrderAcceptedEvent>(x=> { ProductId = 123, Quantity = 10 .... }); onsdag den 12 maj 2010
  • 35.
    It’s hard toget it right the first time around onsdag den 12 maj 2010
  • 36.
    Versioning events usinginterfaces public interface IOrderAcceptedEvent2 : IOrderAcceptedEvent { string SomeNewProperty{ get;set; } } onsdag den 12 maj 2010
  • 37.
    Long running transactions onsdag den 12 maj 2010
  • 38.
    Use sagas tomodel long running transactions public  class  MySaga  :  Saga<MySagaData>,        IAmStartedByMessages<Message1>,        IHandleMessages<Message2> {        public  void  Handle(Message1  message)        {                //  code  to  handle  Message1        }        public  void  Handle(Message2  message)        {                //  code  to  handle  Message2        } } onsdag den 12 maj 2010
  • 39.
    Storing state public  class  MySagaData  :  IContainSagaData {        //  the  following  properties  are  mandatory        public  virtual  Guid  Id  {  get;  set;  }        public  virtual  string  Originator  {  get;  set;  }        public  virtual  string  OriginalMessageId  {  get;  set;  } } onsdag den 12 maj 2010
  • 40.
    Timeouts public  void  Handle(Message1  message) {        this.Data.SomeID  =  message.SomeID;        RequestTimeout(TimeSpan.FromHours(1),  "some  state"); } public  override  void  Timeout(object  state) {      //  some  business  action  like:      if  (!Data.Message2Arrived)            ReplyToOriginator(new  TiredOfWaitingForMessage2()); } onsdag den 12 maj 2010
  • 41.
    Configuration Developers: use code Administrators: use config files onsdag den 12 maj 2010
  • 42.
    Hosting options • Custom hosting • Website • Smartclient • Commandline • Generic host onsdag den 12 maj 2010
  • 43.
    Using the GenericHost public class EndpointConfig: IConfigureThisEndpoint, AsA_Publisher { } onsdag den 12 maj 2010
  • 44.
    Using profiles toadapt to different environments onsdag den 12 maj 2010
  • 45.
    NServiceBus role in CQRS Style applications onsdag den 12 maj 2010
  • 46.
    NServiceBus is opinionated onsdag den 12 maj 2010
  • 47.
    Why all theseopinions? onsdag den 12 maj 2010
  • 48.
    Don’t use messaging for queries • Queries need to return relatively fast • No benefit from the robustness that NSB gives • Use NSB to cache data close and use native apis to get at that data onsdag den 12 maj 2010
  • 49.
    The end • www.nservicebus.com • www.udidahan.com/blog • andreasohlund.blogspot.com onsdag den 12 maj 2010