Coherence Implementa/on Pa1erns Ben Stopford The Royal - - PowerPoint PPT Presentation

coherence implementa on pa1erns
SMART_READER_LITE
LIVE PREVIEW

Coherence Implementa/on Pa1erns Ben Stopford The Royal - - PowerPoint PPT Presentation

Coherence Implementa/on Pa1erns Ben Stopford The Royal Bank of Scotland Some Ideas Nothing More Why do we use Coherence? Fast? Scalable?


slide-1
SLIDE 1

Coherence ¡Implementa/on ¡ Pa1erns ¡

Ben ¡Stopford ¡ The ¡Royal ¡Bank ¡of ¡Scotland ¡

slide-2
SLIDE 2

Some ¡Ideas ¡ ¡

Nothing ¡More ¡

slide-3
SLIDE 3

Why ¡do ¡we ¡use ¡Coherence? ¡

Fast? ¡ Scalable? ¡ Applica/on ¡layer? ¡

slide-4
SLIDE 4

Simplifying ¡the ¡Contract ¡

slide-5
SLIDE 5
  • We ¡don’t ¡want ¡ACID ¡all ¡of ¡the ¡/me ¡
  • We ¡want ¡to ¡pick ¡the ¡bits ¡we ¡need ¡when ¡we ¡

need ¡them ¡

  • We ¡want ¡to ¡use ¡the ¡context ¡of ¡our ¡business ¡

requirement ¡to ¡work ¡our ¡way ¡around ¡the ¡ones ¡ we ¡don’t ¡need. ¡

slide-6
SLIDE 6

Version ¡your ¡Objects ¡

slide-7
SLIDE 7

Why ¡do ¡we ¡care? ¡

Without ¡versioning ¡it’s ¡a ¡free-­‑for-­‑all. ¡ ¡

  • What ¡changed? ¡
  • Was ¡something ¡overwri1en? ¡
  • How ¡can ¡you ¡prevent ¡concurrent ¡updates? ¡
  • What ¡did ¡the ¡system ¡look ¡like ¡10 ¡seconds ¡ago. ¡
  • How ¡can ¡I ¡provide ¡a ¡consistent ¡view? ¡
  • How ¡to ¡I ¡ensure ¡ordering ¡of ¡updates ¡in ¡an ¡

asynchronous ¡system? ¡

slide-8
SLIDE 8

A.1 ¡ A.2 ¡ B.1 ¡ C.1 ¡

Versioned ¡ Cache ¡

C.2 ¡ D.1 ¡

Versioning ¡your ¡Objects ¡

slide-9
SLIDE 9

Versioning ¡your ¡Objects ¡

A.1 ¡ A.2 ¡

Cache ¡

Coherence ¡Trigger ¡

New ¡Version ¡= ¡Old ¡Version ¡+ ¡1 ¡?? ¡

slide-10
SLIDE 10

Running ¡a ¡Coherence ¡Filter ¡

slide-11
SLIDE 11

Using ¡Key-­‑Based ¡Access ¡

slide-12
SLIDE 12

Latest ¡Cache ¡ Write ¡

A ¡ B ¡ C ¡ D ¡

A.1 ¡

A.2 ¡ B.1 ¡ C.1 ¡

Versioned ¡ Cache ¡

C.2 ¡ D.1 ¡

Coherence ¡Trigger ¡

Latest ¡/ ¡Versioned ¡Pa1ern ¡

slide-13
SLIDE 13

Latest ¡/ ¡Versioned ¡Pa1ern ¡

Latest ¡Cache ¡Key ¡= ¡[Business ¡Key] ¡ Versioned ¡Cache ¡Key ¡= ¡[Business ¡Key][Version] ¡

!" #" $" %"

!&'"

