96
VoltDB and Erlang TechPlanet 2012 H. Diedrich http://www.eonblast.com – twitter @hdiedrich

VoltDB and Erlang - Tech planet 2012

Embed Size (px)

DESCRIPTION

VoltDB and Erlang: two very promising beasts, made for the new parallel world, but still lingering in the wings. Not only are they addressing todays challenges but they are using parallel architectures as corner stone of their new and surprising approach to be faster and more productive. What are they good for? Why are we working to team them up? Erlang promises faster implementation, way better maintenance and 4 times shorter code. VoltDB claims to be two orders of magnitude faster than its competitors. The two share many similarities: both are the result of scientific research and designed from scratch to address the new reality of parallel architectures with full force. This talk presents the case for Erlang as server language, where it shines, how it looks, and how to get started. It details Erlang's secret sauce: microprocesses, actors, atoms, immutable variables, message passing and pattern matching. (Note: for a longer version of this treatment of Erlang only see: Why Erlang? http://www.slideshare.net/eonblast/why-erlang-gdc-online-2012) VoltDB's inner workings are explained to understand why it can be so incredibly fast and still better than its NoSQL competitors. The well publicized Node.js benchmark clocking in at 695,000 transactions per second is described and the simple steps to get VoltDB up and running to see the prodigy from up close. Source examples are presented that show Erlang and VoltDB in action. The speaker is creator and maintainer of the Erlang VoltDB driver Erlvolt.

Citation preview

Page 1: VoltDB and Erlang - Tech planet 2012

VoltDB and Erlang

TechPlanet 2012H. Diedrich

http://www.eonblast.com – twitter @hdiedrich

Page 2: VoltDB and Erlang - Tech planet 2012

Your Host

Henning Diedrich

• Founder, CEO Eonblast• Maintainer of Emysql, Erlvolt

Page 3: VoltDB and Erlang - Tech planet 2012

This Presentation

Review With Leasure• Video & Slides will be online at SK planet• Slides will be online at slideshare.net

Page 4: VoltDB and Erlang - Tech planet 2012

Eonblast

Page 5: VoltDB and Erlang - Tech planet 2012

VoltDB and Erlang

Open Source Tech for Scale

1. What makes Them Special?2. How do They Look?3. Are They For Me?

Page 6: VoltDB and Erlang - Tech planet 2012

Made for a Concrete Need Made for Distribution Made for Multi-Core Truly Different Approaches Based on Parallelism Improving on Previous Solutions Corporate-Created Open Source Professional Support Known by Those in the Know

Page 7: VoltDB and Erlang - Tech planet 2012

The free, scaling, SQL DB

Redis + SQL + Scale = VoltDBRedis + SQL + Scale = VoltDB

Page 8: VoltDB and Erlang - Tech planet 2012

Why VoltDB?

Business Perspective

• Reduce Costs• Scale Seamlessly• Maintain Strategic Flexibility

Page 9: VoltDB and Erlang - Tech planet 2012

Why VoltDB?

Production Perspective

• Keep The Good of SQL• Use the Speed of In-Memory• Take Pain out of Scaling

Page 10: VoltDB and Erlang - Tech planet 2012

In-Memory

The Redis of clusters• Today good for 100s of GB of data• Sheds 75% of DBM system activity• Made from scratch for distribution• Full disk persistence

Page 11: VoltDB and Erlang - Tech planet 2012

SQL

The MySQL of NewSQLs• SQL Has Flaws• Essentially Math, Not Syntax• You'd be missing Queries• VoltDB is even 'more SQL than SQL'

Page 12: VoltDB and Erlang - Tech planet 2012

CAP

• Distributed• Consistent• Highly-Available• Partition-Tolerant

… have it all!Brewer on CAP 2012: http://www.infoq.com/articles/cap-twelve-years-later-how-the-rules-have-changed

Page 13: VoltDB and Erlang - Tech planet 2012

ACID

• Atomicity• Consistency• Isolation• Durability

… for granted?

Page 14: VoltDB and Erlang - Tech planet 2012

Double Bookkeeping

• Not Every App needs It• Requires ACID Transactions• Neigh Impossible to emulate• Impossible With BASE

