MemcachedTalk

  • Upload
    quannum

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

  • 8/14/2019 MemcachedTalk

    1/35

    MemcacheRob [email protected]

    Lead DeveloperThe Sound Alliance

    mailto:[email protected]:[email protected]
  • 8/14/2019 MemcachedTalk

    2/35

    About Memcached

    conceived by Brad Fitzpatrick as asolution to the scaling issues faced

    by Livejournalmemcached is a high-

    performance, distributed memoryobject caching system, generic innature, but intended for use inspeeding up dynamic webapplications by alleviating

    database load

  • 8/14/2019 MemcachedTalk

    3/35

  • 8/14/2019 MemcachedTalk

    4/35

    Who we are

    Inthemix.com.au

    Australias busiest music website~ 250,000 pages per dayPlus two other busy sites!Maximum performance for the

    hardware we have

  • 8/14/2019 MemcachedTalk

    5/35

    Current

    Architechture3 Linux servers

    ApacheLighttpd

    Memcache1 MySQL Master

  • 8/14/2019 MemcachedTalk

    6/35

    Why do we use

    memcached?CMS written in OO style, usingActiverecord

    PHP4 and objects don't mix toowellActiverecord gave us fast

    development but reducedperformance

    Call us greedy, but we want both

    Use Rails Memcache!

  • 8/14/2019 MemcachedTalk

    7/35

    Our Application

    CMS written from the ground upEffectively three sites running on

    one codebase

    Uses three seperate databases,but aiming to consolidate more

    Has data namespacingimplemented in most places

    But seperation is not quite there

    yet!

  • 8/14/2019 MemcachedTalk

    8/35

    Our Memcache

    Setup

    We have 3 webservers runningmemcache

    Each server runs three daemonson separate ports - one for eachsite (more on this later!)

  • 8/14/2019 MemcachedTalk

    9/35

    Memcache PoolEach daemon knows about theother 2 daemons and connects to

    them over TCP

    This allows us to store data once,and access it from any server,whether in the pool or not

    Hashing algorithm means that agiven key maps to a single serverEfficient use of memory

    Efficient for cache clearing

  • 8/14/2019 MemcachedTalk

    10/35

    Memcache PoolBut what if we lose a server? WecanIgnore it - we simply get misses

    for any keys we attempt toretrieve

    Remove it - our hashingalgorithm breaks... :(

    We can also add new servers tothe pool after data has been

    stored, but the same hashing

  • 8/14/2019 MemcachedTalk

    11/35

    Memcache Pool

    Consistent hashing will solve theproblem of removing or adding

    servers once data has beenhashed

    Currently in its infancy - not reallyproduction ready

    We simply monitor our daemonsand restart if required

  • 8/14/2019 MemcachedTalk

    12/35

    Installing

    Memcached

    packaged for Fedora, RHEL4/5,

    Ubuntu, Debian, Gentoo and BSD

    OSX? Use Ports!sudo port install memcache

    sudo gem install memcache-client

    sudo gem install cached_model

  • 8/14/2019 MemcachedTalk

    13/35

    Memcache and

    Ruby

    Well use the memcache-clientgemPure Ruby implementation

    Pretty fast!

  • 8/14/2019 MemcachedTalk

    14/35

  • 8/14/2019 MemcachedTalk

    15/35

    Storing Stuff

    Cache.set 'favourite_artist', 'Salvador Dali'skateboarder =Cache.get 'favourite_artist'

    Cache.delete 'favourite_artist'

  • 8/14/2019 MemcachedTalk

    16/35

    Memcache

    NamespacesMemcache doesnt havenamespaces, so we have toimprovise

    Prefix your keys with a namespaceby setting the namespace whenyou connect

    Our solution:Run multiple memcacheinstances on different ports

    Makes it easier to clear out

  • 8/14/2019 MemcachedTalk

    17/35

    Roll your own?

    Memcache-client provides basiccache methodsWhat if we extended ActiveRecord?

    We can, with active_model

  • 8/14/2019 MemcachedTalk

    18/35

    Storing Stuff Part

    Deuxrsharp$ sudo gem install cached_model

    require'cached_model'

    memcache_options = { :compression => true, :debug => false, :namespace => 'hifibuys', :readonly => false, :urlencode => false

    }

    CACHE =MemCache.new memcache_optionsCACHE.servers ='localhost:11211'

  • 8/14/2019 MemcachedTalk

    19/35

    Storing Stuff Part

    Deux

    classArtist < CachedModel

    end

  • 8/14/2019 MemcachedTalk

    20/35

    cached_model

    PerformanceCachedModel is not magic.CachedModel only accelerates simple

    finds for single rows.

    CachedModel wont cache every queryyou run.

    CachedModel isnt smart enough todetermine the dependencies betweenyour queries so that it can acceleratemore complicated queries. If you want to

    cache more complicated queries you

    http://dev.robotcoop.com/Libraries/cached_model/classes/CachedModel.htmlhttp://dev.robotcoop.com/Libraries/cached_model/classes/CachedModel.htmlhttp://dev.robotcoop.com/Libraries/cached_model/classes/CachedModel.htmlhttp://dev.robotcoop.com/Libraries/cached_model/classes/CachedModel.htmlhttp://dev.robotcoop.com/Libraries/cached_model/classes/CachedModel.htmlhttp://dev.robotcoop.com/Libraries/cached_model/classes/CachedModel.htmlhttp://dev.robotcoop.com/Libraries/cached_model/classes/CachedModel.htmlhttp://dev.robotcoop.com/Libraries/cached_model/classes/CachedModel.htmlhttp://dev.robotcoop.com/Libraries/cached_model/classes/CachedModel.html
  • 8/14/2019 MemcachedTalk

    21/35

    Other options

    acts_as_cached provides a similarsolution

  • 8/14/2019 MemcachedTalk

    22/35

    Memcache Storage

    Memcache stores blobs

    The memcache client handlesmarshalling, so you can easilycache objects

    This does however mean that theobjects arent necessarily cross-language

  • 8/14/2019 MemcachedTalk

    23/35

    Memcache Storage

    The most obvious things to storeare objects

    We cache articlesWe cache collections of articles

    We cache template dataWe cache fragments

    We dont cache SQL queries

  • 8/14/2019 MemcachedTalk

    24/35

    What we cache

    Our sites are fairly big and data-rich communities

    Almost every page has editoriallycontrolled attributes along with

    user generated contentLike...

  • 8/14/2019 MemcachedTalk

    25/35

    Our Example

    DatasetArticle

    Joins Artists

    Joins LocationsJoins Genres

    Joins Related ContentJoins Related Forum Activity

    Joins Related Gallery Data

  • 8/14/2019 MemcachedTalk

    26/35

    Our Example

    DatasetArticle (continues)

    ...

    Joins Media Content

    Joins CommentsJoins Rollcalls

    Joins other secret developments

  • 8/14/2019 MemcachedTalk

    27/35

    Our ExampleAn article requires many datafacetsMost dont change that oftenWe also know when they changeYay for the Observer pattern

    User content changes muchmore regularlyCan be changed from outside

    our controlled area (e.g.

    ur xamp e

  • 8/14/2019 MemcachedTalk

    28/35

    ur xamp eSummarySummaryData can be loosely divided into

    editorially controlled and user-

    generatedCache editorially controlled

    content separately from user-

    generated contentSimplest way to implement is in

    fragment caching

  • 8/14/2019 MemcachedTalk

    29/35

    Fragment CachingMemcache allows timed expiry of

    fragments

    Identify areas that changeinfrequently and cacheRemember to measure

    performance before and afterEvidence suggests very large

    gains!

    Use memcache_fragments

  • 8/14/2019 MemcachedTalk

    30/35

    Caching Fragments

    rsharp$ sudo gem install memcache_fragments

    require'memcache_fragments'

    memcache_options = { :compression => true, :debug => false, :namespace => 'hifibuys', :readonly => false, :urlencode => false

    }

    CACHE =MemCache.new memcache_optionsCACHE.servers ='localhost:11211'

  • 8/14/2019 MemcachedTalk

    31/35

    Caching Fragments

    ActionController::Base.fragment_cache_store =:mem_cache_store ,{}ActionController::Base.fragment_cache_store.data = CACHE, {}ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS.merge!({ 'cache'=> CACHE })

  • 8/14/2019 MemcachedTalk

    32/35

    Caching Fragments

    cache/key', :expire => 10.minutes do %> .

  • 8/14/2019 MemcachedTalk

    33/35

    Memcache

    SessionsWe could store our session inMemcacheGreat for load balancing - share

    across a server farm withoutusing a DB store

    Ideal for transient data

    Solution exists indb_memcache_store

    DB backend with memcachelayer - the best of both worlds

  • 8/14/2019 MemcachedTalk

    34/35

    In Summary

    Memcache gives you a distributedcache store

    Very fast and very easy to use

    Lots of ruby and rails librariesmemcache_client

    cached_model

    db_memcache_storememcache_fragments

  • 8/14/2019 MemcachedTalk

    35/35

    Any Questions?