SlideShare ist ein Scribd-Unternehmen logo
1 von 85
Downloaden Sie, um offline zu lesen
PYTHON@INSTAGRAM
Hui Ding & Lisa Guo
May 20, 2017
@TechCrunch
STORIES DIRECT LIVE EXPLORE
“INSTAGRAM, WHAT THE HECK
ARE YOU DOING AT PYCON?”
CIRCA PYCON 2015 (MONTREAL)
“HOW DO YOU RUN DJANGO+PYTHON
AT THAT SCALE?”
“HOW DO YOU RUN DJANGO+PYTHON
AT THAT SCALE?”
"WHY HAVEN’T YOU RE-WRITTEN
EVERYTHING IN NODE.JS YET?"
WHY DID INSTAGRAM

CHOOSE PYTHON?
AND HERE’S WHAT THEY FOUND OUT:
“Friends don’t let friends use RoR”
THINGS INSTAGRAM LOVES ABOUT PYTHON
Easy to become productive
Practicality
Easy to grow

engineering team
Popular language
THINGS INSTAGRAM LOVES ABOUT DJANGO
Maturity of the
language and
Django framework
Use Django user
model for
supporting 3B+
registered users
WE HAVE TAKEN OUR PYTHON+DJANGO STACK QUITE FAR
1 Sharded database support
2
Run our stack across multiple geographically distributed data centers3
Disable garbage-collection to improve memory utilization
WE HAVE TAKEN OUR PYTHON+DJANGO STACK QUITE FAR
WE HAVE TAKEN OUR PYTHON+DJANGO STACK QUITE FAR
PYTHON IS SIMPLE AND CLEAN, AND FAVORS PRAGMATISM.
1 Scope the problem, AKA, do simple things first
2 Use proven technology
3 User first: focus on adding value to user facing features
Subtitle
AT INSTAGRAM, OUR BOTTLENECK IS

DEVELOPMENT VELOCITY, 

NOT PURE CODE EXECUTION
BUT PYTHON IS STILL SLOW, RIGHT?
SCALING PYTHON TO SUPPORT USER AND FEATURE GROWTH
20
40
60
80
00
0 2 4 6 8 10 12 14 16 18 20 22 24Server growthUser growth
PYTHON EFFICIENCY STRATEGY
1 Build extensive tools to profile and understand perf bottleneck
2 Moving stable, critical components to C/C++, e.g., memcached access
3
Async? New python runtime?4
Cythonization
ROAD TO PYTHON 3
1 Motivation
2 Strategy
3 Challenges
4 Resolution
MOTIVATION
def compose_from_max_id(max_id):
‘’’ @param str max_id ’’’
MOTIVATION: DEV VELOCITY
MOTIVATION: PERFORMANCE
uWSGI/web async tier/celery
media storage
user/media
metadata
search/ranking
Python
MOTIVATION: PERFORMANCE
N processes
M CPU cores
N >> M
Request
MOTIVATION: PERFORMANCE
N processes
M CPU cores
N == M
Request
MOTIVATION: COMMUNITY
STRATEGIES
SERVICE DOWNTIME
SERVICE DOWNTIME
PRODUCT SLOWDOWN
MASTER
LIVE
DEVELOP/TEST/DOGFOOD
Create separate branch?
MIGRATION OPTIONS
• Branch sync overhead, error prone
• Merging back will be a risk
• Lose the opportunity to educate
MIGRATION OPTIONS
One endpoint at a time?Create separate branch?
• Common modules across end points
• Context switch for developers
• Overhead of managing separate pools
MIGRATION OPTIONS
One endpoint at a time?Create separate branch? Micro services?
• Massive code restructuring
• Higher latency
• Deployment complexity
MIGRATION OPTIONS
One endpoint at a time?Create separate branch? Micro services?
MAKE MASTER COMPATIBLE
MASTER
PYTHON3
PYTHON2
Third-party packages

3-4 months
Codemod

2-3 months
Unit tests

2 months
Production rollout

