designing scalable architectures with mysql proxy

165
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 1 Designing Scalable Architectures with MySQL Proxy Giuseppe Maxia MySQL Community Team Leader - Sun Microsystems John Loehrer Data Architect, Gaia Online Jimmy Guerrero Sr Product Marketing Manager - Sun Microsystems, Database Group

Upload: warwithin

Post on 11-Apr-2015

398 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Designing Scalable Architectures with MySQL Proxy

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

Designing ScalableArchitectures

with MySQL Proxy

Giuseppe MaxiaMySQL Community Team Leader - Sun Microsystems

John LoehrerData Architect, Gaia Online

Jimmy GuerreroSr Product Marketing Manager - Sun Microsystems, Database Group

Page 2: Designing Scalable Architectures with MySQL Proxy

Agenda• Proxy concepts• MySQL Proxy architecture• Proxy with a single back-end• Proxy with multiple back-ends

Page 3: Designing Scalable Architectures with MySQL Proxy

This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License.

• broken?•missing feature?• not flexible?

Page 4: Designing Scalable Architectures with MySQL Proxy

This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License.

traditional waySolving database problems

Page 5: Designing Scalable Architectures with MySQL Proxy

This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License.

1. file a bug report

traditional waySolving database problems

Page 6: Designing Scalable Architectures with MySQL Proxy

This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License.

1. file a bug report2. wait

traditional waySolving database problems

Page 7: Designing Scalable Architectures with MySQL Proxy

This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License.

1. file a bug report2. wait

traditional waySolving database problems

Page 8: Designing Scalable Architectures with MySQL Proxy

This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License.

source code

modify

new source code

compile

• Open source waySolving database problems

Page 9: Designing Scalable Architectures with MySQL Proxy

This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License.

bring the logic at application

level

• creative (shortsighted) way

Solving database problems

Page 10: Designing Scalable Architectures with MySQL Proxy

This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License.

set the logic at server level

(stored routines)

• creative (enlightened) way

Solving database problems

Page 11: Designing Scalable Architectures with MySQL Proxy

This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License.

set the logic at protocol level

(proxy)

• creative (more enlightened) way

Solving database problems

Page 12: Designing Scalable Architectures with MySQL Proxy

what can you do with MySQL Proxy• create new commands• filter queries (deny specific queries)• collect statistics on usage• implement usage quotas• execute shell commands• create customized logs• implement server-side pivot tables• start/stop a MySQL server remotely• play movies (seriously!)• make coffee (really?)• sharding• load balancing servers

Page 13: Designing Scalable Architectures with MySQL Proxy

Basic principles

Page 14: Designing Scalable Architectures with MySQL Proxy

Basic principles

PROXY CORE

connection hook

read query hook

read result hook

function

Lua script

function

functionfunctionfunction

Page 15: Designing Scalable Architectures with MySQL Proxy

Lua

