streaming, database & distributed systems bridging the divide

82
Streaming, Database & Distributed Systems: Bridging the Divide Ben Stopford (@benstopford) Codemesh 2016

Upload: ben-stopford

Post on 08-Jan-2017

2.007 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Streaming, Database & Distributed Systems Bridging the Divide

Streaming, Database & Distributed Systems:

Bridging the Divide Ben Stopford (@benstopford)

Codemesh 2016

Page 2: Streaming, Database & Distributed Systems Bridging the Divide
Page 3: Streaming, Database & Distributed Systems Bridging the Divide

Event Driven Systems

Most stateful systems have to pull from these three worlds

Page 4: Streaming, Database & Distributed Systems Bridging the Divide

Today we have 2 goals

1.  Understand Stateful Stream Processing (now & near future)

2.  Case for SSP as a general framework for building data-centric systems.

Page 5: Streaming, Database & Distributed Systems Bridging the Divide

Data systems come in different forms

•  Database (OLTP)

•  Analytics Database (OLAP/Hadoop)

•  Messaging

•  Distributed log

•  Stream Processing

•  Stateful Stream Processing

Page 6: Streaming, Database & Distributed Systems Bridging the Divide

Database (OLTP)

Focuses on providing a consistent view that supports updates and queries on individual tuples.

Page 7: Streaming, Database & Distributed Systems Bridging the Divide

Analytics Database (OLAP/Hadoop) 1.  Focuses on aggregations via table scans.

2.  Executes as distributed system

Page 8: Streaming, Database & Distributed Systems Bridging the Divide

Messaging Focuses on asynchronous information transfer with limited state

Page 9: Streaming, Database & Distributed Systems Bridging the Divide

Distributed Log

1.  Similar to messaging, but data can be retained

2.  Executes as distributed system (scale + fault tolerance)

Page 10: Streaming, Database & Distributed Systems Bridging the Divide

Stream Processing

Manipulate concurrent streams of events

Comes from CEP background (ephemeral)

Page 11: Streaming, Database & Distributed Systems Bridging the Divide

Stateful Stream Processing Moves stream processing to be a more general framework for building data-centric systems.

Page 12: Streaming, Database & Distributed Systems Bridging the Divide

What is stream processing?

Data Index

Query Engine

Query Engine

vs

Database Finite source

Stream Processor Infinite source

Page 13: Streaming, Database & Distributed Systems Bridging the Divide

Infinite streams need windows

How many items will we bring into the machine at one time?

Page 14: Streaming, Database & Distributed Systems Bridging the Divide

Windows bound a computation

How many items will we bring into the machine at one time?

Page 15: Streaming, Database & Distributed Systems Bridging the Divide

Buffering allows us to handle late events

How many items will we bring into the machine at one time?

Page 16: Streaming, Database & Distributed Systems Bridging the Divide

Some query Over some time window Emitting at some frequency

Continually executing query

Stream(s)

Stream Processing Engine

Derived Stream

Page 17: Streaming, Database & Distributed Systems Bridging the Divide

Avg(p.time – o.time) From orders, payment Group by payment.region over 1 day window emitting every second

Stream Processing

orders !

payments!

Completion time, by region!

Page 18: Streaming, Database & Distributed Systems Bridging the Divide

Avg(o.time – p.time) From orders, payment Group by payment.region over 1 day window emitting every second

Materialised View (DB )

Query

orders !

payments!

Completion time, by region!

Page 19: Streaming, Database & Distributed Systems Bridging the Divide

Avg(o.time – p.time) From orders, payment, user Group by user.region over 1 day window emitting every second

Stateful Stream Processing

Streams

Stream Processing Engine

Derived Stream

Query

Derived “Table” Table

“View” is output as table or stream

Page 20: Streaming, Database & Distributed Systems Bridging the Divide

Table == Stream + Window0n

== 0 N

Table is a stream with an infinite window (i.e. buffer from 0 -> now)

window !

Page 21: Streaming, Database & Distributed Systems Bridging the Divide

SSP is about creating materialised views.

