API Management Best Practices, Tips, and Tricks: #4 Include a Cache Response Header

Here we are, ready for another edition of API Management Best Practices, Tips, and Tricks. Until now, I have been addressing some tips to apply to your Azure API Management policies. However, today I will address a good Best practice that you must consider while implementing cache on your operations: Including a Cache Response Header on your API responses.

#2 Include a Cache Response Header

In my previous article, I briefly mentioned this topic, but I think it should have its own individual highlight. Headers are an essential part of REST API design, providing a way to include additional information about the request and response. They are a key peace to allow us to control the behavior of the API. Some typical headers used in REST APIs include Content-Type, Accept, Authorization, and User-Agent.

One good best practice while applying cache responses on our APIs – which has the advantage of significantly reducing latency for API callers – is to inform API users when they are receiving a cached response or not. This way, users or systems know if they are working with live-fresh data or not, and provide actions according. Sometimes we cannot rely on a cached version of the resource, sometimes it doesn’t matter. However, by having this strategy, you will be enriching and improving your APIs.

And once again, this is quite simple to accomplish:

<inbound>
	...
			<cache-lookup-value key="tokenValue" variable-name="varTokenValue" />
			<choose>
				<when condition="@(context.Variables.ContainsKey("varTokenValue"))">
					<return-response>
						<set-status code="200" reason="OK" />
						<set-header name="Content-Type" exists-action="override">
							<value>application/json</value>
						</set-header>
						<set-header name="CachedResponse" exists-action="override">
							<value>true</value>
						</set-header>
						<set-body>@((string)context.Variables["varTokenValue"])</set-body>
					</return-response>
				</when>
			</choose>
   ...
</inbound>
...
<outbound>
	...
	<return-response>
		<set-status code="200" reason="OK" />
		<set-header name="Content-Type" exists-action="override">
			<value>text/plain</value>
		</set-header>
		<set-header name="CachedResponse" exists-action="override">
			<value>false</value>
		</set-header>
		<set-body>@((string)context.Variables["varToken"])</set-body>
	</return-response>
   ...
</outbound>

I hope you enjoy this tip and stay tuned for the following Azure API Management Best practices, Tips, and Tricks.

If you liked the content or found it helpful and want to help me write more content, you can buy (or help buy) my son a Star Wars Lego! 

Author: Sandro Pereira

Sandro Pereira lives in Portugal and works as a consultant at DevScope. In the past years, he has been working on implementing Integration scenarios both on-premises and cloud for various clients, each with different scenarios from a technical point of view, size, and criticality, using Microsoft Azure, Microsoft BizTalk Server and different technologies like AS2, EDI, RosettaNet, SAP, TIBCO etc. He is a regular blogger, international speaker, and technical reviewer of several BizTalk books all focused on Integration. He is also the author of the book “BizTalk Mapping Patterns & Best Practices”. He has been awarded MVP since 2011 for his contributions to the integration community.

Leave a Reply

Your email address will not be published. Required fields are marked *

turbo360

Back to Top