35

Erlang plus BDB: Disrupting the Conventional Web Wisdom

Embed Size (px)

Citation preview

Page 1: Erlang plus BDB: Disrupting the Conventional Web Wisdom
Page 2: Erlang plus BDB: Disrupting the Conventional Web Wisdom
Page 3: Erlang plus BDB: Disrupting the Conventional Web Wisdom

Erlang + EDTK + BDB: Disrupting the Web

Margo SeltzerArchitect

Page 4: Erlang plus BDB: Disrupting the Conventional Web Wisdom

What kind of talk is this?

• Evangelism• FUD• Fun

Page 5: Erlang plus BDB: Disrupting the Conventional Web Wisdom

Erlang + EDTK + BDB: Huh?

• General purpose programming language• Runtime system• Developed by Ericsson• Open source• Key feature: designed for ease of developing highly-

reliable, highly-concurrent systems• Enables highly robust applications:

• The AXD 301 includes 2 million lines of Erlang• Reliability: NINE 9’s

Page 6: Erlang plus BDB: Disrupting the Conventional Web Wisdom

Erlang + EDTK + BDB: Huh?

• Erlang Driver Toolkit• Declarative APIs to external library

• Automatic wrapper generation• Tracks library resources and cleans up• Enhanced for BDB support:

• Supports Erlang’s “crash early” error-handling model (supervisors)

• Meshes Erlang-style concurrency with BDB concurrency (private thread pools)

• Adds significant convenience layers for configration and replication

• Minimal overhead

Page 7: Erlang plus BDB: Disrupting the Conventional Web Wisdom

Erlang + EDTK + BDB: Huh?

• Berkeley Database• Database functionality …

• Transactions• Recovery• Replication

• … in a different package• Library linked directly into an application• Programmatic APIs• Schemaless: key/data pairs

Page 8: Erlang plus BDB: Disrupting the Conventional Web Wisdom

Disruptive Technology

• “a technological innovation, product, or service that eventually overturns the existing dominant technology or status quo product in the market.” -- Wikipedia

Page 9: Erlang plus BDB: Disrupting the Conventional Web Wisdom

Outline

• Interpreting the Title• Disecting an Internet Service• Erlang, it’s philosophy and runtime• Putting it all together• Q&A

Page 10: Erlang plus BDB: Disrupting the Conventional Web Wisdom

An Internet ServiceClients

The Internet

Load Balancer

App Servers

Database Servers

.NET Java

Page 11: Erlang plus BDB: Disrupting the Conventional Web Wisdom

An Internet ServiceClients

The Internet

Load Balancer

Database Servers

.NET Java

CGIPHPLISP

Perl

Page 12: Erlang plus BDB: Disrupting the Conventional Web Wisdom

An Internet ServiceClients

The Internet

Load Balancer

ServersBDB

CGIPerl

Application Code

Page 13: Erlang plus BDB: Disrupting the Conventional Web Wisdom

The Software Architecture

• In any case, your software is a messGazillion Active Ports

Fraction of a gazillion threads

A relatively small number of disks

Page 14: Erlang plus BDB: Disrupting the Conventional Web Wisdom

Outline

• Interpreting the Title• Disecting an Internet Service• Erlang, it’s philosophy and runtime• Putting it all together• Q&A

Page 15: Erlang plus BDB: Disrupting the Conventional Web Wisdom

The Erlang Approach

• Don’t fight the problem• Don’t fight the medium (the network)• Don’t fight the medium (distributed software)

Page 16: Erlang plus BDB: Disrupting the Conventional Web Wisdom

Don’t Fight the Problem

• 1:1 concurrency with the problem/solution domain

• Explicit lightweight stateful conversations (addressable processes)

• Arbitrarily rich messages

Page 17: Erlang plus BDB: Disrupting the Conventional Web Wisdom

Don’t Fight the the Network

• Asynchronous send (location-agnostic send and pray)

• Ordered inbox per process• Blocking receive with timeout (message-

selection via pattern-matching)

Page 18: Erlang plus BDB: Disrupting the Conventional Web Wisdom

Don’t Fight Distributed Software

• No shared memory: no mutexes, no mutation• True loose-coupling: processes are free to migrate• Safe to kill any process any time

• Recovery-oriented computing (before it was fashionable)• Let it crash• Propagate exceptions• Know how to recover

Page 19: Erlang plus BDB: Disrupting the Conventional Web Wisdom

Erlang Tricks: Processes

• Belong to the language (runtime), not OS• Very lightweight• Immutable data • Asynchronous message passing• Upgrade application on live, running system• Implication:

• No big deal if a process dies

Page 20: Erlang plus BDB: Disrupting the Conventional Web Wisdom

