Vincent Ritter

Sublime Ads - New Edge API and super mega fast embeds.

In the last few weeks I was looking at a way to improve the API for Sublime Ads. Namely I wanted to concentrate on the most commonly used endpoints which are the "random" API and the "embed" API.

They are basically, at their core, the same thing. The only difference being that one requires an API key, and the other not.

Around 2 weeks ago I made a few changes on how I'm handling "usage" on Sublime Ads which basically limits the free accounts to a certain amount of API calls. Calling the API would, each time, check if you are within the usage allowance (plus some other bits).

I tidied this up and managed to move it further up the code, so that I would only have to call one piece of code once (to check if the API key is valid and if the call is within the usage allowance)... instead of doing it three times for different parts. This dramatically improved speed by around 100ms in production... and yes... I know... dramatic. The joys of learning and improving!

However, I was still not happy about it. Whilst the API (embed & random) was super quick near my servers, it quickly became slow anywhere outside of the region. Yes, it is still quite fast, and I think most people wouldn't care, but it wasn't enough for me.

On top of that, I had a 20 minute outage this past week... rudely it happened as I was getting up for our morning routine for school. This was my first major outage. There was a minor one earlier this year, but it was DNS related - and I can't do anything about that.

Granted this was not a lot... but that was 20 minutes that also could have affected everyone else relying on the API endpoints.

This got me thinking that I need to work on some sort of backup plan, perhaps have a fallback server with a load balancer in front to distribute as needed... or in my case I could use Cloudflare's Load Balancing option also (as I am using them already for DNS and other things like static pages).

Over the past few months I got interested in their Workers offering... which basically gives me on edge computing without having to own servers, nor worry about having multiple servers dotted across the globe. This is called "serverless" if anyone is interested.

But it was all a little over my head, and I couldn't invest any time in looking into it. So instead, I continued to work on the Teams feature, which I teased and also shipped a few extra bits and bobs.

Anyway... the 20 minute downtime...

I sat down and started working on something with Workers and I was immediately sold! Not only was it super easy to develop with (it's just Javascript) but it was also immensely fast and reliable. On top of that, I had an option for edge storage too... which means every request was fast around the globe.

A few days later, and a lot of tweaking, I rewrote the embed and random API to use the edge network.

The results are mind-blowing for me...

Here is the normal embed API endpoint, with an average 430ms response time. This was around 600ms a few days before (before my API tweaks above).

As you can see, it's pretty fast close to home... but anywhere outside of that it gets pretty slow... quickly. Southern hemisphere (you know who you are) is really at a loss with nearly 1 second to get a response. This is not great.

Now let's compare the same thing, but using the edge API.

šŸ¤Æ

As you can see, we're getting a similar response times across the globe just like at home. Yes, the east few check servers still have slightly higher response times, but they are heaps better!

I couldn't be happier with the result!

To be honest, I was super close of not even looking at this solution, and rather concentrate on provisioning a new server, but I am glad I did!

Whilst I don't want to go into a more in-depth take on this, in this post, I do want to share how it gets enabled for embeds.

Embeds on edge

All accounts will be able to use the edge API simply by just using the embed script on their site... which means everything is working with the new edge locations as we speak. You don't need to enable it. It's an automatic process.

When you first request an embed on your site, it will check with the Sublime Ads server and return an ad for you (to display on your website). This request will have some small latency attached (but nothing more than what you've been used to already).

What happens next is that Sublime Ads will enable your edge updates and pass the relevant data to the edge location storage... we do it after the initial request because I want to make sure you're actually using it. This all happens in the background and you don't have to do anything extra. In fact, if you're using embeds right now... I know that you're already using the edge locations šŸ˜‹

During testing I found that it takes around 5 - 60 seconds for edge data to become available/update. So please bear with it initially if it's your first time.

In addition to that, all updates, new ads or categories, all trigger an update to the edge. This also works for scheduled ads and works as before.

Again, you have to make zero changes to enable this. The embed script has been updated to use the new url (which actually is near identical to what it was - thank you Cloudflare - I'll write about how that works in the coming week).

So... to recap...

Embeds are super fast now! Not "scary" fast, but super duper mega fast.

Oh... and if there is downtime on the main server... they'll keep working regardless.

Embed images

With everything so fast, I saw that images were not cached... so they took some time to load. No more! They are now cached so they load extra fast across Cloudflare's network.

I'm hoping for more improvements for images over the coming months.

What about the API?

Ah yes, with the above I have been working on a new API to replace the current one for some parts of it.

I'll announce more soon, but for now we already have embeds sorted and there will also be updates to the "random" API that you're used to. In fact, it's already working in my dev environment but not quite ready for production.

I don't want to migrate the full API over to using Cloudflare Workers... I'd rather have some highly available things like embeds running through there. So this is a happy balance for me.

I'll be updating the documentation soon.

Conclusion

I'm super happy with where it's at now and I can see myself relying much more on "serverless" down the line for many other things.

For now I am happy to have done this to provide the best possible reliability and service for everyone using Sublime Ads.

More posts soon about this very topic and hope to have a few more features before the end of year.