82
SCALING INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 [email protected]

SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 [email protected] INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

Embed Size (px)

Citation preview

Page 1: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

SCALING INSTAGRAM INFRALisa Guo— Nov 7th, 2016 [email protected]

Page 2: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9
Page 3: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

INSTAGRAM HISTORY

2010

201114M users

2012/4/3Android release

2012/4/9Facebook acquisition

2014/1

Page 4: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

INSTAGRAM EVERYDAY

300 Million Users

4.2 Billion likes

95 Million photo/video uploads

100 Million followers

Page 5: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9
Page 6: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

SCALING MEANS

Scale out

Scale up

Scale dev team

Page 7: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

SCALE OUT

Page 8: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

SCALE OUT

“To scale horizontally means to add more nodes to a system, such as adding a new computer to a distributed software application. An example might involve scaling out from one Web server system to

three.”

- Wikipedia

Page 9: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

MICROSERVICE

Page 10: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

SCALING OUT

—>

—>

—> vertical partition horizontal sharding

Page 11: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

SCALING OUT

Page 12: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9
Page 13: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9
Page 14: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

INSTAGRAM STACK

Tuesday, June 25th, 2013

memcache

RabbitMQ

PostgreSQL

Cassandra

Celery

OtherServicesDjango

Page 15: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

STORAGE VS. COMPUTING

• Storage: needs to be consistent across data centers• Computing: driven by user traffic, as needed basis

Page 16: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

SCALE OUT: STORAGE

Tuesday, June 25th, 2013

-Masterless-Async, low latency-Multiple data center ready-Tunable latency vs consistency trade-off

user feeds, stories, activities, and other logs

Page 17: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

SCALE OUT: STORAGE

Tuesday, June 25th, 2013

user, media, friendship etc

• One master, replicas are in each region• Reads are done locally• Writes are cross region to the master.

Page 18: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

COMPUTING

Tuesday, June 25th, 2013

Page 19: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

Tuesday, June 25th, 2013

Django

RabbitMQ PostgreSQL

CassandraCelery

Django

RabbitMQPostgreSQL

CassandraCelery

memcacheDC1 DC2memcache

Page 20: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

MEMCACHE

Tuesday, June 25th, 2013

• Millions of reads/writes per second• Sensitive to network condition• Cross region operation is prohibitive

Page 21: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

feed

get

Django

User R

DC1

Django

PostgreSQL memcache

User Ccomment

setinsert

Page 22: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

Django

memcache PostgreSQL

User Ccomment

insertset

DC1

Django

memcachePostgreSQL

User R

feed

get

DC2

replication

Page 23: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

Django

memcache PostgreSQL

User Ccomment

insertset

DC1

Django

memcachePostgreSQL

User R

feed

set

DC2

replication

Cache invalidate

Cache invalidate

get

Page 24: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

COUNTERS

select count(*) from user_likes_media

where media_id=12345;

100s ms

Page 25: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

Tuesday, June 25th, 2013

COUNTERS

Page 26: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

COUNTER

Tuesday, June 25th, 2013

select count from media_likes where media_id=12345;

10s us

Page 27: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

Cache invalidatedAll djangos try to access DB

Page 28: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

MEMCACHE LEASE

d1 d2 memcache dbtime

lease-get

filllease-get

wait or use stale

read from DB

lease-set

lease-get

hit

Page 29: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

INSTAGRAM STACK - MULTI REGION

Tuesday, June 25th, 2013

Django

RabbitMQ

PostgreSQL

Cassandra

Celery

memcache

Django

RabbitMQ

PostgreSQL

Cassandra

Celery

memcache

DC1 DC2

Page 30: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

SCALING OUT

Tuesday, June 25th, 2013

• Capacity• Reliability• Regional failure ready

Requests/second

Page 31: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

LOAD TEST

Servers

20

40

60

80

100

0 2 4 6 8 10 12 14 16 18 20 22 24CPU instructions

Loaded

Regular

Load Balancer

DjangoServers

Page 32: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

20

40

60

80

100

0 2 4 6 8 10 12 14 16 18 20 22 24

User growth Server growth

Page 33: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

“Don’t count the servers, make the servers count”

Page 34: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

SCALE UP

Page 35: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

SCALE UP

Use as few CPU instructions as possible

Use as few servers as possible

Page 36: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

SCALE UP

Use as few CPU instructions as possible

Use as few servers as possible

Scale up

Page 37: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

CPU

Monitor

Optimize

Analyze

Page 38: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

COLLECT

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, 0); <code you want to measure> ioctl(fd, PERF_EVENT_IOC_DISABLE, 0); read(fd, &count, sizeof(long long));

Page 39: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

DYNOSTATS

20

40

60

80

100

0 2 4 6 8 10 12 14 16 18 20 22 24

Follow

Feed

Explore

Page 40: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

REGRESSION

20

40

60

80

100

0 2 4 6 8 10 12 14 16 18 20 22 24

Page 41: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

GRADUAL REGRESSION

0

20

40

60

80

100

0 2 4 6 8 10 12 14 16 18 20 22 24

Page 42: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

With new feature

Without new feature

Page 43: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9
Page 44: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

CPU

Monitor

Optimize

Analyze

Page 45: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

PYTHON CPROFILE

