Optimized APIs

This section discusses adopting better optimization for the API. There's no concept of one-size-fits-all. In the real world, multiple APIs may support the same service as that service might be serving different types of users and use cases. As we quoted earlier in this chapter, the API should be modeled after the design according to the use case it fulfils and not by the backend services or applications it exposes.

So optimization applies to a specific business request in a particular context. Let's take an example, a web service enables its mobile app consumers to clear electricity bills. In the context of the mobile, the constraints of mobile application should be our primary consideration, as a mobile app is sensitive to network latency, numerous network trips, the size of the data compared to a standard web application, and so on. So, our API design should focus on limiting the backend calls and minimize the size of the data returned.

Concerning granularity, let's consider the preceding example will consume few fine-grained independently invokable APIs. On the other hand, to make the payment, the app may need to use a coarse-grained API from another service (which, in turn, may have many fine-grained APIs). So, our mobile app can use a few other fine-grained APIs directly to fetch the due amount, get the user address, and access account details of the bank that the user wants to use to pay the outstanding amount, and may even need more fine-grained APIs to be incorporated in future and so on. So, designers should consider the layered or tiered approach that we discussed in Chapter 1Introduction to the Basics of RESTful Architecture, to orchestrate and manage those fine-grained APIs.

So, API designs can expose fine-grained APIs for consumers who can access them directly, and coarse-grained services on top of them would support broader use cases, so that the service clients may decide to call the fine-grained APIs directly, or they may decide to use the coarse-grained APIs if they need the combined functionality of multiple fine-grained API calls.

The preceding diagram depicts a logical structure of APIs with their granularities and how those APIs are consumed by the service clients, and it is an example of optimized APIs for the use case that we discussed earlier.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.129.19