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
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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
Agenda• Proxy concepts• MySQL Proxy architecture• Proxy with a single back-end• Proxy with multiple back-ends
This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License.
• broken?•missing feature?• not flexible?
This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License.
traditional waySolving database problems
This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License.
1. file a bug report
traditional waySolving database problems
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
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
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
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
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
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
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
Basic principles
Basic principles
PROXY CORE
connection hook
read query hook
read result hook
function
Lua script
function
functionfunctionfunction
Lua
??Why not ...{Perl ?
PHP?Javascript?[whatever]?
Lua
• SMALL ( < 200 KB)• DESIGNED for
EMBEDDED systems• Widely used (lighttpd)
lighttpd, like MySQL Proxy, was created by Jan Kneschke
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-- ...
User quotas-- read_query (2)-- adding to the totalizer
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
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
User quotas-- read_query_result (3)-- displaying the current bandwidth
User quotas - what the user seesmysql> select repeat('a', 10000);ERROR 7777 (X7777): you have exceeded your query quota
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
User quotas (VERY advanced)-- load_multi
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 | +----------+-------+
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
quick-start guide to mysql muxing
ME
online hangout
for teens
5 million unique users per month
1.3 billion forum entries
How does Gaia use the proxy?
connection multiplexer
turning 7,500 connections ...
into 100 MySQL connections
MySQL Proxy
PHPMySQLServer
100 connections7,500 connections
Why MUX?
a little history ...
Gaia’s data center in 2003
our server farm...
as our site grew ...
some scaling issues
added more servers ...
site explodes
diagnose the problem
GAIA, under the hood
typical LAMP stack
Linux
Share-Nothing Architecture
each apache server = 50 connections
Parent Process
httpd
child httpd
child httpd
...
worker thread
worker thread
listener thread
...
50 connections * 150 servers = stampede
close connections each time
setup / tear-down is expensive
connecting to MySQL
connect readchallenge
writeauth
readauth OK
connect readchallenge
writeauth
readauth OK write query read result
connect readchallenge
writeauth
readauth OK write query read result close
75% 19% 6%
MySQL Thread spawning
Client
Client MySQL Server
connect
Client MySQL Server
connect
Client MySQL Server
Threadconnect
new
Client MySQL Server
Threadconnect
accept
new
threads stack up
MySQL I/O
listen
accept
listen
accept
close
end program
NEW THREAD
listen
accept
blocking read
close
end program
NEW THREAD
listen
accept
blocking read
close
end program
NEW THREAD
blocking write
data
listen
accept
blocking read
close
close
end program
NEW THREAD
close thread
blocking write
eof data
talking to multiple databases ...
Forum DBUser DBSession DBForum Page
start session
get userdata
get topic
get related users
save session
idle threads
we need a connection pool
ideally ...
Event Queue Thread Pool
Threadallocate thread
process query
free
release the thread after each query
it’s like a limo ...
... vs. a taxi
decouple threads from connections
Libevent’s callback I/O very sweet!
listen
register callback
event dispatch
active descriptor Trigger callback
epoll/kqueue rulez! (proxy uses it)
MySQL 6.0 taking the right steps ...
In the meantime ...
by default, mysql-proxy isn’t a pooler
lua hooks are awesome!
hack some lua scripts ...
goal: recycle back-end connections
keep clients connected
proxy stays transparent
how to twist the proxy into a pooler
connecting: find an idle back-end
client
Proxy
client
Proxy
idle poolconnect ahead
authentication
Client Proxy Back-End Pool
MySQL Server
Client Proxy Back-End Pool
MySQL Server
Client Proxy Back-End Pool
MySQL Serverconnect
Client Proxy Back-End Pool
MySQL Serverconnectallocate
Client Proxy Back-End Pool
MySQL Serverconnectallocate
Client Proxy Back-End Pool
MySQL Serverconnectallocate
com-change-user
Client Proxy Back-End Pool
MySQL Serverconnectallocate
com-change-user
challenge
Client Proxy Back-End Pool
MySQL Serverconnectallocate
com-change-user
challenge
authenticate
Client Proxy Back-End Pool
MySQL Serverconnectallocate
com-change-user
challenge
authenticate
OK
Client Proxy Back-End Pool
MySQL Serverconnectallocate
com-change-user
free back-end
challenge
authenticate
OK
run the query
don’t be afraid to let go
Client Proxy
query
Client Proxy Back-End Pool
queryallocate
Client Proxy Back-End Pool
MySQL Serverqueryallocate
process query
Client Proxy Back-End Pool
MySQL Serverqueryallocate
process query
free back-end
connections scale!
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
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
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.