Slide 1

Slide 1 text

Stayin sane while defyin Joel Spolsky with Djan o

Slide 2

Slide 2 text

Hola, me llamo Nick nickbruun @nickbruun http://bruun.co/

Slide 3

Slide 3 text

I’m the CTO of Iconfinder

Slide 4

Slide 4 text

“… the sin le worst strate ic mistake that any software company can make: They decided to rewrite the code from scratch.” That’s Joel!

Slide 5

Slide 5 text

September 2012 LAMP stack Linux, Apache, MySQL, PHP and memcached (dv) (-ish) 16 cores, 16 GiB RAM, death star

Slide 6

Slide 6 text

Scientific chart required processin power Not so far future Sep 2012 May 2007 0 limit of (dv)

Slide 7

Slide 7 text

Restructure Rebuild

Slide 8

Slide 8 text

Fast and flexible Two simple oals: (aka code base nirvana)

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

April 2013 3 M monthly visitors 3 M requests per day 30 GiB data in Post reSQL

Slide 13

Slide 13 text

April 2013 09 PM Mon 22 03 AM 06 AM 09 AM 12 PM 03 PM 0 ms 200 ms 400 ms 600 ms 800 ms 83ms avera e response time 329ms 99th percentile response time

Slide 14

Slide 14 text

We survived! Lon story short… - and learned a few lessons in the process

Slide 15

Slide 15 text

1. ORMs are stupid.

Slide 16

Slide 16 text

Watch it like a boss # In postgresql.conf: log_destination = 'csvlog' log_directory = 'pg_log' logging_collector = on log_filename = 'postgres-%Y-%m-%d_%H%M%S' log_rotation_age = 1d log_rotation_size = 1GB log_min_duration_statement = 0ms log_checkpoints = on log_connections = on log_disconnections = on log_lock_waits = on log_temp_files = 0 dump every statement and its execution time

Slide 17

Slide 17 text

Watch it like a boss $ tail -f "/usr/local/var/postgres/pg_log/ $(ls -1t /usr/local/var/postgres/pg_log/ | head -1)" Latest log file Follow the tail of the log file

Slide 18

Slide 18 text

Did I say stupid? SomeModel.objects.filter( other = None ) SomeModel.objects.filter( other_id = None )

Slide 19

Slide 19 text

Did I say stupid? SomeModel.objects.filter( other = None ) SomeModel.objects.filter( other_id = None ) same exact thing!

Slide 20

Slide 20 text

Did I say stupid? SomeModel.objects.filter( other = None ) SomeModel.objects.filter( other_id = None ) SELECT "myapp_somemodel"."id", "myapp_somemodel"."arbitrary", "myapp_somemodel"."other_id" FROM "myapp_somemodel" LEFT OUTER JOIN "myapp_othermodel" ON ( "myapp_somemodel"."other_id" = "myapp_othermodel"."id" ) WHERE "myapp_othermodel"."id" IS NULL SELECT "myapp_somemodel"."id", "myapp_somemodel"."arbitrary", "myapp_somemodel"."other_id" FROM "myapp_somemodel" WHERE "myapp_somemodel"."other_id" IS NULL

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

ORMs are leaky abstractions count_by_name = {m.name: m.other_set.count() for m in SomeModel.objects.all()} count_by_names = {m['name']: m['num_other'] for m in SomeModel.objects.all().annotate( num_other=Count('other') ).values('name', 'num_other')} If we don’t want to waste resources like this: The essence of SQL leaks throu h:

Slide 23

Slide 23 text

I, for one, welcome our SQL overlords

Slide 24

Slide 24 text

2. Monitor everythin .

Slide 25

Slide 25 text

Graphite Sentry

Slide 26

Slide 26 text

No, your lo file probably isn’t ood enou h.

Slide 27

Slide 27 text

build stuff Side effect: awesome deployment workflow

Slide 28

Slide 28 text

build stuff deploy Side effect: awesome deployment workflow

Slide 29

Slide 29 text

build stuff deploy watch exceptions Side effect: awesome deployment workflow

Slide 30

Slide 30 text

build stuff deploy watch exceptions Side effect: awesome deployment workflow

Slide 31

Slide 31 text

3. Don’t assume. Profile.

Slide 32

Slide 32 text

Hot startup optimization strate y

Slide 33

Slide 33 text

Hot startup optimization strate y 1. Database indexes

Slide 34

Slide 34 text

Hot startup optimization strate y 1. Database indexes 2. Pa e cachin

Slide 35

Slide 35 text

