A Journey of Building Faster Instagram

Python Optimization using Open Source tools

Jimmy Lai

Case Study Django Projects Infrastructure Performance Python 3

Instagram built the backend service using Python Django framework. As we grew as a billion monthly active user service, the performance issue became critical to us. The demand of server comes from not only user growth but also product feature development. To tackle the performance issue systematically, we studied different metrics and proposed users per server and CPU instruction to measure system efficiency. We used cprofile to profile our system, identify bottlenecks and optimize them.
Based on the aspect of optimization, we categorized them as different categories:

* CPython upgrade: new version of CPython always includes some optimization. By upgrading from Python 2 to Python 3, we got 12% CPU saving.
* Faster Python best practices: Python is flexible and we could do the same thing in many different ways. Some of the best practices are much faster than the others. E.g. f-string for string formatting and dict literal for dict construction.
* Faster implementation: Many 3rd party libraries have built to optimize repetitive tasks for Web application. E.g. ujson for json serialization.
* Compilation with Cython: Add ctype to Python code and compile with Cython to gain CPU for core modules.
* C API implementation: Many CPython standard library are implemented in Python. We rewrote some critical library (e.g. asyncio) in C API to gain significant win.

By working on those different optimizations, we were able to deliver wins continuously and made our application faster every year.

Type: Talk (30 mins); Python level: Intermediate; Domain level: Beginner

Jimmy Lai

Instagram

Jimmy Lai is a Software Engineer in Instagram Infrastructure. He developed in Python and shared his experiences in PyCon TW and APAC since 2012. His recent interests include Python efficiency, profiling, optimization and asyncio. He also contributed a few asyncio bug fix and optimation to CPython. This year, he plans to share his efficiency experiences learned from large scale Python web application - Instagram.