Entries from April 2010 ↓
April 18th, 2010 — Programming, Web 2.0
I needed to use a CDN in my ASP MVC app and needed it to be easy to develop against. I chose Amazon CloudFront since our servers are running on Amazon EC2 and we are also using Amazon S3 during our deployment. You can get a good introduction and step-by-step on CloudFront here.
As for the ASP MVC side, I wanted to have something like Url.Content where it converts my URL to my CDN URL while allowing for multiple development environments.
Here is how I did it:
A helper extension class on System.Web.Mvc.UrlHelper to add a new method to Url: This one is CdnContent
Here is an implementation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Web.Mvc;
using System.Configuration;
namespace CdnHelpers
{
public static class CdnHelpers
{
private static AppEnvironment _appEnvironment = AppEnvironment.Unknown;
private static string _cdnBase = string.Empty;
private static AppEnvironment AppEnvironment
{
get
{
if (_appEnvironment == AppEnvironment.Unknown)
{
_appEnvironment =
(AppEnvironment)
Enum.Parse(typeof (AppEnvironment), ConfigurationManager.AppSettings["App.Environment"], true);
}
return _appEnvironment;
}
}
private static string CdnBase
{
get
{
if (string.IsNullOrEmpty(_cdnBase))
{
_cdnBase = ConfigurationManager.AppSettings["Cdn.Base"];
}
return _cdnBase;
}
}
public static string CdsContent(this System.Web.Mvc.UrlHelper url, string contentPath)
{
if (AppEnvironment == AppEnvironment.Local)
{
return url.Content(contentPath);
}
else
{
contentPath = contentPath.TrimStart('~');
return string.Concat(CdnBase, "/", AppEnvironment.ToString().ToLower(), contentPath);
}
}
}
}
AppEnvironment is an Enum:
public enum AppEnvironment
{
Unknown,
Local,
Test,
Dev,
Staging,
Production
}
You need to add two keys to AppSettings of the Web.config:
App.Environment to determine which environment the code is running as and
Cdn.Base which would be something like http://cache.mydomain.com (no trailing slash)
Also add the namespace of the helper class to System.Web/Pages/Namespaces under Web.config
This should be it. Now you can replace a Url.Content with Url.CdnContent in the code.
I didn’t want to redirect all files by their extension to CDN but that’s not difficult to implement.
On deployment to Amazon:
- Amazon CDN files and folders are case sensitive.
- Create folders with the name of your environments under the root of the S3 bucket and deploy the files and folders there.
April 18th, 2010 — Programming
At my company, we are building a fantastic personal search engine (still in private beta. Stay tuned!). As part of our architecture, we needed to cater for scalability and redundancy from the very beginning, so we went for cloud hosted services. We wanted to be able to deploy any of the layers of the system within 15 minutes to cope with the load. Our system is running on Windows OS and is written in .NET 3.5. We tried GoGrid, some smaller Hyper-V VPS providers, Windows Azure and Amazon EC2.
I wrote some time ago about what I think about Azure at this point in time. Since then we tried standard Windows OS cloud providers and haven’t had time to play with Azure again. I’m sure it is going to improve. However, the acquisition of Atebits (the guys behind Tweetie) by Twitter made me think about Microsoft and Amazon again.
Building companies on Microsoft technologies is a risk. You are exposed to Microsoft going bust, changing its direction, etc. But how likely is that? As far as a small business is concerned, negligible. You can safely build a small/medium business and be exposed to Microsoft. Amazon is smaller than Microsoft but for me it is big enough. So I don’t worry about exposing myself to Amazon going bust. But there is one difference between building my business around Azure and building it around Amazon EC2: Lock-in.
Azure locks me in. I need to make changes to my product for it to work in Azure. Amazon EC2 on the other hand is just commoditized Windows Server horsepower. If Amazon decides to raise its prices to a level that is not affordable to me, I can switch to another dozen providers giving me same stuff. And that’s a big problem as your business gets bigger.
The other day some people from a big data integration company were giving us a presentation about their middle-tier systems. The said their system can run on Amazon EC2 and the CTO of the company wasn’t sure if he wants to build exposure against Amazon. I see it another way though: Being exposed to Amazon in this way is the same as being exposed to your ISP. They can go down and take you out of business for a week. But you CAN recover within a reasoable amount of time. Building my business around Azure isn’t the same.
Risk = Probablity x Impact
The probablity of Microsoft making breaking changes to Azure is very low but the impact is very high. The probablity of GoGrid going bust is much higher, but the impact is very low (I can switch or have a backup on another cloud provider).
As it stands, running your business on Azure is like building a business around Twitter. This can change if Azure changes to a more commoditised computation power provider or allows other companies to provide Azure as a service.