Hot startup optimization strate y 1. Database indexes 2. Pa e cachin 3. Database relationship denormalization

Slide 36

Slide 36 text

Hot startup optimization strate y 1. Database indexes 2. Pa e cachin 3. Database relationship denormalization 4. Fine rained cachin

Slide 37

Slide 37 text

Hot startup optimization strate y 1. Database indexes 2. Pa e cachin 3. Database relationship denormalization 4. Fine rained cachin 5. Throw money at it

Slide 38

Slide 38 text

Hot startup optimization strate y 1. Database indexes 2. Pa e cachin 3. Database relationship denormalization 4. Fine rained cachin 5. Throw money at it 6. Go to step 5

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

91ms ori inal response time

Slide 41

Slide 41 text

“… the sin le worst strate ic mistake that any software company can make: They decided to rewrite the code from scratch.” 87.91 % of time spent renderin

Slide 42

Slide 42 text

Japanese temple

Slide 43

Slide 43 text

29ms response time with Jinja2 91ms ori inal response time

Slide 44

Slide 44 text

“… the sin le worst strate ic mistake that any software company can make: They decided to rewrite the code from scratch.” 29.16 % of time spent escapin HTML

Slide 45

Slide 45 text

20ms response time with Jinja2 without auto-escapin 29ms response time with Jinja2 91ms ori inal response time

Slide 46

Slide 46 text

4. Everythin prepacka ed isn’t ood, and everythin ood isn’t prepacka ed.

Slide 47

Slide 47 text

Watch your transactions Transactions

Slide 48

Slide 48 text

Watch your transactions Transactions Cachin +

Slide 49

Slide 49 text

Watch your transactions Transactions Back round tasks + Cachin +

Slide 50

Slide 50 text

Watch your transactions Transactions = ANGER!!1 Back round tasks + Cachin +

Slide 51

Slide 51 text

Enqueuin tasks in transactions Request Back round worker

Slide 52

Slide 52 text

Enqueuin tasks in transactions Request Back round worker Be in transaction

Slide 53

Slide 53 text

Enqueuin tasks in transactions Request Back round worker Be in transaction Create m odel

Slide 54

Slide 54 text

Enqueuin tasks in transactions Request Back round worker Be in transaction Create m odel Add task to queue

Slide 55

Slide 55 text

Dequeue task from queue Enqueuin tasks in transactions Request Back round worker Be in transaction Create m odel Add task to queue

Slide 56

Slide 56 text

Dequeue task from queue Enqueuin tasks in transactions Request Back round worker Be in transaction Create m odel Add task to queue Get m odel

Slide 57

Slide 57 text

Dequeue task from queue Enqueuin tasks in transactions Request Back round worker Be in transaction Create m odel Add task to queue Get m odel raises M odel.DoesNotExist

Slide 58

Slide 58 text

Dequeue task from queue Enqueuin tasks in transactions Request Back round worker Be in transaction Create m odel Add task to queue Com m it transaction Get m odel raises M odel.DoesNotExist

Slide 59

Slide 59 text

With djan o-celery-transactions Request Back round worker

Slide 60

Slide 60 text

With djan o-celery-transactions Request Back round worker Be in transaction

Slide 61

Slide 61 text

With djan o-celery-transactions Request Back round worker Be in transaction Create m odel

Slide 62

Slide 62 text

With djan o-celery-transactions Request Back round worker Be in transaction Create m odel Locally add task to queue

Slide 63

Slide 63 text

With djan o-celery-transactions Request Back round worker Be in transaction Create m odel Locally add task to queue Com m it transaction

Slide 64

Slide 64 text

With djan o-celery-transactions Request Back round worker Be in transaction Create m odel Locally add task to queue Com m it transaction Actually add task to queue

Slide 65

Slide 65 text

Dequeue task from queue With djan o-celery-transactions Request Back round worker Be in transaction Create m odel Locally add task to queue Com m it transaction Actually add task to queue

Slide 66

Slide 66 text

Dequeue task from queue With djan o-celery-transactions Request Back round worker Be in transaction Create m odel Locally add task to queue Com m it transaction Get m odel Actually add task to queue

Slide 67

Slide 67 text

Dequeue task from queue With djan o-celery-transactions Request Back round worker Be in transaction Create m odel Locally add task to queue Com m it transaction Get m odel Actually add task to queue Finish task

Slide 68

Slide 68 text

is awesome

Slide 69

Slide 69 text

is a trap!

Slide 70

Slide 70 text

are a trap! Frameworks

Slide 71

Slide 71 text

Thanks! It’s been awesome listenin to me.