Four years of Go at CloudFlare John Graham-Cumming CloudFlare - - PowerPoint PPT Presentation

four years of go at cloudflare
SMART_READER_LITE
LIVE PREVIEW

Four years of Go at CloudFlare John Graham-Cumming CloudFlare - - PowerPoint PPT Presentation

Four years of Go at CloudFlare John Graham-Cumming CloudFlare You likely use us without knowing it Server Languages 2011 www data request PHP backend pipeline logic Server Languages 2011


slide-1
SLIDE 1

Four ¡years ¡of ¡Go ¡at ¡CloudFlare

John ¡Graham-­‑Cumming

slide-2
SLIDE 2

CloudFlare

You likely use us without knowing it…

slide-3
SLIDE 3

Server ¡Languages ¡2011

PHP

www backend data pipeline request logic

slide-4
SLIDE 4

Server ¡Languages ¡2011

PHP C

www backend data pipeline request logic nginx modules

slide-5
SLIDE 5

Server ¡Languages ¡2011

PHP C C++

www backend data pipeline request logic nginx modules data pipeline

slide-6
SLIDE 6

Server ¡Languages ¡2015

PHP

www backend

slide-7
SLIDE 7

Server ¡Languages ¡2015

Lua PHP

request logic www backend

ngx_lua/OpenResty

slide-8
SLIDE 8

Server ¡Languages ¡2015

Lua PHP

request logic www backend

C

nginx modules

C++ eliminated

slide-9
SLIDE 9

Server ¡Languages ¡2015

Go Lua PHP

data pipeline request logic www backend

C

nginx modules

C++ eliminated

slide-10
SLIDE 10

Server ¡Languages ¡2015

Go Lua PHP

data pipeline Lots of new things! request logic www backend

C

nginx modules

C++ eliminated

slide-11
SLIDE 11

People ¡often ¡ask ¡me: ¡ ¡ “How ¡did ¡you ¡persuade ¡ CloudFlare ¡to ¡use ¡Go?”

slide-12
SLIDE 12

Back ¡in ¡mid-­‑1980s…

slide-13
SLIDE 13

A ¡couple ¡of ¡years ¡later…

slide-14
SLIDE 14

A ¡couple ¡of ¡years ¡later…

slide-15
SLIDE 15
  • ccam
slide-16
SLIDE 16

OK ¡ So, ¡I’m ¡a ¡concurrent-­‑processes-­‑ communicating-­‑via-­‑channels ¡hipster

slide-17
SLIDE 17
slide-18
SLIDE 18

Railgun ¡(2012)

  • First ¡major ¡Go ¡project ¡at ¡CloudFlare ¡
  • Wrote ¡prototype ¡in ¡Perl ¡
  • Delta-­‑compression ¡based ¡acceleration ¡for ¡origin ¡connections ¡
  • 16,626 ¡lines ¡of ¡Go; ¡2,518 ¡lines ¡of ¡Go ¡tests ¡
  • Single ¡executable ¡deployment ¡== ¡happiness ¡
slide-19
SLIDE 19

RRDNS ¡(2013)

  • Authoritative ¡DNS ¡server ¡— ¡has ¡to ¡be ¡fast! ¡
  • Also ¡does ¡DNS ¡proxying ¡and ¡DNSSEC ¡
  • 15,143 ¡lines ¡of ¡Go; ¡9,377 ¡lines ¡of ¡Go ¡tests ¡
slide-20
SLIDE 20

CFSSL ¡(2014)

  • TLS/SSL/PKI ¡Toolkit ¡
  • Can ¡be ¡used ¡as ¡a ¡CA ¡
  • Does ¡optimal ¡cert ¡bundling ¡
  • https://github.com/cloudflare/cfssl
slide-21
SLIDE 21

Polish ¡(2014)

  • Automatic ¡image ¡recompression ¡
  • Recompresses ¡images ¡in ¡CloudFlare ¡cache ¡in ¡

background ¡

  • Uses ¡cgo ¡to ¡talk ¡to ¡FreeImage ¡and ¡shells ¡out ¡to ¡

pngcrush, ¡etc. ¡

  • 5,977 ¡lines ¡of ¡Go; ¡3,635 ¡lines ¡of ¡Go ¡tests
slide-22
SLIDE 22

Data ¡(2014)

  • CloudFlare’s ¡event ¡log ¡aggregation ¡and ¡analysis ¡
  • Event ¡log ¡forwarding ¡
  • Attack ¡analysis ¡
  • Use ¡Cap’n ¡Proto ¡instead ¡of ¡gob ¡
  • 103,986 ¡lines ¡of ¡Go; ¡36,585 ¡lines ¡of ¡Go ¡tests
