HTTP Caching

 

HTTP Caching policy creates a caching space to store the response of different request. So if same request come then it will not go back to end system to retrieve the same information instead it will the response from caching.

HTTP Caching policy is used for those APIs whose response will not change for a long/substantial time. E.g. In an organization Employee details are least likely to change in a day and if we cache the employee information for a day and use that to send the response then it can improve the throughput time and reduce the load from the end system. After a day new information is fetched for the employee and stored in cache.

Same caching can be achieved through MuleSoft cache scope – learn more about it.

To Start with we will create a mule application and connect it with API manager – please see how we can connect the mulesoft application to API Manager

After deployment in Cloudhub the API manager configuration will come as Active

HTTP Caching

 

Now we will apply the HTTP Caching policy

HTTP Caching

 

Select HTTP Caching

HTTP Caching

 

Now update the policy configuration

HTTP Caching

 

  • HTTP Caching Key – Uniquely define the key for the request. E.g. Employee id for employee request. Should be a dataweave expresion
  • Maximum Cache Entries – no of entries (request key – response payload) to cache
  • Entry Time To Live (in Seconds) – cached time after which a new details is fetched or actual mule flow will be called
  • Distributed – True in case we are using cluster or multiple workers
  • Persistent – True in case we want to persist cache data in case of runtime restart

 

HTTP Caching

 

  • Follow HTTP Caching directives – to add proper HTTP headers in response to tell client we are using caching
  • Invalidation Header – to specify in case we don’t want cache result
  • Condition Request Caching Expression – based on which caching will be used so if condition is true then only use caching e.g. storing the result in cache and reuse in case we get the same request
  • Condition Response Caching Expression – based on which caching will be used so if condition is true then only use caching e.g. storing the result in cache and reuse in case we get the same request

 

Lets populate the field as per our need

Additional settings

 

Click apply

 

To understand the Caching better we will add some loggers to show when actual flow is called and when it’s used cached data for sending response

 

Now if we hit this API

 

We can see the logs coming in Cloudhub

 

If we hit the same service again

 

We can see the new HTTP header added by the policy to show what the age of the cached result is and no logs this time in Cloudhub

 

Sample application – sample-rest-service-http-caching-policy

Sample SOAP UI project – sample-rest-service-http-caching-soapui-project

 

  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments