Coherence Implementation Patterns - Sig Nov 2011

70
Coherence Implementa/on Pa1erns Ben Stopford The Royal Bank of Scotland

description

Notes from the Nov Coherence Sig

Transcript of Coherence Implementation Patterns - Sig Nov 2011

Page 1: Coherence Implementation Patterns - Sig Nov 2011

Coherence  Implementa/on  Pa1erns  

Ben  Stopford  The  Royal  Bank  of  Scotland  

Page 2: Coherence Implementation Patterns - Sig Nov 2011

Some  Ideas    

Nothing  More  

Page 3: Coherence Implementation Patterns - Sig Nov 2011

Why  do  we  use  Coherence?  

Fast?  Scalable?  

Applica/on  layer?  

Page 4: Coherence Implementation Patterns - Sig Nov 2011

Simplifying  the  Contract  

Page 5: Coherence Implementation Patterns - Sig Nov 2011

•  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

Version  your  Objects  

Page 7: Coherence Implementation Patterns - Sig Nov 2011

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

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

Versioning  your  Objects  

A.1  

A.2  

Cache  

Coherence  Trigger  

New  Version  =  Old  Version  +  1  ??  

Page 10: Coherence Implementation Patterns - Sig Nov 2011

Running  a  Coherence  Filter  

Page 11: Coherence Implementation Patterns - Sig Nov 2011

Using  Key-­‐Based  Access  

Page 12: Coherence Implementation Patterns - Sig Nov 2011

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

Latest  /  Versioned  Pa1ern  

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

!"

#"

$"

%"

!&'"

!&("

#&'"

$&'"

$&("

%&'"

Page 14: Coherence Implementation Patterns - Sig Nov 2011

Suffers  from  data  duplica/on  

Page 15: Coherence Implementation Patterns - Sig Nov 2011

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

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

Why?  

Page 17: Coherence Implementation Patterns - Sig Nov 2011

Need  to  consider  the  threading  model  

Page 18: Coherence Implementation Patterns - Sig Nov 2011

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

A  third  approach  

Page 20: Coherence Implementation Patterns - Sig Nov 2011

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

So  we  have  3  pa1erns  for  managing  versioning  whilst  retaining  key  

based  access  

Page 22: Coherence Implementation Patterns - Sig Nov 2011

Using  versioning  to  manage  concurrent  changes  

Mul/  Version  Concurrency  Control    (MVCC)  

Page 23: Coherence Implementation Patterns - Sig Nov 2011

Version  1  

Version  2  

Cache  

Coherence  Trigger  

New  Version  =  Old  Version  +  1  ??  

Page 24: Coherence Implementation Patterns - Sig Nov 2011

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

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

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

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

!"#$

!"%$

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

Page 28: Coherence Implementation Patterns - Sig Nov 2011

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

Page 29: Coherence Implementation Patterns - Sig Nov 2011

Reliable  PutAll  

We  want  putAll  to  tell  us  which  objects  failed  the  write  process  

Page 30: Coherence Implementation Patterns - Sig Nov 2011

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

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

failed  

Page 32: Coherence Implementation Patterns - Sig Nov 2011

Synthesising  Transac/onality  

Page 33: Coherence Implementation Patterns - Sig Nov 2011

A  

B  

C  

D  

Cache  

The  Fat  Object  Method  

Page 34: Coherence Implementation Patterns - Sig Nov 2011

The  Single  Entry  Point  Method  (objects  are  stored  separately)  

   Collocate  with  

key  associa/on  

Page 35: Coherence Implementation Patterns - Sig Nov 2011

EP  

All  writes  synchronize  on  the  primary  object.  

Page 36: Coherence Implementation Patterns - Sig Nov 2011

EP  

All  reads  synchronize  on  the  primary  object.  

Page 37: Coherence Implementation Patterns - Sig Nov 2011

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

Reading  several  objects  as  an  atomic  unit  

aka  Joins  

Page 39: Coherence Implementation Patterns - Sig Nov 2011

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

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

Server  Side,  Sharded  Joins  

Transactions

Cashflows

Mtms Aggregator joins data across cluster

Page 42: Coherence Implementation Patterns - Sig Nov 2011

So  we  have  a  set  of  mechanisms  for  reading  and  wri/ng  groups  of  

related  objects.  

Page 43: Coherence Implementation Patterns - Sig Nov 2011

Cluster  Singleton  Service  

Page 44: Coherence Implementation Patterns - Sig Nov 2011

A  service  that  automa/cally  restarts  amer  failure  

Page 45: Coherence Implementation Patterns - Sig Nov 2011

A  service  that  automa/cally  restarts  amer  failure  

Page 46: Coherence Implementation Patterns - Sig Nov 2011

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

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

Implemen/ng  Consistent  Views  and  Repeatable  Queries  

Page 49: Coherence Implementation Patterns - Sig Nov 2011

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

Where  does  the  System  Time  come  from?  

Page 51: Coherence Implementation Patterns - Sig Nov 2011

You  can’t  use  the  System.currentTimeMillis()  in  a  

distributed  environment!  

Page 52: Coherence Implementation Patterns - Sig Nov 2011

You  need  a  cluster  synchronised  clock  

Page 53: Coherence Implementation Patterns - Sig Nov 2011

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

As  we  add  objects  we  /mestamp  them  with  Write  Time  

Singleton    Service  

Write  Time  

Page 55: Coherence Implementation Patterns - Sig Nov 2011

Singleton    Service  

When  we  read  objects  we  use  Read  Time  

Read  Time  

Page 56: Coherence Implementation Patterns - Sig Nov 2011

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

Event  Based  Processing  

A  

B  

C  

D  

Async    Cachestore  

cache.put(key,  val);  

Page 58: Coherence Implementation Patterns - Sig Nov 2011

Messaging  as  a  System  of  Record  

Messaging System (use Topics for scalability)

Page 59: Coherence Implementation Patterns - Sig Nov 2011

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

Easy  Grid  Implementa/on  in  GUIs  

Page 61: Coherence Implementation Patterns - Sig Nov 2011

CQCs  on  a  CQC  

CQC   Cache  CQC  

GUI  JVM  

Define  this  in  config  

Page 62: Coherence Implementation Patterns - Sig Nov 2011

How  do  you  release  quickly  to  a  Coherence  cluster?  

Page 63: Coherence Implementation Patterns - Sig Nov 2011

Rolling  Restart?  

Page 64: Coherence Implementation Patterns - Sig Nov 2011

Disk-­‐Persist  

Page 65: Coherence Implementation Patterns - Sig Nov 2011

Final  Thoughts  

Page 66: Coherence Implementation Patterns - Sig Nov 2011

Data  is  the  most  important  commodity  that  you  have    

Keep  it  safe  

Page 67: Coherence Implementation Patterns - Sig Nov 2011

Use  a  Par//on  Listener  

Page 68: Coherence Implementation Patterns - Sig Nov 2011

Have  Proac/ve  Monitoring  of  Memory  

Page 69: Coherence Implementation Patterns - Sig Nov 2011

Version  your  Objects  

Page 70: Coherence Implementation Patterns - Sig Nov 2011

Thanks  

Slides  &  related  ar/cles  available  at:    

h1p://www.benstopford.com