Poor man's sampling

Written on September 18, 2017

I recently had the challenge to just load a JavaScript library for only 0.10% of our actual website traffic. That’s 1 out of 1,000 page views.

The library I’m referring to is part of a very popular performance monitoring SaaS product. Their pricing model is tight to actual invocations and therefore our costs were skyrocketing with an increased usage of their product.

Given a ton of traffic, all we really needed was a fraction of those page views to actually make it there, without losing any relevant insights. A textbook use case for sampling.

On top of cost savings, this also meant that we significantly improved the load time for the vast majority of page views by not having to load an additional dependency. I call this a win-win!

The “poor man’s sampling” JavaScript code, turned out to be rather trivial:

var x = 1000;
var r = Math.ceil(Math.random()*x); // random number between 1 and x
if (r === 1) {
  // do it, but only 1 out of x times

I’m sharing this story because I believe that sampling can be easy and at the same time extremely powerful. Let me know if this inspired you to implement some “poor man’s sampling” yourself.

Martin Buberl

Purveyor of Internet duct tape.
If you'd like to get in touch, feel free to shout @martinbuberl.