MySQL Scaling with Memcached

Presented by: Bill Takacs
              Director, Product Management


July 9th 2009
Agenda

  • M SQL overview
    MySQL      i


  • Memcached overview


  • What’s Memcached good for – and
    how does it help me scale MySQL?


  • Example architectures


  • Customer examples


2 : Copyright 2009 Gear6 Inc.
About MySQL

     • 12 years old

     • 400+ employees
        00 e p oyees

     • 750 partners
           p

     • 60K downloads/day

     • Customers across every major operating system,
       hardware vendor, geography, industry, and application
       h d         d           h i d t         d    li ti
       type


3 : Copyright 2009 Gear6 Inc.
Cache is King

                     Browser cache

                     Web
                     W b server cache
                                   h

                     Memcached cache

                     MySQL database cache – query cache

                     Disk




4 : Copyright 2009 Gear6 Inc.
Memcached

         “A high performance, distributed memory object caching
              g p            ,                    y    j         g
              system, generic in nature, but intended for use in
            speeding up dynamic web applications by alleviating
                               database load”
                                                 Ref: http://www.danga.com/memcached/

     • Big hash table

     • Created by Danga Interactive for Live Journal

     • Significantly reduced database load

     • Perfect for web sites with high database load

     • In use by Facebook, Twitter, MyYearBook, others

5 : Copyright 2009 Gear6 Inc.
More on Memcached

     • Takes advantage of available DRAM

     • Open source
       Ope sou ce

     • Distributed under BSD license

     • Server - Current version is 1.2.8
           » http://www.danga.com/memcached/download.bml

     • M
       Many Clients
            Cli t
           » http://code.google.com/p/memcached/wiki/Clients


6 : Copyright 2009 Gear6 Inc.
Why Memcached with MySQL?

     • Scale-out of dynamic web sites
                     y

     • Page loads

     • Efficient resource utilization

     • Support for dozens
       to hundreds of nodes

     • Open source




7 : Copyright 2009 Gear6 Inc.
How Does Memcached Work?

                                • Client - Server

                                • H h Function
                                  Hash F  ti

                                • Memcached
                                  » Two stage
                                  » Key / value p
                                      y         pair




8 : Copyright 2009 Gear6 Inc.
Memcached Server

     • Slab allocator

     • Libevent based

     • Simple Protocol (no xml)

     • Server has internal hash table

     • Servers are dumb – they don’t know about each other




9 : Copyright 2009 Gear6 Inc.
Memcached Server

     • Limits
           » Key size = (250 bytes)
           » 1MB Limit
           » 32bit/64bit (maximum size of process)


     • LRU
           » Least recently accessed items are cycled out
           » One LRU exists per “slab class”
                                 slab class
           » LRU “evictions” need not be common


     • Threads? Yes



10 : Copyright 2009 Gear6 Inc.
Memcached Clients

     • Client hashes key to server list

     • Serializes t e object
       Se a es the

     • Many client libraries
          y

     • Authentication




11 : Copyright 2009 Gear6 Inc.
What about…..

     • Redundancy

     • Failover




12 : Copyright 2009 Gear6 Inc.
Consistent Hashing
     • Why?

     • How does it work?
           » Libk t
             Libketama
           » http://www.last.fm/user/RJ/journal/2007/04/10/rz_libketama_-
             _a_consistent_hashing_algo_for_memcache_clients



     • Reference:
     http://www.lexemetech.com/2007/11/consistent-hashing.html
        p                                                g
     http://www8.org/w8-papers/2a-
        webserver/caching/paper2.htmlhttp://www.last.fm/user/RJ/jo
        urnal/2007/04/10/rz_libketama_-
        _a_consistent_hashing_algo_for_memcache_clients
         a consistent hashing algo for memcache clients




13 : Copyright 2009 Gear6 Inc.
Commands

     • Storage commands:
           » Set / Add / Replace /
             Append / Prepend / CAS


     • Retrieval commands:
           » Get / Gets


     • Delete / increment /
       decrement

     • Stats


14 : Copyright 2009 Gear6 Inc.
Memcached UDFs for MySQL

     • Suite of functions that work
       with Memcached and MySQL

     • Leverage power of SQL Engine

     • Combine tasks

     • O
       Open source




15 : Copyright 2009 Gear6 Inc.
Putting it all together

                         Client 1
                         Client-1     Client 2
                                      Client-2            Client 3
                                                          Client-3




                                                      C
                           Server X    Server Y           Server Z

                                      Key = value
                                        y
                                      Foo = seattle
