Book Image

Microservice Patterns and Best Practices

By : Vinicius Feitosa Pacheco
Book Image

Microservice Patterns and Best Practices

By: Vinicius Feitosa Pacheco

Overview of this book

Microservices are a hot trend in the development world right now. Many enterprises have adopted this approach to achieve agility and the continuous delivery of applications to gain a competitive advantage. This book will take you through different design patterns at different stages of the microservice application development along with their best practices. Microservice Patterns and Best Practices starts with the learning of microservices key concepts and showing how to make the right choices while designing microservices. You will then move onto internal microservices application patterns, such as caching strategy, asynchronism, CQRS and event sourcing, circuit breaker, and bulkheads. As you progress, you'll learn the design patterns of microservices. The book will guide you on where to use the perfect design pattern at the application development stage and how to break monolithic application into microservices. You will also be taken through the best practices and patterns involved while testing, securing, and deploying your microservice application. At the end of the book, you will easily be able to create interoperable microservices, which are testable and prepared for optimum performance.
Table of Contents (20 chapters)
Title Page
Dedication
Packt Upsell
Contributors
Preface
Index

Caching strategies


There are some strategies of caches for web applications. These same strategies can be applied for microservices. The most common strategy for caching is when we store the information in a cache after a query. In the following diagram, we receive a request through our API and we will, in the application, make the data query. The first search is performed in the cache and, if the data is not in the cache, the search is performed in the database. To return the query from the database, the value is recorded in the cache:

This kind of strategy is the simplest cache that can be applied. We see the changes we should make in our application to have the cache layer.

Applying cache

The first step is to download dependency with Redis as the connection driver:

$ go get github.com/garyburd/redigo/redis

The Redigo is our communication interface with Redis. We will use Redis as a cache tool of our microservice.

Now, we will create the cache.go file. This file is responsible for delivering...