The granularity of the methods within a service is of equal or greater importance than the granularity of the service itself. Using our bank account example, consider the retrieval of account holder information from the bank account service. A method in the BankAccountService called GetAccountHolder could return just the name of the account holder, or both the name and address of the account holder. As shown in Diagram 2, the BankAccountService returns account holder information with both the name and address.
This scenario works great if the client needs the address information, but if the client doesn't need address information, more data crosses the network than is absolutely necessary. By eliminating the address from the account holder information, we meet the needs of clients that don't need address information. But if we eliminate the address, clients that need address information will have to return to the service to retrieve it. Diagram 3 shows this scenario:
In this scenario, a client that needs both name and address will have to make two network trips to retrieve the street address. To solve both of these problems, consider a BankAccountService with two methods, GetAccountHolder and GetAccountHolderAddress. The GetAccountHolder method returns just the account holder's name. The GetAccountHolderAddress returns both the account holder's name and the account holder's address. An alternate implementation could involve passing in an argument that directs the service as to how much data to return. In a sophisticated implementation, a client would pass the service the list of attributes to return back to it. If these implementations are not possible, it is always better to return more data to minimize network round trips. It is likely that future clients will need the data provided by the service.
The ability of a service to have multi-grained methods that return the appropriate amount of data is important to reduce network traffic. Extra network traffic is either due to too much unnecessary data or too many requests to get the data that a client requests.
Granularity is a difficult problem for us to reconcile when we design service interfaces. It is important to understand the options and to implement the most appropriate interface. In the past, arguments surrounding service interfaces have focused mainly on determining the right granularity. With multi-grained services, it becomes important to find the right granularities for your clients.
About the AuthorMichael Stevens is an independent consultant specializing in service-oriented architectures for the enterprise. He has over fourteen years of experience in information technology architecting and developing software systems, most recently focusing on J2EE solutions. He founded a software company that developed solutions for the mailing industry. He is a certified Java programmer, a member of the IEEE Computer Society, and of the ACM. He may be reached at firstname.lastname@example.org.
Page 2 of 2