Transatlantic Facebook application performance woes

Someone I follow on Twitter reported having problems getting a Facebook application to perform. I don’t know what they are doing so this post is just guessing at their problem, but the fact is that — if you’re not paying attention — you can easily shoot yourself in the foot when building and deploying Facebook apps. The diagram below depicts a random fbml Facebook app deployed to a server located in Denmark being used by a user also situated in Denmark. Note that Facebook doesn’t yet have a datacenter in Europe (they have one on each coast in the US).


The following exchange takes place:

  1. User requests some page related to the application from Facebook
  2. Facebook realizes that serving this request requires querying the application and sends a request for fbml to the app
  3. The app gets the request and decides that in order to respond, it has to query the Facebook API for further info
  4. The Facebook API responds to the query
  5. The application uses the query results and the original request to create a fbml response that is sent to Facebook
  6. Facebook gets the fbml, validates it and macroexpand various fbml tags
  7. Facebook sends the complete page to the user

… so that adds up 6 transatlantic requests pr. page requested by the user. Assuming a 250ms ping time from the Danish app-server to the Facebook datacenter this is a whopping 1.5s latency on top of whatever processing time your server needs AND the time taken by Facebook to process your API request and validate your fbml.

So what do you do? Usually steps 3 and 4 can be eliminated through careful use of fbml and taking advantage of the fact that Facebook includes the ids of all the requesting users friends. Going for an iframe app is also helpful because it eliminates one transatlantic roundtrip and spares Facebook from having to validate any fbml. A very effective measure if you insist on fbml, is simply getting a server stateside — preferably someplace with low ping times to Facebook datacenters. There are plenty of cheap hosting options around, Joyent will even do it for free (I’m not affiliated in any way).

Webcam face detection in C# using Emgu CV

Some time ago I wrote a post on how to do face detection in C# using OpenCV. I’ve since begun using the Emgu CV wrapper instead of opencvdotnet. Emgu CV is much better, in active development and it even runs on Mono. Two gotchas:

  1. You don’t have to install OpenCV, but instead have to copy the relevant dlls (included with the Emgu CV download) to the folder where you code executes.
  2. Open CV and X64 are not friends. If you’re running X64 Windows (and unless you are up to recompiling OpenCV) you have to make sure your app is compiled to X86, instead of the usual “Any CPU”.
  3. Remember to add PictureBox as per the original tutorial.

Here’s sample code:

using System;
using System.Windows.Forms;
using System.Drawing;
using Emgu.CV;
using Emgu.Util;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;

namespace opencvtut
    public partial class Form1 : Form
		private Capture cap;
		private HaarCascade haar;

        public Form1()

        private void timer1_Tick(object sender, EventArgs e)
		using (Image<Bgr, byte> nextFrame = cap.QueryFrame())
			if (nextFrame != null)
				// there's only one channel (greyscale), hence the zero index
				//var faces = nextFrame.DetectHaarCascade(haar)[0];
				Image<Gray, byte> grayframe = nextFrame.Convert<Gray, byte>();
				var faces =
						haar, 1.4, 4,
						new Size(nextFrame.Width/8, nextFrame.Height/8)

				foreach (var face in faces)
					nextFrame.Draw(face.rect, new Bgr(0,double.MaxValue,0), 3);
				pictureBox1.Image = nextFrame.ToBitmap();

        private void Form1_Load(object sender, EventArgs e)
            // passing 0 gets zeroth webcam
			cap = new Capture(0);
            // adjust path to find your xml
			haar = new HaarCascade(

LinqtoCRM and updating entities

There are some pitfalls when retrieving CRM entities with LinqtoCRM and trying to update them through the CRM web service. The most intuitive (but wrong) approach would be this:

var res = from c in p.Linq()
		  select c;

foreach (var con in res)
	con.address1_line1 = "foo";

This fails unfortunately. I think someone at Netcompany (my former employer) worked out why this was at some point, but I’ve forgotten.

Instead what you want to do it is new up new entities yourself while setting the relevant id attribute, and then updating the attributes you want to change:

var res = from c in p.Linq()
		  select new contact() { contactid = c.contactid };

foreach (var con in res)
	con.address1_line1 = "foo";

LinqtoCRM competitor and new version

A few days, a former collegue alerted me to xRM LINQ, a new commercial query provider for Microsoft CRM. I’ve downloaded the trial, of course, and it looks pretty good. xRM LINQ decided not to use usual web service classes and instead provide their own class generator/entity mapper (LinqtoCRM has one too, but only for generating many-to-many classes). This means you can’t mix and match Linq with traditional web service calls, and they had to implement their own create/update functionality. It’s a less gradual and more comprehensive approach than LinqtoCRM but it may give a smoother experience for the programmer. At any rate, I welcome xRM LINQ onto the CRM query provider stage and wish them the best of luck :-).