import cProfile, pstats, StringIO pr = cProfile.Profile()

pr.enable() # ... do something ... pr.disable() s = StringIO.StringIO() sortby = 'cumulative' ps = pstats.Stats(pr, stream=s).sort_stats(sortby) ps.print_stats() print s.getvalue()

Page 46: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9
Page 47: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

CPU - ANALYZEcontinuous profiling

generate_profile explore --start <start-time> --duration <minutes>

Page 48: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

CPU - ANALYZEcontinuous profiling

20

40

60

80

100

0 2 4 6 8 10 12 14 16 18 20 22 24

Caller

Callee

Page 49: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

CPU - ANALYZEdecorator

def get_photos(): ……

def feed(): get_photos()

@log_stats def get_follows(): …… def follow(): get_follows()

@log_stats

Page 50: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

get_follows get_photos

feed follow

log_stats

Page 51: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

get_followsget_photos

feed follow

Keeping Demand in Check

Page 52: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9
Page 53: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

CPU

Monitor

Optimize

Analyze

Page 54: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

igcdn-photos-d-a.akamaihd.net/hphotos-ak-xpl1/t51.2885-19/s300x300/12345678_1234567890_987654321_a.jpg

Page 55: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

igcdn-photos-d-a.akamaihd.net/hphotos-ak-xpl1/t51.2885-19/s150x150/12345678_1234567890_987654321_a.jpg

igcdn-photos-d-a.akamaihd.net/hphotos-ak-xpl1/t51.2885-19/s400x600/12345678_1234567890_987654321_a.jpg

igcdn-photos-d-a.akamaihd.net/hphotos-ak-xpl1/t51.2885-19/s200x200/12345678_1234567890_987654321_a.jpg

igcdn-photos-d-a.akamaihd.net/hphotos-ak-xpl1/t51.2885-19/s300x300/12345678_1234567890_987654321_a.jpg

Page 56: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

CPU - OPTIMIZE

Page 57: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

igcdn-photos-d-a.akamaihd.net/hphotos-ak-xpl1/t51.2885-19/s300x300/12345678_1234567890_987654321_a.jpg

150x150

400x600

200x200

Page 58: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

CPU - OPTIMIZE

C is really faster

• Candidate functions:• Used extensively• Stable

• Cython or C/C++

Page 59: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

CPU - CHALLENGE

cProfile is not free

False positive alerts

Better automation

Page 60: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

Use as few CPU instructions as possible

Use as few servers as possible

Scale up

Page 61: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

SCALE UP: MEMORY

(memory budget /process) X (# of processes) < system memory

Less memory budget/process

===> Dies sooner

===> More processes

Page 62: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

LOAD TEST

Servers

20

40

60

80

100

0 2 4 6 8 10 12 14 16 18 20 22 24CPU instructions

Loaded

Regular

Load Balancer

DjangoServers

Page 63: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

SCALE UP: MEMORY

Code

Large configuration

Page 64: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

SCALE UP: MEMORY

• Run in optimized mode (-O)• Use shared memory• NUMA• Remove dead code

Page 65: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

SCALE UP: LATENCY

Synchronous Processing model

===> All user experience impacted

===> Worker starvation

Single service degradation

===> Fewer CPU instr executedLonger latency

Page 66: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

Stories

FeedDjango

Feed

Stories

SuggestedUsers

ASYNC IO

Page 67: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

Use as few CPU instructions as possible

Use as few servers as possible

Scale up

Page 68: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

SCALE DEV TEAM

Page 69: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

SCALING TEAM

30% engineers joined in last 6 months

Bootcampers - 1 week

Hack-A-Month - 4 weeks

Intern - 12 weeks

Page 70: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

Comment Filtering

Self-harm Prevention

Windows App

Story Viewer Ranking

Video View Notification

Save Draft

First Story Notification

Page 71: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

Which server?

NewTable or New Column?

What Index?Should

I cache it?

Will I lock up DB?

Will I bring down Instagram?

Page 72: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

WHAT WE WANT

• Automatically handle cache• Define relations, not worry about implementations• Self service by product engineers• Infra focuses on scale this service

Page 73: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

TAO

USER1

USER2

USER3mediaposted

posted bylikes

liked by

likes

liked by

Page 74: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

SCALE DEV - END OF POSTGRES

Page 75: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

SHIPPING LOVE

60-80 daily diffs

>120 engineers committed code last month

Page 76: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

RELEASE

• Master, no branch• All features developed on master

gated by configuration• Continuous integration • No branch integration overhead

• No surprises• Iterate fast, collaborate easily• Fast bisect and revert

Page 77: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

Once a week?

40-50 rollouts per day

Once a day?Once a diff!!

Page 78: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

CHECKS AND BALANCES

Code reviewunittest

Code acceptedcommitted Canary To the Wild

Dark launchLoad test

Page 79: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9
Page 80: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

TAKEAWAYS

Scaling is a continuous effort

Scaling is multi-dimensional

Scaling is everybody’s responsibility

Page 81: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9

QUESTIONS?

Page 82: SCALING INSTAGRAM INFRA - QCon San Francisco … INSTAGRAM INFRA Lisa Guo— Nov 7th, 2016 lguo@instagram.com INSTAGRAM HISTORY 2010 2011 14M users 2012/4/3 Android release 2012/4/9