Download - Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Transcript
Page 1: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

DistributedCachinginanEphemeralWorldRahulSinghFounder&CEOdistelli.comJune7th2017

Page 2: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Distributed?Ephemeral?Huh?

Page 3: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Distributed?Ephemeral?Huh?

…becauseKubernetes

Howdidwegethere?

Page 4: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Thegoodole’days…

Oracle

ApacheFCGI

•  ASingleC++binaryrunninginFCGIworkers•  UnderApachetalkingtoanOracleDatabase

Page 5: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Thegoodole’days…

Oracle

ApacheFCGI

•  Caching?Whocares…

...whenyou’retryingtogetthethingtolinkwithoutrunningoutofmemory

Page 6: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Thegoodole’days…

Oracle

ApacheFCGI

•  Scaling?

Justaddmoreboxes!TheoracleDBcanhandleit.

Page 7: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Thegoodole’days…

OracleRAC

ApacheFCGI

•  Scaling?

DBgeNngtobeaproblem?...OracleRACtotherescue!

Page 8: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Thegoodole’days…

ScalingupwasthesoluOontoeveryproblem

Whybotherwithcachingwhenyoucanburn$400Min3years

Page 9: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Eventuallessonlearned…

Youcan’tscaleupforever

Page 10: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Eventuallessonlearned…

Youhavetoscaleout!

Page 11: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

That’sthedistributedpart

AddmoresmallerboxesLotsofsmalldatabasesbehindRESTAPIs

SplitupthatFCGIbinaryintoMicroservices

Page 12: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Microservices

LoadBalancer

MulOpleBackendServiceInstancesInMulOpleIndividualDatacenters

Serviceclients(website)connectsviaaLB

OneservicebackedbyonesmallDB

Page 13: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Microservices

HundredsofFrontendclients

HundredsofBackendServices

Page 14: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Microservices

LotsofsmallpiecesofsoSwarerunningonmanysmallboxes

Allscaledoutindependently

Page 15: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Microservices

Difficulttoarchitectanddifficulttooperate

But…onceyougetitgoingit’sgreat!Themachinesweren’tgoinganywhere

Page 16: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

MicroservicesandthenDockerhappened

PackageitasacontainerRunitanywhere

anyXme

Page 17: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

DockerPackageitasacontainer

Runitanywhere!Really?Anywhere?

RunitanyXme!

Really?AnyXme?

Page 18: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Docker

How?

Page 19: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Kubernetes+Docker

Page 20: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Kubernetes+Docker

KuberneteswillscheduleyourcontainersanywhereonyourpoolofserversanyOme.

Page 21: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Kubernetes+Docker

Whathappenedtoallthatstability?Whereismystuff?

KubernetescanshutdownacontaineratanyOme.

KubernetescanstartacontaineratanyOme

That’sEphemeral

Page 22: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

DistributedandEphemeral

MicroservicespackagedasdockercontainersscheduledbyKubernetesDistributed Ephemeral

Page 23: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

What’sthisgottodowithcaching?

IfservicesandprocessesarelonglivedandhavestableIPaddressesthencachingisrelaOvelyeasy

Ifthereisasingleendpointtoretrieveaspecific

objectthencachingisrelaOvelyeasy

Page 24: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

DistributedcachinginanEphemeralworld

InadistributedcachethecachekeysarespreadacrossmulOplemachines

Retrievingaspecificobjectdependsonfindingthe

machinethat’scachingtheobjectforthatkey

Ifmachines(orcontainers)movearoundthenitbecomesdifficulttokeeptrackofwhatiscachedwhere

Page 25: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

DistributedcachinginanEphemeralworld

Distributedcachinghasmanychallenges

•  FindingnodesthatholdaparOcularkey•  Whathappenswhennodesfailorarerestarted•  HowdoesCacheinvalidaOonwork

Page 26: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

DistributedcachinginanEphemeralworld

Distributedcachinghasmanyadvantages

•  Cachelookuptrafficisdistributedacrossmanynodes•  CachesizeiseffecOvelyequaltosumofcachesoneverynode•  Nosinglepointoffailure•  Scalableandperformant

Page 27: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Designingadistributedcache

LoadBalancer

RememberthisMicroservice?

Page 28: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Designingadistributedcache

LoadBalancer

Singlededicatedcachebox

Notreallyscalable

Page 29: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Designingadistributedcache

LoadBalancer

Distributethecacheacrosstheservicenodesthemselves

Page 30: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Designingadistributedcache

LoadBalancer

OR...Haveadedicatedfleetofcachenodesanddistributethecacheacrossthem

Page 31: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Designingadistributedcache

YourcacheisnowspreadacrossmulOplenodes

Page 32: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Designingadistributedcache

IfthereareNnodesinyourcachefleet...then…

Eachnodeholds1/Nofthecache

Themathfromcaptainobvious

Ifthereare20nodesinyourcachefleetthenN=20Ifyourcachecontains100keystheneachnodeshouldholdapproximately5keys