A less welcome addition is a company called Softpedia, a Romanian outfit. I won’t link to them, to avoid giving them any more Google Juice, but you can find them by googling LinqtoCRM. They seem to be screen-scraping CodePlex and similar sites for projects with permissive licenses and then put up copy-cat pages with downloads for these project on their own site. While not illegal, it’s not very useful for project owners or users either. They’ve been caught inflating their Wikipedia article and many user report trojans and similar on siteadvisor (to be fair, this seems to happen for other popular download sites too).

In other news, a new version of LinqtoCRM is out. It fixes some bugs that have surfaced over the last few months. I’ve also reorganised the wiki, hopefully making it easier for people to find what they’re looking for.

Found your start-up in Hong Kong?

I’ve just returned from a trip to Hong Kong. While there, I toured several startup parks and incubators and talked to a lot of entrepreneurs and some government officials. I think it just may be a pretty cool place to found your tech startup. Read on for reasons why.

In the fall (of 2008) I won a trip to Hong Kong by submitting a business idea on the back of a napkin to a competition run be the Øresund Entrepreneurship Academy. You can read more about the competition and my winning it here (including a picture of me holding a bouquet of flowers, a rare and uncommon sight). While I agree that sending more-or-less random people halfway around the world is a rather dubious use of taxpayer money, I was hard pressed to complain and dutifully went along.

I’ll start off with an interesting fact: Hong Kong has been an administrative region of the Peoples Republic of China since 1997, yet for the past 15 consecutive years it has been named the freest economy in the world by the Heritage Foundation. How do you like that, an area under the nominal thumb of communist China is on the top of a list published by a conservative American Think-tank? And it’s not the one of countries to invade next — I think it’s great!

The explanation for this wonderful paradox is that Hong Kong is administered under the “one country, two systems” regime. So while the Peoples Liberation Army diligently liberated Hong Kong after the British left, they limited themselves to doing just that, and have been holed up in their barracks ever since. Hong Kong is thus still governed under the principle of “Positive non-interventionism” formalized under John James Cowperthwaite, the colony’s financial secretary in the ‘60s. Some consequences of interest to entrepreneurs are:

  • Taxes are very low, with corporate tax at 16.5% and income taxes capped at 15% (most pay much less).
  • There is no value-added tax or sales tax.
  • There are no tariffs or customs on any imports, including wine and spirits.
  • Registering a limited liability company is easy and costs about 300 USD.
  • There are no controls on capital flows so you are free to brings investment in and take profits out.
  • The local currency is tied to the US Dollar so you run no currency risks if you are from that country.
  • There’s a strong and independent common-law based judiciary which strictly enforces IP rights.

Old Milton Friedman was a big fan of these policies (which brought the Hong Kong per capita GDP from 28% to 137% of Britain’s between 1960 to 1996) and wrote a great article for National Review in 1997.