slide-23
SLIDE 23

Started ¡using ¡Go ¡pre-­‑v1.0

  • First ¡Go ¡project ¡shipped ¡using ¡go1.0.2 ¡(mid-­‑2012) ¡
  • crypto ¡and ¡compression ¡were ¡slow ¡
  • used ¡cgo ¡to ¡call ¡C/assembler ¡code ¡
  • log/syslog ¡was ¡“odd” ¡
  • Worst ¡problem… ¡on ¡BSD



 
 


slide-24
SLIDE 24

Started ¡using ¡Go ¡pre-­‑v1.0

  • First ¡Go ¡project ¡shipped ¡using ¡go1.0.2 ¡(mid-­‑2012) ¡
  • crypto ¡and ¡compression ¡were ¡slow ¡
  • used ¡cgo ¡to ¡call ¡C/assembler ¡code ¡
  • log/syslog ¡was ¡“odd” ¡
  • Worst ¡problem… ¡on ¡BSD



 
 


slide-25
SLIDE 25

Started ¡using ¡Go ¡pre-­‑v1.0

  • First ¡Go ¡project ¡shipped ¡using ¡go1.0.2 ¡(mid-­‑2012) ¡
  • crypto ¡and ¡compression ¡were ¡slow ¡
  • used ¡cgo ¡to ¡call ¡C/assembler ¡code ¡
  • log/syslog ¡was ¡“odd” ¡
  • Worst ¡problem… ¡on ¡BSD



 
 


slide-26
SLIDE 26

Started ¡using ¡Go ¡pre-­‑v1.0

  • First ¡Go ¡project ¡shipped ¡using ¡go1.0.2 ¡(mid-­‑2012) ¡
  • crypto ¡and ¡compression ¡were ¡slow ¡
  • used ¡cgo ¡to ¡call ¡C/assembler ¡code ¡
  • log/syslog ¡was ¡“odd” ¡
  • Worst ¡problem… ¡on ¡BSD



 
 


