-
Notifications
You must be signed in to change notification settings - Fork 84
Request Response
There are times when you need to ask a question within your application and get an answer. This might be something like GetAccountDetails(1234).
This pattern is called Request/Response. The name makes sense, we make a Request and wait for the Response.
If we need to scale this we can add extra responding service instances for built in, pull based load balancing.
Note that in a loosely coupled architecture, ideally the individual components act autonomously and Request Response isn't a patten you want to overuse. Also due to the way the return queues for messages are structured in Nimbus, relying on too many Request Response calls isn't necessarily the most performant options.
That said, there are times when you just need it so Nimbus has a rich way to return data from another service in your application.
To make a request on the bus we need to implment a BusRequest. This looks like:
public class AccountDetailsRequest : IBusRequest<AccountDetailsRequest, AccountDetailsResponse>
{
public int AccountId {get; set;}
}
This looks somewhat awkward, but we're associating the request type with our expected response type. Now we can put it on the bus like this:
var response = await bus.Request(new AccountDetailsRequest{AccountID=1234});
To implement the handler to fulfil this request we create a class that implements the IHandleRequest interface.
public class HandleAccountDetailRequests : IHandleRequest<AccountDetailsRequest, AccountDetailsResponse>
{
public async Task<AccountDetailsResponse> Handle(AccountDetailsRequest request)
{
//Load customer details from database
return new AccountDetailsResponse {Name= customer.Name};
}
}
We can add multiple listeners for the request as seen in the diagram above and this will balance the requests across the handlers in a pull based way without additional configuration.
Azure Service Bus
Windows Service Bus
Redis
In Process
Configuring Nimbus With Autofac
Configuring Nimbus With Windsor
Configuring Nimbus With Ninject
Sending a Command on the Bus
Publishing an Event on the Bus
Request Response
Multicast Request Response
Multicast Request Response - take First
Large Message Support