<commit_before>---
layout: page
title: "Request Response"
category: doc
date: 2014-03-09
order: 6
---

##Why

There are times when you need to ask a question within your application and get an answer. This might be something like GetAccountDetails(1234). 


##The Pattern

This pattern is called Request/Response. The name makes sense, we make a Request and wait for the Response. 

![](../images/RequestResponse.png)

If we need to scale this we can add extra responding service instances for built in, pull based load balancing.

![](../images/RequestResponseScaled.png)


Note that in a loosely couple architecture, ideally the individual components act autonomously and Request Response isn't a patten you don't 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.


##How

To make a request on the bus we need to implment a BusRequest. This looks like:

	public class AccountDetailsRequest : BusRequest<AccountDetailsRequest, AccountDetailsResponse>
	{
		public int AccountId {get; set;}
	
	}

This looks somewhat awkward, but where 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});



##Handling it

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)<commit_msg>image paths<commit_after>---
layout: page
title: "Request Response"
category: doc
date: 2014-03-09
order: 6
---

##Why

There are times when you need to ask a question within your application and get an answer. This might be something like GetAccountDetails(1234). 


##The Pattern

This pattern is called Request/Response. The name makes sense, we make a Request and wait for the Response. 

![](../../images/RequestResponse.PNG)

If we need to scale this we can add extra responding service instances for built in, pull based load balancing.

![](../../images/RequestResponseScaled.PNG)


Note that in a loosely couple architecture, ideally the individual components act autonomously and Request Response isn't a patten you don't 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.


##How

To make a request on the bus we need to implment a BusRequest. This looks like:

	public class AccountDetailsRequest : BusRequest<AccountDetailsRequest, AccountDetailsResponse>
	{
		public int AccountId {get; set;}
	
	}

This looks somewhat awkward, but where 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});



##Handling it

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)
{'fraction_non_alphanumeric': 0.05549450549450549, 'fraction_numerical': 0.00934065934065934, 'mean_word_length': 4.6890625, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5200186', 'n_tokens_mistral': 946, 'n_tokens_neox': 870, 'n_words': 480}