oscon - performance vs scalability

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

Upload: gleicon-moraes

Post on 16-Apr-2017

14.482 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: OSCon - Performance vs Scalability

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

Page 2: OSCon - Performance vs Scalability

$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

Page 3: OSCon - Performance vs Scalability

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.”

Page 4: OSCon - Performance vs Scalability

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

Page 5: OSCon - Performance vs Scalability

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

Page 6: OSCon - Performance vs Scalability

Scalability, not performance

Page 7: OSCon - Performance vs Scalability

Scalability, not performance1. Performance: same work with less resources2. Scalability: more work with added resources3. Scientific measures for performance: How much and How fast4. Tricks to improve performance can hurt scalability.5. Non-Blocking I/O is not an optimization for Performance6. 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)

Page 8: OSCon - Performance vs Scalability

JAVA concurrency in practice

Page 9: OSCon - Performance vs Scalability

Blocking code example - Download a list of URLs

Ruby

Python

Page 10: OSCon - Performance vs Scalability

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)

Page 11: OSCon - Performance vs Scalability

Benchmark - Blocking

Page 12: OSCon - Performance vs Scalability

Benchmark – Blocking + Threads

Page 13: OSCon - Performance vs Scalability

Benchmark - Non-Blocking (Eventmachine based)

Page 14: OSCon - Performance vs Scalability

Benchmark - Results

2 urlsBlocking I/O: 0m4.061sBlocking I/O + Threads: 0m2.914sNon-blocking I/O: 0m1.027s

100 urlsBlocking I/O: 2m46.769sBlocking I/O + Threads: 0m33.722sNon-blocking I/O: 0m11.495s

Page 15: OSCon - Performance vs Scalability

Python and Twisted

Page 16: OSCon - Performance vs Scalability

Python and Twisted – callback styles

Explicit

Inline

Page 17: OSCon - Performance vs Scalability

Python and Twisted – Generators

Generators

Page 18: OSCon - Performance vs Scalability

Python and Twisted – Deferreds

Not Deferreds

Deferreds

Page 19: OSCon - Performance vs Scalability

Python and Twisted – Callbacks

Callbacks

Page 20: OSCon - Performance vs Scalability

Python and Twisted – Inline callbacks

Results from getPage

Page 21: OSCon - Performance vs Scalability

Python and GEvent

Monkey patch the socket module

Page 22: OSCon - Performance vs Scalability

Python and GEvent

Create and start greenlets

Page 23: OSCon - Performance vs Scalability

Ruby and EventMachine

Page 24: OSCon - Performance vs Scalability

Ruby and EventMachine - Generators

Generator

Page 25: OSCon - Performance vs Scalability

Ruby and EventMachine - Deferreds

Sort of deferred

Page 26: OSCon - Performance vs Scalability

Ruby and EventMachine - Callbacks

Success and errorcallbacks

Page 27: OSCon - Performance vs Scalability

Ruby, EventMachine and EM-Synchrony

Page 28: OSCon - Performance vs Scalability

Ruby, EventMachine and EM-Synchrony

Inline results

Page 29: OSCon - Performance vs Scalability

Node.js

Page 30: OSCon - Performance vs Scalability

Node.js – Creating and ending the requests

Start requests

End requests

Page 31: OSCon - Performance vs Scalability

Node.js – Events handlers and callbacks

Response event

Page 32: OSCon - Performance vs Scalability

Erlang

Page 33: OSCon - Performance vs Scalability

Erlang – Spawning one download process per url

Page 34: OSCon - Performance vs Scalability

A reminder of what we wanted to do

Page 35: OSCon - Performance vs Scalability

Blocking code example - Download a list of URLs

Ruby

Python

Page 36: OSCon - Performance vs Scalability

Architectural building blocks

Page 37: OSCon - Performance vs Scalability

Message Queues – decoupling downloads

Page 38: OSCon - Performance vs Scalability

Message Queues – decoupling db writes on a webservice

Page 39: OSCon - Performance vs Scalability

Coupled comments webservice

Receive and write to db

Page 40: OSCon - Performance vs Scalability

Message Queue – Comment producer

Receive and write to queue,

release connection

Page 41: OSCon - Performance vs Scalability

Message Queue – Comment consumer

Take from queueand write to db

Page 42: OSCon - Performance vs Scalability

Cache

Page 43: OSCon - Performance vs Scalability

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

Page 44: OSCon - Performance vs Scalability

Cache – code from http://github.com/gleicon/vuvuzelr

Check if it’s on cacheReturn if it is

Feed the cache and set expiration

Page 45: OSCon - Performance vs Scalability

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/

Page 46: OSCon - Performance vs Scalability

Q & A

Page 47: OSCon - Performance vs Scalability

Thanks !