void runtime·SysUnused(void *v, uintptr n) { USED(v); USED(n); // TODO(rsc): call madvise MADV_DONTNEED }

slide-27
SLIDE 27

What ¡we ¡write ¡in ¡Go

  • “Production ¡Lines” ¡
  • Small ¡programs ¡that ¡do ¡small ¡jobs ¡and ¡need ¡to ¡scale ¡up ¡and ¡down ¡
  • Polish, ¡data ¡pipeline ¡
  • “Cronnies” ¡
  • Small ¡programs ¡that ¡run ¡periodically ¡but ¡sometimes ¡need ¡a ¡kick ¡
  • “Network ¡I/O” ¡
  • Programs ¡that ¡do ¡a ¡lot ¡of ¡network ¡I/O ¡
  • RRDNS, ¡Railgun ¡
  • “Crypto ¡Things” ¡
  • Internal ¡cryptographic ¡tools
slide-28
SLIDE 28

What ¡we ¡write ¡in ¡Go

  • “Production ¡Lines” ¡
  • Small ¡programs ¡that ¡do ¡small ¡jobs ¡and ¡need ¡to ¡scale ¡up ¡and ¡down ¡
  • Polish, ¡data ¡pipeline ¡
  • “Cronnies” ¡
  • Small ¡programs ¡that ¡run ¡periodically ¡but ¡sometimes ¡need ¡a ¡kick ¡
  • “Network ¡I/O” ¡
  • Programs ¡that ¡do ¡a ¡lot ¡of ¡network ¡I/O ¡
  • RRDNS, ¡Railgun ¡
  • “Crypto ¡Things” ¡
  • Internal ¡cryptographic ¡tools
slide-29
SLIDE 29

What ¡we ¡write ¡in ¡Go

  • “Production ¡Lines” ¡
  • Small ¡programs ¡that ¡do ¡small ¡jobs ¡and ¡need ¡to ¡scale ¡up ¡and ¡down ¡
  • Polish, ¡data ¡pipeline ¡
  • “Cronnies” ¡
  • Small ¡programs ¡that ¡run ¡periodically ¡but ¡sometimes ¡need ¡a ¡kick ¡
  • “Network ¡I/O” ¡
  • Programs ¡that ¡do ¡a ¡lot ¡of ¡network ¡I/O ¡
  • RRDNS, ¡Railgun ¡
  • “Crypto ¡Things” ¡
  • Internal ¡cryptographic ¡tools
slide-30
SLIDE 30

What ¡we ¡write ¡in ¡Go

  • “Production ¡Lines” ¡
  • Small ¡programs ¡that ¡do ¡small ¡jobs ¡and ¡need ¡to ¡scale ¡up ¡and ¡down ¡
  • Polish, ¡data ¡pipeline ¡
  • “Cronnies” ¡
  • Small ¡programs ¡that ¡run ¡periodically ¡but ¡sometimes ¡need ¡a ¡kick ¡
  • “Network ¡I/O” ¡
  • Programs ¡that ¡do ¡a ¡lot ¡of ¡network ¡I/O ¡
  • RRDNS, ¡Railgun ¡
  • “Crypto ¡Things” ¡
  • Internal ¡cryptographic ¡tools
slide-31
SLIDE 31

What ¡we ¡didn’t ¡write ¡in ¡Go

  • Keyless ¡SSL ¡
  • Wanted ¡to ¡link ¡against ¡OpenSSL ¡
  • 4,175 ¡lines ¡of ¡C; ¡2,044 ¡lines ¡of ¡tests ¡
  • “Request ¡Logic” ¡
  • nginx ¡integration ¡with ¡LuaJIT ¡excellent ¡and ¡fast ¡
  • www ¡backend ¡
  • Lots ¡of ¡legacy ¡PHP ¡code

Seemed like a good idea at the time!

slide-32
SLIDE 32

What ¡we ¡didn’t ¡write ¡in ¡Go

  • Keyless ¡SSL ¡
  • Wanted ¡to ¡link ¡against ¡OpenSSL ¡
  • 4,175 ¡lines ¡of ¡C; ¡2,044 ¡lines ¡of ¡tests ¡
  • “Request ¡Logic” ¡
  • nginx ¡integration ¡with ¡LuaJIT ¡excellent ¡and ¡fast ¡
  • www ¡backend ¡
  • Lots ¡of ¡legacy ¡PHP ¡code

Uses libuv for concurrency

slide-33
SLIDE 33

Aside: ¡Shakespeare ¡as ¡ OpenSSL ¡programmer ¡

MACDUFF O horror, horror, horror! Tongue nor heart cannot conceive nor name thee! MACBETH & LENNOX What’s the matter? MACDUFF Confusion now hath made his masterpiece.

slide-34
SLIDE 34

What ¡we ¡didn’t ¡write ¡in ¡Go

  • Keyless ¡SSL ¡
  • Wanted ¡to ¡link ¡against ¡OpenSSL ¡
  • 4,175 ¡lines ¡of ¡C; ¡2,044 ¡lines ¡of ¡tests ¡
  • “Request ¡Logic” ¡
  • nginx ¡integration ¡with ¡LuaJIT ¡excellent ¡and ¡fast ¡
  • www ¡backend ¡
  • Lots ¡of ¡legacy ¡PHP ¡code
slide-35
SLIDE 35

What ¡we ¡didn’t ¡write ¡in ¡Go

  • Keyless ¡SSL ¡
  • Wanted ¡to ¡link ¡against ¡OpenSSL ¡
  • 4,175 ¡lines ¡of ¡C; ¡2,044 ¡lines ¡of ¡tests ¡
  • “Request ¡Logic” ¡
  • nginx ¡integration ¡with ¡LuaJIT ¡excellent ¡and ¡fast ¡
  • www ¡backend ¡
  • Lots ¡of ¡legacy ¡PHP ¡code
slide-36
SLIDE 36

Today’s ¡Pain ¡Points

  • Garbage ¡Collection ¡
  • Stop ¡the ¡world ¡is ¡a ¡world ¡of ¡hurt ¡
  • Spend ¡significant ¡effort ¡on ¡eliminating ¡garbage ¡

creation ¡

  • Third-­‑party ¡import ¡management
slide-37
SLIDE 37

Garbage ¡Elimination

  • Top ¡tips ¡(thanks ¡Daniel ¡Morsing!) ¡
  • Use ¡io.Reader/io.Writer ¡ ¡
  • Don’t ¡allocate ¡small ¡structs; ¡pass ¡them ¡by ¡value ¡
  • Use ¡arrays ¡for ¡buffers ¡instead ¡of ¡slices ¡when ¡you ¡

can ¡

  • Profile!
slide-38
SLIDE 38

Lots ¡of ¡little ¡programs

  • torhoney: ¡get ¡list ¡of ¡Tor ¡exit ¡nodes, ¡score ¡against ¡

Project ¡Honeypot ¡

  • https://github.com/cloudflare/golibs ¡
  • bytepool, ¡circularbuffer, ¡ewma, ¡lrucace, ¡pool, ¡

spacesaving ¡

  • http://cloudflare.github.io/#cat-­‑Go