Materialised as a table, or materialised as a stream

Page 22: Streaming, Database & Distributed Systems Bridging the Divide

Features: similar to database query engine

Join Filter Aggr- egate

View Windowed Streams

Page 23: Streaming, Database & Distributed Systems Bridging the Divide

Can distribute over many machines in two dimensions

Join Filter Aggr- egate

View

Join Filter Aggr- egate

View

Join Filter Aggr- egate

View

Scale Out Scale Forward

Page 24: Streaming, Database & Distributed Systems Bridging the Divide

Stateful Stream Processing engines typically use Kafka (a distributed commit log)

Join Filter Aggr- egate

View

Kafka (a distributed log)

Page 25: Streaming, Database & Distributed Systems Bridging the Divide

A log is very simple idea

Messages are added at the end of the log

Just think of the log as a file

Old New

Page 26: Streaming, Database & Distributed Systems Bridging the Divide

Readers have a position & scan

Sally is here

George is here

Fred is here

Old New

Scan Scan

Scan

Page 27: Streaming, Database & Distributed Systems Bridging the Divide

Can “Rewind & Replay” the log

Rewind & Replay

Page 28: Streaming, Database & Distributed Systems Bridging the Divide

Compacted Log (Tabular View)

Version 3

Version 2

Version 1

Version 2

Version 1

Version 5

Version 4

Version 3

Version 2

Version 1

Version 2

Version 3

Version 5

STEAM (All versions)

COMPACTED STREAM (Latest Key only)

Page 29: Streaming, Database & Distributed Systems Bridging the Divide

The log is a Distributed System

For scalability and fault tolerance

Page 30: Streaming, Database & Distributed Systems Bridging the Divide

Shard on the way in

Producers

Kafka

Consumers

Page 31: Streaming, Database & Distributed Systems Bridging the Divide

Each shard is a queue

Producers

Kafka

Consumers

Page 32: Streaming, Database & Distributed Systems Bridging the Divide

Producers

Kafka

Many consumers share partitions

in one topic

Consumers share consumption of a single topic

Page 33: Streaming, Database & Distributed Systems Bridging the Divide

The Log reassigns data on failure

Producers

Kafka

Many consumers share partitions in

one topic

Page 34: Streaming, Database & Distributed Systems Bridging the Divide

Kafka supplies two levels of leader election

Replicas in Kafka have an elected leader

Consumers in Kafka have an elected leader

Page 35: Streaming, Database & Distributed Systems Bridging the Divide

The log is important for SSP

Maintains History: Acts like a “push based” distributed file system

Page 36: Streaming, Database & Distributed Systems Bridging the Divide

The log is important: Two Primitives

Stream

Compacted Stream (‘table’)

Page 37: Streaming, Database & Distributed Systems Bridging the Divide

The Log is, to a streaming engine, what HDFS is to Hadoop

Page 38: Streaming, Database & Distributed Systems Bridging the Divide

But it’s a bit more than a HDFS replacement: Processors inherit the idea of “membership” from the log

Page 39: Streaming, Database & Distributed Systems Bridging the Divide

So stateful Stream Processors use the Log

Join Filter Aggr- egate

View

Kafka (Distributed Log)

Page 40: Streaming, Database & Distributed Systems Bridging the Divide

They also use local storage

Join Filter Aggr- egate

View

(1) a Kafka

(2) Local KV Store

Page 41: Streaming, Database & Distributed Systems Bridging the Divide

Local KV store has a few uses

(1)  It caches streams on disk (2)  It caches “tables” on disk

Join Filter Aggr- egate

View

This makes join operations fast as they’re entirely local

Streams just cache recent messages to help with joins

Tables are fully “realised” locally

Page 42: Streaming, Database & Distributed Systems Bridging the Divide

Stateful Stream Processing

stream

Compacted stream

Join

Stream data

Stream-Tabular Data

Infinite Stream

Locally Cached Table

(disk resident)

Kafka Kafka Streams

Page 43: Streaming, Database & Distributed Systems Bridging the Divide

