1
FaaS Composition using Kafka and CloudEvents
Neil Avery,
Office of the CTO,
@avery_neil
22
The world is changing.
33
The New Business Reality
Past
Technology was
Innovation required for growth
Running the business on yesterday’s data was
“good enough”
Today
Technology
Innovation required for survival
Yesterday’s data = failure.
Modern, real-time data infrastructure
is required.
44
Transportation
55
Transportation
ETA
Real-time sensor
diagnostics
Driver-rider match
Banking
Fraud detection
Trading and risk
systems
Mobile applications /
customer experience
Retail
Real-time inventory
Real-time POS
reporting
Personalization
Entertainment
Real-time
recommendations
Personalized
news feed
In-app purchases
66
What enables this
transformation?
77
Cloud Machine
Learning
Mobile Event
Streaming
Rethink
Decision Making
Rethink
User Experience
Rethink
Data
Rethink
Data Centers
8
is a distributed event streaming platform
Publish & Subscribe
to Events
Store
Events
Process & Analyze
Events
99
1010
Out of the tar pit, 2006
1111
“We believe that the major
contributor to this complexity in
many systems is the handling of
state and the burden that this adds
when trying to analyse and reason
about the system.”
Out of the tar pit, 2006
1212
We like ‘all the things’
1313
1414
What about Microservices?
1515
What are microservices?
Microservices are a software development
technique - a variant of the service-oriented
architecture (SOA) architectural style that
structures an application as a collection of
loosely coupled services.
https://en.wikipedia.org/wiki/Microservices
1616
structures an application as a collection of
loosely coupled services.
https://en.wikipedia.org/wiki/Microservices
this is new!
1717
So what went wrong?
1818
Too many cooks
19
REST anyone? Making changes is risky
20
Handling state is hard Cache?
Embedded?
Route to right instance?
21
Shifts in responsibility, redundancy
22
● Scaling is hard
● Handling state is hard
● Sharing, coordinating is hard
● Run a database in each microservice - is hard
What have we learned about microservices?
2323
Microservices
We had it all wrong (again)
FIX: Make them asynchronous and use
streams of events
We didn’t really
know what they
were anyway
2424
Why?
Inversion of responsibility
25
Typical event → command interaction
26
Event → first interaction
27
Evolvability
2828
Stop!
Isn’t FaaS like a fine-grained, event
driven microservice?
2929
Microservice/FaaS/Stream-Processor
Single atomic unit
does one thing
like
What is FaaS?
●
●
●
●
●
●
●
DRIVEN BY EVENTS
{...}
Stream processors FaaS (serverless fns)
{...}
Kafka cluster
stream
processing
Kafka Streams
KSQL
Producer/Consumer
33
Capture behavior
34
Time travel user experience?
how many users
affected?has it happened
before?
Ask many questions of the same data, again and again
time
35
Evolvability
user experience?
how many users
affected?
has it happened
before?
new old
supports data change, logic change, logic extension, schema
evolution, loose coupling, add processors, A/B path
{
user: 100
type: bid
item: 389
cat: bikes/mtb
region: dc-east
}
Kafka partitions give you horizontal scale
/bikes/ by item-id
key#
Key
space
{...}
{...}
{...}
Topic
Partition
Partition
assignment
Stream
processor
FaaS
function
37
Stream processors handle state
Stream
processor
Stream
processor
Stream
processor
Topic: click-stream
Interactive query
CDC events from KTable
CDC Stream
partition
partition
partition
CQRS
Elastic
3838
Rate of adoption
● Serverless is the top-growing extended cloud service for the
second year in a row, with a 50 percent growth over 2018
24 to 36 percent adoption.
● Stream processing is tied for first, increasing from
20 to 30 percent adoption.
Rightscale cloud report 2019
3939
Composition
4040
Composition
4141
Composition styles
1. Manual wiring
2. Chaining
3. Choreography
4. Orchestration / Workflow
(Step functions, Durable Fns,
Zeebee, Apache Airflow)
4242
Composition styles
1. Manual wiring
2. Chaining
3. Choreography
4. Orchestration / Workflow
(Step functions, Durable Fns,
Zeebee, Apache Airflow)
43
44
45
46
payment.incoming.pre
2.
Payment
Inflight
payment.incoming
1.
{...}
Fraudulent
Check
“FaaS Connector”
a bridge between Kafka topic(s) and serverless function(s)
{...}
{...}
{...}
{...}
bad stream
48
Bad stream?
As we say in IT...
4949
50
FaaS Connector modes
1. Sync:
● fire and queue response
● preserve order
2. Async:
● fire and forget
● order agnostic
https://www.confluent.io/hub
→ AWS Lambda Connector (preview)
→ Azure Functions Connector (preview)
→ GCP Functions Connector (coming)
<confluent-platform>
../bin/connect-standalone ./etc/json-standalone.properties ./etc/AwsLambdaSinkConnector.properties
5151
Confluent Hub
confluent.io/hub
52
Kafka Streams
● Data driven (dataflow)
● Partition aware
● Scale
● Resilient
● Delivery guarantees
● Replay events and time-travel
[Java, Scala]
53
Stream processing ‘FaaS’ Patterns
● Filter (gate)
● Transform
● Fan-out (one to many)
● Fan-in (many to one - join)
● Branch/Choose (if ‘n’ then)
● Aggregate
● ...more...
54
Kafka Streams: Filter/Gate - payment.isComplete()
{
StreamsBuilder builder = new StreamsBuilder();
KStream<String, Payment> incoming = builder.stream(TASK_TOPIC);
incoming.filter(
(key, payment) -> payment.isComplete()).to(TASK_COMPLETE);
…
Topology gateTopology = builder.build();
streams = new KafkaStreams(gateTopology, streamsConfig);
streams.start();
}
55
Kafka Streams: Fan out
{
StreamsBuilder builder = new StreamsBuilder();
KStream<String, Payment> incoming = builder.stream(TASK_TOPIC);
incoming.flatMapValues(payment -> {
ArrayList<String> fanTasks = new ArrayList<>();
for (int i = 0; i < payment.getAmount(); i++) {
fanTasks.add("fan-" + i);
}
return fanTasks;
}
).to(TASK_FANOUT); flatMapValues: Many V to the same K
Or
flatMap: Many K,V to one K,V pair
56
Kafka Streams: Fan In (join)
builder.stream(TASK_FANOUT)
.groupByKey(Grouped.with(Serdes.String(), Serdes.String()))
.windowedBy(SessionWindows.with(Duration.ofMinutes(1)))
.reduce(new Reducer<String>() {
public String apply(String agg1, String newValue) {
return agg1 + newValue;
}
})
.toStream()
.map((key, value) ->
new KeyValue<>(key.key() + "@" +
key.window().start() + "->" +
key.window().end(), value))
.to(TASK_REDUCE);
or: groupBy(KeyValueMapper)
Streaming events to FaaS: Fan-out Fan-in
{...}
{...}
{...}
{ bid:100; sold:1000;}
/bid-history
{
stream-lib.TDigest(values[])
}
{
stream-lib.TDigest(values[])
}
{
stream-lib.TDigest(values[])
}
Calculating percentiles using a ‘unit-of-work’ pattern
{
digest.merge(digest)
}
Stream processor with
a session window to
join related events by
key (k, v[1-10])
Stream
processor
fan out is natural (data-driven)
OR
use a Stream processor flatMap()
58
Multiple Bounded contexts
- Chaining
- Layering (Events as APIs)
2.
Logistics
payment.incoming 1.
Payments
payment.complete
{faas}
Central nervous system
appappappapp
Payments Department 2
{faas}appappappapp
Department 3 Department 4
Pattern: Central nervous system
Choreography
Choreography
Orchestration
Hierarchical topic naming
{faas}
What is the message format James?
appappappapp
Payments Department 2
Cloud Events: bringing order to chaos
I’m so confused!
https://cloudevents.io/
{
"specversion" : "0.4-wip",
"type" : "com.github.pull.create",
"source" : "https://github.com/cloudevents/spec/pull",
"subject" : "123",
"id" : "A234-1234-1234",
"time" : "2018-04-05T17:31:00Z",
"comexampleextension1" : "value",
"comexampleextension2" : {
"othervalue": 5
},
"datacontenttype" : "text/xml",
"data" : "<much wow="xml"/>"
}
● Specification (an Event envelope)
● Transport Bindings (Kafka, AMQP, HTTP others)
● SDKs: Java, .NET, Go-lang etc
● Still early - nearly version 1.0
● Useful for exposing to external apps (Events as APIs)
● Support is coming for Kafka SerDes and CE.clients (sdk)
CloudEvents is a specification for describing event data in common formats
to provide interoperability across services, platforms and systems.
You will adopt CloudEvents
6464
What about that software crisis that started in
1968?
“We believe that the major contributor to this complexity
in many systems is the handling of state and the burden
that this adds when trying to analyse and reason about
the system.”
Out of the tar pit, 2006
Key takeaways
● Event-Streaming apps
● Patterns
● Composition
● Scale
66
Questions?
@avery_neil
“Journey to event driven” blog
1. Event-first thinking
2. Programming models
3. Serverless
4. Pillars of event-streaming ms’s
https://bit.ly/2tFfU84
or @avery_neil twitter profile
67
@avery_neil

Cloud Native London 2019 Faas composition using Kafka and cloud-events