- In our constant quest to prioritize efficiency, Instagram engineers have developed a way to process new videos that reduces the cost of producing basic video encodings by 94%.
- With this method in place, Meta’s video infrastructure can continue to scale without needing to add more machines.
- This frees up resources so more people can watch advanced encodings, which provide clearer, smoother video. This is especially beneficial for people in countries that have slower internet connections.
Instagram’s growing user base of over 2 billion monthly active users requires us to get the best possible performance from our server fleet. At the start of 2021, we did projections that showed that within 12 months we wouldn’t have enough capacity to provide video downloads for everyone. But in our never-ending quest to prioritize efficiency, we’ve discovered a way to manage this growing demand and scale our infrastructure by doing more with the machines we already have.
Instagram creates multiple encoded versions of uploaded videos, each with different characteristics. By reallocating one video encoding type to help generate another, we reduced the compute resources we spend on less-watched video encodings by 94%. With more resources available, we can produce more advanced encodings, allowing more people to enjoy clearer video with smoother playback.
Where Instagram Spends Video Computing
We generate two types of video encoding for each video uploaded to Instagram:
- Minimum feature encodings are compatible with all Instagram clients. Their less efficient compression is easier to decode and play for older devices.
- Advanced encodings use new compression technologies for higher quality playback. In the example below, close-ups of two video frames show that we can deliver sharper detail with fewer bits (note the clarity of the video on the right versus the one on the left).
The problem was that we were spending over 80% of our resources dealing with minimal feature encodings. If we stayed on this trajectory, minimal functionality would monopolize our resources within a year. As a result, videos would start taking longer to post – or not posting at all. Our advanced encodings only covered 15% of the total watch time, and we anticipated that spending all our math on minimal feature releases would soon prevent us from providing advanced video encoding watch time.
Remove redundant workloads
Instagram creates two classes of minimal feature encodings. For each video, we generate basic adaptive bitrate (ABR) encodings – our most-watched minimum feature type. For more stable playback, customers can select the version that best matches their connection speed to avoid blocking caused by bandwidth changes – a technique called adaptive bitrate streaming.
We rarely provide progressive encodes, the other kind of minimal functionality, but we continue to produce them to maintain compatibility with older versions of the Instagram app that don’t support ABR playback.
Traditionally, we have created ABR and progressive encodings from the original file that the client uploaded to our back-end. But this process hogs computational resources: as the following terminal command shows, it takes 86.17 seconds of CPU time to transcode a 23-second video to 720p.
$ time ffmpeg -i input.mp4 -vf scale=-1:720 -c:v libx264 output.mp4 86.17s user 1.32s system 964% cpu 9.069 total
We noticed that the settings for both sets of encodings were similar. They used the same codec with only minor differences in encoding profile and preset. Then it occurred to us: we could replace our basic ABR encodings with the video frames of progressive encodings by repackaging them into an ABR compatible file structure. This would virtually eliminate the cost of generating our base ABR encodings. The following terminal command times show that it only takes 0.36 seconds to generate a manifest file and repackage the video frames into an ABR compatible file structure for the same input video.
$ time MP4Box -add input.mp4 -dash 2000 -profile dashavc264:onDemand -out manifest.mpd video_output.mp4 0.36s user 2.22s system 95% cpu 2.690 total
This approach frees up computation for producing advanced encodings, although it comes at the expense of the compression efficiency of our basic ABR encodings. Our theory was that Generating more advanced encodings would be a net benefit for people who use Instagram.
Build a framework to test our theory
We had to prove our theory before we could ship to production. If we compared the base ABR encodings before and after our change, we would only see regressions. We also needed to measure the net effect of more advanced encodings. The diagram below shows the higher watchdog time we expected for advanced encodings after releasing our basic ABR computation. This would compensate for the lower compression efficiency of the new base ABR.
To measure this, we built a test frame that replicated a small percentage of traffic on a test pool and a control pool of equal processing power. We registered the encodings of each pool in different namespaces so that we could identify them later as part of the catalog of control or test videos. Then, at the time of delivery, people would see encodings from one catalog or the other. This would allow us to measure whether the new coding scheme was better.
From this test, we proved that even though we degrade the compression efficiency of basic ABR encodings in the test pool, the higher watchtime for advanced encodings more than compensates for this.
Push to production
After running this optimization, we saw significant gains in terms of compute savings and increased watch time of advanced encoding. Our new encoding scheme has reduced the cost of generating our base ABR encodings by 94%. With more resources available, we were able to increase the overall watch time coverage of advanced encodings by 33%. This means that today more people on Instagram can enjoy clearer and smoother video. This is especially important for providing a great experience for people in countries that have slower internet connections.
There is even more technical innovation needed as Instagram’s growing user base will continue to place increasing demands on our server fleet. Stay tuned!
Over the years, Instagram has worked continuously to improve its product offerings. Given our scale – including 2 billion monthly active users on our platform and over 140 billion Reels played every day on Instagram and Facebook – our work can have a huge impact. If you are interested, join us!
Thanks to Haixia Shi for initiating the idea of this efficiency optimization. Thanks to Richard Shyong for implementing the optimization and testing framework, which allows us to measure all computational efficiency investments. Thanks to Runshen Zhu and Ang Li for the discussions that led to the investment in this perimeter. Thanks to our partners Atasay Gokkaya, Justin Li, Chia-I Wei, and Zainab Zahid for helping to test pool provisioning and video compute efficiency discussions.