e.g. Useful for Enrichment

stream

Compacted stream

Join

Orders

Customers

Kafka Kafka Streams

Local DB

Page 44: Streaming, Database & Distributed Systems Bridging the Divide

Aggregates need intermediary state

stream

Compacted stream

Join

Orders

Customers

Kafka Sum(orders) group by region

Persist current value, in case we fail

Page 45: Streaming, Database & Distributed Systems Bridging the Divide

State store inherits durability from the log

State store flushes back to the log

Join Filter Aggr- egate

View

Page 46: Streaming, Database & Distributed Systems Bridging the Divide

Separate Data, Processing & View

View

Orders Payments View

View

Storage Layer (a Kafka)

Processing & View

Query

Page 47: Streaming, Database & Distributed Systems Bridging the Divide

You can query the views from anywhere

View

Orders Payments View

View

Storage Layer (a Kafka)

Processing & View

Query

Page 48: Streaming, Database & Distributed Systems Bridging the Divide

So what happens on failure?

View

Orders Payments View

View

Storage Layer (a Kafka)

Processing & View

Page 49: Streaming, Database & Distributed Systems Bridging the Divide

Clustering Reroutes Data to surviving node

View

Orders Payments View

View

Storage Layer (Kafka)

Ownership of partitions is re-routed from dead node

Processing & View

Page 50: Streaming, Database & Distributed Systems Bridging the Divide

But what about state?

View

Orders Payments View

View

Storage Layer (Kafka)

“Cold” replica of state takes over

Processing & View

Page 51: Streaming, Database & Distributed Systems Bridging the Divide

Primitives for sharding & replication

Stock

Orders Payments Stock

Stock

Redundant copies are cached on other nodes

Sharding spread data over processors

Page 52: Streaming, Database & Distributed Systems Bridging the Divide

So processors inherit much from the log

Clustering comes from the log

You just write the functional bit

Page 53: Streaming, Database & Distributed Systems Bridging the Divide

General framework for distributed, realtime data computation

Protection from broker failure

Protection from engine failure

Join tables & streams (in process)

Event Driven

Create views which can be queried

Query

Page 54: Streaming, Database & Distributed Systems Bridging the Divide

But stream processing has a

problem

Page 55: Streaming, Database & Distributed Systems Bridging the Divide

Correctness Guarantees in multi layer topologies

Join Filter Aggr- egate

View

Join Filter Aggr- egate

View

Join Filter Aggr- egate

View

Join Filter Aggr- egate

View

Join Filter Aggr- egate

View

Page 56: Streaming, Database & Distributed Systems Bridging the Divide

Join Filter Aggr- egate

View

Join Filter Aggr- egate

View

Join Filter Aggr- egate

View

Join Filter Aggr- egate

View

Duplicates are a side effect of all at-least-once delivery mechanisms

Data is rerouted, on failure, which can cause duplicates

Page 57: Streaming, Database & Distributed Systems Bridging the Divide

Idempotance isn’t enough

Join Filter Aggr- egate

View

Join Filter Aggr- egate

View

Filter

Join Filter Aggr- egate

View

Join Filter Aggr- egate

View

Page 58: Streaming, Database & Distributed Systems Bridging the Divide

Distributed Snapshots* (transactions)

Join Filter Aggr- egate

View

Join Filter Aggr- egate

View

Join Filter Aggr- egate

View

Transaction markers: [Begin], [Prepare], [Commit], [Abort]

Buffer

Chandy, Lamport - Distributed Snapshots: Determining Global States of Distributed Systems

*In development in Kafka

Page 59: Streaming, Database & Distributed Systems Bridging the Divide
Page 60: Streaming, Database & Distributed Systems Bridging the Divide
Page 61: Streaming, Database & Distributed Systems Bridging the Divide

So why use these tools?

Page 62: Streaming, Database & Distributed Systems Bridging the Divide

(1) Streaming is a superset of batch

Page 63: Streaming, Database & Distributed Systems Bridging the Divide

Databases look backwards

Page 64: Streaming, Database & Distributed Systems Bridging the Divide

