scaling applications with go
TRANSCRIPT
SCALING APPLICATIONS
WITH GO
-Vimlesh Sharma
Introduction
What does Go offer?
Where go can be used?
Go in Production
Go in comparison to other language
Case studies
TABLE OF CONTENTS
Go is a general-purpose programming language, like Python,
Java, or C
It is commonly referred to as golang.
A programming language developed at Google in 2007
Main contributor were Robert Griesemer, Rob Pike, and Ken
Thompson.
INTRODUCTION
WHAT DOES GO OFFER ?
Go is relatively
compact language
easier to learn
Go language spec is webpage (http://golang.org/ref/spec )
that prints as a 56 page PDF compared to others
Java SE – 768 pages http://docs.oracle.com/javase/specs/
Scala – 183 pages http://www.scala-
lang.org/docu/files/ScalaReference.pdf
Go documentation is better than other languages.
A SIMPLE LANGUAGE THAT IS EASY TO
LEARN AND READ.
Go combines ease of programming in an interpreted &
dynamically typed language.
Also it provides high efficiency and safety of a statically
typed, compiled language.
var intA int
intA = 32 // intA has 32 and is static type int
intB := 42 // intB has value 42 and dynamic type int
STATICALLY TYPED, BUT WITH A DYNAMIC
FEEL
Like C and C++, Go compiles to native machine code so that
we don’t need environments such as CLR and JVM to run Go
applications.
Go compiler compiles programs very quickly that helps
particularly when compiling large applications
‘Gocc ’ is a go compiler’s compiler
https://code.google.com/p/gocc/
COMPILED TO NATIVE MACHINE CODE
Go is concurrent system
Concurrency is a way to structure a program by breaking it
into pieces that can be executed independently.
Concurrency vs. parallelism
Concurrency is about dealing with lots of things at once
Parallelism is about doing lots of things at once
Not the same, but related
Concurrency is about structure, parallelism is about execution
Concurrency provides a way to structure a solution to solve a
problem that may (but not necessarily) be parallelizable
GO SUPPORTS CONCURRENCY
In Go, concurrency is a first -class citizen in the core language
Go introduces Goroutines, which lets you run functions
concurrently.
You can communicate and pass values between Goroutines
using Channels.
Goroutines and Channels are great features of Go that let you
leverage concurrency and parallelism
LANGUAGE-LEVEL CONCURRENCY FEATURES
Go’s standard libraries are exemplary.
Go also provide 3rd party packages.
Go’s package management system is designed to work with
modern scenarios
e.g. sharing it with developers overs GIT repository) unlike NPM,
NuGet, RubyGem
EXPANSIVE STANDARD LIBRARY
Go provides lots of tools for managing documentation,
testing, package management, and more.
go doc : can be used to pull documentation for anything in
codebase.
go test : This is one of the easiest testing setup
go get : This is used to pull the shared source code from GIT
GREAT TOOLS
WHERE CAN GO CAN BE
USED?
Server application
Command-line tools - https://github.com/codegangsta/cli
Web applications
Games - http://go-ngine.com/
Scientific computing -
https://archive.fosdem.org/2014/schedule/event/hpc_devroo
m_go/
Android & IOS development (in upcoming versions)
Many More...
USAGE
USING GO IN
PRODUCTION
Go produces statically linked binaries that are self sufficient.
Single binary makes deployment super -easy.
This is great when compared to other dependency setup that
is required for most other application.
SINGLE BINARY
Go has excellent support for cross compilation.
gonative is a simple tool which creates a build of Go that can
cross compile to all platforms. –
http://github.com/inconshreveable/gonative
http://dave.cheney.net/2013/07/09/an-introduction-to-cross-
compilation-with-go-1-1
CROSS COMPILATION
There is excellent support for
Live profiling capability for CPU
Memory
Goroutines
OS threads
Lock contention etc
Read more at http://blog.golang.org/profiling -go-programs
Use go toolchain and profile the application on-demand.
BUILT-IN PROFILING
C++ Java Javascript Scala Go
Type Safe
Garbage Collection
Compilation Slow Slow NA Slow Fast
Concurrency
Ease of Programming
Efficiency Highest Moderate Slow Good Close to C++
GO IN COMPARISON TO OTHER LANGUAGE
BENCHMARKING ON JSON RESPONSE.
Nodejs 32.8% https://www.techempower.com/benchmarks/#section=data-r9&hw=i7&test=json
CASE STUDY FROM
GOPHERCONINDIA 15
Ruby is a complex language
Memory consumption and speed
Lot of caching data in memory for speed, GC in Ruby consumed a lot
of time ultimately hindering performance.
Concurrency
The safest way to do concurrency in Ruby is through the use of
multiple processes, so app uses many workers and hence memory
consumption.
FROM A RUBY MONOLITH TO MICROSERVICES
IN GO
Instead of refactoring the entire application, moving selected
component to a micro services architecture in go increased
the efficiency.
Main Application + Go Micro services boost application
performance.
https://sourcegraph.com/blog/live/gopherconindia/1126565
68167
SOLUTION/OBSERVATION
N1QL pronounced as “nickel”.
N1QL is a query language developed in Go for Couchbase, a
distributed NoSQL database.
With N1QL scalable application can be developed quickly
N1QL: A QUERY LANGUAGE IN GO
SIGNIFICANT LOWER DEVELOPMENT COST.
C++ 210 days Go 150 days C++ 400 bugs Go 250 bugs
Benchmarks show that Go compares favorably with C++ and
Java
In C++, they had to experiment with a bunch of dif ferent
libraries
In Java, they had to fiddle around with a bunch of parameters
l ike the min heap size
Go worked well out of the box -
https://sourcegraph.com/blog/live/gopherconindia/1116380
91937
SOLUTION/OBSERVATION
Extract dominant color from images.
Generate Insight based on product color
Pain Point:
They were having high system usage.
Avg mem. consumption was more than 70%
PROCESSING IMAGES WITH GO
Existing architecture
Modified architecture
Serves more request
Server usage never exceeds more than 50 -60%
Easier deployment
Only golang standard library used -
https://sourcegraph.com/blog/live/gopherconindia/1116486
97747
SOLUTION/OBSERVATION
Requirement
Static binary.
Efficient Code.
Zero latency(real time)
Cross Platform
Initial choice was Python (Dropped – Restricted library)
EMBD a package entirely written in Go
High Performance.
SOLUTION/OBSERVATION
https://sourcegraph.com/blog/live/gopherconindia/111626334627
LIST OF COMPANIES USING GO IN
PRODUCTION
Golang’s 3 hours online jumpstart course
http://tour.golang.org/welcome/1
Testimonials
https://sendgrid.com/blog/convince-company-go-golang/
http://www.scriptrock.com/blog/our-experience-with-golang
Further reading
http://golang.org/doc/
https://talks.golang.org/
http://thenewstack.io/a-closer-look-at-golang-from-an-architects-
perspective/
QUICK LINKS
THANKS