(Eventual Consistency)

Page 15: VoltDB and Erlang - Tech planet 2012

The Magic

• Compiled-In Queries• Single-Thread Execution• Snowflakes & Clones

Page 16: VoltDB and Erlang - Tech planet 2012

Partitions

State#

Page 17: VoltDB and Erlang - Tech planet 2012

K Safety 1

State#

Page 18: VoltDB and Erlang - Tech planet 2012

Snowflake Structures

PK

FK

FK

FK

FK

FK

FK

PK

Page 19: VoltDB and Erlang - Tech planet 2012

Snowflake Structures

FK

FK

FKFK

FKPK

Page 20: VoltDB and Erlang - Tech planet 2012

Other Data

PK

PK

PK

Page 21: VoltDB and Erlang - Tech planet 2012

Partitions

State#

A B C E F G H I J K L M N O P

Page 22: VoltDB and Erlang - Tech planet 2012

Partitions

State#

A B C E F G H I J K L M N O P

A-Z A-Z A-Z A-Z A-Z

Page 23: VoltDB and Erlang - Tech planet 2012

Single Thread Execution

• One Thread per Partition• One Thread per Transaction• One Thread can't race itself• Main Design Criterion

Page 24: VoltDB and Erlang - Tech planet 2012

Benchmark

• Node.js• Amazon EC2• 64 core node.js clusters + 96 core VoltDB cluster• 695,000 transactions per second (TPS)• 2,780,000 operations per second• 100,000 TPS per 8 core client• 12,500 TPS per node.js core• Stable even under overload• Pretty much linear scale

Details: http://voltdb.com/company/blog/695k-tps-nodejs-and-voltdb

Page 25: VoltDB and Erlang - Tech planet 2012

BenchmarkTV Contest

• Millions of callers • Small set of candidates• Massive peak• One transaction is one vote• Callers are identified by their telephone number• Callers must not be allowed to vote more than once

Page 26: VoltDB and Erlang - Tech planet 2012

BenchmarkCREATE TABLE contestants ( contestant_number integer NOT NULL , contestant_name varchar(50) NOT NULL , CONSTRAINT PK_contestants PRIMARY KEY ( contestant_number ) );

CREATE TABLE votes ( phone_number bigint NOT NULL , state varchar(2) NOT NULL , contestant_number integer NOT NULL);

CREATE TABLE area_code_state ( area_code smallint NOT NULL , state varchar(2) NOT NULL , CONSTRAINT PK_area_code_state PRIMARY KEY ( area_code ) );

Page 27: VoltDB and Erlang - Tech planet 2012

Benchmark

// Check if the vote is for a valid contestant

SELECT contestant_number FROM contestants WHERE contestant_number = ?;

// Check if the voter has exceeded their allowed number of votes

SELECT num_votes FROM v_votes_by_phone_number WHERE phone_number = ?;

// Check an area code to retrieve the corresponding state

SELECT state FROM area_code_state WHERE area_code = ?;

// Record a vote

INSERT INTO votes (phone_number, state, contestant_number) VALUES (?, ?, ?);

The Transaction in 4 Operations

Page 28: VoltDB and Erlang - Tech planet 2012

Drivers

• Java• C, C++• node.js• Python• Ruby• Go• Erlang

Page 29: VoltDB and Erlang - Tech planet 2012

• VoltDB, Inc. 2009 – commercial developer, support• Open Source – 100% dictatorial by VoltDB, Inc• Made for OLTP – fast cheap writes, high throughput• CA of CAP – 100% consistent & highly available• Simple SQL – real queries• In-memory – 100x faster than MySQL• ACID transactions – double bookkeeping• Distributed – for painless growth• Linear scale – predictable, low cost• Replication, Snapshots – disk persistence, hot backup• More SQL than SQL – clean separation of data

Page 30: VoltDB and Erlang - Tech planet 2012

Getting Started

• Download and Install from voltdb.com• Open Getting Started inside the installation• Run Hello World locally• Read M.I.T. Paper A New Architecture• Watch webinars at voltdb.com• Free Help also from VoltDB staff