Batch == Streaming from offset 0

Query

Query

Query

Distributed File System (HDFS)

Query

Query

Query

Distributed Log (Kafka)

MPP Batch System MPP Streaming System

Page 65: Streaming, Database & Distributed Systems Bridging the Divide

Streaming is the superset of batch

Streaming

Batch

Database

Global, Linearisible consistency model

Page 66: Streaming, Database & Distributed Systems Bridging the Divide

(2) Separates store & view

Page 67: Streaming, Database & Distributed Systems Bridging the Divide

“Engine” part is lightweight but stateful

Storage Just a java process which uses a library

Log handles fault tolerance of both layers

Page 68: Streaming, Database & Distributed Systems Bridging the Divide

Separates Concerns of Model & View – Think MVC

Storage View & Controller

Model

Page 69: Streaming, Database & Distributed Systems Bridging the Divide

Physically Separates Read & Write – Think CQRS

Storage View & Controller

Model

Page 70: Streaming, Database & Distributed Systems Bridging the Divide

Database vs SSP

Data Index

Query Engine

Query Engine

vs

Database Stateful Stream Processor

Query

Query

View

Index Data

Page 71: Streaming, Database & Distributed Systems Bridging the Divide

(3) Decentralised approaches are more general

Page 72: Streaming, Database & Distributed Systems Bridging the Divide

Rather than pushing processing into an “appliance”

(code -> data)

Centralised Processing

App

Page 73: Streaming, Database & Distributed Systems Bridging the Divide

Data Decentric Architecture

Distributed Log

Decentralised Processing over many user-specific views

Page 74: Streaming, Database & Distributed Systems Bridging the Divide

This more general than than just

analytics use cases

Page 75: Streaming, Database & Distributed Systems Bridging the Divide

It’s more than taking a database and adding push

notifications

Page 76: Streaming, Database & Distributed Systems Bridging the Divide

Whether you’re building a hulking, multistage, analytic platform

Query

Final View

Intermediary View (2)

Intermediary View (1)

Page 77: Streaming, Database & Distributed Systems Bridging the Divide

Or a simple microservice that needs to run hot-hot & scale

Business Logic Manage local

state

Join various streams

Hot secondary instance

Page 78: Streaming, Database & Distributed Systems Bridging the Divide

Composable Primatives

Declarative Function

Traditional DB

Work Distribution

Replication

Sharding

Query Engine

Distributed DB Distributed Systems

Membership

Global Consistency

Page 79: Streaming, Database & Distributed Systems Bridging the Divide

General framework for distributed, event-driven data computation

Protection from broker failure

Protection from engine failure

Join tables & streams (in process)

Event Driven

Create views which can be queried

Query

Page 80: Streaming, Database & Distributed Systems Bridging the Divide

Stateful Stream Processing

Framework for building a streaming data systems, just for you “~)

Page 81: Streaming, Database & Distributed Systems Bridging the Divide

Find out more:

•  http://www.confluent.io/blog/introducing-kafka-streams-stream-processing-made-simple/

•  https://martin.kleppmann.com/2015/02/11/database-inside-out-at-salesforce.html

•  http://cidrdb.org/cidr2015/Papers/CIDR15_Paper16.pdf

•  https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/cidr07p42.pdf

•  http://highscalability.com/blog/2015/5/4/elements-of-scale-composing-and-scaling-data-platforms.html

•  https://speakerdeck.com/bobbycalderwood/commander-decoupled-immutable-rest-apis-with-kafka-streams

•  https://timothyrenner.github.io/engineering/2016/08/11/kafka-streams-not-looking-at-facebook.html

•  https://www.madewithtea.com/processing-tweets-with-kafka-streams.html

•  http://www.infolace.com/blog/2016/07/14/simple-spatial-windowing-with-kafka-streams/

•  http://www.slideshare.net/zacharycox/updating-materialized-views-and-caches-using-kafka

Page 82: Streaming, Database & Distributed Systems Bridging the Divide

The end

@benstopford http://benstopford.com