Improve API Performance using policies (Part 1)
In this blog, I wanted to share some tips on improving the performance of APIs. Every time an API is consumed from API Management, the relevant call hits the backend system which provides the service. If your data does not change quite often, it makes sense to cache the data for a certain interval. Once the data is cached, the subsequent calls for the same API do not go to the backend system. This improves API’s performance through reduced latency and network traffic.
We can leverage an API Policy “Response Cache” to help cache the response.
- Access a trial landscape, you need to have a developer account first. If you still need to create a developer account, you can start from here: Signing Up for a Developer Account.
- Setup your API Management service by registering an ES4 System. I have documented this earlier in Building & Consuming API’s using SAP API Management (Part 1).
Assuming you have got an API created for ES4 ABAP system, navigate to the API in the API portal and click on the “Policies” link as shown below
On the left-hand side, select “PreFlow” displayed under “ProxyEndpoints” and then click on the + sign for the Response Cache policy on the right-hand side.
Provide a name for the policy and ensure that the stream is “Incoming Request”
In the code which gets populated, change the KeyFragment Ref to as shown below.
Here is the explanation for each of the properties
- KeyFragment ref is used to specify a value that gets included in the cache key
- TimeoutInSec represents the number of seconds after which a cache entry should expire
- SkipCacheLookup represents the expression which if evaluated to true will ignore the cache and refresh it
- SkipCachePopulation represents the expression which if evaluated to true will skip writing to cache
Response Cache Policy needs to be added to both Incoming requests as well as Outgoing response. Click on the + sign next to the existing Response Cache API policy (as shown below)
Select the Stream as “Outgoing Response”.
This is how your Policy flow would look like
Both the policies will have the same XML code. Click on Update to save all the policy settings
To test the API, click on the Debug link.
In the Debug view, trigger the debug by clicking on “Start Debug” and then navigate out by clicking on the back button of “API Debug viewer”
Navigate to resources tab of the same API and scroll to the SalesOrder Entity and click on the link “SalesOrderCollection”
This will execute the API in a separate browser window. Once the data is loaded, refresh the screen to view the data one more time.
Navigate back to the API and click on the Debug link.
This should display statistics of the last two calls triggered for this API. Notice that the GET operation for the first call was 905ms. It drastically reduced to 30ms for the second call as the data was fetched from the cache.
In the next part, I will show how to use another API policy which also can help in improving the performance of your APIs.