Book Image

Building Microservices with Go

By : Nic Jackson
Book Image

Building Microservices with Go

By: Nic Jackson

Overview of this book

Microservice architecture is sweeping the world as the de facto pattern to build web-based applications. Golang is a language particularly well suited to building them. Its strong community, encouragement of idiomatic style, and statically-linked binary artifacts make integrating it with other technologies and managing microservices at scale consistent and intuitive. This book will teach you the common patterns and practices, showing you how to apply these using the Go programming language. It will teach you the fundamental concepts of architectural design and RESTful communication, and show you patterns that provide manageable code that is supportable in development and at scale in production. We will provide you with examples on how to put these concepts and patterns into practice with Go. Whether you are planning a new application or working in an existing monolith, this book will explain and illustrate with practical examples how teams of all sizes can start solving problems with microservices. It will help you understand Docker and Docker-Compose and how it can be used to isolate microservice dependencies and build environments. We finish off by showing you various techniques to monitor, test, and secure your microservices. By the end, you will know the benefits of system resilience of a microservice and the advantages of Go stack.
Table of Contents (18 chapters)
Title Page
Credits
About the Author
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface
Index

Benchmarking and profiling


Go has two excellent ways to analyze the performance of your code. We have benchmark tests and the fantastic pprof.

Benchmarks

Benchmarking is a way of measuring the performance of your code by executing it multiple times with a fixed workload. We took a look at this briefly in Chapter 1, Introduction to Microservices, where we ascertained that the json.Marshal method was slower than the json.Encode method. While this is a useful feature, I find it tough to work out what I should benchmark. If I am writing an algorithm, then this is relatively straightforward. However, when writing a microservice that is predominately interacting with a database, it is far more challenging.

To demonstrate how easy it is to execute benchmarks in Go, take a look at chandlers/search_bench_test.go:

  11 func BenchmarkSearchHandler(b *testing.B) { 
  12   mockStore = &data.MockStore{} 
  13   mockStore.On("Search", "Fat Freddy's Cat").Return([]data.Kitten{ 
  14     data.Kitten{ 
 ...