16 : Copyright 2009 Gear6 Inc.
Some Architectures
Memcached and Single MySQL
                      Read          Write


                                                Web Servers


                                                Application Servers


                                                Memcached Clients



                                                Memcached Servers

                                 Cache Update
                                                 MySQL


18 : Copyright 2009 Gear6 Inc.
MySQL Replication Scale-Out

Web / Application Servers              MySQL
            +                            +
  Memached Clients               Memcached Servers

                                        Writes



                                                      MySQL
                                                     Replication
                                        Reads


                                        Reads




19 : Copyright 2009 Gear6 Inc.
Memcached and MySQL Replication
  Read from cache                Write   Read from slave
                                                            Application Severs
                                                                     +
                                                            Memcached Clients



                                                           Memcached Servers



      Cache Update
                                                              MySQL Master


                                                              MySQL Slaves




20 : Copyright 2009 Gear6 Inc.
Memcached and MySQL Replication
  Read from cache                          Write   Read from slave
                                                                       Application Severs
                                                                                +
                                                                       Memcached Clients


          Shard1 Cust_id 0-999                       Shard1 Cust_id 1000 -1999




 Cache Update                                       Cache Update

                                  MySQL                               MySQL
                                 Replication                         Replication



21 : Copyright 2009 Gear6 Inc.
About Gear6
  • First and leading provider
    of Memcached solutions

  • Memcached solution including

          • High density

          • High Availability

          • Advanced memory
            management

          • Enhanced reporting
            capabilities
                biliti

          • Support for multi-tenancy

          • Disruption free
            software upgrades

          • 100% c e t co pat b e
             00% client compatible


22 : Copyright 2009 Gear6 Inc.
Credits and References
     • Designing and Implementing Scalable Applications with Memcached and MySQL, Monty
        Taylor
     http://www.mysql.com/why-mysql/white-papers/mysql_wp_memcached.php

     • Memcached Study, Brian Acker
     http://download.tangent.org/talks/Memcached%20Study.pdf
     htt //d    l dt       t    /t lk /M     h d%20St d df

     • A Bunch of Great Strategies for Using Memcached and MySQL Better Together, Todd Hoff
     http://highscalability.com/bunch great strategies using memcached and mysql better together
     http://highscalability.com/bunch-great-strategies-using-memcached-and-mysql-better-together

     • Memcached Functions for MySQL: Seamless Caching for MySQL, Patrick Galbraith
     http://www.scribd.com/doc/15014678/memcached-Functions-for-MySQL-Seemless-Caching-in-
        MySQL

     • Memcached Protocol
     http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt
     http://code sixapart com/svn/memcached/trunk/server/doc/protocol txt




23 : Copyright 2009 Gear6 Inc.
Thank You!




                    For more information
                             information:

             www.gear6.com
             www gear6 com




24 : Copyright 2009 Gear6 Inc.

