High Performance Systems in Go - GopherCon 2014

38
High Performance Systems in Go Derek Collison April 24, 2014 GopherCon

Transcript of High Performance Systems in Go - GopherCon 2014

Page 1: High Performance Systems in Go - GopherCon 2014

High Performance Systems in Go

Derek Collison April 24, 2014 GopherCon

Page 2: High Performance Systems in Go - GopherCon 2014

About

Architected/Built TIBCO Rendezvous and EMS Messaging SystemsDesigned and Built CloudFoundry at VMwareCo-founded AJAX APIs group at GoogleDistributed SystemsFounder of Apcera, Inc. in San Francisco, CA@[email protected]

Derek Collison

Page 3: High Performance Systems in Go - GopherCon 2014

Why Go?• Simple Compiled Language

• Good Standard Library

• Concurrency

• Synchronous Programming Model

• Garbage Collection

• STACKS!

Page 4: High Performance Systems in Go - GopherCon 2014

Why Go?

• Not C/C++

• Not Java (or any JVM based language)

• Not Ruby/Python/Node.js

Page 5: High Performance Systems in Go - GopherCon 2014
Page 6: High Performance Systems in Go - GopherCon 2014

What about High Performance?

Page 7: High Performance Systems in Go - GopherCon 2014

NATS

Page 8: High Performance Systems in Go - GopherCon 2014
Page 9: High Performance Systems in Go - GopherCon 2014

NATS Messaging 101• Subject-Based

• Publish-Subscribe

• Distributing Queueing

• TCP/IP Overlay

• Clustered Servers

• Multiple Clients (Go, Node.js, Java, Ruby)

Page 10: High Performance Systems in Go - GopherCon 2014
Page 11: High Performance Systems in Go - GopherCon 2014
Page 12: High Performance Systems in Go - GopherCon 2014

NATS• Originally written to support CloudFoundry

• In use by CloudFoundry, Baidu, Apcera and others

• Written first in Ruby -> 150k msgs/sec

• Rewritten at Apcera in Go (Client and Server)

• First pass -> 500k msgs/sec

• Current Performance -> 5-6m msgs/sec

Page 13: High Performance Systems in Go - GopherCon 2014

Tuning NATS (gnatsd)

or how to get from 500k to 6m

Page 14: High Performance Systems in Go - GopherCon 2014
Page 15: High Performance Systems in Go - GopherCon 2014

Target Areas

• Shuffling Data

• Protocol Parsing

• Subject/Routing

Page 16: High Performance Systems in Go - GopherCon 2014

Target Areas

• Shuffling Data

• Protocol Parsing!

• Subject/Routing

Page 17: High Performance Systems in Go - GopherCon 2014

Protocol Parsing• NATS is a text based protocol

• PUB foo.bar 2\r\nok\r\n

• SUB foo.> 2\r\n

• Ruby version based on RegEx

• First Go version was port of RegEx

• Current is zero allocation byte parser

Page 18: High Performance Systems in Go - GopherCon 2014
Page 19: High Performance Systems in Go - GopherCon 2014
Page 20: High Performance Systems in Go - GopherCon 2014
Page 21: High Performance Systems in Go - GopherCon 2014

Some Tidbits

• Early on, defer was costly

• Text based proto needs conversion from ascii to int

• This was also slow due to allocations in strconv.ParseInt

Page 22: High Performance Systems in Go - GopherCon 2014

defer

Page 23: High Performance Systems in Go - GopherCon 2014

defer Results

golang1.3 looks promising

Page 24: High Performance Systems in Go - GopherCon 2014

parseSize

Page 25: High Performance Systems in Go - GopherCon 2014

parseSize vs

strconv.ParseInt

Page 26: High Performance Systems in Go - GopherCon 2014

Target Areas

• Shuffling Data

• Protocol Parsing

• Subject/Routing

Page 27: High Performance Systems in Go - GopherCon 2014

Subject Router

• Matches subjects to subscribers

• Utilizes a trie of nodes and hashmaps

• Has a frontend dynamic eviction cache

• Uses []byte as keys (Go’s builtin does not)

Page 28: High Performance Systems in Go - GopherCon 2014

Subject Router

• Tried to avoid []byte -> string conversions

• Go’s builtin hashmap was slow pre 1.0

• Built using hashing algorithms on []byte

• Built on hashmaps with []byte keys

Page 29: High Performance Systems in Go - GopherCon 2014

Hashing Algorithms

Page 30: High Performance Systems in Go - GopherCon 2014

Hashing Algorithms

Page 31: High Performance Systems in Go - GopherCon 2014

Jesteress

Page 32: High Performance Systems in Go - GopherCon 2014

HashMap Comparisons

Page 33: High Performance Systems in Go - GopherCon 2014

Some Lessons Learned• Use go tool pprof (linux)

• Avoid short lived objects on the heap

• Use the stack or make long lived objects

• Benchmark standard library builtins (strconv)

• Benchmark builtins (defer, hashmap)

• Don’t use channels in performance critical path

Page 34: High Performance Systems in Go - GopherCon 2014

Big Lesson Learned?

Page 35: High Performance Systems in Go - GopherCon 2014

Go is a good choice for performance based

systems

Page 36: High Performance Systems in Go - GopherCon 2014

Go is getting better faster than the others

Page 37: High Performance Systems in Go - GopherCon 2014

Thanks

Page 38: High Performance Systems in Go - GopherCon 2014

Resources

• https://github.com/apcera/gnatsd

• https://github.com/apcera/nats

• https://github.com/derekcollison/nats