Microservices Recipes
Microservices Recipes
Eberhard Wolff
This book is for sale at http://leanpub.com/microservices-recipes
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Acknowledgement . . . . . . . . . . . . . . . . . . . . . 1
Basics: Microservices . . . . . . . . . . . . . . . . . . . . . 3
Independent Systems Architecture (ISA) Principles . . . 3
Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Principles . . . . . . . . . . . . . . . . . . . . . . . . . 3
Reasons . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Self-contained Systems . . . . . . . . . . . . . . . . . . 6
Conclusion & Outlook . . . . . . . . . . . . . . . . . . 7
What next? . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Introduction
This brochure introduces the terms microservice and self-contained
system. It continues with an overview of different concepts and
recipes for the implementation of microservices. The recipe metaphor
expresses that the text describes each approach in practical terms.
For each recipe one example implementation is provided. Readers
must combine several recipes for their projects, as they would need
to do for the menu of a multi-course meal. And finally, there are
variations and alternatives for every recipe. Experiments invite you
to get hands-on experience with the examples.
The code for the examples can be found on Github. There is also
an overview¹, which briefly explains all the demos in this brochure
and a few additional ones.
A detailed presentation of the recipes and other concepts around
microservices can be found in the book Microservices - A Practical
Guide².
Acknowledgement
Terms
In the principles the term must is used for principles that have to
be strictly adhered to. Should describes principles that have many
advantages, but do not necessarily have to be complied with.
The ISA principles speak of a system. The IT landscape of a
company consists of many systems. Every system can work with
a different architecture and can therefore be implemented based on
different principles.
Principles
Reasons
Self-contained Systems
Frontend integration creates loose coupling. If links are used for the
integration, only the URL has to be known by the other system.
What is behind the URL and how the information is presented can
be changed without affecting the system that displays the URL in a
link.
Another benefit of frontend integration is the free choice of frontend
technologies. Especially with frontend technologies, there are lots of
innovations. Constantly, there are new JavaScript frameworks and
new ways to design attractive user interfaces (UIs). An important
advantage of microservices is the freedom of technology. Every mi-
croservice can choose its own technologies. If technology freedom
should also apply to the frontend, then every microservice must
contain its own frontend, potentially using a different frontend
technology. For this, the frontends of the microservices must be
integrated so that the frontends appear to be part of a single system.
Thanks to frontend integration, the entire functionality for a do-
main is implemented in a single microservice. For example, a
microservice might be responsible for displaying received messages
even if the display of the messages is integrated in the UI of another
microservice. If more information, such as a priority of the message,
Concept: Frontend Integration 9
⁷https://www.w3.org/TR/esi-lang
Concept: Frontend Integration 10
1 <html>
2 <head>
3 ...
4 <esi:include src="/common/header"></esi:include>
5 </head>
6
7 <body>
8 <div class="container">
9 <esi:include src="/common/navbar"></esi:include>
10 ...
11 </div>
12 <esi:include src="/common/footer"></esi:include>
13 </body>
14 </html>
browser, you see that the browser does not interpret the ESI tags, it
rather displays a garbled web page.
The example uses the web cache Varnish⁹ as an ESI implementation.
The common microservice provides the content for the ESI tags. The
Varnish runs at http://localhost:8080/ when the Docker container
runs on the local computer. Listing 2 shows the HTML that Varnish
returns.
As you can see, the common microservice adds headers and footers
⁹https://varnish-cache.org/
Concept: Frontend Integration 12
Because the system uses a Varnish cache, the HTML fragments are
cached for 30 seconds. That can be seen by looking at the time in
the navigation bar, which changes only every 30 seconds. If one
of the microservices fails, the time for caching is even extended to
15 minutes. The configuration for Varnish can be found in the file
default.vcl in the directory docker/varnish/ in the example.
This example is available on the internet¹¹. You can also run the
example on your own laptop as a set of Docker containers provided
¹¹https://crimson-portal.herokuapp.com/
Concept: Frontend Integration 14
Conclusion
Experiments
Definition
Kafka¹⁴ differs from other MOMs mainly in the fact that it perma-
nently stores the messages it transmits instead of discarding them
after transmission.
The main concepts of Kafka are:
• There are three APIs: the producer API for sending data,
the consumer API for receiving data and the streams API to
transform the data.
• Kafka organizes data in records. They contain the transmitted
data as value. Records also have a key and a timestamp.
• Topics contains records. Usually, records of a certain kind are
sent as part of a topic.
• Topics are divided into partitions. When a producer creates a
new record, the record is appended to a partition of the topic.
The distribution of records to partitions is implemented by
the producer and usually based on the key of the record.
• Kafka stores the offset for each consumer in each partition.
This offset indicates which record the consumer last read
in the partition. If a consumer has processed a record, the
consumer can commit a new offset. For every consumer
Kafka only needs to store the offset in each partition which
is relatively lightweight.
• In a consumer group there is exactly one consumer for each
partition. This ensures that a record is processed by only
one consumer: The record is stored in a partition, which is
processed by one consumer thanks to the consumer group.
• Log compaction is a mechanism that can be used to delete old
records: Â Â If there are multiple records with the same ID,
a log compaction deletes all of these records except the last
one. This can be used to remove events that are superseded
by newer events.
¹⁴https://kafka.apache.org/
Concept: Asynchronous Microservices 19
If the Docker containers run on the local machine, the web applica-
tion is available at http://localhost:8080/. The web interface is served
by an Apache httpd web server acting as a reverse proxy. It forwards
the HTTP requests to the microservices.
microservice can read just the data that is relevant for the respective
microservice from the JSON data structure.
All the shipping microservice instances and all the invoicing mi-
croservice instances are each organized in a consumer group. This
means that the records for the orders are load balanced over all
consumers, but each record is sent to just one consumer. This
ensures that only one invoicing microservice writes an invoice for
an order and only one shipping microservice delivers the goods.
The shipping microservice and the invoicing microservice store the
information from the records in their own database schemas. All
microservices use the same Postgres database.
Each Kafka record contains an order. The key is the ID of the order
with the suffix created, for example 1created.
client must then periodically poll the Atom document and process
new entries. That’s not very efficient. It can be optimized by HTTP
caching. Then data will only be transferred if there are really new
entries. Pagination can also ensure that only the most recent entries
are transmitted, not all.
An example of an asynchronous integration of microservices with
Atom can be found at https://github.com/ewolff/microservice-atom.
https://github.com/ewolff/microservice-atom/blob/master/HOW-TO-
RUN.md explains in detail the necessary steps to run the example.
Atom has the advantage of being based on REST and HTTP. As
a result, no MOM must be operated. In most cases, teams already
have experiences with HTTP and web servers. Thus, the operations
can be ensured even with large amounts of data.
Unfortunately, this type of communication can not ensure that
an order is only received and processed by a single microservice
instance. However, if one of the microservices instances in the
example application reads a new order from the Atom feed, it
first checks whether there is already an entry for this order in the
database, and it will only create an entry itself if this is not the case.
Therefore, only one entry in the database is created for each order.
It is not mandatory to use the Atom format. You can also use your
own format to make the changes available as a list and then provide
details with links. Likewise, a different feed format such as RSS¹⁹ or
JSON Feed²⁰ can be used.
Other MOMs
Of course, also other MOMs than Kafka can be used. For exam-
ple, there are JMS implementations²¹, the Java messaging service
¹⁹http://web.resource.org/rss/1.0/spec
²⁰http://jsonfeed.org/
²¹https://en.wikipedia.org/wiki/Java_Message_Service#Provider_implementations
Concept: Asynchronous Microservices 22
Conclusion
Experiments
²²https://jcp.org/aboutJava/communityprocess/final/jsr914/index.html
²³https://en.wikipedia.org/wiki/Advanced_Message_Queuing_Protocol#
Implementations
²⁴https://www.amqp.org/
Concept: Synchronous
Microservices
Many microservice systems use synchronous communication. This
chapter shows how synchronous microservices can be implemented
with different technologies.
Definition
Challenges
Recipe: Kubernetes
Docker
Kubernetes Concepts
1 #!/bin/sh
2 if [ -z "$DOCKER_ACCOUNT" ]; then
3 DOCKER_ACCOUNT=ewolff
4 fi;
5 ...
6 kubectl run catalog \\
7 --image=docker.io/$DOCKER_ACCOUNT/microservice-kuberne\
8 tes-demo-catalog:latest
9 \\
10 --port=80
11 kubectl expose deployment/catalog --type="LoadBalancer"\
12 --port 80
13 ...
Conclusion
Experiments
³⁹https://github.com/ewolff/microservice-kubernetes/blob/master/HOW-TO-RUN.md
What next?
This booklet can only provide a brief introduction to microservices.
The book Microservices - A Practical Guide⁴⁰ contains a detailed de-
scription of the examples in this brochure. In addition, it contains an
introduction to microservices and an overview of technologies for
the monitoring of microservices. There is also a German version⁴¹.
The book Microservices⁴² describes the motivation, architecture,
and concepts of microservices. There is also a German version⁴³.
Finally, the free Microservices Primer⁴⁴ provides an overview of the
basic motivation and architecture of microservices. There is also a
German version⁴⁵.
⁴⁰http://practical-microservices.com/
⁴¹http://microservices-praxisbuch.de/
⁴²http://microservices-book.com/
⁴³http://microservices-buch.de/
⁴⁴http://microservices-book.com/primer.html
⁴⁵http://microservices-buch.de/ueberblick.html