memcached may 29

48
1 Copyright 2008 MySQL AB The World’s Most Popular Open Source Database Designing and Implementing Scalable Applications with Memcached and MySQL Farhan "Frank" Mashraqi Director of Business Operations and Technical Strategy - Fotolog Inc Monty Taylor Senior Consultant - Sun Microsystems Jimmy Guerrero Sr Product Marketing Manager - Sun Microsystems, Database Group

Upload: warwithin

Post on 12-Nov-2014

193 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: memcached may 29

1Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Designing and ImplementingScalable Applications with Memcached and MySQL

Farhan "Frank" MashraqiDirector of Business Operations and Technical Strategy - Fotolog Inc

Monty TaylorSenior Consultant - Sun Microsystems

Jimmy GuerreroSr Product Marketing Manager - Sun Microsystems, Database Group

Page 2: memcached may 29

2Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

• Sun – MySQL Overview

• Technical Introduction

• Architectures

• Use Cases

• Solutions

• Next Steps plus Q & A

Page 3: memcached may 29

3Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

MySQL Helps Sun...

• Grow in a $15B database market

• Reach new customers

• Position its offerings for Web companies

• Deliver innovation to community & customers

• Complete its software platform offerings

Sun Helps MySQL…

• Expand market reach and revenue opportunities

• Enhance enterprise appeal with global services

• Enhance performance and integration

• Resources for accelerated growth

Sun Acquires MySQL

Page 4: memcached may 29

4Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

About MySQL

Leadership * Innovation * Open * Ubiquitous

• 12 years old

• 400+ employees

• 750 partners

• 60K downloads/day

• Customers across every major

operating system, hardware

vendor, geography, industry,

and application type

Page 5: memcached may 29

5Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Established & Emerging Companies

craigslist

Web 2.0

Enterprise 2.0

SaaS

Telecom

OEM & ISV

Page 6: memcached may 29

6Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Technical Introduction

Monty TaylorSenior Consultant - Sun Microsystems

[email protected]

Page 7: memcached may 29

7Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

“Cache is King”

• Browser Cache

• Web Server Cache

• Memcached Cache

• MySQL Database Cache

• Disk Storage

ms

Page 8: memcached may 29

8Copyright 2008 MySQL AB 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” *

* http://www.socialtext.net/memcached/index.cgi?faq

• Created by Danga Interactive to speed up LiveJournal’s 20

million+ dynamic page views per day for 1 million+ users

• Significantly dropped database load, delivering faster page

loads, better resource utilization, and faster access to

databases

• Perfect for dynamic sites that generate high database load

• Used by Facebook, YouTube, Wikipedia, others!

Page 9: memcached may 29

9Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

What is Memcached?

• Runs wherever RAM is available

– Application, Web, Database or dedicated memcached servers

– Low CPU utilization

– Multiple operating systems and architectures supported

• Open Source - Community Driven

• Memcached Server is licensed under BSD

• Current version is 1.2.x series

– http://www.danga.com/memcached/download.bml

• Various Client APIs and libraries available

– Perl, Python, Ruby, Java, C#, C, Lua, MySQL, more….

– http://www.danga.com/memcached/apis.bml

Page 10: memcached may 29

10Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Why Use Memcached With MySQL?

• 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 can be supported in a

memcached cluster

• No interconnect or proprietary networking required

• Extensible and customizable

Page 11: memcached may 29

11Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

How Does Memcached Work?

Hash Function

• A hash is a procedure for turning data into a small integer that serves as an index into an array

• Speeds up table lookup or data comparison tasks

Memcached

• Two-stage hash, like a giant hash table looking up key = value pairs

• Client hashes the key against a list of servers

• When the server is identified, the client sends its request

• Server performs a hash key lookup for the actual data

Page 12: memcached may 29

12Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Memcached Server

• Slab allocator

– Memory permanently allocated

– Classes created by chunk size

– Cannot reassign slab pages

• Libevent based

• Simple protocol (no xml)

• Server has internal hash table

• Servers know nothing about each other

Page 13: memcached may 29

13Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Memcached Server• Limits

– Key size (250 bytes)

– Data size (under 1 MB)

– 32bit/64bit (maximum size of process)

– Maxbytes (limits item cache)

• LRU

– Least recently accessed items are cycled out

– One LRU exists per “slab class”

– LRU “evictions” need not be common

• Threads

– New in version 1.2

– Big instances (16 GB+)

– Large multiget requests

Page 14: memcached may 29

14Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Memcached Commands

• set/get/replace

• append/prepend

• increment/decrement

• cas (compare and swap atomic)

• stats (detail)

Page 15: memcached may 29

15Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Memcached Clients

• Client hashes key to server list (distribution)

• Serializes the object

• Compresses data

• “Consistent hashing” client

• Other things to know about memcached…

– Dumping and loading data

– Redundancy

– Failover

– Authentication

Page 16: memcached may 29

16Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Basic Memcached Example

mc

ms

Client X

1) set key “foo” with value “seattle”

2) hashes the key against server list

3) Server B is selected