Gear6 Webinar - MySQL Scaling with Memcached

  • 1.
    MySQL Scaling withMemcached Presented by: Bill Takacs Director, Product Management July 9th 2009
  • 2.
    Agenda •M SQL overview MySQL i • Memcached overview • What’s Memcached good for – and how does it help me scale MySQL? • Example architectures • Customer examples 2 : Copyright 2009 Gear6 Inc.
  • 3.
    About MySQL • 12 years old • 400+ employees 00 e p oyees • 750 partners p • 60K downloads/day • Customers across every major operating system, hardware vendor, geography, industry, and application h d d h i d t d li ti type 3 : Copyright 2009 Gear6 Inc.
  • 4.
    Cache is King Browser cache Web W b server cache h Memcached cache MySQL database cache – query cache Disk 4 : Copyright 2009 Gear6 Inc.
  • 5.
    Memcached “A high performance, distributed memory object caching g p , y j g system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load” Ref: http://www.danga.com/memcached/ • Big hash table • Created by Danga Interactive for Live Journal • Significantly reduced database load • Perfect for web sites with high database load • In use by Facebook, Twitter, MyYearBook, others 5 : Copyright 2009 Gear6 Inc.
  • 6.
    More on Memcached • Takes advantage of available DRAM • Open source Ope sou ce • Distributed under BSD license • Server - Current version is 1.2.8 » http://www.danga.com/memcached/download.bml • M Many Clients Cli t » http://code.google.com/p/memcached/wiki/Clients 6 : Copyright 2009 Gear6 Inc.
  • 7.
    Why Memcached withMySQL? • Scale-out of dynamic web sites y • Page loads • Efficient resource utilization • Support for dozens to hundreds of nodes • Open source 7 : Copyright 2009 Gear6 Inc.
  • 8.
    How Does MemcachedWork? • Client - Server • H h Function Hash F ti • Memcached » Two stage » Key / value p y pair 8 : Copyright 2009 Gear6 Inc.
  • 9.
    Memcached Server • Slab allocator • Libevent based • Simple Protocol (no xml) • Server has internal hash table • Servers are dumb – they don’t know about each other 9 : Copyright 2009 Gear6 Inc.
  • 10.
    Memcached Server • Limits » Key size = (250 bytes) » 1MB Limit » 32bit/64bit (maximum size of process) • LRU » Least recently accessed items are cycled out » One LRU exists per “slab class” slab class » LRU “evictions” need not be common • Threads? Yes 10 : Copyright 2009 Gear6 Inc.
  • 11.
    Memcached Clients • Client hashes key to server list • Serializes t e object Se a es the • Many client libraries y • Authentication 11 : Copyright 2009 Gear6 Inc.
  • 12.
    What about….. • Redundancy • Failover 12 : Copyright 2009 Gear6 Inc.
  • 13.
    Consistent Hashing • Why? • How does it work? » Libk t Libketama » http://www.last.fm/user/RJ/journal/2007/04/10/rz_libketama_- _a_consistent_hashing_algo_for_memcache_clients • Reference: http://www.lexemetech.com/2007/11/consistent-hashing.html p g http://www8.org/w8-papers/2a- webserver/caching/paper2.htmlhttp://www.last.fm/user/RJ/jo urnal/2007/04/10/rz_libketama_- _a_consistent_hashing_algo_for_memcache_clients a consistent hashing algo for memcache clients 13 : Copyright 2009 Gear6 Inc.
  • 14.
    Commands • Storage commands: » Set / Add / Replace / Append / Prepend / CAS • Retrieval commands: » Get / Gets • Delete / increment / decrement • Stats 14 : Copyright 2009 Gear6 Inc.
  • 15.
    Memcached UDFs forMySQL • Suite of functions that work with Memcached and MySQL • Leverage power of SQL Engine • Combine tasks • O Open source 15 : Copyright 2009 Gear6 Inc.
  • 16.
    Putting it alltogether Client 1 Client-1 Client 2 Client-2 Client 3 Client-3 C Server X Server Y Server Z Key = value y Foo = seattle 16 : Copyright 2009 Gear6 Inc.
  • 17.
  • 18.
    Memcached and SingleMySQL Read Write Web Servers Application Servers Memcached Clients Memcached Servers Cache Update MySQL 18 : Copyright 2009 Gear6 Inc.
  • 19.
    MySQL Replication Scale-Out Web/ Application Servers MySQL + + Memached Clients Memcached Servers Writes MySQL Replication Reads Reads 19 : Copyright 2009 Gear6 Inc.
  • 20.
    Memcached and MySQLReplication Read from cache Write Read from slave Application Severs + Memcached Clients Memcached Servers Cache Update MySQL Master MySQL Slaves 20 : Copyright 2009 Gear6 Inc.
  • 21.
    Memcached and MySQLReplication Read from cache Write Read from slave Application Severs + Memcached Clients Shard1 Cust_id 0-999 Shard1 Cust_id 1000 -1999 Cache Update Cache Update MySQL MySQL Replication Replication 21 : Copyright 2009 Gear6 Inc.
  • 22.
    About Gear6 • First and leading provider of Memcached solutions • Memcached solution including • High density • High Availability • Advanced memory management • Enhanced reporting capabilities biliti • Support for multi-tenancy • Disruption free software upgrades • 100% c e t co pat b e 00% client compatible 22 : Copyright 2009 Gear6 Inc.
  • 23.
    Credits and References • Designing and Implementing Scalable Applications with Memcached and MySQL, Monty Taylor http://www.mysql.com/why-mysql/white-papers/mysql_wp_memcached.php • Memcached Study, Brian Acker http://download.tangent.org/talks/Memcached%20Study.pdf htt //d l dt t /t lk /M h d%20St d df • A Bunch of Great Strategies for Using Memcached and MySQL Better Together, Todd Hoff http://highscalability.com/bunch great strategies using memcached and mysql better together http://highscalability.com/bunch-great-strategies-using-memcached-and-mysql-better-together • Memcached Functions for MySQL: Seamless Caching for MySQL, Patrick Galbraith http://www.scribd.com/doc/15014678/memcached-Functions-for-MySQL-Seemless-Caching-in- MySQL • Memcached Protocol http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt http://code sixapart com/svn/memcached/trunk/server/doc/protocol txt 23 : Copyright 2009 Gear6 Inc.
  • 24.
    Thank You! For more information information: www.gear6.com www gear6 com 24 : Copyright 2009 Gear6 Inc.