Page 33: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Designingadistributedcache

Eachnodeholds1/Nofthecache

Themathfromcaptainobvious

Ifthereare20nodesinyourcachefleetthenN=20Ifyourcachecontains100keystheneachnodeshouldholdapproximately5keys

dependsonhowyoudistributethekeys

Page 34: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Hashingkeysintobuckets(acrossnodes)

dependsonhowyoudistributethekeys

itdependsonthehashingalgorithmyouuse

Page 35: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Hashingkeysintobuckets(akaacrossnodes)SimpleHashAlgorithm:ModHashingForasetofNnodes,keyKisonnodeBidenOfiedby:b=KmodN

N=10K=22

0 1 2 3 4 5 6 7 8 9

Page 36: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Hashingkeysintobuckets(akaacrossnodes)

Important:ThevalueofNchangesasnodesfailandnewnodesarestarted.Mostooenbecausekubernetesstarts/stopsacontainer

Page 37: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Hashingkeysintobuckets(akaacrossnodes)

Important:ThevalueofNchangesasnodesfailandnewnodesarestarted.Mostooenbecausekubernetesstarts/stopsacontainer

Aproblemwithmodhashing:Whenthenumberofnodeschanges,everyelementisrehashed.

Page 38: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Hashingkeysintobuckets(akaacrossnodes)Aproblemwithmodhashing:Whenthenumberofnodeschanges,everyelementisrehashed.

N=10K=22

0 1 2 3 4 5 6 7 8 9

Ifnode8diesthenN=9Kmod9!=Kmod10

N=9K=22

Page 39: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Hashingkeysintobuckets(akaacrossnodes)Ifeverykeyinthecachemovestoadifferentnodewhenasinglenodefailsthenthecachemissratesgoupooenandaffectperformance.QuesOon:Howdoyouhashkeyssothatwhenasinglenodefailsonlythekeysonthatnodearerehashed?Moregenerally:Howdoyouhashkeyssothatffailuresrehashonly1/fofthetotalcache

Page 40: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Hashingkeysintobuckets(akaacrossnodes)

ConsistentHashing

Page 41: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

ConsistentHashing

0

1

2

34

6

7

8

9

5

1.  Placenodesonacircle2.  Placekeysonthesamecircle

3.  AkeyKhashestonodeN>K

ProgrammaOcally1.  CalculateScoreforeachnodeN2.  Maintainanorderedlistofscores

3.  ForKeykcomputescoreforK4.  FindNwhereN>K

Page 42: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

ConsistentHashing

0

1

2

34

6

7

8

9

5

WhennodeN=8diesonlykeysonthatnodearerehashed.Therestofthecachestaysthesameandconsequentlynodefailuresdon’tresultinincreasedcachemissrates

Page 43: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Addressing

0

1

2

34

6

7

8

9

5

Ifnodesmovearound,howdoyoukeeptrackofwhichnodesarewhereandwhattheiraddressis?

Page 44: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Addressing

0

1

2

34

6

7

8

9

5

AssigneachnodeauniqueIDandanIP:PORTcombinaOonEverynodecommunicatesithealthtoallothernodes.Gossipismostefficientforthis

Page 45: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Addressing,Liveness&NodeHealth

AssigneachnodeauniqueIDandanIP:PORTcombinaOonEverynodecommunicatesithealthtoallothernodes.Gossipismostefficientforthis

Page 46: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Addressing,Liveness&NodeHealth

Gossip:Everynodeheartbeatstoarandomnodeevery10secondsMembershipdataissharedwithallothernodesoneveryiteraOonWhenmissingheartbeatsaredetectedthenodeisconsidereddeadandthekeysarerehashed.

Page 47: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Addressing,Liveness&NodeHealth

KeyRequirement:Whenrehashingbecauseanodedies,itsimportanttogivethenewnodeanewuniqueIDNeverreuseIds

Page 48: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Cachingisanextensivefield

Wehaven'teventalkedaboutcacheinvalidaOonBackupcaching:ShouldIcachethesamekeyonmulOplenodesforredundancy?Shortanswer:No.Itsnotworthit.`Longeranswer:itdepends.

Page 49: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Recap

CachingisrelaOvelyeasy–justuseLRUcacheswithTTLThingsgetmoredifficultatscale.ThingsgetmoredifficultwithdistributedmicroservicesThingsgetmoredifficultwhenyourcachenodesareephemeralHandlingcachenodefailuresisimportantifyouwanthighcachehitrates

Page 50: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

Measure,Measure,Measure

CachinglendsitselfwelltomeasurementVerysaOsfyingtoseehighcachehitratesVerypainfultoseehighcachemissrates

Page 51: Distributed Caching in an Ephemeral World Rahul Singh€¦ · Distributed Caching in an Ephemeral World Rahul Singh Founder & CEO distelli.com June 7th 2017

We’rehiringafullstack/frontenddeveloperReact,Java,DistributedSystems

Makeasbigimpactonasmallteam

hwps://www.distelli.com/kubernetes