Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Instagram Under the Hood
Search
Carl Meyer
November 04, 2016
Technology
8
8.4k
Instagram Under the Hood
Django Under the Hood 2016
Carl Meyer
November 04, 2016
Tweet
Share
Other Decks in Technology
See All in Technology
New Relicを活用したSREの最初のステップ / NRUG OKINAWA VOL.3
isaoshimizu
3
630
CysharpのOSS群から見るModern C#の現在地
neuecc
2
3.5k
Oracle Cloud Infrastructureデータベース・クラウド:各バージョンのサポート期間
oracle4engineer
PRO
28
13k
個人でもIAM Identity Centerを使おう!(アクセス管理編)
ryder472
4
230
EventHub Startup CTO of the year 2024 ピッチ資料
eventhub
0
120
20241120_JAWS_東京_ランチタイムLT#17_AWS認定全冠の先へ
tsumita
2
300
なぜ今 AI Agent なのか _近藤憲児
kenjikondobai
4
1.4k
【Pycon mini 東海 2024】Google Colaboratoryで試すVLM
kazuhitotakahashi
2
540
AGIについてChatGPTに聞いてみた
blueb
0
130
Zennのパフォーマンスモニタリングでやっていること
ryosukeigarashi
0
150
AWS Lambdaと歩んだ“サーバーレス”と今後 #lambda_10years
yoshidashingo
1
180
BLADE: An Attempt to Automate Penetration Testing Using Autonomous AI Agents
bbrbbq
0
320
Featured
See All Featured
Rails Girls Zürich Keynote
gr2m
94
13k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
A Tale of Four Properties
chriscoyier
156
23k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
Documentation Writing (for coders)
carmenintech
65
4.4k
YesSQL, Process and Tooling at Scale
rocio
169
14k
We Have a Design System, Now What?
morganepeng
50
7.2k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
27
4.3k
Teambox: Starting and Learning
jrom
133
8.8k
Transcript
Django Under the Hood 2016 Carl Meyer INSTAGRAM UNDER THE
HOOD
None
None
None
4,200,000,000 EVERY DAY
2,300,000,000,000
None
October 2010
— Mike Krieger “SUPER EASY SET-UP... ONE WAY OF DOING
THINGS... EASY TESTING.”
1M Instagrammers December 2010
None
None
None
None
5M Instagrammers June 2011
USERS LIKES COMMENTS MEDIA
class VerticalPartitionRouter(object): DB_FOR_MODEL = { 'likes.like': 'likes', 'comments.comment': 'comments', 'media.media':
'media', } def _db_for(self, model_or_obj): label = model_or_obj._meta.label_lower return self.DB_FOR_MODEL.get(label, 'default') def db_for_read(self, model, **hints): return self._db_for(model) def db_for_write(self, model, **hints): return self._db_for(model) def allow_relation(self, obj1, obj2, **hints): return self._db_for(obj_1) == self._db_for(obj_2)
None
USERS LIKES COMMENTS MEDIA
None
LOGICAL SHARDS (PG SCHEMAS) PHYSICAL SERVERS
LOGICAL SHARDS (PG SCHEMAS) PHYSICAL SERVERS
commit 5c7034fa8b934569cce5c1bf4bb202f2f3f18bc9 Author: Mike Krieger Date: Tue Jul 19 23:47:26
2011 -0700 WIP
class ShardedObject(object): def insert(self, shard_on_id, from_table, values): shard, db =
get_conn_for_shard_key(shard_on_id) cursor = db.cursor() placeholders = ','.join( [("%%(%s)s" % key) for key in values.keys()]) columns = ','.join(values.keys()) insert_statement = ( "INSERT INTO idb%s.%s (%s) VALUES (%s)" % (shard, from_table, columns, placeholders) ) cursor.execute(insert_statement, values) db.commit()
138726300013410905 SHARDED UNIQUE IDS TIMESTAMP SHARD ID SEQUENCE CREATE OR
REPLACE FUNCTION insta5.next_id... CREATE TABLE insta5.our_table ( "id" bigint NOT NULL DEFAULT insta5.next_id(), ...rest of table schema... )
None
40M Instagrammers April 2012
Memcached
Data center A Memcached Data center B Memcached Invalidator Invalidator
MULTI-REGION CACHE INVALIDATION
CONTEMPLATING THE TAO
TAO Memcached Memcached Memcached Memcached Memcached
TAO DATA MODEL Jan follows Pat. Pat posts a photo.
Jan authors a comment on the photo. Pat likes the comment. Jan Pat Follows Followed by "Contemplative cat!" Comment on Has comment Posted Posted by Authored Authored by Liked by Likes
CONTEMPLATING THE TAO
None
500M Instagrammers June 2016
“JUST KEEP FIXING UNTIL THE TESTS PASS.” UPGRADING DJANGO
INSTAGRAM: (1.3 + 1.8) Now compatible with Django 3.1TM
INSTAGRAM: Now compatible with Django Django 1.8!
OUR (MONKEY) PATCHES 40 1 Don't recompile URL regexes for
every active language. 2 Don't try to load translations from an app with no locale directory. 3Unlazified settings!
from django.conf import settings def force_unlazified_settings(): for key in dir(settings):
settings.__dict__[key] = getattr(settings, key) UNLAZY ALL THE SETTINGS!
INSTAGRAM: Now compatible with Django Django 1.8! (and fast as
ever)
500M+ Instagrammers Today!
Proxygen Django & uWSGI TAO Cassandra Everstore Celery & RabbitMQ
None
None
Active Last Minute ???
COUNTING CPU INSTRUCTIONS WITH PERF struct perf_event_attr pe; pe.type =
PERF_TYPE_HARDWARE; pe.config = PERF_COUNT_HW_INSTRUCTIONS; fd = perf_event_open(&pe, 0, -1, -1, 0); ioctl(fd, PERF_EVENT_IOC_ENABLE); // code whose CPU instructions you want to measure ioctl(fd, PERF_EVENT_IOC_DISABLE); read(fd, &count, sizeof(long long));
CPU instructions/s CPU instructions/s
CPU instructions/s CPU instructions/s
None
AppWeight
Continuous deployment 30-50 deploys per day
None
DYNOSTATS class DynostatsMiddleware(object): def process_request(self, req): req.dynostats_enabled = ( 1
== random.randint(1, settings.DYNO_SAMPLE_RATE)) if req.dynostats_enabled: # uses Linux perf library req.dyno_start_cpu_instr = get_cpu_instructions() # use clock_gettime from librt req.dyno_start_wall_time = get_real_wall_time() req.dyno_start_cpu_time = get_process_cpu_time() # uses /proc/<pid>/statm req.dyno_start_rss_mem = get_process_rss_mem() def process_response(self, req, response): if req.dynostats_enabled: # get end values, send to scribe w/ req details return response
None
None
None
CPROFILE class ProfilerMiddleware(object): def process_request(self, req): req.cprofile_enabled = ( 1
== randint(1, settings.CPROFILE_SAMPLE_RATE)) if req.cprofile_enabled: req.profiler = cProfile.Profile() req.profiler.enable() def process_response(self, req, response): if req.cprofile_enabled: req.profiler.disable() req.profiler.create_stats() send_to_scribe(msgpack.dumps(profiler.stats))
None
None
None
import cProfile import resource def get_cpu_instr(): # use perf to
get CPU instructions cpu_profiler = cProfile.Profile(timer=get_cpu_instr) def get_rss_mem(): return resource.getrusage( resource.RUSAGE_SELF).ru_maxrss mem_profiler = cProfile.Profile(timer=get_rss_mem) CUSTOM CPROFILE TIMERS
A B X Y
A B X Y cached_property
A B X Y
FIXING EFFICIENCY REGRESSIONS - Fixing the obvious. - Don't do
useless work. - Cache things that don't change. - Change a .py to a .pyx: Cython. - Rewrite in C.
tightly integrated loosely coupled
make the easy things easy and the hard things possible
— Mike Krieger “SUPER EASY SETUP.”
— Mike Krieger “THE PIECES WERE PLUGGABLE ENOUGH... EVEN WITH
OUR OWN ORM WE COULD USE MOST OF THE REST OF DJANGO.”
AN INCOMPLETE LIST OF THE DJANGO WE RELY ON -
HTTP stack - requests and responses - contrib.sessions - contrib.auth - middleware - url routing - settings - forms - i18n - contrib.gis - django.utils - cache backends - HTTP decorators - CSRF - signals - management commands
None
async(io) pypy? CPython JIT? traffic replay python 3
None
engineering.instagram.com
[email protected]
@carljm
None
None
PHOTOS database by RockIcon, smiley by Vandana Agrawal, server by
Alexander Skowalsky, from Noun Project https://www.flickr.com/photos/yashh/2834704689 https://unsplash.com/photos/KEXUeZIev10 https://unsplash.com/photos/pd4lo70LdbI https://unsplash.com/photos/jh2KTqHLMjE https://www.flickr.com/photos/johnsonderman/15144843722 https://www.flickr.com/photos/kennethreitz/5521545772/ https://www.instagram.com/p/mNj4L3OTzj/ https://www.flickr.com/photos/67926342@N08/6175870684 https://www.flickr.com/photos/lytfyre/6489338411 https://unsplash.com/photos/4fQAMZNaGUo https://unsplash.com/photos/glHJybGNt1M https://www.flickr.com/photos/nedrichards/51132692 https://www.flickr.com/photos/sophistechate/2913053678 https://www.flickr.com/photos/elviskennedy/6784123582 https://unsplash.com/photos/HkTMcmlMOUQ