30
So you think you know pub/sub ? Udi Dahan in Particular @udidahan

So you think you know pub/sub ? Udi Dahan in

Embed Size (px)

DESCRIPTION

Publish/subscribe basics Enables one-to-many communication Pub Sub1 Sub2 Sub3

Citation preview

Page 1: So you think you know pub/sub ? Udi Dahan in

So you think you knowpub/sub ?

Udi Dahanin Particular

@udidahan

Page 2: So you think you know pub/sub ? Udi Dahan in

Agenda

Basics Patterns Distribution

Page 3: So you think you know pub/sub ? Udi Dahan in

Publish/subscribe basics

Enables one-to-many communication

Pub

Sub1

Sub2

Sub3

Page 4: So you think you know pub/sub ? Udi Dahan in

Publish/subscribe basics

Enables one-to-many communication

Should really be called “subscribe/publish”

Pub

Sub1

Sub2

Sub3

sub

sub

sub

Page 5: So you think you know pub/sub ? Udi Dahan in

Publish/subscribe basics

Enables one-to-many communication

Should really be called “subscribe/publish”

Not the same as multicast – it’s more reliable

Page 6: So you think you know pub/sub ? Udi Dahan in

Publisher

Subscriber

Subscriber

Subscriber

Subscriber

Page 7: So you think you know pub/sub ? Udi Dahan in

Publish/subscribe basics

Enables one-to-many communication

Should really be called “subscribe/publish”

Not the same as multicast – it’s more reliable

Is about logical, not physical data distribution

Each event should be processed once

Page 8: So you think you know pub/sub ? Udi Dahan in

Publisher

Sub3_2

Sub3_1

Sub2

Sub1

Sub3 LB

Page 9: So you think you know pub/sub ? Udi Dahan in

But what about data sync?

Keeping in-proc caches synced in a web farm

Use a distributed cache for that (Redis, etc)

Do not build your own distributed cacheNot unless you absolutely HAVE to

More on this later

Page 10: So you think you know pub/sub ? Udi Dahan in

Subscribers can be publishers too

Think peer-to-peer, not client/server

PS1

PS2

PS3PS4

Page 11: So you think you know pub/sub ? Udi Dahan in

Avoid shared resources

Shared databases create tight coupling

PS1

PS2

PS3PS4

DB

Page 12: So you think you know pub/sub ? Udi Dahan in

Seek out autonomy

But preserve the “single source of truth”

PS1

PS2

PS3PS4

Page 13: So you think you know pub/sub ? Udi Dahan in

Basics

Patterns

Distributio

n

Page 14: So you think you know pub/sub ? Udi Dahan in

Events – not commands

Always publish events – not commands

Examples:OrderCancelled, AccountCreated

Something that already happened – a factSubscribers can’t invalidate events

But what about failures?

Page 15: So you think you know pub/sub ? Udi Dahan in

Technological failures

Deserialization failuresMove off to “error” queue for admin to handleLikely to be returned for reprocessing later

Transient failures (deadlocks & other exceptions)

Retry + backoff & escalate to error queue

Process & server crashesTX processing for complete rollback*

Page 16: So you think you know pub/sub ? Udi Dahan in

Insufficient transactionality risks

DB

Q

Q

Entity ID

Entity IDnot in DB

System gets out of sync

Page 17: So you think you know pub/sub ? Udi Dahan in

Careful with XYZ_Updated events

Simple CRUD domains less suitable for implementation on top of pub/sub

In-order event processing usually not guaranteed

Can be mitigated with sequence numbers

… and logic which matches them to entity versions

Consider “Valid-to/from” semantics

Page 18: So you think you know pub/sub ? Udi Dahan in

Auditing / Journaling

Copy msg to another queue after processing

Supported out-of-the-box by most queuesExtract to longer-term storage

So the queue doesn’t “explode”

A central log of everything that happened

Can be difficult to interpret by itself

Page 19: So you think you know pub/sub ? Udi Dahan in

Leveraging message headers

Endpoint 1

Message ID: 1

Conversation ID: 1Message ID: 2

Conversation ID: 1Message ID: 3

Audit

Endpoint 2 Endpoint 3

Maintain a conversation ID header for cross-endpoint message flows

Page 20: So you think you know pub/sub ? Udi Dahan in

Basics

Patterns

Distributio

n

Page 21: So you think you know pub/sub ? Udi Dahan in

Content-based “pub/sub”

When subscriber-side filtering won’t scale

User defines rules about what’s “interesting”

And that can change at runtime

It’s primarily about physical data distribution

Not logical division of responsibilities

Page 22: So you think you know pub/sub ? Udi Dahan in

Finance

Subscribing to updates of specific stocks

Page 23: So you think you know pub/sub ? Udi Dahan in

Industrial / Internet of Things

Subscribing to events about sensor states

Page 24: So you think you know pub/sub ? Udi Dahan in

Solutions – well, it depends

For small numbers of users (internal employees)

Keep a single distributed cache up to dateHave user machines poll the cache every second

Across multiple sites, have a cache at each site

User machines poll the cache of their site

In short – no real use of pub/sub

Page 25: So you think you know pub/sub ? Udi Dahan in

“Clicks & mortar” Retail

Distributing price changes / end-of-day orders

Page 26: So you think you know pub/sub ? Udi Dahan in

“Pub/sub” between geographic sites

Also focused on data distribution

Often want visibility into progress of distribution

Which sites haven’t received the data yet

Geographic sites tend to have business meaning

Page 27: So you think you know pub/sub ? Udi Dahan in

“Clicks & mortar” Retail

Cross-site distribution done within a SOA service

Not really pub/sub

Page 28: So you think you know pub/sub ? Udi Dahan in

Summary

Basics

Patterns

Distributio

n

Page 29: So you think you know pub/sub ? Udi Dahan in

Q&A

Page 30: So you think you know pub/sub ? Udi Dahan in

Thank you

Udi Dahanin Particular