coherence implementation patterns - sig nov 2011
DESCRIPTION
Notes from the Nov Coherence SigTRANSCRIPT
![Page 1: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/1.jpg)
Coherence Implementa/on Pa1erns
Ben Stopford The Royal Bank of Scotland
![Page 2: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/2.jpg)
Some Ideas
Nothing More
![Page 3: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/3.jpg)
Why do we use Coherence?
Fast? Scalable?
Applica/on layer?
![Page 4: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/4.jpg)
Simplifying the Contract
![Page 5: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/5.jpg)
• 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.
![Page 6: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/6.jpg)
Version your Objects
![Page 7: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/7.jpg)
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?
![Page 8: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/8.jpg)
A.1
A.2
B.1
C.1
Versioned Cache
C.2
D.1
Versioning your Objects
![Page 9: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/9.jpg)
Versioning your Objects
A.1
A.2
Cache
Coherence Trigger
New Version = Old Version + 1 ??
![Page 10: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/10.jpg)
Running a Coherence Filter
![Page 11: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/11.jpg)
Using Key-‐Based Access
![Page 12: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/12.jpg)
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
![Page 13: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/13.jpg)
Latest / Versioned Pa1ern
Latest Cache Key = [Business Key] Versioned Cache Key = [Business Key][Version]
!"
#"
$"
%"
!&'"
!&("
#&'"
$&'"
$&("
%&'"
![Page 14: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/14.jpg)
Suffers from data duplica/on
![Page 15: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/15.jpg)
A.1
A.L
B.L
C.1
Versioned Cache
C.L
D.L
Write Coherence Trigger
Latest Marker Pa1ern
Well Known Marker Version
![Page 16: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/16.jpg)
However our trigger can’t use cache.put()
Why?
![Page 17: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/17.jpg)
Need to consider the threading model
![Page 18: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/18.jpg)
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); }
![Page 19: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/19.jpg)
A third approach
![Page 20: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/20.jpg)
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
![Page 21: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/21.jpg)
So we have 3 pa1erns for managing versioning whilst retaining key
based access
![Page 22: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/22.jpg)
Using versioning to manage concurrent changes
Mul/ Version Concurrency Control (MVCC)
![Page 23: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/23.jpg)
Version 1
Version 2
Cache
Coherence Trigger
New Version = Old Version + 1 ??
![Page 24: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/24.jpg)
Concurrent Object Update (2 Clients update the same object at the same /me)
A.1 A.2
Client1 Client2
A.2
![Page 25: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/25.jpg)
Concurrent Object Update (Client2 fails to update dirty object)
A.1
A.2
Versioned Cache
Client1 Client2
![Page 26: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/26.jpg)
Concurrent Object Update (Client 2 updates clean object)
A.1
A.2
A.3
Versioned Cache
Client1
Client2
![Page 27: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/27.jpg)
So a concurrent update results in an error and must be retried.
!"#$
!"%$
&'()*+#$ &'()*+%$
![Page 28: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/28.jpg)
What’s going to happen if we are using putAll?
![Page 29: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/29.jpg)
Reliable PutAll
We want putAll to tell us which objects failed the write process
![Page 30: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/30.jpg)
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)
![Page 31: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/31.jpg)
This gives us a reliable mechanism for knowing what worked and what
failed
![Page 32: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/32.jpg)
Synthesising Transac/onality
![Page 33: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/33.jpg)
A
B
C
D
Cache
The Fat Object Method
![Page 34: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/34.jpg)
The Single Entry Point Method (objects are stored separately)
Collocate with
key associa/on
![Page 35: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/35.jpg)
EP
All writes synchronize on the primary object.
![Page 36: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/36.jpg)
EP
All reads synchronize on the primary object.
![Page 37: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/37.jpg)
Wri/ng Orphaned Objects
Write orphaned objects first
Write read entry point object last
This mechanism is subtly flawed
![Page 38: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/38.jpg)
Reading several objects as an atomic unit
aka Joins
![Page 39: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/39.jpg)
The trivial approach to joins
Get Cost Centers
Get LedgerBooks
Get Source Books
Get Transac-‐/ons
Get MTMs
Get Legs
Get Cost Centers
Network
Time
![Page 40: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/40.jpg)
Server Side, Sharded Joins
Orders Shipping Log
Common Key
Use KeyAssocia/on to keep related en//es together
![Page 41: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/41.jpg)
Server Side, Sharded Joins
Transactions
Cashflows
Mtms Aggregator joins data across cluster
![Page 42: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/42.jpg)
So we have a set of mechanisms for reading and wri/ng groups of
related objects.
![Page 43: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/43.jpg)
Cluster Singleton Service
![Page 44: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/44.jpg)
A service that automa/cally restarts amer failure
![Page 45: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/45.jpg)
A service that automa/cally restarts amer failure
![Page 46: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/46.jpg)
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.
![Page 47: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/47.jpg)
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(); } }
![Page 48: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/48.jpg)
Implemen/ng Consistent Views and Repeatable Queries
![Page 49: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/49.jpg)
Bi-‐temporal
public interface MyBusinessObject{ //data public Date getBusinessDate(); public Date validFrom(); public Date validTo(); }
Business Time
System Time
![Page 50: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/50.jpg)
Where does the System Time come from?
![Page 51: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/51.jpg)
You can’t use the System.currentTimeMillis() in a
distributed environment!
![Page 52: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/52.jpg)
You need a cluster synchronised clock
![Page 53: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/53.jpg)
Singleton Service
Repeatable Time: A guaranteed Tick
Write Time
Read Time
Replicated Caches (pessimis/c)
Write first
Write second
![Page 54: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/54.jpg)
As we add objects we /mestamp them with Write Time
Singleton Service
Write Time
![Page 55: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/55.jpg)
Singleton Service
When we read objects we use Read Time
Read Time
![Page 56: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/56.jpg)
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
![Page 57: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/57.jpg)
Event Based Processing
A
B
C
D
Async Cachestore
cache.put(key, val);
![Page 58: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/58.jpg)
Messaging as a System of Record
Messaging System (use Topics for scalability)
![Page 59: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/59.jpg)
Messaging as a System of Record
A
B
C
D
Trigger
cache.put(key, val);
JMS
TOPIC
![Page 60: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/60.jpg)
Easy Grid Implementa/on in GUIs
![Page 61: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/61.jpg)
CQCs on a CQC
CQC Cache CQC
GUI JVM
Define this in config
![Page 62: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/62.jpg)
How do you release quickly to a Coherence cluster?
![Page 63: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/63.jpg)
Rolling Restart?
![Page 64: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/64.jpg)
Disk-‐Persist
![Page 65: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/65.jpg)
Final Thoughts
![Page 66: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/66.jpg)
Data is the most important commodity that you have
Keep it safe
![Page 67: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/67.jpg)
Use a Par//on Listener
![Page 68: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/68.jpg)
Have Proac/ve Monitoring of Memory
![Page 69: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/69.jpg)
Version your Objects
![Page 70: Coherence Implementation Patterns - Sig Nov 2011](https://reader033.vdocuments.mx/reader033/viewer/2022051816/546f9dcab4af9f440b8b45e8/html5/thumbnails/70.jpg)
Thanks
Slides & related ar/cles available at:
h1p://www.benstopford.com