4) connects to Server B and sets key

Client Z

5) get key “foo”

6) connects to Server B

7) requests “foo” and gets value “seattle”

mc mc

X Y Z

ms ms

A B C

hash server list

select server

connect

set key value

get key

connect

get value

key = valuefoo = seattle

Page 17: memcached may 29

17Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Memcached vs MySQL Query Cache

• Distributed• Unlimited Size• Generic Data Storage• No Direct Invalidation• Maximum 1 MB

• Centralized (Single Server)• Limited by maximum addressable space of mysqld• Database data only• Invalidated by any write on tables used in SELECT• Configurable per item

ms ms ms

Page 18: memcached may 29

18Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Memcached vs MySQL Cluster

• Distributed• Unlimited Size• Generic Data Storage• No Direct Invalidation• Maximum 1 MB

• Distributed• Limited Size• Datatype storage limits• No invalidation (DELETE instead)• HA and Redundant• SQL and NDB API support

ms ms ms

Application Nodes(NDB API and/or MySQL Server)

Data Nodes

ManagementNode

NDB Storage Engine

Page 19: memcached may 29

19Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Architectures

Page 20: memcached may 29

20Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Memcached & MySQL

Web Servers

Application Servers

MemcachedServers

MySQL Server

MemcachedClients mc mc

ms

read write

mc

ms ms

cache update

Page 21: memcached may 29

21Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

On-Demand Read Scalability for MySQL

MySQL Replication

• Designed for on-demand read scalability

• Cost-effective alternative to Scale-Up

• Asynchronous replication

• Statement or row-level replication support

• No special networking components

• Fully-supported via MySQL Enterprise subscription

• Enterprise Monitor discovers and monitors replication

topologies

Page 22: memcached may 29

22Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

MySQL Replication – Scale-Out

reads

reads

MySQLReplication

writes

MySQL ReplicationWeb/AppServers

Page 23: memcached may 29

23Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Memcached & MySQL Replication

Application Servers &

Memcached Clients

MySQLSlaves

MySQLMaster

read from cache write

mc

MemcachedServers ms

mc

cache update

MySQLReplication

mc

read from slave

ms ms

Page 24: memcached may 29

24Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Sharding - Application Partitioning

Cust_id 1-999

Cust_id 1000-1999

Cust_id 2000-2999

Web/AppServers

Sharding Architecture

Page 25: memcached may 29

25Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Memcached, Sharding & MySQL Replication

msms

cache update

MySQLReplication

msms msms msms

cache update

MySQLReplication

msms msms

shard 1 (cust 1-999) shard 2 (cust 1000-1999)

read from cache

write

mc mc mc

read from slave

write

read from slave

read from cache

read from slave

write

read from cache

Page 26: memcached may 29

26Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Use Cases

Farhan "Frank" MashraqiDirector of Business Operations and Technical Strategy

Fotolog Inc

[email protected]

Page 27: memcached may 29

27Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Fotolog Introduction• World’s largest photo blogging community

– > 18 million members

– > 155 million page views

• Ranked top 57 site

– 13th before Alexa changed ranking algorithm

Page 28: memcached may 29

28Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Memcached @ Fotolog

• 51 instances on 21 servers

• ~254G available to memcached servers

• ~175G in use

• Cache everything that is slow to query, fetch or

calculate

Page 29: memcached may 29

29Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Memcached Use Cases

• Non-deterministic cache: Classic memcached model

• “State” cache

• Deterministic cache

• Proactive cache

• Replacement/add-on for file system cache

• Partial page cache

• Application-based memcached replication

Page 30: memcached may 29

30Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Non-Deterministic Cache:

Classic Memcached Model

• Check memcached, check database, then populate memcached

• Previously using classic implementation: – App servers running memcached on “spare” memory

• 90% CPU usage

• Memory garbage collected nearly once a minute

• Experienced blocking on memcached on app servers

• 45 memcached instances dedicated to non-deterministic cache– Each instance (on average):

• ~440 gets per second

• ~40 sets per second

• ~11 gets/set

Page 31: memcached may 29

31Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Non-Deterministic Cache:

Classic Memcached Model

• Ideal for:

– Caching objects that are to be queried several times

– Replacement for MySQL’s query cache

– Caching relationships and other lists

– Slow data that’s used across many pages

– Don’t cache if its more taxing to cache than you’ll save

– Tag clouds and auto-suggest lists

Page 32: memcached may 29

32Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

State Caching

• “Who’s online?”

• ~9G dedicated

• Multiple instances

• Ideal for caching:

– Sessions

– Current state of users

• Especially for IM applications

Page 33: memcached may 29

33Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Deterministic Cache

• Memcached has what database has…read scalability

• Multiple dedicated cache pools

• Cache has to be populated when the site “starts”– Maintained by application from that point onwards

– ~ 90,000 gets / second across cache cluster

– ~ 300 sets / second

– get/set ratio of ~ 300

• Ideal for caching:– Heavily accessed data/objects/lists

– User credentials

– User profiles

– User preferences

– Active media belonging to users