http://community.voltdb.com/forum• Use EC2 to benchmark your ideas

Page 31: VoltDB and Erlang - Tech planet 2012

Upcoming Blog Post

• Looking at 14 databases• Riak, Cassandra, Membase, H-Base, Voldemort, MySQL, MySQL Cluster, Redis, Redis

Cluster, Tokyo Cabinet, Memcached, CouchDB, Couchbase, VoltDB, MongoDB, Postgres

• In the light of what games need• Unbiased comparison• Twitter @hdiedrich

Page 32: VoltDB and Erlang - Tech planet 2012
Page 33: VoltDB and Erlang - Tech planet 2012

Erlang may be to Javawhat Java was to C++

C++ – pointers = JavaJava – deadlocks = Erlang

Page 34: VoltDB and Erlang - Tech planet 2012

Why Erlang?

Business Perspective

• Reduce Costs• Reduce Downtimes• Shorten Time To Market

Page 35: VoltDB and Erlang - Tech planet 2012

Why Erlang?

Production Perspective

• High Productivity• Low Hardware Requirements• Fewer Errors

Page 36: VoltDB and Erlang - Tech planet 2012

Who Is using It?

“You probably use systemsbased on Erlang/OTP every day

without knowing it.“

Mike Williams

Page 37: VoltDB and Erlang - Tech planet 2012

Erlang Game Servers

Zynga: FarmVille via membase, Activision Blizzard: Call of Duty, Bigpoint: Battle Star Galactica, Wooga: Magic Land

Page 38: VoltDB and Erlang - Tech planet 2012

Erlang Poster Childs

Klarna AB

• Financial Services for E-Commerce• 30 seconds downtime in 3 years

Distributed Databases• Membase

• Riak

• BigCouch

Page 39: VoltDB and Erlang - Tech planet 2012

Sweet Spots• Stateful Servers with High Throughput• Cluster Distribution Layers• Chats

Page 40: VoltDB and Erlang - Tech planet 2012

Origins

PLEX• Ericsson makes billions with telecom switches• They used PLEX, an all proprietary software• PLEX delivers, but has bad productivity

Page 41: VoltDB and Erlang - Tech planet 2012

Origins• The 80's: Ericsson Computer Science Lab

Joe Armstrong, Robert Virding, Mike Williams

„What aspects of computer languages make it easier to program telecom systems?“

Page 42: VoltDB and Erlang - Tech planet 2012

Erlang was Built For

• Reliability• Maintenance• Distribution• Productivity

Page 43: VoltDB and Erlang - Tech planet 2012

Concurrency Built In

• In the Language, not OS• Easier Testing• Massively Battle-Tested• Closer To Real Problems

Page 44: VoltDB and Erlang - Tech planet 2012

The Magic

• Microprocesses• Pattern Matching*

• Immutable Variables

* Not your familiar Regex string matching

Page 45: VoltDB and Erlang - Tech planet 2012

Thinking Erlang

• The Actor Model• Thinking Parallel• Thinking Functional• Thinking Processes• Pattern Matching• Let It Crash!

Page 46: VoltDB and Erlang - Tech planet 2012

The Actor ModelCarl Hewitt 1973• Behavior• State• Parallel• Asynchronous Messages• Mailboxes• No Shared State• Self-Contained Machines

Benefits• More true to the real world• Better suited for parallel hardware• Better suited for distributed architectures• Scaling garbage collection (sic!)• Less Magic

Data Code

Object

Data Code

Actor

Process

Page 47: VoltDB and Erlang - Tech planet 2012

Asynchronous Messages

Message dispatch is one-way, truly asynchronous.Not function calls but something in their own right.Clean break from the FP paradigm.

Pid ! Msg

Page 48: VoltDB and Erlang - Tech planet 2012

Thinking ProcessesWhat should be a Process?

Processes• Don’t share State• Communicate Asynchronously• Are Very Cheap to create And keep• Monitor Each Other• Provide Contention Handling• Constitute the Error Handling Atom

“It's easy!” Joe Armstrong

Page 49: VoltDB and Erlang - Tech planet 2012

Objects share Threads

