Professional Documents
Culture Documents
PREV NEXT
⏮ ⏭
1. Why Event-Driven Microservices 3. Communication and Data Contracts
🔎
Introduction
An event-driven microservice is a small application built to fulfil a specific
bounded context. Consumer microservices consume and process events from one
or more input event streams, whereas producer microservices produce events to
event streams for other services to consume. It is common for an event-driven
microservice to be both a consumer of one set of input event streams and a
producer to another set of output event streams. These services may be stateless
(Chapter 6), stateful (Chapter 7) and may also contain synchronous request-
response APIs (Chapter 12). These services all share the common functionality of
sourcing and/or sinking their event data from and to the event broker.
Communication between event-driven microservices is completely
asynchronous.
Event streams are served by an event-broker, which we will look at in more detail
in the second half of this chapter. Running microservices at any meaningful scale
often necessitates the usage of deployment pipelines and container management
systems, also discussed near the end of this chapter.
Building Topologies
The term topology will come up frequently in this book, as it also will in other
materials on event-driven microservices. This term can often be used to mean the
processing logic of an individual microservice; It may also be used to refer to the
graph-like relationship between individual microservices, event-streams, and
request-response APIs. For the sake of disambiguation, let’s use the following
terminology:
The example below shows a single microservice topology ingesting from two
input event streams.
TIP
TIP
https://learning.oreilly.com/library/view/building-event-driven-microservices/9781492057888/ch02.html 1/6
4/9/2020 2. Event Driven Microservice Fundamentals - Building Event-Driven Microservices
Key Value
Let’s take a look at the three main event types that we’ll be working with in this
book, and that you are likely to encounter in your own domain.
Unkeyed Event
Key Value
Keyed Event
This event type contains a key but does not represent any entity data. This is
often used for partitioning the stream of events to guarantee data locality within a
single partition of an event stream (more on this later in the chapter). The
distinction is that the keyed event describes an interaction with an entity, but not
the entity itself. An example of this could be a stream of events, keyed on ISBN,
indicating which user has interacted with the book.
Key Value
Entity Event
An entity is a unique thing, and is keyed on the unique id of that thing. The entity
event describes the properties and state of this thing at a given point in time. An
entity is most commonly an object in the business context. For a book publisher,
an example of this could be a book entity, keyed on ISBN, as shown in the table
below. The value field contains all the necessary information related to the
unique entity.
Key Value
https://learning.oreilly.com/library/view/building-event-driven-microservices/9781492057888/ch02.html 2/6
4/9/2020 2. Event Driven Microservice Fundamentals - Building Event-Driven Microservices
Schematization selections such as Avro and Protobuf provide two features that
are leveraged heavily in event-driven microservices. First, they provide an
evolution framework, where certain sets of changes can be safely made to the
schemas without requiring downstream consumers to make a code change.
Secondly, they also provide the means to generate typed classes (where
applicable) to convert the schematized data into plain old objects of the language
of your choice. This makes the creation of business logic far simpler and more
transparent in the development of microservices. Chapter 3: Event Theory and
Design covers these topics in greater detail.
Event broker systems suitable for large-scale enterprises all generally follow the
same model. Multiple, distributed event brokers work together in a cluster to
provide a platform for the production and consumption of event streams. This
model provides several essential features that are required for running an event
driven ecosystem at scale
High Availability: A cluster of event broker nodes provides clients with the
means of connecting to other nodes in the case of a broker failure. This permits
the clients to maintain full uptime.
Though there are different ways in which event data can be stored, replicated and
accessed behind the scenes of an event broker, they all generally provide the
same mechanisms of storage and access to their clients.
The minimal requirements of the underlying storage of the data by the broker.
Indexing: Events are assigned an index when written to the event stream. This is
used by the consumers to manage the consumption of data, as they can specify
which offset to begin read from. The difference between the consumers current
index and the tail index gives a measurement of consumer lag. This metric can be
used to scale up the number of consumers when high, and scale them down when
it is low. Additionally, it can also be used to awaken Functions-as-a-Service
logic.
Infinite Retention: Events streams need the option to retain events for an infinite
period of time. This property is foundational for the maintenance of state in an
event stream.
Replayability: Event streams must be replayable, such that any consumer can
read whatever data it requires. This provides the basis for the single source of
truth and is foundational for communicating state between microservices.
Support Tooling Support tools are essential for effectively developing event-
driven microservices. Many of these tools are bound to the implementation of the
event-broker itself. Some of these include: * Browsing of event and schema data
* Quotas, access-control, and topic management * Monitoring, throughput, and
lag measurements
Hosted Services
https://learning.oreilly.com/library/view/building-event-driven-microservices/9781492057888/ch02.html 3/6
4/9/2020 2. Event Driven Microservice Fundamentals - Building Event-Driven Microservices
Message brokers have a long history and have been used in large-scale message-
oriented middleware architectures by numerous organizations. Message brokers
provide the means for systems to communicate across a network through
publish/subscribe message queues. Producers write messages to a queue, while a
consumer consumers these messages and processes them accordingly. Messages
are then acknowledged as consumed and deleted either immediately or shortly
thereafter. Message brokers are designed to handle a different type of problem
than event-brokers, and have been proven to be very successful at providing the
middleware necessary in message-oriented middleware architectures.
“Event brokers are much more about the immutable, append-only log of facts and
the model of consumers being being able to pick up and reprocess from
anywhere in the log at any time. It’s incredibly powerful and is why Kafka has
been so hugely successful (though obviously there are others). It’s about
preserving the state of event ordering.”
Event brokers, on the other hand, are designed around providing an ordered log
of facts. Event brokers meet two very specific needs that are not provided by the
message broker. For one, the message broker only provides queues of messages,
where the consumption of the message is handled on a per-queue basis.
Applications which share consumption from a queue will each receive only a
subset of the records. This makes it impossible to correctly communicate state
via events, since each consumer is not able to obtain a full copy of all events.
Unlike the message broker, the event broker maintains a single ledger of records
and manages individual access via indices, so that each independent consumer
can access all required events.
Keep in mind that the concept of queues, as used in message brokers, still have a
role in event-driven microservices. Queues provide useful access patterns that
may be awkward to implement with strictly partitioned event streams. The
patterns introduced by message broker systems are certainly valid patterns for
EDM architectures, but they are not sufficient for the full scope of duties required
by a full EDM architecture. For the reasons discussed above, the remainder of the
book will not focus on any message broker architectures or application design,
but will focus instead on the usage of event brokers in event-driven microservice
architectures.
Though not a definitive standard, most of the commonly available event brokers
use an append-only immutable log. Events are appended at the end of the log and
are given an auto-incrementing index ID. Consumers of the data use a reference
to the index ID to access data. Events can then be consumed as either an event-
stream or as a queue, depending on the needs of the business and the available
functionality of the event broker.
CO N S U M I N G A S A N E V E N T ST R E A M
CO N S U M I N G A S A Q U E U E
You have 2 days le in your trial, Reddychan. Subscribe today. See pricing options.
https://learning.oreilly.com/library/view/building-event-driven-microservices/9781492057888/ch02.html 4/6
4/9/2020 2. Event Driven Microservice Fundamentals - Building Event-Driven Microservices
The durable and immutable log provides the storage mechanism for the single
source of truth, with the event broker becoming the only location in which
services consume and produce data. In doing so, each consumer is guaranteed to
be given an identical copy of the data as all the other consumers.
Adopting the event broker as the single source of truth often requires a culture
shift in the organization. Whereas previously a team may simply write direct
SQL queries to access data in a monolith’s database, now the monolith’s data
must additionally be published to the event broker. The developers managing the
monolith must ensure that the data produced is fully accurate, for any
disagreement between the event streams and the monolith’s database must always
be considered a failure of the producing team. Consumers of the data no longer
couple directly on the monolith, but instead consume directly from the event
streams.
Initiatives are being taken by numerous competitors to make VMs cheaper and
more efficient. These include Google’s gVisor, Amazon’s Firecracker, and Kata
Containers, to mention just a few currently notable candidates. As these
technologies improve VMs will become a much more competitive alternative to
using containers for your microservice needs. It is worth keeping an eye on this
domain should your needs be driven by security-first requirements.
Summary
In this chapter we took a look at the basic requirements behind event-driven
microservices. The event broker forms the main mechanism of data
communiation, providing realtime event streams at scale for other services to
consume. Containerization and container management systems provide the
mechanisms necessary for running microservices at scale. Lastly, we take a look
at some of the important principles underlying events, event-driven logic and a
first look at how to manage state in an distributed event-driven world.
https://learning.oreilly.com/library/view/building-event-driven-microservices/9781492057888/ch02.html 5/6
4/9/2020 2. Event Driven Microservice Fundamentals - Building Event-Driven Microservices
You have 2 days le in your trial, Reddychan. Subscribe today. See pricing options.
https://learning.oreilly.com/library/view/building-event-driven-microservices/9781492057888/ch02.html 6/6