Kafka and Mongodb
Kafka and Mongodb
Apache Kafka 1
How Kafka Works & What it Provides 1
Related Technologies 6
We Can Help 11
Resources 12
Introduction
In today's data landscape, no single system can provide all modern data ingestion and routing pipelines. A new
of the required perspectives to deliver real insight. Deriving generation of technologies is needed to consume and
the full meaning from data requires mixing huge volumes exploit today's data sources. This paper digs into these
of information from many sources. technologies (Kafka in particular) and how they're used.
The paper also examines where MongoDB fits into the
At the same time, we're impatient to get answers instantly;
data streaming landscape and includes a deep dive into
if the time to insight exceeds 10s of milliseconds then the
how it integrates with Kafka.
value is lost – applications such as high frequency trading,
fraud detection, and recommendation engines can't afford
to wait. This often means analyzing the inflow of data Apache Kafka
before it even makes it to the database of record. Add in
zero tolerance for data loss and the challenge gets even
more daunting. How Kafka Works & What it Provides
As the number, variety, and velocity of data sources grow, Kafka provides a flexible, scalable, and reliable method to
new architectures and technologies are needed. distribute streams of event data from one or more
pr
producers
oducers to one or more consumers
consumers. Examples of
Apache Kafka and data streams are focused on ingesting
events (or messages
messages) include:
the massive flow of data from multiple fire-hoses and then
routing it to the systems that need it – filtering, • A periodic sensor reading such as the current
aggregating, and analyzing en-route. temperature
Enterprise messaging systems are far from new, but even • A user adding an item to the shopping cart in an online
frameworks from the last 10 years (such as ActiveMQ and store
RabbitMQ) are not always up to the job of managing • A Tweet being sent with a specific hashtag
1
• A log entry generated for each click in a web application In Kafka, topics are further divided into partitions to
support scale out. As each message is produced, the
Streams of Kafka events are organized into topics
topics. A
producer determines the correct partition for a message
producer chooses a topic to send a given event to and
(depending on its topic and message key), and sends it to
consumers select which topics they pull events from. For
an appropriate br
broker
oker for that partition. In this way, the
example, a financial application could pull NYSE stock
processing and storage for a topic can be linearly scaled
trades from one topic, and company financial
across many brokers. Similarly, an application may scale
announcements from another in order to look for trading
out by using many consumers for a given topic, with each
opportunities.
pulling events from a discrete set of partitions.
The consumers typically receive streamed events in near
A consumer receives events from a topic's partition in the
real-time, but Kafka actually persists data internally,
order that they were added by the provider, but the order is
allowing consumers to disconnect for hours and then catch
not guaranteed between different partitions. While this
up once they’re back online. In fact, an individual consumer
appears troublesome, it can be mitigated by controlling
can request the full stream of events from the first event
how events are assigned to partitions. By default, the
stored on the broker. Administrators can choose to keep
mapping of events to partitions is random but a
the full history for a topic, or can configure an appropriate
partitioning key can be defined such that 'related' events
deletion policy. With this system, it's possible for different
are placed in the same partitions. In our financial
consumers to be processing different chunks of the
application example, the stock symbol could be used as the
sequence of events at any given time. Each event in a topic
partitioning key so that all events for the same company
is assigned an offset to identify its position within the
are written to the same partition – if the application then
stream. This offset is unique to the event and never
has just one consumer pulling from that partition then you
changes.
have a guarantee that all trades for that stock are
processed in the correct order.
High A
Availability
vailability can be implemented using multiple
Kafka brokers for each topic partition. For each partition
there is a single broker that acts as the leader in addition
to one or more followers
followers. The leader handles all reads and
Figur
Figuree 1: Kafka Topics & Partitions
writes for the topic partition and the followers each
replic
eplicate
ate from the leader. Should the leader fail, one of the
The ability to reliably replay events that have already been
followers is automatically promoted to be the new leader.
received, in the exact order in which they were received,
Typically, each broker acts as the leader for some partitions
provides a number of benefits such as:
and as a follower for others. This replication approach
• Newly added consumers can catch up on everything prevents the loss of data when a broker fails and increases
that's happened Kafka's availability,
2
Kafka Use Cases
Log Aggr
Aggregation
egation: This has traditionally involved the
collection of physical log files from multiple servers so that
they can be stored in a central location – for example,
HDFS. Analyzing the data would then be performed by a
periodic batch job. More modern architectures use Kafka to
combine real-time log feeds from multiple sources so that
the data can be constantly monitored and analyzed –
reducing the interval between an event being logged and
its consequences being understood and acted upon.
Each Kafka broker stores all of the data for its topic Event Sour
Sourcing
cing: Rather than maintaining and storing the
partitions on disk in order to provide persistence. Because latest application state, event sourcing relies on storing all
the data is immutable and append-only, each broker is able of the changes to the state (e.g., [x=150, x++, x+=12,
to handle a large number of writes, and the cost of reading x-=2]) in the original order so that they can be replayed to
the most recent message remains constant as the volume recreate the final state. This pattern is often used in
of data stored grows. In spite of Kafka's ability to work financial applications. Kafka is well suited to this design
efficiently with large data volumes, it is often desirable to approach as it can store arbitrarily long sequences of
remove old or obsolete data; Kafka users can choose events, and quickly and efficiently provide them in the
between two different algorithms for managing space: correct sequence to an application.
retention policies and log compaction:
Micr
Microservices
oservices: Microservice architectures break up
• Retention policy
policy: You can choose, on a per-topic basis, services into small, discrete functions (typically isolated
to delete log files after they reach a certain age, or the within containers) which can only communicate with each
number of bytes in the topic exceeds a certain size other through well defined, network-based APIs. Examples
• Log compaction
compaction: Log Compaction is an optional Kafka include eCommerce applications where the service behind
feature that reduces the storage size for keyed data the 'Buy Now' button must communicate with the inventory
(e.g., change logs for data stored in a database). Log service. A typical application contains a large number of
compaction allows you to retain only the most recent microservices and containers. Kafka provides the means
message with a given key, and delete older messages for containers to pass messages to each other – multiple
with the same key. This can be useful for operations like containers publishing and subscribing to the same topics
database updates, where you only care about the most so that each container has the data it needs. Since Kafka
recent message. persists the sequences of messages, when a container is
rescheduled, it is able to catch up on everything it has
missed; when a new container is added (e.g., to scale out) it
can bootstrap itself by requesting prior event data.
3
Figur
Figure
e 3: Stream Processing with Kafka & MongoDB
More information on Microservices can be found in the Streaming, Apache Flink, and Kafka Streams are used to
Microservices: The Evolution of Building Modern process events as they pass through, while interesting
Applications white paper as well as in Enabling events and results are written to a database like MongoDB
Microservices: Containers & Orchestration Explained. where they're used for analysis and operational decisions.
This is the pattern used for the Indian smart housing
Str
Stream
eam Pr
Processing
ocessing: stream processing involves filtering,
project described later – where MongoDB stores
manipulating, triggering actions, and deriving insights from
aggregated energy sensor data which is used for billing
the data stream as it passes through a series of functions.
and energy performance benchmarking between
Kafka passes the event messages between the processing
properties.
functions, merging and forking the data as required.
Technologies such as Apache Storm, Samza, Spark
Figur
Figure
e 4: Lambda Architecture
4
Lambda ArArcchitectur
hitecturee: Applications following the Lambda Some data may still need to be kept in silos due to security
Architecture (Figure 4) augment data produced by stream concerns, but it's worth asking the question about which
processing on recent events (the Speed Layer) with views topics can be made available to all internal users. Kafka 0.9
built from batch processing jobs run on the full, historical supports secure authentication and per-topic ACLs, which
data set (the Batch Layer). allow administrators to control access to individual topics.
The Lambda Architecture is coming under increasing J SON is a suitable common data format for
scrutiny due to the operational complexity of managing two standardization, and it's also worth considering Apac
Apache
he
distributed systems which must implement the same logic. Avr
vro
o, which allows schemas to be enforced on any given
A more contemporary, scalable, and less complex solution topic. Avro also has the advantage that it's more compact
is described below in Operationalizing the Data Lake with than JSON. It's very simple to map between JSON and
MongoDB. Avro, which helps when integrating with MongoDB.
Internet of T
Things
hings (IoT)
(IoT): IoT applications must cope with Each topic should use a common schema, but it's also
massive numbers of events being generated by a multitude important to recognize that schemas evolve over time;
of devices. Kafka plays a vital role in providing the fan-in consider including a schema version identifier in each
and real-time collection of all of that sensor data. A message.
common use case is telematics, where diagnostics from a
Kafka is extremely good at ingesting and storing massive
vehicle's sensors must be received and processed back at
numbers of events in real-time; this makes it a great buffer
base.
to smooth out bursty traffic. An example could be the
Once captured in Kafka topics, the data can be processed results produced at the end of a customer classification
in multiple ways, including stream processing or Lambda Hadoop batch job, which must then be propagated into
architectures. It is also likely to be stored in an operational personalization and marketing applications.
database such as MongoDB, where it can be combined
As in any distributed system, poor network performance is
with other stored data to perform real-time analytics and
the enemy of low latency and high throughput. Where
support operational applications such as triggering
possible, keep processing local and replicate data between
personalized offers.
Kafka clusters in different data centers.
5
can be written that increases the same balance by $100. Apac
Apache
he Storm
Storm: Provides batch, distributed processing of
There are other scenarios where it's harder to roll back the streaming data. Storm is often used to implement Stream
clock; consider a rogue temperature sensor event from a Processing, where it can act as both a Kafka consumer of
data center that triggers cutting power and a halon dump. raw data and a producer of derived results.
6
• Kafka includes replication so events are not lost if a
A Diversion – Graphically Build
single cluster node is lost.
Data Streams Using Node-RED
Apac
Apachehe Flink
Flink: A framework for distributed big data
analytics using a distributed data flow engine. Any data
storage is external to Flink; e.g., in MongoDB or HDFS. Node-RED provides a simple, graphical way to build data
Flink often consumes its incoming data from Kafka. pipelines – referred to as flows
flows. There are community
provided nodes which act as connectors for various
Apac
Apache he A
Avr
vro
o: A data serialization system, very often used devices and APIs. At the time of writing, there are almost
for event messages in Kafka. The schema used is stored 500 nodes available covering everything from Nest
with the messages and so serialization and deserialization thermostats and Arduino boards, to Slack and Google
is straight-forward and efficient. Avro schemas are defined Hangouts, to MongoDB. Flows are built by linking together
in JSON, making it simple to use with languages, libraries, nodes and by adding custom JavaScript code. Node-RED
and databases such as MongoDB designed to work with is a perfect tool to get small IoT projects up and running
JSON. quickly or to run at the edge of the network to collect
sensor data.
Figur
Figuree 5: Example Node-RED Flow
7
• The Fetch Weather node makes a web API call to To recreate this flow, simply import this definition.
retrieve the current weather and forecast data for our
location; that data is then used in two ways:
After executing the flow, the Tweet gets sent and the data
is stored in MongoDB:
db.weather.findOne()
{
"_id" : ObjectId("571e2e9865177b7a1f40ddb5"),
"weatherNow" : {
"time" : ISODate("2016-04-25T14:49:58Z"),
"summary" : "Mostly Cloudy",
"icon" : "partly-cloudy-day",
"nearestStormDistance" : 0,
"precipIntensity" : 0.0033,
"precipIntensityError" : 0.0021,
"precipProbability" : 0.21,
"precipType" : "rain",
"temperature" : 51.51,
"apparentTemperature" : 51.51,
"dewPoint" : 39.57,
"humidity" : 0.64,
"windSpeed" : 13.41,
"windBearing" : 314,
"visibility" : 10,
"cloudCover" : 0.7,
"pressure" : 1007.32,
"ozone" : 387.18
}
}
8
Operationalizing the Data Lake • MongoDB exposes these models to the operational
processes, serving queries and updates against them
with MongoDB with real-time responsiveness.
Figur
Figure
e 6: Design pattern for operationalizing the data lake
9
MongoDB As a Kafka Producer An example use of change streams using the mongo shell:
db.simples.update({name:'Billy'},
When using any database as a producer, it's necessary to {$set:{score:50}})
capture all database changes so that they can be written to
myCursor.forEach(printjson);
Kafka. With MongoDB, you can do this using change {
str
streams
eams (new in MongoDB 3.6). The change stream API "_id" : {
"clusterTime" : {
enables applications to register for real-time notifications
"ts" : Timestamp(1505741442, 1)
of inserts, updates, or deletions in the database. Change },
streams allow applications to instantly view, filter, and act "uuid" : UUID("9bcd6be7-9c7f-4ee5-\
a278-659f8ce13d1a"),
on changes to data as they occur. "documentKey" : {
"_id" : ObjectId("59bfc4a33497\
You open a change stream against a collection, and it then bef0711d9346")
tracks all data changes within that collection – you can }
},
access the results using regular cursor operations. You "operationType" : "update",
create a change stream using the aggregation pipeline, "ns" : {
"db" : "clusterdb",
where $changeStream is the first stage. You may limit
"coll" : "simples"
which changes are included using the operationType },
option (inserts, deletes, replacements, or updates – or any "documentKey" : {
"_id" : ObjectId("59bfc4a33497bef0\
combination). $match, $project, $addFields, 711d9346")
$replaceRoot, and $redact can be used in subsequent },
"updateDescription" : {
stages to filter further and massage the notifications. "updatedFields" : {
"score" : 50
},
"removedFields" : [ ]
}
}
changeStream.on('change', function(change) {
// This is where you can write the
// contents of `data` to the Kafka topic
console.log(change);
});
10
MongoDB As a Kafka Consumer Josh Softwar
Software e: Part of a project near Mumbai, India that
will house more than 100,000 people in affordable smart
homes. Data from millions of sensors, is pushed to Kafka
In order to use MongoDB as a Kafka consumer, the and then processed in Spark before the results are written
received events must be converted into BSON documents to MongoDB. MongoDB Connector for Hadoop is used to
before they are stored in the database. It's a simple and connect the operational and analytical data sets. More
automatable process to convert the received JSON or Avro details on this project and its use of Kafka, MongoDB and
message payload into a Java object and apply any required Spark can be found in this blog post.
business logic on that object before encoding it as a
BSON document which is then written to MongoDB.
11
Recent Additions to Kafka of advanced software, support, certifications, and other
services designed for the way you do business.
• Support for record headers (to be used by the MongoDB Cloud Manager is a cloud-based tool that helps
application or middleware) you manage MongoDB on your own infrastructure. With
• Request rate quotas, limiting the rate that consumers automated provisioning, fine-grained monitoring, and
can retrieve messages (existing functionality can be continuous backups, you get a full management suite that
used to limit the volume of data retrieved) reduces operational overhead, while maintaining full control
over your databases.
• Improved resiliency
MongoDB Professional helps you manage your
Kafka 0.10 was released in May 2016 with these key
deployment and keep it running smoothly. It includes
features:
support from MongoDB engineers, as well as access to
• Kafka Streams is a Java library for building distributed MongoDB Cloud Manager.
stream processing apps using Kafka; in other words
Development Support helps you get up and running quickly.
enabling applications to transform input Kafka topics
It gives you a complete package of software and services
into output Kafka topics and/or invoke external services
for the early stages of your project.
or write to a database. Kafka Streams is intended to
distinguish itself from the more analytics-focused MongoDB Consulting packages get you to production
frameworks such as Spark, Storm, Flink, and Samza by faster, help you tune performance in production, help you
targeting core application functionality. scale, and free you up to focus on your next release.
• Performance enhancements for compressed streams.
MongoDB Training helps you become a MongoDB expert,
• Rack-aware replica assignment can be used to ensure from design to operating mission-critical systems at scale.
that a leader and its followers are run in different racks, Whether you're a developer, DBA, or architect, we can
improving availability. make you better at MongoDB.
We Can Help
We are the MongoDB experts. Over 4,300 organizations
rely on our commercial products, including startups and
more than half of the Fortune 100. We offer software and
services to make your life easier:
12
Resources
13