best practices-for-production-environments

45
Best Practices for Production Environments

Upload: artem-kovardin

Post on 19-Aug-2014

2.601 views

Category:

Engineering


6 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Best practices-for-production-environments

Best Practices for Production Environments

Page 2: Best practices-for-production-environments

These slides may not make much sense without a narrative!

For the proper article, see http://peter.bourgon.org/go-in-production

Page 3: Best practices-for-production-environments
Page 4: Best practices-for-production-environments

Some GoProjects

Bazooka Traffic tier Media streaming Search & Explore glue Prometheus Stream backend* HLS implementation** Many others…

Page 5: Best practices-for-production-environments

Dev environment Repo structure Formatting and style Configuration Logging and telemetry Validation and testing Dependency management ᕕ( ᐛ )ᕗ Build and deploy

Page 6: Best practices-for-production-environments

Dev environment

Page 7: Best practices-for-production-environments

Single GOPATH for all projects

Work in $GOPATH/src/github.com/soundcloud/foo

vim, Sublime Text, emacs—no IDEs

Page 8: Best practices-for-production-environments

Repo structure

Page 9: Best practices-for-production-environments

github.com/soundcloud/whatever/ README.md Makefile main.go support.go

Page 10: Best practices-for-production-environments

github.com/soundcloud/whatever/ README.md Makefile main.go support.go foo/ foo.go bar.go

Page 11: Best practices-for-production-environments

github.com/soundcloud/whatever/ README.md Makefile whatever-server/ main.go whatever-worker/ main.go foo/ foo.go bar.go

Page 12: Best practices-for-production-environments

Formatting and style

Page 13: Best practices-for-production-environments

go fmt

Google’s Code Review Guidelines

Avoid named return parameters

Avoid make and new (unless you know sizes)

Use struct{} for sentinel value: sets, signal chans

Break long lines on parameters

Page 14: Best practices-for-production-environments

func process(dst io.Writer, readTimeout, writeTimeout time.Duration, allowInvalid bool, max int, src <-chan util.Job) { // ...}

Page 15: Best practices-for-production-environments

func process( dst io.Writer, readTimeout, writeTimeout time.Duration, allowInvalid bool, max int, src <-chan util.Job,) { // ...}

Page 16: Best practices-for-production-environments

f := foo.New(foo.Config{ “zombo.com”, conference.KeyPair{ Key: “gophercon”, // String value Value: 2014, }, os.Stdout,})

Page 17: Best practices-for-production-environments

Configuration

Page 18: Best practices-for-production-environments

package flag

Page 19: Best practices-for-production-environments

func main() { var ( foo = flag.String(“foo”, “doch”, “...”) bar = flag.Int(“bar”, 34, “...”) ) flag.Parse() // ...}

Page 20: Best practices-for-production-environments

Logging and telemetry

Page 21: Best practices-for-production-environments

Logging and telemetry

Page 22: Best practices-for-production-environments

package log

Page 23: Best practices-for-production-environments

Logging and telemetry

Page 24: Best practices-for-production-environments

Pushmodel

Pullmodel

vs.

GraphiteStatsd

AirBrake

expvarPrometheus

others?

Page 25: Best practices-for-production-environments

Testing and validation

Page 26: Best practices-for-production-environments

Testing and validation

Page 27: Best practices-for-production-environments

package testing☞reflect.DeepEqual☜

Page 28: Best practices-for-production-environments

// +build integrationvar fooAddr = flag.String(...)func TestFoo(t *testing.T) { f, err := newFoo(*fooAddr) // ...}

Page 29: Best practices-for-production-environments

Testing and validation

Page 30: Best practices-for-production-environments

Save Buildonon

go fmt goimports

or go vet golint go test

andand maybe

Deployon

go test -tags=integration

gocov?

Page 31: Best practices-for-production-environments

YAGNI

Page 32: Best practices-for-production-environments

Dependency management

Page 33: Best practices-for-production-environments
Page 34: Best practices-for-production-environments

How importantis your project?

go get -d& hope!

VENDOR

Eh

Very

import proxy?

Page 35: Best practices-for-production-environments

VENDOR

submoduleswithsubtreeswith a toolwith

✖✖✖means copy your dependencies

Page 36: Best practices-for-production-environments

Binary

VENDOR

Library

_vendorsubdirectory

Blessed build, withprefixed GOPATH+

vendorsubdirectory

Rewrite yourimports+

Page 37: Best practices-for-production-environments

Build and deploy

Page 38: Best practices-for-production-environments

Build and deploy

Page 39: Best practices-for-production-environments

Development“Official”

☞ go build

☞ make

Page 40: Best practices-for-production-environments

GOVER=go1.2.1 !STAGE=.stage GOPATH=$(CURDIR)/$(STAGE)/gopath GOROOT=$(CURDIR)/$(STAGE)/go GOCC=$(GOROOT)/bin/go GO=TMPDIR=/tmp GOROOT=$(GOROOT) GOPATH=$(GOPATH) $(GOCC) !OS=$(shell uname) ARCH=$(shell uname -m) GOOS=$(subst Darwin,darwin,$(subst Linux,linux,$(OS))) GOARCH=$(subst x86_64,amd64,$(ARCH)) GOPKG=$(subst darwin-amd64,darwin-amd64-osx10.8,$(GOVER).$(GOOS)-$(GOARCH).tar.gz) !PKGBASE=github.com/soundcloud/goku PKGPATH=$(GOPATH)/src/$(PKGBASE) !all: build !build: $(GOCC) $(PKGPATH) GOPATH=$(GOPATH) GOROOT=$(GOROOT) GO=$(GOCC) make -C roshi-server build GOPATH=$(GOPATH) GOROOT=$(GOROOT) GO=$(GOCC) make -C roshi-walker build GOPATH=$(GOPATH) GOROOT=$(GOROOT) GO=$(GOCC) make -C reader build GOPATH=$(GOPATH) GOROOT=$(GOROOT) GO=$(GOCC) make -C writer build GOPATH=$(GOPATH) GOROOT=$(GOROOT) GO=$(GOCC) make -C gap-backfill build !clean: GOPATH=$(GOPATH) GOROOT=$(GOROOT) GO=$(GOCC) make -C roshi-server clean GOPATH=$(GOPATH) GOROOT=$(GOROOT) GO=$(GOCC) make -C roshi-walker clean

Page 41: Best practices-for-production-environments

Build and deploy

Page 42: Best practices-for-production-environments

StatefulStateless

Any/no model12-Factor model

ProvisionedScaled

vs.

Containers Containers?

MySQL, RedisRequest router

Page 43: Best practices-for-production-environments

$ git push bazooka master$ bazooka scale -r <new> -n 4 ...$ # validate$ bazooka scale -r <old> -n 0 ...

Deploying stateless services

Page 44: Best practices-for-production-environments

Embrace simplicity

Page 45: Best practices-for-production-environments

Thanks! !

Peter Bourgon @peterbourgon

Questions?