What to do with a PAAS that gets missing? Go FAAS (serverless functions), what else!

So this story is an easy one. I had written a simple NodeJS+Express API and some Angular page to create a small fun app I all “Youtube Guesser”. It’s all about trying to guess how many hits a random YouTube video received and it using the YouTube API to that end. Formerly it all was hosted on OpenShift v2 which just ran out of support and my API went offline. It’s just a fun coding exercise and the loss no big deal.

However I thought to myself, why not use this as an excuse to exchange the backend with serverless functions? I was sort of investigating Azure Functions for a while now as you can see from other blog posts of mine, so I went for it.

Put aside the usual debugging hassle with serverless functions which is certainly true for Azure Functions as well but recently improved, there are some other things to be aware of.

To start with, the structure of the code is significantly different simply because you start coding right “inside a function”, hence the whole notion of “serverless functions” of course. That is not the case with code like the one I used, so I had to carefully copy & replace code. That is still no big deal but running the whole thing revealed one big downside: there are no “global objects”.

That is not a big deal if your code does not depend on it. If you use frameworks like Mongoose to connect to a MongoDB more than likely there will be some global objects however. In my case the code broke and I had to put in a conditional check like this in:

try {
youTubeVideo = mongoose.model(‘YouTubeVideo’);
} catch (error) {
youTubeVideo = mongoose.model(‘YouTubeVideo’,youTubeVideoSchema);

Only then would it all work again.

The next big catch is that serverless functions come, given the way they are procured, with widely unpredictable response times. In case of Azure Functions that is certainly the case and a cold function call (=a function not frequently called) means a hefty startup penalty. If you depend on snappy responses a clear recommendation is to keep the Function warm by pinging it every couple of minutes. Azure can cover you on that one or you use a tool outside of Azure.

Anyway, the advantages are vast – cost wise Azure Functions are very attractive with 1 MM calls free no matter of your subscription tier and the team behind Functions has done a great job of supporting a whole lot of programming languages. Deployments are easy, use a GIT repository that comes “with” your Function project or GitHub or some other options if those are not good enough for you. Integration with Application Insights is an appealing option, of course you can and probably should for professional usage scenarios consider hooking up your Functions with API Management.

Could this have worked with AWS Lambda and AWS API Gateway? Certainly. Google Cloud Functions? I guess so, have not tried it yet and it still not generally available anyway (October 2017). The principles behind serverless functions are similar enough either way, so porting that piece of Node code to another platform is not painful.

I do not feel the urge to do that however, the backend was successfully replaced. Want to guess the counts of some YouTube videos? Be my guest and try it now. (Only the web page is in a Google Cloud bucket, all data is fed in via Azure Functions reading from a Mongo DB that does not sit on Azure. Yeah it is an interesting setup I know, should have migrated the DB into CosmosDB but then again, point proven and other things waiting so – nah.)