Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database Beginner's Guide to Website Performance with MySQL & memcached Adam.

Similar presentations


Presentation on theme: "1 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database Beginner's Guide to Website Performance with MySQL & memcached Adam."— Presentation transcript:

1 1 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database Beginner's Guide to Website Performance with MySQL & memcached Adam Donnison Web Developer – MySQL.com – Sun Microsystems

2 2 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database About MySQL.com More than 70,000 downloads per day > 3 million registered users > 3 million unique visitors each month > 20 million page views per month

3 3 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database About MySQL.com Not just www.mysql.comwww.mysql.com – dev.mysql.com – DevZone – forums.mysql.com – blogs.mysql.com – solutions.mysql.com – planetmysql.org – shop.mysql.com Common web platform Common code libraries

4 4 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database What is Memcached? “A high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load”

5 5 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database Why Use Memcached? Enables massive scale-out of dynamic web-sites Faster page loads Allows for more efficient use of existing database resources Can easily utilize idle computing resources Dozens to hundreds of nodes are supported No interconnect or proprietary networking required Extensible and customizable

6 6 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database Memcached at MySQL.com Alleviate scaling issues related to database replication Speed up session management Speed up and simplify distributed image deployment More to come...

7 7 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database The Scaling Problem As load rises, add web servers Hit database as choke point Replicate database Modify code to handle split reads/writes Replication lag issues

8 8 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database Scaling MySQL.com 1998 – Single web server in Monty's garage 2004 – Two web servers, manual failover – DNS load balancing – Slave on each web server 2006 – Three web servers – External load balancer with automatic failover – Two slaves – separate to web servers 2007 – Memcached 2008 – Extra slave, extra two web servers 2009 – New colo – Three extra web servers – Two extra DB servers

9 9 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database The Scaling Problem The User Profile – Write the profile to master – Redisplay page, reading from slave – Replication lag – page inconsistent!

10 10 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database First Order Solutions Read from Master – Eliminates prime benefit of replication – Doesn't scale Write data to PHP session – Adds file I/O overhead – Increases load time for PHP pages – Increases PHP memory usage – Doesn't scale

11 11 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database Using Memcached Write through memcached to database Read first from memcached – On miss read from database Use Object Orientation – Memcached becomes plug-able add-on Distribute over as many nodes as web servers – Good complement to web server – Maximises server usage

12 12 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database When not to use Memcached Not a substitute for optimized queries – Optimize first, then look for caching opportunities No faster than a primary key lookup – Think about what data you want and where it is best to get it from – No point in “optmizing” something that doesn't need it

13 13 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database Planning your Memcached Deployment What is your memory usage pattern? – Do you have servers that can spare memory? What are you going to use Memcached for? Have you thought through the implications? – Memcached is non persistent – not a replacement for a DB – Data is by key store/retrieve – no searching on keys Do you need to rethink code?

14 14 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database Primary Use Cases Where data is stored and retrieved by single key Where failure to find data is non-fatal and data can be recovered from another source Where speed is required and mitigates code complexity

15 15 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database The Big Questions How often is the data accessed? – Determines minimum cache age requirement How often is the data modified? – Determines maximum cache age requirement How timely is the data? – Do you really need to see the data as soon as it changes?

16 16 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database Example: Replication Lag Use write-through caching – Data is read from memcached, written to memcached first – On read miss, read from database into memcached – On write, write to memcached and then to DB Set expiry to larger than expected lag Can be built as a database abstraction layer

17 17 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database Example: Page Cache Page is prepared only if not found in cache Use for pages that are dynamically generated but don't change often Long expiry can be used Don't use for static content Page can be prepared offline and fed to the cache

18 18 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database Getting Memcached Memcached comes in two parts – Server implements cache – Client API allows code to implement cache integration http://code.google.com/p/memcached/ – Primary site – Includes documentation, Wiki, downloads, APIs http://danga.com/memcached/ – Original site Your Linux distro

19 19 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database Installing Server Server uses port 11211 by default – Change using the -p option Uses 64MB by default – Change using -m option Listens on all interfaces – Change using -l option Run as another user using -u option Example: – memcached -u nobody -l 127.0.0.1 -p 11211 -m 512

20 20 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database Choosing Client Most languages now have API support Several options for some, like PHP – PECL has two options, memcache and memcached memcache is more mature but native memcached wraps libmemcached but is currently beta Clients require libevent On Linux, uses epoll

21 21 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database Use of memcached at MySQL.com

22 22 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database Use of Memcached at MySQL.com Each web server runs Apache, PHP and memcached Memcached set to use 512MB on each server Servers communicate over private network Memcached listens only on private network

23 23 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database MySQL_Memcache_Wrapper Provides simple interface to memcache client API Handles default expiry Provides for automated connection to server pool Handles detection of memcache support and falls back intelligently

24 24 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database Example Wrapper /** * Detect if memcached is available and create the server connections. * * @param string $prefix namespace to use * @param int $expiry seconds to allow data to exist before automatically cleaned */ public function __construct($prefix = '', $expiry = 86400) { $this->prefix = $prefix; $this->expiry = $expiry; if (config('use_memcache') && extension_loaded('memcache')) { $this->memcache = new Memcache; foreach (config('memcache_pool') as $srv) { $this->memcache->addServer($srv); } $this->use_memcache = true; } else { $this->use_memcache = false; }

25 25 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database Handling No Memcached public function get($key) { if ($this->use_memcache) { return $this->memcache->get($this->prefix. $key); } else { return null; }

26 26 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database MySQL_Db_Memcache Extends our DB abstraction class Provides automated Memcache support for get/save Primarily designed to handle replication lag Allows quick plug-and-play use with existing code

27 27 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database MySQL_Imagecache Utilises memcached Provides for caching of small images – Icons – User photos – Logos Allows upload from any server, deployment from any server

28 28 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database MySQL_Session_SaveHandler Save Handler for Zend_Session Implements memcached to provide data storage Non-traditional view of sessions – Sessions designed not to be permanent or even of long duration – Data used in sessions can be retrieved by other means should session die – Session != login

29 29 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database Summary Memcached provides non-persistent, distributed cache Can be used for a variety of situations including traditional and non-traditional Not a substitute for optimization at code and DB Can provide substantial performance benefits Provides no in-built security


Download ppt "1 Copyright 2009 Sun Microsystems Inc. The World’s Most Popular Open Source Database Beginner's Guide to Website Performance with MySQL & memcached Adam."

Similar presentations


Ads by Google