Pattern: Computation Function

How can a client invoke side-effect-free remote processing on the provider side to have a result calculated from its input?


The final version of this pattern is featured in our book Patterns for API Design: Simplifying Integration with Loosely Coupled Message Exchanges.

Pattern: Computation Function

a.k.a. Stateless Computation Operation, Calculation Action, Side-Effect-Free/Stateless Operation

Context

The requirements for an application indicate that something has to be calculated. While the input is available locally and the output will be used in the same place, the calculation should not be run there for cost, efficiency, workload, security, or other reasons.

For instance, an API client might want to ask the API endpoint provider whether some data data meets certain conditions or might want to convert it from one format to another.

Problem

How can a client invoke side-effect-free remote processing on the provider side to have a result calculated from its input?

Forces

The following forces apply when introducing side-effect-free processing on the provider side:

  • Reproducibility and trust
  • Performance
  • Workload management

Pattern forces are explained in depth in the book.

Solution

Introduce an API operation cf with cf: in -> out to the API endpoint, which often is a Processing Resource. Let this Computation Function validate the received request message, perform the desired function cf, and return its result in the response.

This Computation Function neither accesses nor changes the server-side application state as shown in Figure 1.

Sketch

A solution sketch for this pattern from pre-book times is:

Figure 1: A Computation Function is a stateless operation neither reading nor writing to provider-side storage.

Examples

A simple, rather self-explanatory example of a Transformation Service is shown in Figure 3

Figure 3: A Processing Resource providing Transformation Services

A call to find out about the health of a service (a.k.a. heartbeat test message) is another example of a simple command exposed remotely within a Processing Resource endpoint (see Figure 4).

Figure 4: Examples of Validation Services: Health Check operations

Such “I am alive” operations (a.k.a. “ping” calls) accepting test messages) are often added to mission-critical API implementations as part of a systems management strategy (here: fault and performance management). Its pre- and postconditions are simple; its API contract is sketched in the above UML snippet. Neither system transactions nor business-level compensation (undo) is required in this simple example.

Are you missing implementation hints? Our papers publications provide them (for selected patterns).

Consequences

The resolution of pattern forces and other consequences are discussed in our book.

Known Uses

Serverless computing lambdas, deployed to public clouds such as AWS or Azure, may be seen as Computation Functions as well (unless they are combined with cloud storage offerings, which makes them stateful).

Online JSON and JSON schema validators operate purely input-based and qualify as Validation Services; they do not have to take server-internal state into account (other than the schema definition and their metametamodels). The same holds for image manipulation and PDF processing tools, both of which are abundant on the Web today (Transformation Service).

A basic known use of the Validation Service variant of this pattern can be found in the Cargo root entity and the RouteSpecification in the Cargo Aggregrate of the Domain-Driven Design Sample Application. It implements several validation routines (that are not exposed in the remote API of the application).

A major German car manufacturer offers a REST level 2 user profile management microservice for all its clients; the analysis endpoint of this service qualifies as a known use of the Validation Service variant: It offers a POST operation to validate data strictly, suggests corrections, and converts addresses and phone numbers to country-specific standards (without storing them); profile updates are also supported by the microservice, but at another endpoint address. Swagger/Open API contracts are exposed in an API Description portal/website.

Terravis, a process integration platform Berli, Lübke, and Möckli (2014), offers an API to internal clients to calculate a valid payment date. The operation is called by specifying a desired payment date. Depending on certain rules (e.g., taking bank holidays into accounts as well as weekends), the next feasible payment date is calculated and returned to the caller. This operation does not change the provider-side application state, all required input comes from the received request message. Moreover, Terravis offers Solution-Internal APIs for generating various business documents such as contracts. Data is passed from the calling clients to the document generation API, which validates the data for completeness and conformance with certain business rules and then generates a PDF document statelessly.

More Information

Related Patterns

The pattern compares to its siblings as this: 

The Service pattern in Domain-Driven Design (DDD) includes similar semantics (but is broader) and can help to identify Computation Function candidates during endpoint identification Vernon (2013).

Other Sources

Service types are a topic covered by SOA literature from the early 2000s, e.g., “Enterprise SOA” Krafzig, Banke, and Slama (2004) and “SOA in Practice” Josuttis (2007). While the service type taxonomies in these books are more focussed on the overall architecture, some of the basic services and utility services have responsibilities that do not require read or write access to provider/server state and therefore qualify as instance of this pattern an its variants.

The design-by-contract approach in the object-oriented programming method and language Eiffel Meyer (1997) includes validation into business commands/domain methods and automates pre- and postcondition checking. This program-internal approach can be seen as an alternative to external Validation Services (but also as a rather advanced known use of it).

A lot of online resources on serverless computing exist. One starting point is the web site and blog “Serverless” by J. Daly.

References

Berli, Walter, Daniel Lübke, and Werner Möckli. 2014. “Terravis – Large Scale Business Process Integration Between Public and Private Partners.” In Proc. INFORMATIK 2014, 1075–90. Gesellschaft für Informatik e.V.
Hohpe, Gregor, and Bobby Woolf. 2003. Enterprise Integration Patterns: Designing, Building, and Deploying Messaging Solutions. Addison-Wesley.
Josuttis, Nicolai. 2007. SOA in Practice: The Art of Distributed System Design. O’Reilly.
Krafzig, Dirk, Karl Banke, and Dirk Slama. 2004. Enterprise SOA: Service-Oriented Architecture Best Practices (the Coad Series). Prentice Hall.
Meyer, Bertrand. 1997. Object-Oriented Software Construction. 2nd ed. Prentice-Hall.
Pautasso, Cesare, Ana Ivanchikj, and Silvia Schreier. 2016. “A Pattern Language for RESTful Conversations.” In Proceedings of the 21st European Conference on Pattern Languages of Programs (EuroPLoP). Irsee, Germany.
Vernon, Vaughn. 2013. Implementing Domain-Driven Design. Addison-Wesley.
Voelter, Markus, Michael Kircher, and Uwe Zdun. 2004. Remoting Patterns - Foundations of Enterprise, Internet, and Realtime Distributed Object Middleware. Wiley.