erlang: software for a concurrent world

48
Erlang “Software for a concurrent world” CWI Amsterdam Arjan Scherpenisse 14 december 2012 [email protected]

Upload: arjan

Post on 05-Jul-2015

582 views

Category:

Documents


3 download

DESCRIPTION

Introduction to Erlang for the talk at the Programming Environments Meetup at the Centrum for Mathematics and Computer Science (CWI), December 14, 2012.

TRANSCRIPT

Page 1: Erlang: Software for a Concurrent world

Erlang

“Software for a concurrent world”

CWI Amsterdam

Arjan Scherpenisse14 december 2012

[email protected]

Page 2: Erlang: Software for a Concurrent world

Agenda

● Intro● Language essentials● Concurrency / failure● OTP● VM / GC / optimisations / pitfalls

Page 4: Erlang: Software for a Concurrent world
Page 5: Erlang: Software for a Concurrent world
Page 6: Erlang: Software for a Concurrent world
Page 7: Erlang: Software for a Concurrent world

Erlang Philosophy

● The world is concurrent● Things in the world dont share data● Things communicate with messages● Things fail

- Joe Armstrong

Page 8: Erlang: Software for a Concurrent world

Language essentials

Page 9: Erlang: Software for a Concurrent world
Page 10: Erlang: Software for a Concurrent world

Data types

● Integer / float● Atoms● Tuples● Lists● Binaries

… so where are strings?

Page 11: Erlang: Software for a Concurrent world

Datatypes (cont)

● Functions● Pids● Ports

Page 12: Erlang: Software for a Concurrent world
Page 13: Erlang: Software for a Concurrent world
Page 14: Erlang: Software for a Concurrent world
Page 15: Erlang: Software for a Concurrent world
Page 16: Erlang: Software for a Concurrent world
Page 17: Erlang: Software for a Concurrent world
Page 18: Erlang: Software for a Concurrent world
Page 19: Erlang: Software for a Concurrent world
Page 20: Erlang: Software for a Concurrent world
Page 21: Erlang: Software for a Concurrent world

Concurrency / failure

Page 22: Erlang: Software for a Concurrent world

Concurrency

● spawn, spawn_link● !, receive

Page 23: Erlang: Software for a Concurrent world
Page 24: Erlang: Software for a Concurrent world
Page 25: Erlang: Software for a Concurrent world
Page 26: Erlang: Software for a Concurrent world
Page 27: Erlang: Software for a Concurrent world
Page 28: Erlang: Software for a Concurrent world
Page 29: Erlang: Software for a Concurrent world
Page 30: Erlang: Software for a Concurrent world
Page 31: Erlang: Software for a Concurrent world
Page 32: Erlang: Software for a Concurrent world
Page 33: Erlang: Software for a Concurrent world

Fundamental pieces

Page 34: Erlang: Software for a Concurrent world

OTP: Open Telecom Platform

Page 35: Erlang: Software for a Concurrent world
Page 36: Erlang: Software for a Concurrent world

OTP

● application– Contains independent code, multiple applications

per Erlang node

● supervisor– Supervises worker processes and supervisors

● gen_server– Basic work unit

Page 37: Erlang: Software for a Concurrent world

GC / optimisation / pitfalls

Page 38: Erlang: Software for a Concurrent world

Performance

● Processes are cheap● Data copying is expensive● Handle much of request in single process● Cache locally in process dict● Reduce size of messages● Bigger binaries (> 64 bytes) are shared and reference

counted, make use of them.● String processing is expensive● Keep eye on process inboxes

Page 39: Erlang: Software for a Concurrent world

Garbage Collection

● per process, no global gc sweep = no locking of vm

● can specify per-process initial heap size● prevent big GCs

● keep big data outside process● or in process dict● large binaries = external, use refcounting

Page 40: Erlang: Software for a Concurrent world

Common Pitfalls

● Message inbox overflowing● Atom table injection / crash● VM tweaks needed for

– nr of open file descriptors

– nr of processes

– heap size

Page 41: Erlang: Software for a Concurrent world
Page 42: Erlang: Software for a Concurrent world
Page 43: Erlang: Software for a Concurrent world

Outro

Page 44: Erlang: Software for a Concurrent world

What I mainly use it for

● Full-stack web development– http://zotonic.com/

● HA backend systems

Preventing stuff like this → :-)

Page 45: Erlang: Software for a Concurrent world
Page 46: Erlang: Software for a Concurrent world
Page 47: Erlang: Software for a Concurrent world

Sources of Inspiration

● Erlang - the ghetto

● Intro to erlang (slideshare)

● Erlang - message passing concurrency (slideshare)

● Erlang concepts (slideshare)

● Concurrency and Paralellism in Erlang (hopkins univ)

Page 48: Erlang: Software for a Concurrent world

Thanks!

Arjan [email protected]

http://twitter.com/acscherp http://miraclethings.nl/