Erlang Tricks: Links

• Links connect processes• On process crash, all linked processes get

message.• Notified processes can clean up, takeover, do

whatever is necessary.• Easy to create supervisors• No recovery: just crash, cleanup and restart

Page 21: Erlang plus BDB: Disrupting the Conventional Web Wisdom

Erlang Tricks: Concurrency

• Use concurrency to structure the application“My first message is that concurrency is best

regarded as a program structuring principle”Tony Hoare, 2001

• Concurrency-oriented programming• Share nothing• Pure message passing• Let it crash

Page 22: Erlang plus BDB: Disrupting the Conventional Web Wisdom

Erlang Nuts and Bolts

• Hello World-module(hello).-export(hello_world/0).hello_world() ->

io:format(“Hello world.~n”, []).

Erlang (BEAM) emulator version 5.5.5 [source] [async-threads:0] [kernel-poll:false]

Eshell V5.5.5 (abort with ^G)1> c(hello).{ok,hello}2> hello:hello_world().Hello world.ok3>

Page 23: Erlang plus BDB: Disrupting the Conventional Web Wisdom

Spawning Processes

• Pid = spawn(module, function, args).• For example:

Erlang (BEAM) emulator version 5.5.5 [source] [async-threads:0] [kernel-poll:false]

Eshell V5.5.5 (abort with ^G)1> spawn(hello, hello_world, []).<0.32.0>Hello world.2>

Page 24: Erlang plus BDB: Disrupting the Conventional Web Wisdom

Message send and receive

• Send: Pid ! message.• Receive:

receivePattern1 [when Guard1] ->

Expression1;Pattern2 [when Guard2] ->

Expression2;…

end

Page 25: Erlang plus BDB: Disrupting the Conventional Web Wisdom

Send/Receive Example-module(food).-export([dinner/0]).

dinner() ->receivesunday ->io:format("Sunday is fish.~n", []),dinner();monday ->io:format("Monday is chicken.~n", []),dinner();tuesday ->io:format("Tuesday is beef.~n", []),dinner();Other ->io:format("Time to go home.~n", [])end.

Page 26: Erlang plus BDB: Disrupting the Conventional Web Wisdom

Send/Receive Execution

Eshell V5.5.5 (abort with ^G)1> Pid = spawn(food, dinner, []).<0.32.0>2> Pid ! tuesday.tuesdayTuesday is beef.

3> Pid ! sunday.sundaySunday is fish.

4> Pid ! wednesday.wednesdayTime to go home.

5> Pid ! monday.monday6>

Page 27: Erlang plus BDB: Disrupting the Conventional Web Wisdom

BDB in Erlang

• Erlang:• Is Functional (not procedural)• Communicates via messages• Communicates asynchronously

• BDB:• Is Procedural• Communicates via shared memory• Blocks on locks/IO/etc

Page 28: Erlang plus BDB: Disrupting the Conventional Web Wisdom

EDTK fixes mismatch

• Erlang interfaces to outside world via ports.• EDTK automatically generates code that

wraps library API, making library look like Erlang process(es).

• Provides framework to:• Clean up BDB resources after a crash• Manage threadpools to deal with BDB blocking• Manage administrative processes• Manage replication groups

Page 29: Erlang plus BDB: Disrupting the Conventional Web Wisdom

And the code looks like BDB

DB = ?BDB:db_create(Port, []).?BDB:db_open(Port, DB, void, “database”, “”,

bdb_DB_BTREE, [bdb_DB_CREATE, 8#644).?BDB:db_put(Port, DB, void “foo”, “foodata”, []).{Key, Data} = ?BDB:db_get(Port,

DB, void, “foo”, <<>>, []).?BDB:db_close(Port, DB, []).

Page 30: Erlang plus BDB: Disrupting the Conventional Web Wisdom

Outline

• Interpreting the Title• Disecting an Internet Service• Erlang, it’s philosophy and runtime• Putting it all together• Q&A

Page 31: Erlang plus BDB: Disrupting the Conventional Web Wisdom

An Internet ServiceClients

The Internet

Load Balancer

Servers

Page 32: Erlang plus BDB: Disrupting the Conventional Web Wisdom

How does it Perform?

From: http://www.sics.se/~joe/apachevsyaws.html

Page 33: Erlang plus BDB: Disrupting the Conventional Web Wisdom

Acknowledgements

• Joe Armstrong (Erlang)• Scott Lystig Fritchie (EDTK)• Chris Newcombe (EDTK extensions for BDB)

Page 34: Erlang plus BDB: Disrupting the Conventional Web Wisdom

AQ&

Page 35: Erlang plus BDB: Disrupting the Conventional Web Wisdom