Save Money by Setting Cache-Control on Windows Azure Blobs

August 11, 2013 — 5 Comments

cloud-storageOn Windows Azure everything including bandwidth has a price tag, but don’t be alarmed. The price tag isn’t that steep.

The important thing to remember on the cloud, is that we’re talking about economies of scale. This also means, that the cost of operation grows along with the scale and reach of your application.

Setting Cache-Control on publicly accessible Windows Azure Blobs can help reduce bandwidth by preventing consumers from having to continuously download resources. Additionally, it will greatly contribute to creating a more responsive end-user experience.

Cache-Control allows you to specify a relative amount of time to cache data after it was received. It’s mostly recommended when you need control over how caching is done.


Data Transfers Pricing Details For Windows Azure (August 2013)

OUTBOUND DATA TRANSFERS ZONE 1 ZONE 2
First 5 GB / Month Free Free
5 GB – 10 TB 1 / Month $0.12 per GB $0.19 per GB
Next 40 TB / Month $0.09 per GB $0.15 per GB
Next 100 TB / Month $0.07 per GB $0.13 per GB
Next 350 TB / Month $0.05 per GB $0.12 per GB

Cache-Control Headers

max-age=[seconds] — specifies the maximum amount of time that a representation will be considered fresh. Similar to Expires, this directive is relative to the time of the request, rather than absolute. [seconds] is the number of seconds from the time of the request you wish the representation to be fresh for.

s-maxage=[seconds] — similar to max-age, except that it only applies to shared (e.g., proxy) caches.

public — marks authenticated responses as cacheable; normally, if HTTP authentication is required, responses are automatically private.

private — allows caches that are specific to one user (e.g., in a browser) to store the response; shared caches (e.g., in a proxy) may not.

no-cache — forces caches to submit the request to the origin server for validation before releasing a cached copy, every time. This is useful to assure that authentication is respected (in combination with public), or to maintain rigid freshness, without sacrificing all of the benefits of caching.

no-store — instructs caches not to keep a copy of the representation under any conditions.

must-revalidate — tells caches that they must obey any freshness information you give them about a representation. HTTP allows caches to serve stale representations under special conditions; by specifying this header, you’re telling the cache that you want it to strictly follow your rules.

proxy-revalidate — similar to must-revalidate, except that it only applies to proxy caches.

Setting Cache-Control on Windows Azure Blobs

To set the Cache-Control on a blob you will need to get a blob reference (CloudBlockBlob) and set the CacheControl property found in the blob’s Properties. Then call the SetProperties method to persist the new
Cache-Control value.

The following code uses the Windows Azure SDK 2.1 to creates a Container. Then it creates a Block Blob and uploads a short HTML document. Once the blob exists on Windows Azure Blob Storage, it sets the Cache-Control.

public void Execute()
{
    var blob = MakeCloudBlockBlob();

    var htmlStream = MakeHtmlStream();

    blob.UploadFromStream(htmlStream);

    var cacheControl = "max-age=3600, must-revalidate";

    blob.Properties.CacheControl = cacheControl;
    blob.SetProperties();
}

private static CloudBlockBlob MakeCloudBlockBlob()
{
    var connectionString = "DefaultEndpointsProtocol=https;AccountName=<account>;AccountKey=<key>";
    var account = CloudStorageAccount.Parse(connectionString);

    var client = account.CreateCloudBlobClient();

    var container = client.GetContainerReference("blobs");
    container.CreateIfNotExists();

    var blob = container.GetBlockBlobReference("test.html");
    return blob;
}

private static MemoryStream MakeHtmlStream()
{
    var htmlStream = new MemoryStream();
            
    WriteToStream(htmlStream);

    htmlStream.Position = 0;
    return htmlStream;
}

private static void WriteToStream(MemoryStream htmlStream)
{
    var writer = new StreamWriter(htmlStream);
    writer.Write("<html><body>this is a test</body></html>");
    writer.Flush();
}

 

The Results

The code will generate a Blob with the following properties.

8-11-2013 4-36-39 PM

The following is an HTTP Trace of a subsequent request for the blob. This request results in a HTTP Status Code 304 Not Modified. This effectively confirms that the resource is taken from the client’s local cache and that it is not downloaded from the server. Consequently, greatly enhancing the end-user’s experience by reducing the amount of bandwidth necessary to satisfy the consumers needs.

If the client has performed a conditional GET request and access is allowed, but the document has not been modified, the server SHOULD respond with this status code. The 304 response MUST NOT contain a message-body, and thus is always terminated by the first empty line after the header fields.

8-11-2013 4-15-11 PM

About these ads

5 responses to Save Money by Setting Cache-Control on Windows Azure Blobs

Trackbacks and Pingbacks:

  1. Azure Blob, caching a json file not working - Windows Azure Blog - August 11, 2013

    […] Which is exactly the same as a sample over here Sample of setting Cache-Control […]

    Like this

  2. Microsoft TechNet - August 21, 2013

    Windows Azure-Tipp: Geringere Bandbreite durch Cache-Control

    Die monatlichen Kosten für Windows Azure-Lösungen hängen u.a. von der benötigten Bandbreite ab, je höher

    Like this

  3. From my reading list #1 | Pascal Laurin - August 21, 2013

    […] Save Money by Setting Cache-Control on Windows Azure Blobs by Alexandre Brisebois Nice to know about how to save bandwidth when using Blob storage. […]

    Like this

  4. TechCenter - Blog - September 4, 2013

    Auszug – Microsoft TechNet NewsFlash 17/2013

    Hallo Community, und weiter geht es mit dem Auszug aus dem Microsoft TechNet Newsflash. Der Auszug

    Like this

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s