Multiple objects share threads.Objects can be accessed across threads.Threads - and objects - share state.

Page 50: VoltDB and Erlang - Tech planet 2012

State, code and process form a unity: the actor.Like processes, actors do not share state.In fact, like humans. Who mostly work quite well.

Actors are Processes

Page 51: VoltDB and Erlang - Tech planet 2012

Objects and Threads

C C++ C# Java JavaScript Node Lua Python

Page 52: VoltDB and Erlang - Tech planet 2012

Objects and Threads

Idle Threads

Page 53: VoltDB and Erlang - Tech planet 2012

Objects and Threads

Unwanted surviving objects

Leeroy Jenkins!!!

Page 54: VoltDB and Erlang - Tech planet 2012

Objects and Threads

Prematurely destroyed objects

2*B || !2*B ?

Page 55: VoltDB and Erlang - Tech planet 2012

Erlang Actors

Erlang Actors: State + Code + Process

Page 56: VoltDB and Erlang - Tech planet 2012

Erlang Actors

One dies.

Page 57: VoltDB and Erlang - Tech planet 2012

Erlang Actors

It's restarted.

Page 58: VoltDB and Erlang - Tech planet 2012

Errors propagate

Loss of state can affect multiple modules.State errors cross thread boundaries.Defensive code multiplies.

Page 59: VoltDB and Erlang - Tech planet 2012

Objects need Locks

● System design is disrupted by explicit locks.● Overly cautious locking slows things down.● Forgotten locks create errors that show under load.

Locked Locked

Waiting

Doing X1

Doing X2

Resource

Worker 2

Worker 1

Page 60: VoltDB and Erlang - Tech planet 2012

Crashed Locks Stall

● Locks can need cross-thread error handling.● Stalling and time outs aggravate load.

Locked

Waiting

Doing X1

Resource

Worker 2

Worker 1

Doing X2

Locked

Page 61: VoltDB and Erlang - Tech planet 2012

Processes are Transactional

● One actor is one process, obviously cannot “race itself”.● Mandating a job type to an actor creates a transactional funnel.● Only one such job will ever be executing at any one time.

Do X1 for me!

Do X2 for me!

Doing X1 Doing X2Funnel

Page 62: VoltDB and Erlang - Tech planet 2012

Thinking Parallel

“It's not easy.” Robert Virding

• The Generals’ Problem• Lamport Clocks• No Guarantees

Page 63: VoltDB and Erlang - Tech planet 2012

Generals’ Problem

State

Two generals need to agree when to attack.

Page 64: VoltDB and Erlang - Tech planet 2012

Generals’ Problem

State

One sends a messenger.

Page 65: VoltDB and Erlang - Tech planet 2012

Generals’ Problem

State

Acknowledge.

Page 66: VoltDB and Erlang - Tech planet 2012

Generals’ Problem

State

ACK the ACK. Etc.

Page 67: VoltDB and Erlang - Tech planet 2012

Generals’ Problem

State

The messenger may get lost.

Page 68: VoltDB and Erlang - Tech planet 2012

Byzantine Generals

State

The generals, actually, too.

Page 69: VoltDB and Erlang - Tech planet 2012

Lamport Clocks

Source: Lamport http://research.microsoft.com/users/lamport/pubs/time-clocks.pdf

Order matters more than time.

Page 70: VoltDB and Erlang - Tech planet 2012

Thinking Functional

Small Functions

+ Immutable Variables

→ Don’t assign variables: return results!

Complete State in Plain Sight

→ Awful for updates in place.

→ Awsome for debugging & maintenance.

Erlang is not side-effect free at all.

Page 71: VoltDB and Erlang - Tech planet 2012

Immutable Variables

Can’t assign a second time:

A = A + 1.

A = 1, A = 2.

Page 72: VoltDB and Erlang - Tech planet 2012

Immutable VariablesIt has to be:

B = A + 1.

A = 1, B = 2.

● Prevent Coding Errors● Provide Transactional Semantic● Allow For Pattern Matching Syntax● Can be a Nuisance

Page 73: VoltDB and Erlang - Tech planet 2012

Pattern Matching

This can mean two things:

A = func().

