From Design to Deployment
01
From Design to Deployment
by Chris Richardson
with Floyd Smith
© NGINX, Inc. 2016
02
Table of Contents
1
2
3
Foreword iii
Introduction to Microservices 1
Building Monolithic Applications 1
Marching Toward Monolithic Hell 3
Microservices – Tackling the Complexity 4
The Benefits of Microservices 8
The Drawbacks of Microservices 9
Summary 11
Microservices in Action: NGINX Plus as a Reverse Proxy Server 11
Using an API Gateway 12
Introduction 12
Direct Client-to-Microservice Communication 15
Using an API Gateway 15
Benefits and Drawbacks of an API Gateway 17
Implementing an API Gateway 17
Performance and Scalability 17
Using a Reactive Programming Model 18
Service Invocation 18
Service Discovery 19
Handling Partial Failures 19
Summary 20
Microservices in Action: NGINX Plus as an API Gateway 20
Inter-Process Communication 21
Introduction 21
Interaction Styles 22
Defining APIs 24
Evolving APIs 24
Handling Partial Failure 25
IPC Technologies 26
Asynchronous, Message-Based Communication 26
Synchronous, Request/Response IPC 29
REST 29
Thrift 31
Message Formats 31
Summary 32
Microservices in Action: NGINX and Application Architecture 33
i
4
5
6
7
Service Discovery 34
Why Use Service Discovery? 34
The Client-Side Discovery Pattern 35
The Server-Side Discovery Pattern 37
The Service Registry 38
Service Registration Options 39
The Self-Registration Pattern 39
The Third-Party Registration Pattern 41
Summary 42
Microservices in Action: NGINX Flexibility 43
Event-Driven Data Management for Microservices 44
Microservices and the Problem of Distributed
Data Management 44
Event-Driven Architecture 47
Achieving Atomicity 50
Publishing Events Using Local Transactions 50
Mining a Database Transaction Log 51
Using Event Sourcing 52
Summary 54
Microservices in Action: NGINX and Storage Optimization 54
Choosing a Microservices Deployment Strategy 55
Motivations 55
Multiple Service Instances per Host Pattern 56
Service Instance per Host Pattern 58
Service Instance per Virtual Machine Pattern 58
Service Instance per Container Pattern 60
Serverless Deployment 62
Summary 63
Microservices in Action: Deploying Microservices
Across Varying Hosts with NGINX 63
Refactoring a Monolith into Microservices 64
Overview of Refactoring to Microservices 65
Strategy #1: Stop Digging 66
Strategy #2: Split Frontend and Backend 67
Strategy #3: Extract Services 69
Prioritizing Which Modules to Convert into Services 69
How to Extract a Module 69
Summary 71
Microservices in Action: Taming a Monolith with NGINX 72
Resources for Microservices and NGINX 73
ii
Foreword
by Floyd Smith
The rise of microservices has been a remarkable advancement in application
development and deployment With microservices, an application is developed,
or refactored, into separate services that “speak” to one another in a well-defined way –
via APIs, for instance. Each microservice is self-contained, each maintains its own
data store (which has significant implications), and each can be updated independently
of others.
Moving to a microservices-based approach makes app development faster and easier
to manage, requiring fewer people to implement more new features. Changes can be
made and deployed faster and easier. An application designed as a collection of
microservices is easier to run on multiple servers with load balancing, making it easy
to handle demand spikes and steady increases in demand over time, while reducing
downtime caused by hardware or software problems.
Microservices are a critical part of a number of significant advancements that are
changing the nature of how we work. Agile software development techniques, moving
applications to the cloud, DevOps culture, continuous integration and continuous
deployment (CI/CD), and the use of containers are all being used alongside microservices
to revolutionize application development and delivery
NGINX software is strongly associated with microservices and all of the technologies
listed above. Whether deployed as a reverse proxy, or as a highly efficient web server,
NGINX makes microservices-based application development easier and keeps
microservices-based solutions running smoothly
With the tie between NGINX and microservices being so strong, we’ve run a seven-part
series on microservices on the NGINX website Written by Chris Richardson, who has
had early involvement with the concept and its implementation, the blog posts cover
the major aspects of microservices for app design and development, including how to
make the move from a monolithic application. The blog posts offer a thorough overview
of major microservices issues and have been extremely popular.
iii
In this ebook, we’ve converted each blog post to a book chapter, and added a sidebar
to each chapter with information relevant to implementing microservices in NGINX.
If you follow the advice herein carefully, you’ll solve many potential development
problems before you even start writing code. This book is also a good companion
to the NGINX Microservices Reference Architecture, which implements much of the
theory presented here
The book chapters are:
1. Introduction to Microservices – A clear and simple introduction to microservices,
from its perhaps overhyped conceptual definition to the reality of how microservices
are deployed in creating and maintaining applications
2. Using an API Gateway – An API Gateway is the single point of entry for your entire
microservices-based application, presenting the API for each microservice. NGINX Plus
can effectively be used as an API Gateway with load balancing, static file caching,
and more
3. Inter-process Communication in a Microservices Architecture – Once you break
a monolithic application into separate pieces – microservices – the pieces need to
speak to each other. And it turns out that you have many options for inter-process
communication, including representational state transfer (REST). This chapter gives
the details
4. Service Discovery in a Microservices Architecture – When services are running
in a dynamic environment, finding them when you need them is not a trivial issue.
In this chapter, Chris describes a practical solution to this problem
5. Event-Driven Data Management for Microservices – Instead of sharing a unified
application-wide data store (or two) across a monolithic application, each microservice
maintains its own unique data representation and storage This gives you great
flexibility, but can also cause complexity, and this chapter helps you sort through it.
6. Choosing a Microservices Deployment Strategy – In a DevOps world, how you do
things is just as important as what you set out to do in the first place. Chris describes
the major patterns for microservices deployment so you can make an informed
choice for your own application.
7. Refactoring a Monolith into Microservices – In a perfect world, we would always get
the time and money to convert core software into the latest and greatest technologies,
tools, and approaches, with no real deadlines. But you may well find yourself converting
a monolith into microservices, one… small… piece… at… a… time Chris presents a
strategy for doing this sensibly.
We think you’ll find every chapter worthwhile, and we hope that you’ll come back to this
ebook as you develop your own microservices apps
Floyd Smith
NGINX, Inc
iv
1
Introduction to
Microservices
Microservices are currently getting a lot of attention: articles, blogs, discussions on
social media, and conference presentations. They are rapidly heading towards the peak
of inflated expectations on the Gartner Hype cycle At the same time, there are skeptics
in the software community who dismiss microservices as nothing new. Naysayers claim
that the idea is just a rebranding of service-oriented architecture (SOA). However, despite
both the hype and the skepticism, the Microservices Architecture pattern has significant
benefits – especially when it comes to enabling the agile development and delivery of
complex enterprise applications
This chapter is the first in this seven-chapter ebook about designing, building,
and deploying microservices You will learn about the microservices approach and
how it compares to the more traditional Monolithic Architecture pattern This ebook
will describe the various elements of a microservices architecture. You will learn about
the benefits and drawbacks of the Microservices Architecture pattern, whether it makes
sense for your project, and how to apply it.
Let’s first look at why you should consider using microservices.
Building Monolithic Applications
Let’s imagine that you were starting to build a brand new taxi-hailing application
intended to compete with Uber and Hailo. After some preliminary meetings and
requirements gathering, you would create a new project either manually or by using
a generator that comes with a platform such as Rails, Spring Boot, Play, or Maven.
Microservices – From Design to Deployment
1
Ch. 1 – Introduction to Microservices
This new application would have a modular hexagonal architecture, like in Figure 1-1:
PASSENGER
RESTAPI
DRIVER
WEBUI
MYSQL
MYSQL
ADAPTER
Monolithic
Architecture
TWILIO
ADAPTER
SENDGRID
ADAPTER
PASSENGER
MANAGEMENT
BILLING
NOTIFICATION PAYMENTS
YOURBANK
0000 0000 0000 0000
00/00
YOUR NAME
TRIP
MANAGEMENT
DRIVER
MANAGEMENT
STRIPE
ADAPTER
Figure 1-1. A sample taxi-hailing application.
At the core of the application is the business logic, which is implemented by modules
that define services, domain objects, and events. Surrounding the core are adapters
that interface with the external world. Examples of adapters include database access
components, messaging components that produce and consume messages, and web
components that either expose APIs or implement a UI
Microservices – From Design to Deployment
2
Ch. 1 – Introduction to Microservices