Service granularity principle
In software engineering and software architecture, service granularity is a design concern in service-based systems, including service-oriented architecture (SOA) and microservices architectures. It refers to the scope and cohesion of functionality encapsulated by a service and the boundaries of its responsibilities. Service granularity affects how services are defined, how they communicate, and how functionality is distributed across a system. In SOA, it is often addressed during service-oriented modeling to balance reuse and abstraction. In microservices architectures, granularity is a primary factor in determining independent deployability, operational overhead, and the complexity of inter-service communication.
Definition
Service granularity is both an application domain concern, as well as a software interface design issue ; it is a property of the service contract exposed by a service provider. It pertains to the semantics and syntax of the in and out message content, which can be viewed as instances of two general Enterprise Integration Patterns, and . By definition a coarse-grained service operation has broader scope than a fine-grained service, although the terms are relative. The former typically requires increased design complexity but can reduce the number of calls required to complete a task.Criteria
Due to the fallacies of distributed computing, finding an adequate granularity is hard. There is no single simple answer but a number of criteria exist. A primary goal of service modeling and granularity design is to achieve loose coupling and modularity, which are two of the essential SOA principles, and to address other architecturally significant requirements.Many forces influence the service granularity; four particularly relevant factors to consider when designing for an adequate granularity are performance, message size, transactionality and business function:
Business function
Ideally, each service operation maps to a single business function, although if a single operation can provide multiple functions without adding design complexity or increasing message sizes, this generality can reduce implementation and usage costs.Performance
Web services are accessed remotely and calls to web service operation create more network overhead. Reducing the number of service requests reduces that overhead.Message size
Coarse-grained services may pass more data than fine-grained services, including data that is not specifically required for the task. This complicates message processing in the endpoint and might in turn harm performance. Reducing message size may require adding a more fine-grained operation.Quality-of-service characteristics including transactionality
For conceptual clarity each service operation should perform a single system-level transaction and leave data integrity across service boundaries to the business logic in the service consumer. This also simplifies error recovery, and typically eases design.Many more decision criteria for finding an appropriate granularity exist; there is no global optimum. Sixteen such coupling criteria are compiled from the literature in.