The meaning depends on whether

A is already assigned.

Page 74: VoltDB and Erlang - Tech planet 2012

Pattern Matching

The common, mixed case:

{ok, A} = func().

ok is an assertion AND

A is being assigned.

Page 75: VoltDB and Erlang - Tech planet 2012

Pattern Matching

The common, mixed case:

{ok, A} = func().

“This makes it hard to give Erlang a syntax like e.g. Lua.”

Robert Virding

Page 76: VoltDB and Erlang - Tech planet 2012

Let It Crash!

• No Defense Code• On Error, restart Entire Process• Built-In Process Supervision & Restart• Missing Branches, Matches cause Crash

→ Shorter, Cleaner Code

→ Faster Implementation

→ More Robust: handles All Errors

Page 77: VoltDB and Erlang - Tech planet 2012

Syntax

● Small● Easy● Stable● Declarative● Inspired by Prolog and ML● Obvious State, Implicit Thread

Page 78: VoltDB and Erlang - Tech planet 2012

Fibonacci

Looks like the math description

fib(0) -> 0;fib(1) -> 1;fib(N) when N>1 -> fib(N-1) + fib(N-2).

Page 79: VoltDB and Erlang - Tech planet 2012

Hello, World!

-module(hello).

-export([start/0]).

start() ->

io:format("Hello, World!~n").

Page 80: VoltDB and Erlang - Tech planet 2012

Hello, World! -module(hello).-export([start/0, loop/0]).

start() -> Pid = spawn(hello, loop, []), Pid ! hello.

loop() -> receive hello -> io:format("Hello, World!~n"), loop() end.

From Edward Garson's Blog at http://egarson.blogspot.de/2008/03/real-erlang-hello-world.html

Page 81: VoltDB and Erlang - Tech planet 2012

Start-module(hello).-export([start/0, loop/0]).

start() -> Pid = spawn(hello, loop, []), Pid ! hello.

loop() -> receive hello -> io:format("Hello, World!~n"), loop() end.

From Edward Garson's Blog at http://egarson.blogspot.de/2008/03/real-erlang-hello-world.html

Page 82: VoltDB and Erlang - Tech planet 2012

Output-module(hello).-export([start/0, loop/0]).

start() -> Pid = spawn(hello, loop, []), Pid ! hello.

loop() -> receive hello -> io:format("Hello, World!~n"), loop() end.

From Edward Garson's Blog at http://egarson.blogspot.de/2008/03/real-erlang-hello-world.html

Page 83: VoltDB and Erlang - Tech planet 2012

Process Spawning-module(hello).-export([start/0, loop/0]).

start() -> Pid = spawn(hello, loop, []), Pid ! hello.

loop() -> receive hello -> io:format("Hello, World!~n"), loop() end.

From Edward Garson's Blog at http://egarson.blogspot.de/2008/03/real-erlang-hello-world.html

New Process

Page 84: VoltDB and Erlang - Tech planet 2012

Blocking Receive-module(hello).-export([start/0, loop/0]).

start() -> Pid = spawn(hello, loop, []), Pid ! hello.

loop() -> receive hello -> io:format("Hello, World!~n"), loop() end.

From Edward Garson's Blog at http://egarson.blogspot.de/2008/03/real-erlang-hello-world.html

Page 85: VoltDB and Erlang - Tech planet 2012

Message Passing-module(hello).-export([start/0, loop/0]).

start() -> Pid = spawn(hello, loop, []), Pid ! hello.

loop() -> receive hello -> io:format("Hello, World!~n"), loop() end.

From Edward Garson's Blog at http://egarson.blogspot.de/2008/03/real-erlang-hello-world.html

Page 86: VoltDB and Erlang - Tech planet 2012

Pattern Matching-module(hello).-export([start/0, loop/0]).

start() -> Pid = spawn(hello, loop, []), Pid ! hello.

loop() -> receive hello -> io:format("Hello, World!~n"), loop() end.

From Edward Garson's Blog at http://egarson.blogspot.de/2008/03/real-erlang-hello-world.html

Page 87: VoltDB and Erlang - Tech planet 2012

