The term “Microservice Architecture” has been in talks for quite some time now. It is the hot new gossip in Software Architectures. So what is a Microservice? Why should we use it? Why should we use Microservice Architecture in Golang? And what are the pros?
So in this blog, I’ll be covering up the questions related to it. So without much further ado, let’s start.
What is a Microservice?
Microservice is a technique for software development. A form of SOA(Service-Oriented Architecture) style. What it does is, it structures the application as a collective pack of many loosely coupled services. In this architecture, the services coded are usually fine-grained, and the protocols are not that heavy. If we look for a specific definition for Microservice, then there is None, but there are certain characteristics that revolve around the automated deployment, business capability, the decentralized control of data and the intelligence in endpoints.
Why should we use Microservice?
This setup of architecture helps us picture the entire application in parts or in small modules, which makes it easier to understand, develop, and test. It helps us in seeing the services as separated yet clearly specify the use of it in the application. On top of that, it helps in making the project more immune to architectural erosion (It is referred to as the difference between the planned architecture and the final architecture after the development is complete). It helps in parallelizing the development by creating different independent teams to deploy and scale the services assigned to them. It is easier to refactor the code in this architecture. And it enables the continuous flow of delivery and deployment.
Why Build Microservices in Golang?
Before diving in the question. I’d first like to state the advantages of Golang. Although go is a new language compared to others, it has many advantages. Programs coded in golang are more robust. They can withstand heavy loads that allow applications to build with loaded services. Golang is more suited for multiprocessor systems and web apps. Moreover, it integrates with GitHub easily to manage the distributed packages.
The most use of microservice architecture is done when the application needs to be scalable.
And if there is one language that can perfectly fit the criteria, then it is – Golang, the reason that it does is because of its inheritance from C-family programming languages, the components written in golang are easier to combine with components coded in other languages that reside in the same family.
Though Go belongs to the C family, yet it is much more efficient than C/C++. And the syntax is much more simple, somewhat like Python. Golang has many stable syntaxes. It has not changed much since its first public release, making it backward compatible. That gives golang an upper hand compared to other languages. Apart from all this, Golang’s performance beats down Pythons and Javas Performance by a really high number. It is simple as C/C++, making it a great option to develop microservice applications. And for the cherry on top, it has simple to read and understand.
Frameworks in Golang for Microservice Architecture
Now let us discuss the frameworks that we can use for microservice architecture. The frameworks that I’ll be taking into consideration are:
- Go Micro (https://micro.mu/)
- Go Kit (https://gokit.io/)
- Gizmo (https://github.com/NYTimes/gizmo)
Go Micro is, by far the most popular RPC framework I have come across. It is a pluggable RPC framework. Go Micro provides us with the following:
- Service Discovery (Applications are automatically registered with the service discovery system)
- Load Balancing (It provides with client-side load balancing, which helps in balancing the requests between the instances of service)
- Synchronous Communication (Provides with a Req/Res Transport Layer)
- Asynchronous Communication (Has built-in publishing and subscribe capabilities)
- Message Encoding (Encoding and Decoding based on the Content-Type present in the header.)
- RPC Client/Server packages (Leverages the above-stated features and provides exposure to the interface utilized in building Microservices)
The Go Micro architecture can be represented as follows
It is a three-layered stack.
The first layer contains a service abstraction.
The second layer is the client-server model. The server comprises of the blocks that are used in writing the service while the client provides us with an interface for the sole purpose of making requests to the services written in the server model.
The third layer has the plugins of the following types:
- Broker (provides an interface to the message broker in async communication)
- Codec (used to encrypt or decrypt the messages)
- Registry (provides a service search facility)
- Selector (has a load balancing abstraction build up on registry)
- Transport (the channel for the synchronous req/res communication between services)
It also provides a feature named Sidecar. Sidecar enables you to integrate the services written in languages other than Go. It also provides us with gRPC encoding/decoding, service registration and HTTP handlers.
Go Kit is a programming toolkit for building microservices. Unlike Go Micro, it is a library that can be imported in a binary package. The rules of Go Kit are quite simple. They are as follows:
- No Global State
- Declarative Composition
- Explicit Dependencies
- Interface as Contracts
- Domain Driven Design
Go Kit provides you with the packages for the following:
- Authentication (BasicAuth and JWT)
- Transport (HTTP, gRPC, etc)
- Logging (interface for structured logging in services)
- Metrics (CloudWatch, Statsd, Graphite, and others)
- Tracing ( Zipkin and Opentracing)
- Service discovery (Consul, Etcd, Eureka, and others)
- Circuitbreaker (Hystrix implementation in Go)
The Service Architecture of Go Kit is as follows.
Gizmo is a microservice toolkit from the New York Times. It provides packages to put together server and pubsub daemons. It exposes the following packages:
- Server (offers two server implementations: SimpleServer (over HTTP), RPCServer (over gRPC))
- Server/kit (experimental package based on Go Kit)
- Config (contains functions to the configuration from JSON files, JSON blobs in Consul k/v, or environment variables)
- Pubsub (provides generic interfaces for publishing and consuming data from the queues)
- Pubsub/pubsubtest (contains test implementations of the publisher and subscriber interfaces)
- Web (exposes functions for parsing types from request queries and payloads)
Pubsub package provides interfaces to work with the following queues:
- pubsub/aws: for Amazon SNS/SQS
- pubsub/gcp: for Google Pubsub
- pubsub/kafka: for Kafka topics
- pubsub/http: for publishing via HTTP
So, in my opinion, Gizmo sits somewhere between Go Micro and Go Kit. It’s not a complete “blackbox” like Go Micro. At the same time, it’s not as raw as Go Kit. It provides higher-level building components such as config and pubsub packages.
Kite is a framework for developing microservices in Go. It exposes RPC Client and Server packages. Created services are automatically registered with a service discovery system Kontrol. Kontrol is written in Kite and it’s a Kite service itself. It means that the Kite microservices work well within its environment. If you need to connect Kite microservice to another service discovery system, it will require customization. This is one of the significant reasons I opy out Kite from the list and decided not to review this framework.
So, if you find this blog useful and wondering how to create versatile microservices in Golang, then hire golang developers from us and leverage the top-of-the-line expertise.