While working on an assignment, we came across an urgent task from the customer where we need to fix a performance issue for the YouTube API integration.

What is it?

The Rails app we received had a YouTube integration using yt gem, using which we need to search YouTube videos and fetch metadata – content details, statistics, status, id etc.

The current implementation was done in such a manner that it takes 12–15 seconds to search and find all metadata for 12 videos which is really huge time. We need to reduce this to the limits as per the standards such that it can fetch and render the results in 1–3 seconds at max!

Understand the path followed to identify the issue

What it YT Gem?

YT gem is a ruby client for YouTube API, which is a wrapper around YouTube API. As per the documentation, they claim that it is better than the original google-api-ruby-client because ruby client is more generic and YT has been coded better!

Debug Debug Debug …

So the first step would be to setup the application, run it and reproduce the issue.

We found that when it throws the search API call, it usually taken 10–12 seconds because there are certain API calls which are sent for each video!

So, the flow is as following,

  1. Search the videos using search term and collect the video ids.
  2. Because we need to find meta data which was somehow not available in the original search call, it has called an API for each video ID to load the meta data. And this was the bottleneck!

So, how to fix?

First of all, We went through the documentation of the YT gem and YouTube APIs and confirmed that what we are trying to fetch is supported by both of them.

We found that though the previous developer doubted that YT gem does not support fetching multiple videos in one API call, it is supported! Based on that we have to fix the code which is using the loop as of now and just make a single API call!

See the code below which has been fixed,

After this change, the performance of this method has been improved almost by 85-90%! It was taking 10–12 seconds for searching videos, which has been now reduced to 1–1.3 seconds only!

Sometimes we may miss a very small point, but fixing it could affect the overall flow or performance in a very different manner!

Click here for more details…

At BoTree Technologies, we build enterprise applications with our RoR team of 25+ engineers.

We also specialize in Python, RPA, AI, Django, JavaScript and ReactJS.

Consulting is free – let us help you grow!