4 months
THIRD-PARTY PACKAGES
Rule: No Python3, no new package
Rule: No Python3, no new package
Delete unused, incompatible packages
twisted
django-paging
django-sentry
django-templatetag-sugar
dnspython
enum34
hiredis
httplib2
ipaddr
jsonfig
pyapns
phpserialize
python-memcached
thrift
THIRD-PARTY PACKAGES
Upgraded packages
Rule: No Python3, no new package
Delete unused, incompatible packages
THIRD-PARTY PACKAGES
CODEMOD
modernize -f libmodernize.fixes.fix_filter <dir> -w -n
raise, metaclass, methodattr, next, funcattr, library renaming, range,
maxint/maxsize, filter->list comprehension, long integer, itertools,
tuple unpacking in method, cython, urllib parse, StringiO, context
mgr/decorator, ipaddr, cmp, except, nested, dict iter fixes, mock
Failed
include_list: passed tests
Passed
UNIT TESTS
FailedPassed
UNIT TESTS
exclude_list: failed tests
FailedPassed
UNIT TESTS
• Not 100% code coverage
• Many external services have mocks
• Data compatibility issues typically do not show up in unit tests
UNIT TESTS: LIMITS
100%
20%
0.1%
EMPLOYEES
DEVELOPERS
ROLLOUT
100%
20%
0.1%
EMPLOYEES
DEVELOPERS
ROLLOUT
CHALLENGES
CHALLENGES
1 Unicode
2 Data format incompatible
3
4 Dictionary ordering
Iterator
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
from __future__ import unicode_literals
CHALLENGE: UNICODE/STR/BYTES
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
from __future__ import unicode_literals
CHALLENGE: UNICODE/STR/BYTES
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
from __future__ import unicode_literals
CHALLENGE: UNICODE/STR/BYTES
mymac = hmac.new(‘abc’)
TypeError: key: expected bytes or bytearray, but got 'str'
value = ‘abc’
if isinstance(value, six.text_type):
value = value.encode(encoding=‘utf-8’)
mymac = hmac.new(value)
Error
Fix
CHALLENGE: UNICODE/STR/BYTES
ensure_binary()
ensure_str()
ensure_text()
mymac = hmac.new(ensure_binary(‘abc’))
Helper functions
Fix
CHALLENGE: PICKLE
memcache_data = pickle.dumps(data, pickle.HIGHEST_PROTOCOL)
data = pickle.load(memcache_data)
Write
Read
Memcache
Me
Python3
Others
Python2
CHALLENGE: PICKLE
Memcache
Me
Python3
Others
Python2
memcache_data = pickle.dumps(data, pickle.HIGHEST_PROTOCOL)
data = pickle.load(memcache_data)
Write
Read
4
ValueError: unsupported pickle protocol: 4
2
CHALLENGE: PICKLE
pickle.dumps({'a': '爱'}, 2) UnicodeDecodeError: 'ascii' codec
can’t decode byte 0xe9 in position
0: ordinal not in range(128)
memcache_data = pickle.dumps(data, 2)
Write
Python2 writes Python3 reads
pickle.dumps({'a': '爱'}, 2){u'a': u'u7231'} != {'a': '爱'}
Python2 reads Python3 writes
memcache_data = pickle.dumps(data, 2)
Write
CHALLENGE: PICKLE
CHALLENGE: PICKLE
Memcache
Python3
Python2
4
Memcache
2
map()
filter()
dict.items()
1 CYTHON_SOURCES = [a.pyx, b.pyx, c.pyx]
2 builds = map(BuildProcess, CYTHON_SOURCES)
3 while any(not build.done() for build in builds):
4 pending = [build for build in builds if not build.started()]
<do some work>
CHALLENGE: ITERATOR
1 CYTHON_SOURCES = [a.pyx, b.pyx, c.pyx]
2 builds = map(BuildProcess, CYTHON_SOURCES)
3 while any(not build.done() for build in builds):
4 pending = [build for build in builds if not build.started()]
<do some work>
CHALLENGE: ITERATOR
1 CYTHON_SOURCES = [a.pyx, b.pyx, c.pyx]
2 builds = map(BuildProcess, CYTHON_SOURCES)
3 while any(not build.done() for build in builds):
4 pending = [build for build in builds if not build.started()]
<do some work>
CHALLENGE: ITERATOR
1 CYTHON_SOURCES = [a.pyx, b.pyx, c.pyx]
2 builds = list(map(BuildProcess, CYTHON_SOURCES))
3 while any(not build.done() for build in builds):
4 pending = [build for build in builds if not build.started()]
<do some work>
CHALLENGE: ITERATOR
'{"a": 1, "c": 3, "b": 2}'
CHALLENGE: DICTIONARY ORDER
Python2
Python3.5.1
>>> testdict = {'a': 1, 'b': 2, 'c': 3}
>>> json.dumps(testdict)
Python3.6
Cross version
'{"c": 3, "b": 2, "a": 1}'
'{"c": 3, "a": 1, "b": 2}'
'{"a": 1, "b": 2, "c": 3}'
>>> json.dumps(testdict, sort_keys=True)
'{"a": 1, "b": 2, "c": 3}'
ALMOST THERE...
CPU instructions per request
max Requests Per Second
-12%
0%
Memory configuration difference?
12%
if uwsgi.opt.get('optimize_mem', None) == 'True':
optimize_mem()
b
RESOLUTION
FEB 2017
PYTHON3
PYTHON2
Saving of 30%

(on celery)
INSTAGRAM ON PYTHON3
Saving of 12%