China has promised that Hong Kong can shape its own policies for at least fifty years after the takeover, leaving another 37 years of laissez faire. The current Hong Kong political system has some democratic traits, but business interests are generally much more prominent than in Western-style democracies (I’m not saying that’s a good thing, just stating a fact). Freedom of Speech is respected and the government officials we talked to (from the Hong Kong Trade Development Council and Invest in Hong Kong) were very forthright and mostly positive in their estimates of Mainland Chinese intentions. The foreign officials we met (the Danish and Swedish general consuls) were slightly more cautious, but the general consensus seems to be that China is unlikely to mess with Hong Kong if for no other reasons than because the city is such an important conduit of goods and services to and from the mainland (Port of Hong Kong is the third largest in the world by container throughput). Hong Kong is also useful as a demonstration to Taiwan that it is now safe to return to The Motherland. A good example of the two systems at work is the recent cancellation of Oasis concerts in Beijing and Shanghai, apparently because Noel Gallagher played at a Free Tibet event in 1997. The concert in Hong Kong is still on.

Our itinerary included visits to two incubators operated by Hong Kong Science & Technology Parks. The Science Park is particularly impressive, newly built and stretching over 22 hectares of seaside property with shared IC labs and wet-labs should you need them. The other one is the InnoCentre, which focuses more in design startups. The programs at both incubators feature heavily subsidized rent, it’s free for the first year and then ramps up until the program ends in two to three years. Programs include financial aid packages to the tune of about 100,000 USD which can be used to cover non-recurring operational costs. The admission criteria are not onerous, other than your business idea having to pass several panels judging soundness and profitability. In particular, the incubators are open to foreign nationals registering their companies in Hong Kong, as long as they plan to hire local staff. We met a Swede and a Brit who had set up shop in Hong Kong and looked pretty chuffed. Whether you like government meddling with start-ups or not, these incubators just seemed very no-nonsense and well-thought out.

Hong Kong has a young, well-educated and tech-savvy population with most people using at least two mobile phones for work and private use respectively. In the MTR (Subway/Metro/Underground), which has excellent connectivity, you’ll see everyone punching away at iPhones and Blackberrys. There’s a more-or-less citywide wifi provided by either telcos or freely by the government and broadband is widely available. The transport infrastructure is ruthlessly efficient: The MTR will take you most places you want to go in air conditioned, escalated comfort and to top that off there’s a profusion of busses, trams, ferries and escalators. The airport has frequent flights to most places in Asia and abroad. Most Hong Kongers are immigrants or refugees (or descendants thereof) who have fled the excesses of various mainland governments. They’re self-reliant, industrious and hardworking. English knowledge is still widespread and many schools teach English as the first language.

While it lacks a good venture capital and business angel community, Hong Hong has excellent financial institutions. The Hong Kong Stock Exchange is the second biggest in the world in terms of IPO value. Asian banks have lower exposure to the global financial crisis because the buttoned down somewhat after the Asian financial crisis in the late ‘90s. Asia is traditionally a saving economy where people tend save up money before they go an buy stuff, as opposed to taking out a mortgage straight away. I’m not implying mortgages are bad, this is just to say that there’s a lot of money hidden away in bank accounts and mattresses in Hong Kong and the rest of Asia. Chinese banks, indeed, are lending freely now, as this Economist article details.

Other than rather steep housing costs, Hong Kong is a pretty cheap place to live. Transport is cheap and a good meal, with drinks, can be had for less than 10 USD. A live-in maid working six days a week is less than 1000 USD a month. For fun, you can go to the horse races or take the boat to Macau to gamble or just look at the lights. Somewhat surprisingly, Hong Kong has pretty good hiking, including the 100km MacLehose Trail. There are also lots of swimmable beaches dotted around the islands. Pollution can be bad in the built up areas, but I found it be no worse than Manhattan, say. Hong Kong is extremely safe, with a crime-rate that is lower than most large cities.

Is Hong Kong a good place to found your tech start-up? I’m certainly contemplating it: Taxes are low, it’s very livable and there’s robust government support for high-tech entrepreneurs.

Here is some recent related discussion:

Here are the startups we visited while in Hong Kong (thanks for having us!):