??Why not ...{Perl ?

PHP?Javascript?[whatever]?

Page 16: Designing Scalable Architectures with MySQL Proxy

Lua

• SMALL ( < 200 KB)• DESIGNED for

EMBEDDED systems• Widely used (lighttpd)

lighttpd, like MySQL Proxy, was created by Jan Kneschke

Page 17: Designing Scalable Architectures with MySQL Proxy

Lua

Very popular among game writers

Page 18: Designing Scalable Architectures with MySQL Proxy

Proxy overview

connect_server

Lua script

read_auth

read_auth_result

read_handshake

read_query

read_query_result

disconnect_client

global context

session contextsession

contextsession contextsession

contextsession contextsession

contextsession context

Page 19: Designing Scalable Architectures with MySQL Proxy

Proxy overview/usr/local/sbin/mysql-proxy \ --proxy-lua-script=/path/name.lua

IMPORTANT!THE SCRIPT DOES NOT START UNTIL THE FIRST

CLIENT CONNECTION

Page 20: Designing Scalable Architectures with MySQL Proxy

read_query and read_query_resultclient

MySQL Proxy

SERVER

query

functionread_query

function

read_query_result

query

result

result

if a query is passed directly to the server, its result is NOT evaluated by read_query_result

Page 21: Designing Scalable Architectures with MySQL Proxy

read_query and read_query_resultclient

MySQL Proxy

SERVER

query

function read_query

function

read_query_result

query queue

query

result

result

only if a query is added to the query queue, its result is evaluated by read_query_result

queryquery

Page 22: Designing Scalable Architectures with MySQL Proxy

An example: user quotas• You want to limit user access • When the user reaches a given amount of data,

further queries should be rejected• the quota must work across sessions

(code online)http://forge.mysql.com/tools/tool.php?id=111

Page 23: Designing Scalable Architectures with MySQL Proxy

User quotas-- some global variables

-- proxy.global.bandwidth-- will sum up the used bytes

proxy.global.bandwidth = proxy.global.bandwidth or {}

-- session_user will identify the user-- throughout the sessionlocal session_user

Page 24: Designing Scalable Architectures with MySQL Proxy

User quotas-- session_user needs to be initialized-- when the user information is passed-- (during the authentication)

function read_auth( auth ) session_user = auth.username proxy.global.bandwidth[session_user] = proxy.global.bandwidth[session_user] or 0end

Page 25: Designing Scalable Architectures with MySQL Proxy

BREAK (a handy Lua idiom)-- simple assignment

-- corresponds to:

if a ~= nilthen a = aelse a = 0 end

if a == nilthen a = 0else a = a end

a = a or 0

Page 26: Designing Scalable Architectures with MySQL Proxy

BREAK (a handy Proxy Lua function )-- returns an error to the client

function error_result (msg) proxy.response = { type = proxy.MYSQLD_PACKET_ERR, errmsg = msg, errcode = 7777, sqlstate = 'X7777', } return proxy.PROXY_SEND_RESULTend

Page 27: Designing Scalable Architectures with MySQL Proxy

User quotas-- read_query (1)-- checking if the quota has been-- exceededfunction read_query (packet ) if proxy.global.bandwidth[session_user] > 10000 and session_user ~= 'root' then return error_result( 'you have exceeded your query quota') end-- ...

Page 28: Designing Scalable Architectures with MySQL Proxy

User quotas-- read_query (2)-- adding to the totalizer

-- ... proxy.global.bandwidth[session_user ] = proxy.global.bandwidth[session_user] + packet:len() proxy.queries:append(1, packet ) return proxy.PROXY_SEND_QUERYend

Page 29: Designing Scalable Architectures with MySQL Proxy

User quotas-- read_query_result (1)-- adding row headers to the totalizer

function read_query_result(inj) local fields = inj.resultset.fields local rows = inj.resultset.rows if fields then for i = 1, #fields do proxy.global.bandwidth[session_user] = proxy.global.bandwidth[session_user] + (fields[i] and fields[i].name:len() or 0) end

Page 30: Designing Scalable Architectures with MySQL Proxy

User quotas-- read_query_result (2)-- adding rows contents to the totalizer

if rows then for row in rows do for i = 1, #fields do proxy.global.bandwidth[session_user] = proxy.global.bandwidth[session_user] + (row[i] and row[i]:len() or 0) end end end end

Page 31: Designing Scalable Architectures with MySQL Proxy

User quotas-- read_query_result (3)-- displaying the current bandwidth

print (session_user .. ' -> ' .. proxy.global.bandwidth[session_user])end

Page 32: Designing Scalable Architectures with MySQL Proxy

User quotas - what the user seesmysql> select repeat('a', 10000);ERROR 7777 (X7777): you have exceeded your query quota

Page 33: Designing Scalable Architectures with MySQL Proxy

User quotas (VERY advanced)-- you can create another module-- to be loaded at run time-- (this is really advanced)-- and in such module you define-- a SHOW QUOTAS command

Page 34: Designing Scalable Architectures with MySQL Proxy

User quotas (VERY advanced)-- load_multi

Page 35: Designing Scalable Architectures with MySQL Proxy

User quotas (VERY advanced)mysql> pload show_quotas.lua;+--------------------------------+| info |+--------------------------------+| module "show_quota.lua" loaded | +--------------------------------+mysql> show quotas;+----------+-------+| name | quota |+----------+-------+| simple | 3578 | | root | 2111 | | msandbox | 102 | +----------+-------+

Page 36: Designing Scalable Architectures with MySQL Proxy

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

Designing ScalableArchitectures

with MySQL Proxy

Giuseppe MaxiaMySQL Community Team Leader - Sun Microsystems

John LoehrerData Architect, Gaia Online

Jimmy GuerreroSr Product Marketing Manager - Sun Microsystems, Database Group

Page 37: Designing Scalable Architectures with MySQL Proxy

quick-start guide to mysql muxing

Page 38: Designing Scalable Architectures with MySQL Proxy

ME

Page 39: Designing Scalable Architectures with MySQL Proxy
Page 40: Designing Scalable Architectures with MySQL Proxy

online hangout

Page 41: Designing Scalable Architectures with MySQL Proxy
Page 42: Designing Scalable Architectures with MySQL Proxy

for teens

Page 43: Designing Scalable Architectures with MySQL Proxy
Page 44: Designing Scalable Architectures with MySQL Proxy

5 million unique users per month

Page 45: Designing Scalable Architectures with MySQL Proxy

1.3 billion forum entries

Page 46: Designing Scalable Architectures with MySQL Proxy

How does Gaia use the proxy?

Page 47: Designing Scalable Architectures with MySQL Proxy
Page 48: Designing Scalable Architectures with MySQL Proxy

connection multiplexer

Page 49: Designing Scalable Architectures with MySQL Proxy

turning 7,500 connections ...

Page 50: Designing Scalable Architectures with MySQL Proxy

into 100 MySQL connections

Page 51: Designing Scalable Architectures with MySQL Proxy

MySQL Proxy

PHPMySQLServer

100 connections7,500 connections

Page 52: Designing Scalable Architectures with MySQL Proxy

Why MUX?

Page 53: Designing Scalable Architectures with MySQL Proxy

a little history ...

Page 54: Designing Scalable Architectures with MySQL Proxy

Gaia’s data center in 2003

Page 55: Designing Scalable Architectures with MySQL Proxy
Page 56: Designing Scalable Architectures with MySQL Proxy

our server farm...

Page 57: Designing Scalable Architectures with MySQL Proxy
Page 58: Designing Scalable Architectures with MySQL Proxy

as our site grew ...

Page 59: Designing Scalable Architectures with MySQL Proxy
Page 60: Designing Scalable Architectures with MySQL Proxy

some scaling issues

Page 61: Designing Scalable Architectures with MySQL Proxy
Page 62: Designing Scalable Architectures with MySQL Proxy

added more servers ...

Page 63: Designing Scalable Architectures with MySQL Proxy
Page 64: Designing Scalable Architectures with MySQL Proxy

site explodes

Page 65: Designing Scalable Architectures with MySQL Proxy
Page 66: Designing Scalable Architectures with MySQL Proxy

diagnose the problem

Page 67: Designing Scalable Architectures with MySQL Proxy
Page 68: Designing Scalable Architectures with MySQL Proxy

GAIA, under the hood

Page 69: Designing Scalable Architectures with MySQL Proxy

typical LAMP stack

Page 70: Designing Scalable Architectures with MySQL Proxy
Page 71: Designing Scalable Architectures with MySQL Proxy

Linux

Page 72: Designing Scalable Architectures with MySQL Proxy
Page 73: Designing Scalable Architectures with MySQL Proxy
Page 74: Designing Scalable Architectures with MySQL Proxy
Page 75: Designing Scalable Architectures with MySQL Proxy

Share-Nothing Architecture

Page 76: Designing Scalable Architectures with MySQL Proxy
Page 77: Designing Scalable Architectures with MySQL Proxy

each apache server = 50 connections

Page 78: Designing Scalable Architectures with MySQL Proxy

Parent Process

httpd

child httpd

child httpd

...

worker thread

worker thread

listener thread

...

Page 79: Designing Scalable Architectures with MySQL Proxy

50 connections * 150 servers = stampede

Page 80: Designing Scalable Architectures with MySQL Proxy
Page 81: Designing Scalable Architectures with MySQL Proxy

close connections each time

Page 82: Designing Scalable Architectures with MySQL Proxy

setup / tear-down is expensive

Page 83: Designing Scalable Architectures with MySQL Proxy
Page 84: Designing Scalable Architectures with MySQL Proxy

connecting to MySQL

Page 85: Designing Scalable Architectures with MySQL Proxy

connect readchallenge

writeauth

readauth OK

Page 86: Designing Scalable Architectures with MySQL Proxy

connect readchallenge

writeauth

readauth OK write query read result

Page 87: Designing Scalable Architectures with MySQL Proxy

connect readchallenge

writeauth

readauth OK write query read result close

75% 19% 6%

Page 88: Designing Scalable Architectures with MySQL Proxy

MySQL Thread spawning

Page 89: Designing Scalable Architectures with MySQL Proxy

Client

Page 90: Designing Scalable Architectures with MySQL Proxy

Client MySQL Server

connect

Page 91: Designing Scalable Architectures with MySQL Proxy

Client MySQL Server

connect

Page 92: Designing Scalable Architectures with MySQL Proxy

Client MySQL Server

Threadconnect

new

Page 93: Designing Scalable Architectures with MySQL Proxy

Client MySQL Server

Threadconnect

accept

new

Page 94: Designing Scalable Architectures with MySQL Proxy

threads stack up

Page 95: Designing Scalable Architectures with MySQL Proxy
Page 96: Designing Scalable Architectures with MySQL Proxy

MySQL I/O

Page 97: Designing Scalable Architectures with MySQL Proxy

listen

accept

Page 98: Designing Scalable Architectures with MySQL Proxy

listen

accept

close

end program

NEW THREAD

Page 99: Designing Scalable Architectures with MySQL Proxy

listen

accept

blocking read

close

end program

NEW THREAD

Page 100: Designing Scalable Architectures with MySQL Proxy

listen

accept

blocking read

close

end program

NEW THREAD

blocking write

data

Page 101: Designing Scalable Architectures with MySQL Proxy

listen

accept

blocking read

close

close

end program

NEW THREAD

close thread

blocking write

eof data

Page 102: Designing Scalable Architectures with MySQL Proxy

talking to multiple databases ...

Page 103: Designing Scalable Architectures with MySQL Proxy
Page 104: Designing Scalable Architectures with MySQL Proxy

Forum DBUser DBSession DBForum Page

start session

get userdata

get topic

get related users

save session

Page 105: Designing Scalable Architectures with MySQL Proxy

idle threads

Page 106: Designing Scalable Architectures with MySQL Proxy
Page 107: Designing Scalable Architectures with MySQL Proxy

we need a connection pool

Page 108: Designing Scalable Architectures with MySQL Proxy
Page 109: Designing Scalable Architectures with MySQL Proxy

ideally ...

Page 110: Designing Scalable Architectures with MySQL Proxy

Event Queue Thread Pool

Threadallocate thread

process query

free

Page 111: Designing Scalable Architectures with MySQL Proxy

release the thread after each query

Page 112: Designing Scalable Architectures with MySQL Proxy

it’s like a limo ...

Page 113: Designing Scalable Architectures with MySQL Proxy
Page 114: Designing Scalable Architectures with MySQL Proxy

... vs. a taxi

Page 115: Designing Scalable Architectures with MySQL Proxy
Page 116: Designing Scalable Architectures with MySQL Proxy

decouple threads from connections

Page 117: Designing Scalable Architectures with MySQL Proxy

Libevent’s callback I/O very sweet!

listen

register callback

event dispatch

active descriptor Trigger callback

Page 118: Designing Scalable Architectures with MySQL Proxy

epoll/kqueue rulez! (proxy uses it)

Page 119: Designing Scalable Architectures with MySQL Proxy

MySQL 6.0 taking the right steps ...

Page 120: Designing Scalable Architectures with MySQL Proxy
Page 121: Designing Scalable Architectures with MySQL Proxy

In the meantime ...

Page 122: Designing Scalable Architectures with MySQL Proxy
Page 123: Designing Scalable Architectures with MySQL Proxy

by default, mysql-proxy isn’t a pooler

Page 124: Designing Scalable Architectures with MySQL Proxy
Page 125: Designing Scalable Architectures with MySQL Proxy

lua hooks are awesome!

Page 126: Designing Scalable Architectures with MySQL Proxy
Page 127: Designing Scalable Architectures with MySQL Proxy

hack some lua scripts ...

Page 128: Designing Scalable Architectures with MySQL Proxy
Page 129: Designing Scalable Architectures with MySQL Proxy

goal: recycle back-end connections

Page 130: Designing Scalable Architectures with MySQL Proxy
Page 131: Designing Scalable Architectures with MySQL Proxy

keep clients connected

Page 132: Designing Scalable Architectures with MySQL Proxy
Page 133: Designing Scalable Architectures with MySQL Proxy

proxy stays transparent

Page 134: Designing Scalable Architectures with MySQL Proxy
Page 135: Designing Scalable Architectures with MySQL Proxy

how to twist the proxy into a pooler

Page 136: Designing Scalable Architectures with MySQL Proxy
Page 137: Designing Scalable Architectures with MySQL Proxy

connecting: find an idle back-end

Page 138: Designing Scalable Architectures with MySQL Proxy

client

Proxy

Page 139: Designing Scalable Architectures with MySQL Proxy

client

Proxy

idle poolconnect ahead

Page 140: Designing Scalable Architectures with MySQL Proxy

authentication

Page 141: Designing Scalable Architectures with MySQL Proxy

Client Proxy Back-End Pool

MySQL Server

Page 142: Designing Scalable Architectures with MySQL Proxy

Client Proxy Back-End Pool

MySQL Server

Page 143: Designing Scalable Architectures with MySQL Proxy

Client Proxy Back-End Pool

MySQL Serverconnect

Page 144: Designing Scalable Architectures with MySQL Proxy

Client Proxy Back-End Pool

MySQL Serverconnectallocate

Page 145: Designing Scalable Architectures with MySQL Proxy

Client Proxy Back-End Pool

MySQL Serverconnectallocate

Page 146: Designing Scalable Architectures with MySQL Proxy

Client Proxy Back-End Pool

MySQL Serverconnectallocate

com-change-user

Page 147: Designing Scalable Architectures with MySQL Proxy

Client Proxy Back-End Pool

MySQL Serverconnectallocate

com-change-user

challenge

Page 148: Designing Scalable Architectures with MySQL Proxy

Client Proxy Back-End Pool

MySQL Serverconnectallocate

com-change-user

challenge

authenticate

Page 149: Designing Scalable Architectures with MySQL Proxy

Client Proxy Back-End Pool

MySQL Serverconnectallocate

com-change-user

challenge

authenticate

OK

Page 150: Designing Scalable Architectures with MySQL Proxy

Client Proxy Back-End Pool

MySQL Serverconnectallocate

com-change-user

free back-end

challenge

authenticate

OK

Page 151: Designing Scalable Architectures with MySQL Proxy

run the query

Page 152: Designing Scalable Architectures with MySQL Proxy

don’t be afraid to let go

Page 153: Designing Scalable Architectures with MySQL Proxy
Page 154: Designing Scalable Architectures with MySQL Proxy

Client Proxy

query

Page 155: Designing Scalable Architectures with MySQL Proxy

Client Proxy Back-End Pool

queryallocate

Page 156: Designing Scalable Architectures with MySQL Proxy

Client Proxy Back-End Pool

MySQL Serverqueryallocate

process query

Page 157: Designing Scalable Architectures with MySQL Proxy

Client Proxy Back-End Pool

MySQL Serverqueryallocate

process query

free back-end

Page 158: Designing Scalable Architectures with MySQL Proxy

connections scale!

Page 159: Designing Scalable Architectures with MySQL Proxy
Page 160: Designing Scalable Architectures with MySQL Proxy

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

Giuseppe MaxiaMySQL Community Team Leader - Sun Microsystems

John LoehrerData Architect, Gaiaonline

Jimmy GuerreroSr Product Marketing Manager - Sun Microsystems, Database Group

This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License.

Designing ScalableArchitectures

with MySQL Proxy

Page 161: Designing Scalable Architectures with MySQL Proxy

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

MySQL software which can route database queries to the appropriate database in a scale-out environment.

• Improves/scales throughput of reads for online applications• Helps customers reduce cost of adding slaves

• Intelligently route reads across slaves• Use database least behind, least loaded• Remove latent slaves from read rotation• Other distribution algorithms

MySQL Load Balancer

Page 162: Designing Scalable Architectures with MySQL Proxy

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

MySQL Enterprise Monitor feature that allows users to trace, monitor, and analyze MySQL query activity for specific servers, users, and applications.

• Adaptive “Evil” query collection/tracing• Historical browsing/analysis

• “Needle in a haystack” identification of worst queries

• Worst execution times, # of execs, etc.

SQL code is the #2 cause of performance issues 97% of those surveyed will use this

MySQL Query Analyzer

Page 163: Designing Scalable Architectures with MySQL Proxy

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

MySQL Query Analyzer

Page 164: Designing Scalable Architectures with MySQL Proxy

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

Next StepsMySQL Proxy Forumhttp://forums.mysql.com/list.php?146

Documentation – MySQL Proxy

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

Documentation – MySQL Load Balancer

http://dev.mysql.com/doc/refman/5.1/en/load-balancer.html

Beta Testing MySQL Proxy-Enabled Products

[email protected]

MySQL Proxy Online Poll

http://dev.mysql.com/tech-resources/quickpolls/

Page 165: Designing Scalable Architectures with MySQL Proxy

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

Questions?

Giuseppe MaxiaMySQL Community Team Leader - Sun Microsystems

John LoehrerData Architect, Gaiaonline

Jimmy GuerreroSr Product Marketing Manager - Sun Microsystems, Database Group

This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License.