– Outsourcing logins to memcached

Page 34: memcached may 29

34Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Proactive Caching

• “It’s magically there”

• Fun with binlogs

– Easier to implement for UPDATEs

– A little bit of extra logic required for INSERTs

– DELETEs can be honored easily

• Ideal for

– Pre-Populating Cache: Keeping memcached updated

minimizes calls to database if object not present

– “Warm up” cache in cases of cross data-center replication

Page 35: memcached may 29

35Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Proactive Caching

• Fun with UDFs

• Memcached UDFs for MySQL

– Major contributing author: Patrick Galbraith

– Allows you to manage memcached when used with triggers.

– Implemented using libmemcached

– Compatible with all versions of MySQL

– Examples:

• memc_servers_set()

• memc_set()

• memc_get()

• memc_delete()

• memc_increment()

• memc_decrement()

• memc_replace()

• Complete list available at

– http://tangent.org/586/Memcached_Functions_for_MySQL.html

Page 36: memcached may 29

36Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Proactive Caching

• Fun with blackhole– thanks to Brian Aker

• Create a blackhole table– CREATE TABLE blackhole_table … ENGINE=blackhole;

• Replicate this table

• Add triggers at right places.

• Example: Make multiple INSERTs– INSERT <real_insert>;

– INSERT INTO blackhole_table VALUES (memc_set(id));

• Benefits– Actual data is not sent to slave disks

– Not CPU intensive for slaves

Page 37: memcached may 29

37Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Add-on/Replacement for NFS File System Cache

• NFS has a significant overhead

– File system cache for NFS systems not enough

• Ideal for caching:

– XML files

– Small size media stored on NFS mounts

Page 38: memcached may 29

38Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Partial Page Caching

• Certain partial page elements are…

– Very costly to generate

– Shared among many pages

– Viewed several times

• Examples:

– Top users within a region

– Popular photo lists

– Featured users

• Once generated these can be shared

Page 39: memcached may 29

39Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Application-Based Memcached Replication

• Ideal for deterministic cache cluster

• Redundancy

– Application writes to multiple memcached pools

• High availability

– Take node out of rotation

Page 40: memcached may 29

40Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Memcached Wishlist

• “Built-in” replication

• Integration with binary logs

• A “hybrid” storage engine?

• Production support

– Now fulfilled thanks to Sun/MySQL

Page 41: memcached may 29

41Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Solutions

Jimmy GuerreroSr Product Marketing Manager

Sun Microsystems - Database Group

[email protected]

Page 42: memcached may 29

42Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Memcached for MySQL

• Support is built into your MySQL Enterprise subscription

http://www.mysql.com/products/enterprise/memcached.html

• MySQL Enterprise– 24x7 Production Support

– Enterprise Monitor

– MySQL Enterprise Server

– Additional Add-ons Available

• MySQL Professional Services– MySQL Scale, High-Availability and Replication Jumpstart

Page 43: memcached may 29

43Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

MySQL Enterprise

Deploy and monitor an unlimited number of servers for the

price of a single CPU of Oracle

MySQL Enterprise

MySQLEnterpriseServer

MySQLEnterpriseMonitor

24x7ProductionSupport

MySQLEnterpriseUnlimited

Enterprise database and monitoring software, plus

production support delivered in an annual subscription

Page 44: memcached may 29

44Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Why MySQL Enterprise?

• Deliver on SLAs

• Increase availability

• Optimize performance

• Effectively monitor scale-out environments

• Faster time to launch

• Develop & deliver on proven open source components

• Keep costs predictable

Page 45: memcached may 29

45Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

MySQL Enterprise Monitor

• Consolidated view of entire MySQL environment

• Auto discovery of MySQL Servers & replication topologies

• Over 100 customizable rules for monitoring and alerting

• Identifies problems before they occur

• Reduces risk of downtime

• Improve delivery of SLAs

• Scale-out with less DBAs

Page 46: memcached may 29

46Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Why Memcached with MySQL?

• 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 can be supported in a

memcached cluster

• No interconnect or proprietary networking required

• Extensible and customizable

Page 47: memcached may 29

47Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Next Steps

Memcached for MySQL -

http://www.mysql.com/products/enterprise/memcached.html

Whitepapers - http://www.mysql.com/why-mysql/white-papers/

• “Designing and Implementing Scalable Applications with Memcached and MySQL”

• “How MySQL Powers Web 2.0”• “Enabling Enterprise 2.0 with MySQL”

Documentation -

http://dev.mysql.com/doc/refman/5.1/en/ha-memcached.html

Discussion Forum - http://forums.mysql.com/list.php?150

Miscellaneous Resources -

http://www.mysql.com/products/enterprise/memcached.html

Page 48: memcached may 29

48Copyright 2008 MySQL AB The World’s Most Popular Open Source Database

Questions?

Farhan "Frank" MashraqiDirector of Business Operations and Technical Strategy - Fotolog Inc

Monty TaylorSenior Consultant - Sun Microsystems

Jimmy GuerreroSr Product Marketing Manager - Sun Microsystems, Database Group