Tail Recursion-module(hello).-export([start/0, loop/0]).

start() -> Pid = spawn(hello, loop, []), Pid ! hello.

loop() -> receive hello -> io:format("Hello, World!~n"), loop() end.

From Edward Garson's Blog at http://egarson.blogspot.de/2008/03/real-erlang-hello-world.html

Page 88: VoltDB and Erlang - Tech planet 2012

Productivity

Motorola Study running 2002 – 2006:

“Erlang shows …• 2x higher throughput• 3x better latency• 3 - 7x shorter code … than the equivalent C++ implementation.”

Page 89: VoltDB and Erlang - Tech planet 2012

• Ericsson AB 1983 – commercial developer, support• Open Source – 100% dictatorial by Ericsson• Professional Support – by veterans at Erlang Solutions• Made for telecom – complexity & robustness• Functional – improves maintainability• Actors Model – better than OO for parallel worlds• Distributed – seemless use of multi-cores• Parallel – Processes as basic building block• Simple Language – Easy to learn and maintain• Huge Library – OTP is made for server development• Battle-tested – billion $ projects, 1 million LOC• Great Community – bright and friendly devs

Erlang

Page 90: VoltDB and Erlang - Tech planet 2012

Learning Erlang

• Scour Post-Mortems• Download and Install from erlang.org• Fred’s site Learn You Some Erlang! • Joe’s book Programming Erlang• IRC #erlounge• Erlang Mailing List• Local Erlounge Meetings• Erlang Factories & User Conferences

Page 91: VoltDB and Erlang - Tech planet 2012

Erlvolt

Page 92: VoltDB and Erlang - Tech planet 2012

Erlvolt

• The Erlang VoltDB Driver• Stable Synchronous Version• Upcoming Asynchronous Version

https://github.com/Eonblast/Erlvolt

Page 93: VoltDB and Erlang - Tech planet 2012

Erlvolt

Inserterlvolt:callProcedure(Connection, "Insert", ["안녕하세요", "세계", "Korean"]),

SelectResponse = erlvolt:callProcedure(Connection, "Select", ["Korean"]),

Row = erlvolt:fetchRow(Table, 1),

io:format("~n~n~s, ~s!~n",

[ erlvolt:getString(Row, Table, "HELLO"),

erlvolt:getString(Row, Table, "WORLD") ]);

Page 94: VoltDB and Erlang - Tech planet 2012

ResourcesErlang

Webhttp://www.erlang.org/ http://learnyousomeerlang.com/http://www.erlang.org/static/getting_started_quickly.html

Listhttp://erlang.org/mailman/listinfo/erlang-questionshttp://groups.google.com/group/erlang-programming

Bookshttp://pragprog.com/book/jaerlang/programming-erlanghttp://shop.oreilly.com/product/9780596518189.do

Referenceshttp://erldocs.com/ http://www.erlang.org/doc/

Post Mortemshttp://www.facebook.com/note.php?note_id=14218138919 http://www.slideshare.net/wooga/erlang-the-big-switch-in-social-games

Erlvolt

https://github.com/Eonblast/Erlvolt

Eonblast

https://www.eonblast.com

VoltDB

Downloadhttp://voltdb.com/products-services/downloads

Webinarshttp://community.voltdb.com/weninars

Forumhttp://community.voltdb.com/forum

Voter Examplehttps://github.com/VoltDB/voltdb/tree/master/examples/voter

Benchmark Blog Posthttp://voltdb.com/company/blog/695k-tps-nodejs-and-voltdb

Volt’s Magic Saucehttp://nms.csail.mit.edu/~stavros/pubs/hstore.pdf

Page 95: VoltDB and Erlang - Tech planet 2012

Upcoming

• Erlvolt 2 – asynchronous Erlang VoltDB driver• 14 New Databases for Games – Blog Post

TBA on Twitter @hdiedrich

Page 96: VoltDB and Erlang - Tech planet 2012

Questions

• Email: hdiedrich*eonblast.com• Twitter: @hdiedrich• VoltDB: http://community.voltdb.com/forum• Erlang: [email protected] • Eonblast: http://www.eonblast.com• Game News: @eonblast