(on uwsgi/django)
CPU MEMORY
June 2016
Python3 migration
Sept 2015 Dec 2016 Apr 2017
400M
500M 600M
Video View Notification
Save Draft
Comment Filtering
Story Viewer Ranking
First Story Notification
Self-harm Prevention
Live
MOTIVATION: TYPE HINTS
Type hints - 2% done
def compose_from_max_id(max_id: Optional[str]) -> Optional[str]:
MyPy and typeshed contribution
Tooling - collect data and suggest type hints
MOTIVATION: ASYNC IO
Asynchronize web framework
Parallel network access within a request
MOTIVATION: COMMUNITY
Benchmark web workload
Run time, memory profiling, etc
Pycon2017 instagram keynote
Pycon2017 instagram keynote

Weitere ähnliche Inhalte

Ähnlich wie Pycon2017 instagram keynote

Streaming Trend Discovery: Real-Time Discovery in a Sea of Events with Scott ...
Streaming Trend Discovery: Real-Time Discovery in a Sea of Events with Scott ...Streaming Trend Discovery: Real-Time Discovery in a Sea of Events with Scott ...
Streaming Trend Discovery: Real-Time Discovery in a Sea of Events with Scott ...
Databricks
 

Ähnlich wie Pycon2017 instagram keynote (20)

Open source projects with python
Open source projects with pythonOpen source projects with python
Open source projects with python
 
Easy contributable internationalization process with Sphinx @ PyCon APAC 2016
Easy contributable internationalization process with Sphinx @ PyCon APAC 2016Easy contributable internationalization process with Sphinx @ PyCon APAC 2016
Easy contributable internationalization process with Sphinx @ PyCon APAC 2016
 
Streaming Trend Discovery: Real-Time Discovery in a Sea of Events with Scott ...
Streaming Trend Discovery: Real-Time Discovery in a Sea of Events with Scott ...Streaming Trend Discovery: Real-Time Discovery in a Sea of Events with Scott ...
Streaming Trend Discovery: Real-Time Discovery in a Sea of Events with Scott ...
 
JS Fest 2018. Никита Галкин. Микросервисная архитектура с переиспользуемыми к...
JS Fest 2018. Никита Галкин. Микросервисная архитектура с переиспользуемыми к...JS Fest 2018. Никита Галкин. Микросервисная архитектура с переиспользуемыми к...
JS Fest 2018. Никита Галкин. Микросервисная архитектура с переиспользуемыми к...
 
Practicing Python 3
Practicing Python 3Practicing Python 3
Practicing Python 3
 
Programming with Python - Basic
Programming with Python - BasicProgramming with Python - Basic
Programming with Python - Basic
 
PyCon Taiwan 2013 Tutorial
PyCon Taiwan 2013 TutorialPyCon Taiwan 2013 Tutorial
PyCon Taiwan 2013 Tutorial
 
Python. Why to learn?
Python. Why to learn?Python. Why to learn?
Python. Why to learn?
 
DevOps Practice in Nonprofit - Abdurrachman Mappuji
DevOps Practice in Nonprofit - Abdurrachman MappujiDevOps Practice in Nonprofit - Abdurrachman Mappuji
DevOps Practice in Nonprofit - Abdurrachman Mappuji
 
Fast data mining flow prototyping using IPython Notebook
Fast data mining flow prototyping using IPython NotebookFast data mining flow prototyping using IPython Notebook
Fast data mining flow prototyping using IPython Notebook
 
Ladypy 01
Ladypy 01Ladypy 01
Ladypy 01
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Austin Python Meetup 2017: How to Stop Worrying and Start a Project with Pyth...
Austin Python Meetup 2017: How to Stop Worrying and Start a Project with Pyth...Austin Python Meetup 2017: How to Stop Worrying and Start a Project with Pyth...
Austin Python Meetup 2017: How to Stop Worrying and Start a Project with Pyth...
 
Christian Strappazzon - Presentazione Python Milano - Codemotion Milano 2017
Christian Strappazzon - Presentazione Python Milano - Codemotion Milano 2017Christian Strappazzon - Presentazione Python Milano - Codemotion Milano 2017
Christian Strappazzon - Presentazione Python Milano - Codemotion Milano 2017
 
Building Data Pipelines in Python
Building Data Pipelines in PythonBuilding Data Pipelines in Python
Building Data Pipelines in Python
 
PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat
PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat
PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat
 
The Onward Journey: Porting Twisted to Python 3
The Onward Journey: Porting Twisted to Python 3The Onward Journey: Porting Twisted to Python 3
The Onward Journey: Porting Twisted to Python 3
 
Take a Stroll in the Bazaar
Take a Stroll in the BazaarTake a Stroll in the Bazaar
Take a Stroll in the Bazaar
 
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
CrashCourse: Python with DataCamp and Jupyter for Beginners
CrashCourse: Python with DataCamp and Jupyter for BeginnersCrashCourse: Python with DataCamp and Jupyter for Beginners
CrashCourse: Python with DataCamp and Jupyter for Beginners
 

Kürzlich hochgeladen

VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 

Kürzlich hochgeladen (20)

Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 

Pycon2017 instagram keynote