chapter twelve

12 Designing an efficient API

 

This chapter covers

  • Network-, processing-, and data-efficient API design
  • Cache and conditional requests
  • Data volume optimization
  • Bulk operations
  • Optimization-specific API layer

Since the Shopping company implemented its newly redesigned API with additional features, there has been a drop in the number of product purchases. Website and mobile analytics indicate that users are experiencing wait times of over 500 ms when searching for products. This issue is not due to a missing index in the database or inadequate infrastructure but rather the inefficient design of the API itself. Searching for products necessitates firing multiple API requests for each product found to gather all the necessary data and display it to the users.

Efficiency is not a concern only at the implementation level. An efficient API design can lead to several benefits. These include optimized smartphone battery usage, reduced wait times for end users, and minimal impact on 5G data plans. Furthermore, it can enhance provider infrastructure efficiency, lowering cloud infrastructure costs through reduced CPU usage and data downloads.

12.1 What is an efficient API

12.1.1 How an API can be inefficient

12.1.2 How design contributes to API efficiency

12.1.3 When to care about efficiency

12.2 Focusing on needs and user-friendliness to be efficient

12.2.1 What we’ve learned so far

12.2.2 Analyzing an inefficient flow

12.2.3 Optimizing each operation

12.2.4 Rethinking the flow

12.3 Enabling caching and conditional requests

12.3.1 Overviewing caching and conditional requests

12.3.2 Don’t let consumers decide how to cache

12.3.3 Defining caching policies according to data and context

12.3.4 Leveraging HTTP caching directives and conditional requests

12.4 Optimizing data volume

12.4.1 Enabling resource model selection

12.4.2 Toggling the return of updated or created resource

12.4.3 Enabling field selection

12.4.4 Considering partial update over total replacement

12.5 Optimizing pagination