Slide 1

Slide 1 text

Performance vs Scalability Blocking or non-blocking code, code complexity and architectural blocks to avoid code complexity

Slide 2

Slide 2 text

$whoami work: http://locaweb.com site: http://7co.cc blog: http://zenmachine.wordpress.com code: http://github.com/gleicon RestMQ: http://restmq.com twitter: @gleicon

Slide 3

Slide 3 text

Required Listening “But your new shoes are worn at the heels and your suntan does rapidly peel and your wise men don't know how it feels to be thick as a brick.”

Slide 4

Slide 4 text

Objective   To discuss how scalability sometimes get confused with performance when speaking about non-blocking I/O   To show how most of languages have frameworks to deal with non-blocking I/O   Examine an example in different languages and the basic blocks of these frameworks   Discuss that this programming model is mandatory, not even migrate from Blocking to non- blocking I/O is needed for most cases. The best solution may not lie in code complexity, but in the right architecture.   Discuss a bit about Message Queues, Cache and Redis

Slide 5

Slide 5 text

Basics • Asynchronous I/O - Posix aio_* • Non-Blocking I/O - Select, Poll, EPoll, Kqueue • Reactor - Pattern for multiplexing I/O • Future Programming – Generators and Deferreds • Event Emitters • The c10k problem - http://www.kegel.com/c10k.html

Slide 6

Slide 6 text

Scalability, not performance

Slide 7

Slide 7 text

Scalability, not performance 1.  Performance: same work with less resources 2.  Scalability: more work with added resources 3.  Scientific measures for performance: How much and How fast 4.  Tricks to improve performance can hurt scalability. 5.  Non-Blocking I/O is not an optimization for Performance 6.  The time spent waiting for an external resource can be used to other tasks, given a known introduced complexity in the code. 7.  Most of time architectural changes can avoid deeper code changes (1,2,3,4 - Goetz et al, Java Concurrency in Practice) (5,6,7 - Lucindo, R. http://slidesha.re/aYz4Mk, wording on 6 is mine)

Slide 8

Slide 8 text

JAVA concurrency in practice

Slide 9

Slide 9 text

Blocking code example - Download a list of URLs Ruby Python

Slide 10

Slide 10 text

Benchmark   Compare blocking and non-blocking Ruby code to download a list of URLs   A naive example to examine the effects of both paradigms on the final code   The ‘algorithm’ to download an URL is (should be) the same in both versions   If we had more cores, or more powerful cores, it should not affect the performance of a single download (I/O bound operation)

Slide 11

Slide 11 text

Benchmark - Blocking

Slide 12

Slide 12 text

Benchmark – Blocking + Threads

Slide 13

Slide 13 text

Benchmark - Non-Blocking (Eventmachine based)

Slide 14

Slide 14 text

Benchmark - Results 2 urls Blocking I/O: 0m4.061s Blocking I/O + Threads: 0m2.914s Non-blocking I/O: 0m1.027s 100 urls Blocking I/O: 2m46.769s Blocking I/O + Threads: 0m33.722s Non-blocking I/O: 0m11.495s

Slide 15

Slide 15 text

Python and Twisted

Slide 16

Slide 16 text

Python and Twisted – callback styles Explicit Inline

Slide 17

Slide 17 text

Python and Twisted – Generators Generators

Slide 18

Slide 18 text

Python and Twisted – Deferreds Not Deferreds Deferreds

Slide 19

Slide 19 text

Python and Twisted – Callbacks Callbacks

Slide 20

Slide 20 text

Python and Twisted – Inline callbacks Results from getPage

Slide 21

Slide 21 text

Python and GEvent Monkey patch the socket module

Slide 22

Slide 22 text

Python and GEvent Create and start greenlets

Slide 23

Slide 23 text

Ruby and EventMachine

Slide 24

Slide 24 text

Ruby and EventMachine - Generators Generator

Slide 25

Slide 25 text

Ruby and EventMachine - Deferreds Sort of deferred

Slide 26

Slide 26 text

Ruby and EventMachine - Callbacks Success and error callbacks

Slide 27

Slide 27 text

Ruby, EventMachine and EM-Synchrony

Slide 28

Slide 28 text

Ruby, EventMachine and EM-Synchrony Inline results

Slide 29

Slide 29 text

Node.js

Slide 30

Slide 30 text

Node.js – Creating and ending the requests Start requests End requests

Slide 31

Slide 31 text

Node.js – Events handlers and callbacks Response event

Slide 32

Slide 32 text

Erlang

Slide 33

Slide 33 text

Erlang – Spawning one download process per url

Slide 34

Slide 34 text

A reminder of what we wanted to do

Slide 35

Slide 35 text

Blocking code example - Download a list of URLs Ruby Python

Slide 36

Slide 36 text

Architectural building blocks

Slide 37

Slide 37 text

Message Queues – decoupling downloads Fetch Page 1st parse Fetch Page 1st parse Fetch Page 1st parse Fetch Page 1st parse Consumer Message Queue

Slide 38

Slide 38 text

Message Queues – decoupling db writes on a webservice

Slide 39

Slide 39 text

Coupled comments webservice Receive and write to db

Slide 40

Slide 40 text

Message Queue – Comment producer Receive and write to queue, release connection

Slide 41

Slide 41 text

Message Queue – Comment consumer Take from queue and write to db

Slide 42

Slide 42 text

Cache

Slide 43

Slide 43 text

No cache – code from http://github.com/gleicon/vuvuzelr

Slide 44

Slide 44 text

Cache – code from http://github.com/gleicon/vuvuzelr Check if it’s on cache Return if it is Feed the cache and set expiration

Slide 45

Slide 45 text

Redis   Key/Value store   Key is a string, Value can be string, hash, list, set, scored set   Atomic operations   Publish/Subscription channels   Set/Scored Sets operations (union, diff, intersection)   UUID Generator, Distributed Cache, Message Queue (RestMQ, Resque), Serialized object storage, throttle control and more at http://rediscookbook.org/

Slide 46

Slide 46 text

Q & A

Slide 47

Slide 47 text

Thanks !