!&(" #&'" $&'" $&(" %&'"

slide-14
SLIDE 14

Suffers ¡from ¡data ¡duplica/on ¡

slide-15
SLIDE 15

A.1 ¡ A.L ¡ B.L ¡ C.1 ¡

Versioned ¡ Cache ¡

C.L ¡ D.L ¡

Write ¡

Coherence ¡Trigger ¡

Latest ¡Marker ¡Pa1ern ¡

Well ¡Known ¡ Marker ¡Version ¡

slide-16
SLIDE 16

However ¡our ¡trigger ¡can’t ¡use ¡ cache.put() ¡

Why? ¡

slide-17
SLIDE 17

Need ¡to ¡consider ¡the ¡threading ¡model ¡

slide-18
SLIDE 18

So ¡we’ll ¡need ¡to ¡use ¡the ¡backing ¡map ¡ directly ¡

public ¡void ¡copyObjectToVersionedCacheAddingVersion(MapTrigger.Entry ¡entry) ¡ { ¡ ¡ ¡ ¡MyValue ¡value ¡= ¡(MyValue)entry.getValue(); ¡ ¡ ¡ ¡MyKey ¡versionedKey ¡= ¡(MyKey)value.getKey(); ¡ ¡ ¡ ¡ ¡BinaryEntry ¡binary ¡= ¡(BinaryEntry)entry; ¡ ¡ ¡ ¡Binary ¡binaryValue ¡= ¡binaryEntry.getBinaryValue(); ¡ ¡ ¡ ¡ ¡Map ¡map ¡= ¡binary.getContext().getBackingMap("VersionedCacheName"); ¡ ¡ ¡ ¡map.put(toBinary(versionedKey), ¡binaryValue); ¡ } ¡

slide-19
SLIDE 19

A ¡third ¡approach ¡

slide-20
SLIDE 20

The ¡Collec/ons ¡Cache ¡

A ¡

[O1, ¡O2, ¡O3..] ¡

B ¡

[O1, ¡O2, ¡O3..] ¡

C ¡

[O1, ¡O2, ¡O3..] ¡

D ¡

[O1, ¡O2, ¡O3..] ¡

Collec/onsCache ¡ Trigger ¡Appends ¡to ¡Collec/on ¡

collec/onsCache.put(key, ¡val); ¡ collec/onsCache.invoke(key, ¡ new ¡LastValueGe1er()); ¡ …or ¡override ¡backing ¡store ¡

slide-21
SLIDE 21

So ¡we ¡have ¡3 ¡pa1erns ¡for ¡managing ¡ versioning ¡whilst ¡retaining ¡key ¡ based ¡access ¡

slide-22
SLIDE 22

Using ¡versioning ¡to ¡manage ¡ concurrent ¡changes ¡

Mul/ ¡Version ¡Concurrency ¡Control ¡ ¡ (MVCC) ¡

slide-23
SLIDE 23

Version ¡1 ¡ Version ¡2 ¡

Cache ¡

Coherence ¡Trigger ¡

New ¡Version ¡= ¡Old ¡Version ¡+ ¡1 ¡?? ¡

slide-24
SLIDE 24

Concurrent ¡Object ¡Update ¡

(2 ¡Clients ¡update ¡the ¡same ¡object ¡at ¡the ¡same ¡/me) ¡

A.1 ¡ A.2 ¡

Client1 ¡ Client2 ¡

A.2 ¡

slide-25
SLIDE 25

Concurrent ¡Object ¡Update ¡

(Client2 ¡fails ¡to ¡update ¡dirty ¡object) ¡

A.1 ¡ A.2 ¡

Versioned ¡ Cache ¡ Client1 ¡ Client2 ¡

slide-26
SLIDE 26

Concurrent ¡Object ¡Update ¡

(Client ¡2 ¡updates ¡clean ¡object) ¡

A.1 ¡ A.2 ¡ A.3 ¡

Versioned ¡ Cache ¡ Client1 ¡ Client2 ¡

slide-27
SLIDE 27

So ¡a ¡concurrent ¡update ¡results ¡in ¡ an ¡error ¡and ¡must ¡be ¡retried. ¡

!"#$ !"%$

&'()*+#$ &'()*+%$

slide-28
SLIDE 28

What’s ¡going ¡to ¡happen ¡if ¡we ¡are ¡ using ¡putAll? ¡

slide-29
SLIDE 29

Reliable ¡PutAll ¡

We ¡want ¡putAll ¡to ¡tell ¡us ¡which ¡

  • bjects ¡failed ¡the ¡write ¡process ¡
slide-30
SLIDE 30

Reliable ¡PutAll ¡

Client ¡ Extend ¡

Node ¡ Node ¡ Node ¡ Node ¡ Node ¡ Node ¡

Invocable: ¡

  • Split ¡keys ¡by ¡member ¡
  • Send ¡appropriate ¡values ¡

to ¡each ¡member ¡

  • Collect ¡any ¡excep/ons ¡

returned ¡ ¡ Invocable: ¡

  • Write ¡entries ¡to ¡

backing ¡map ¡(we ¡ use ¡an ¡EP ¡for ¡ this) ¡

slide-31
SLIDE 31

This ¡gives ¡us ¡a ¡reliable ¡mechanism ¡ for ¡knowing ¡what ¡worked ¡and ¡what ¡ failed ¡

slide-32
SLIDE 32

Synthesising ¡Transac/onality ¡

slide-33
SLIDE 33

A ¡ B ¡ C ¡ D ¡

Cache ¡

The ¡Fat ¡Object ¡Method ¡

slide-34
SLIDE 34

The ¡Single ¡Entry ¡Point ¡Method ¡

(objects ¡are ¡stored ¡separately) ¡

¡ ¡

Collocate ¡with ¡ key ¡associa/on ¡

slide-35
SLIDE 35

EP ¡

All ¡writes ¡synchronize ¡on ¡the ¡ primary ¡object. ¡

slide-36
SLIDE 36

EP ¡

All ¡reads ¡synchronize ¡on ¡the ¡ primary ¡object. ¡

slide-37
SLIDE 37

Wri/ng ¡Orphaned ¡Objects ¡

Write ¡orphaned ¡objects ¡first ¡ Write ¡read ¡ entry ¡point ¡

  • bject ¡last ¡

This ¡mechanism ¡is ¡subtly ¡flawed ¡

slide-38
SLIDE 38

Reading ¡several ¡objects ¡as ¡an ¡ atomic ¡unit ¡

aka ¡Joins ¡

slide-39
SLIDE 39

The ¡trivial ¡approach ¡to ¡joins ¡

Get ¡ Cost ¡ Centers ¡ Get ¡ Ledger Books ¡ Get ¡ Source ¡ Books ¡ Get ¡ Transac

  • ­‑/ons ¡

Get ¡ MTMs ¡ Get ¡ Legs ¡ Get ¡ Cost ¡ Centers ¡ Network Time ¡

slide-40
SLIDE 40

Server ¡Side, ¡Sharded ¡Joins ¡

Orders ¡ Shipping ¡Log ¡

Common ¡Key ¡

Use ¡KeyAssocia/on ¡to ¡ keep ¡related ¡en//es ¡ together ¡

slide-41
SLIDE 41

Server ¡Side, ¡Sharded ¡Joins ¡

Transactions Cashflows Mtms

Aggregator joins data across cluster

slide-42
SLIDE 42

So ¡we ¡have ¡a ¡set ¡of ¡mechanisms ¡for ¡ reading ¡and ¡wri/ng ¡groups ¡of ¡ related ¡objects. ¡

slide-43
SLIDE 43

Cluster ¡Singleton ¡Service ¡

slide-44
SLIDE 44

A ¡service ¡that ¡automa/cally ¡restarts ¡ amer ¡failure ¡

slide-45
SLIDE 45

A ¡service ¡that ¡automa/cally ¡restarts ¡ amer ¡failure ¡

slide-46
SLIDE 46

What ¡is ¡the ¡cluster ¡singleton ¡good ¡for ¡

  • Adding ¡indexes ¡
  • Loading ¡data ¡
  • Keeping ¡data ¡up ¡to ¡date ¡
  • Upda/ng ¡cluster ¡/me ¡
  • You ¡can ¡probably ¡think ¡of ¡a ¡bunch ¡of ¡others ¡
  • yourselves. ¡
slide-47
SLIDE 47

Code ¡for ¡Cluster ¡Singleton ¡

//run in a new thread on every Cache Server while (true) { boolean gotLock = lockCache.lock("singletonLock", -1); if (gotLock) { //Start singletons wait(); } }

slide-48
SLIDE 48

Implemen/ng ¡Consistent ¡Views ¡ and ¡Repeatable ¡Queries ¡

slide-49
SLIDE 49

Bi-­‑temporal ¡ ¡

public ¡interface ¡MyBusinessObject{ ¡ ¡ ¡ ¡//data ¡ ¡ ¡ ¡ ¡public ¡Date ¡getBusinessDate(); ¡ ¡ ¡ ¡public ¡Date ¡validFrom(); ¡ ¡ ¡ ¡public ¡Date ¡validTo(); ¡ } ¡ ¡

Business ¡ Time ¡ System ¡ Time ¡

slide-50
SLIDE 50

Where ¡does ¡the ¡System ¡Time ¡ come ¡from? ¡

slide-51
SLIDE 51

You ¡can’t ¡use ¡the ¡ System.currentTimeMillis() ¡in ¡a ¡ distributed ¡environment! ¡

slide-52
SLIDE 52

You ¡need ¡a ¡cluster ¡synchronised ¡ clock ¡

slide-53
SLIDE 53

Singleton ¡ ¡ Service ¡

Repeatable ¡Time: ¡A ¡guaranteed ¡Tick ¡

Write ¡Time ¡ Read ¡Time ¡

Replicated ¡Caches ¡ (pessimis/c) ¡

Write ¡first ¡ Write ¡second ¡

slide-54
SLIDE 54

As ¡we ¡add ¡objects ¡we ¡/mestamp ¡them ¡ with ¡Write ¡Time ¡

Singleton ¡ ¡ Service ¡

Write ¡Time ¡

slide-55
SLIDE 55

Singleton ¡ ¡ Service ¡

When ¡we ¡read ¡objects ¡we ¡use ¡Read ¡ Time ¡

Read ¡Time ¡

slide-56
SLIDE 56

Singleton ¡ ¡ Service ¡

Repeatable ¡Time: ¡A ¡guaranteed ¡Tick ¡

Write ¡Time ¡ Read ¡Time ¡

Replicated ¡Caches ¡ (pessimis/c) ¡

Write ¡first ¡ Write ¡second ¡

7 ¡ 8 ¡ 7 ¡ 8 ¡ 7 ¡ 8 ¡ 6 ¡ 7 ¡ 6 ¡ 7 ¡ 6 ¡ 7 ¡

slide-57
SLIDE 57

Event ¡Based ¡Processing ¡

A ¡ B ¡ C ¡ D ¡

Async ¡ ¡ Cachestore ¡

cache.put(key, ¡val); ¡

slide-58
SLIDE 58

Messaging ¡as ¡a ¡System ¡of ¡Record ¡

Messaging System (use Topics for scalability)

slide-59
SLIDE 59

Messaging ¡as ¡a ¡System ¡of ¡Record ¡

A ¡ B ¡ C ¡ D ¡

Trigger ¡

cache.put(key, ¡val); ¡

JMS

TOPIC ¡

slide-60
SLIDE 60

Easy ¡Grid ¡Implementa/on ¡in ¡GUIs ¡

slide-61
SLIDE 61

CQCs ¡on ¡a ¡CQC ¡

CQC ¡ Cache ¡

CQC ¡

GUI ¡JVM ¡

Define ¡this ¡in ¡config ¡

slide-62
SLIDE 62

How ¡do ¡you ¡release ¡quickly ¡to ¡a ¡ Coherence ¡cluster? ¡

slide-63
SLIDE 63

Rolling ¡Restart? ¡

slide-64
SLIDE 64

Disk-­‑Persist ¡

slide-65
SLIDE 65

Final ¡Thoughts ¡

slide-66
SLIDE 66

Data ¡is ¡the ¡most ¡important ¡ commodity ¡that ¡you ¡have ¡ ¡

Keep ¡it ¡safe ¡

slide-67
SLIDE 67

Use ¡a ¡Par//on ¡Listener ¡

slide-68
SLIDE 68

Have ¡Proac/ve ¡Monitoring ¡of ¡ Memory ¡

slide-69
SLIDE 69

Version ¡your ¡Objects ¡

slide-70
SLIDE 70

Thanks ¡

Slides ¡& ¡related ¡ar/cles ¡available ¡at: ¡ ¡

h1p://www.benstopford.com ¡

¡