tag:blogger.com,1999:blog-54121502734156969142018-03-06T03:59:45.614-05:00Jay Janssen -- MySQL GuyMySQL, Yahoo, High Availability and other goodies...Jay Janssennoreply@blogger.comBlogger9125tag:blogger.com,1999:blog-5412150273415696914.post-79464759094049401572007-11-05T17:59:00.000-05:002007-11-05T18:52:04.435-05:00Fairness in Storage Engine BenchmarkingIs it fair to compare Innodb to MyISAM with innodb_flush_log_at_trx_commit=1 and flush=OFF? <br /><br />Granted different applications have different requirements. But, when you're trying to show a graph comparing the two Engines, isn't it reasonable to try to keep the same settings as close to equal as possible? <br /><br />I suppose that begs the question of how similar innodb_flush_log_at_trx_commit is to flush, and if it's even possible to get a valid comparison. <br /><br />Innodb tends to be slower (I'm on 4.1 currently, ancient history), but it seems to me that it's doing a whole lot more for the money. What do you think?<br /><br />[UPDATE] Additionally, I'm running sequential benchmarks back to back, for example loading the table, then turning around and running a read test on the table. For innodb, this often means that a good chunk of the buffer pool is dirty and the read test has to slog through while the dirty buffers are being flushed, potentially changing performance. <br /><br />Should sequential benchmarks have some built in time to allow the database to return to a stable state, or is it better just to plow straight through them all at once?Jay Janssennoreply@blogger.com4tag:blogger.com,1999:blog-5412150273415696914.post-15054027969658932112007-11-05T15:35:00.001-05:002007-11-06T16:27:28.722-05:00What is Innodb doing?I've been running a series of sysbench tests on Innodb with MySQL 4.1 My tests include doing both reads and writes on the table. <br /><br />I've noticed that after the test is complete, and while there are no processes in show processlist, I see a fairly large amount of buffer pool reads and writes, as well as a large number of Modified db pages. Now, I know that it could take a while for innodb to flush out the dirty buffers after a large amount of writes, but I'm seeing the Modified db pages actually getting <span style="font-weight:bold;">larger</span> for a while, and then gradually getting to the point where it finally settles down and returns to 0.<br /><br />The only thing I can think of is Innodb is doing some sort of table optimization, which begs the question of whether I should rerun my tests with the optimized table or not. <br /><br />Has anyone else seen this?<br /><br />UPDATE: I believe I have figured out what's happening. My sysbench test is working on a 100M row table, and the last run I do is 10M inserts with these options:<br /><br /> --oltp-test-mode=nontrx<br /> --oltp-nontrx-mode=insert<br /><br />I found out that this test deletes all of my rows before it runs, so I see around 50k deletes per second for quite a while, and then the test runs. After the test finishes, I suspect Innodb is cleaning up a lot of unused pages in the data file, hence all the read and write activity on the buffer pool.Jay Janssennoreply@blogger.com2tag:blogger.com,1999:blog-5412150273415696914.post-65065344671973489522007-10-12T11:54:00.001-04:002007-10-12T12:56:15.343-04:00Creating a Job queue in InnodbThis post is half-this is how you do it, and half-is there a better way to do it?<br /><br />So you want to build a system that does jobs. You want the jobs to be able to be run in parallel for speed, but also for redundancy. This system needs to be coordinated so, for example, the same jobs aren't being done twice, the status of every job is easy to see, and multiple servers can run jobs by simply querying the central source.<br /><br />How can we build code around Innodb to have MySQL be our central controlling scheme?<br /><br /><pre class="Apple-style-span" style="font-family:'courier new';"><pre class="Apple-style-span" style="font-size:small;"><br />CREATE TABLE IF NOT EXISTS job_queue(<br /> id int(10) not null auto_increment,<br /><br /> updated timestamp not null,<br /> started timestamp not null,<br /><br /> state ENUM('NEW', 'WORKING', 'DONE', 'ERROR' ) default 'NEW',<br /><br /> PRIMARY KEY ( id ),<br /> KEY( STATE )<br />) ENGINE=Innodb;</pre></pre><br />In this schema, our job table has a unique id, started and last updated time, and a STATE. In a real system, there would probably be some more meta data here about the nature of the job.<br /><br />When new jobs need to be done, rows are inserted into the table (with started=NOW(), updated changes automatically)<br /><br /><pre class="Apple-style-span" style="font-family:'courier new';"><pre class="Apple-style-span" style="font-size:small;">insert into job_queue set started=NOW();</pre></pre><br /><br />Now, in perl, we write our job controller. This program could be run on every server you want to run jobs:<br /><br /><pre class="Apple-style-span" style="font-family:'courier new';"><pre class="Apple-style-span" style="font-size:small;"><br />#!/usr/bin/perl<br /><br /># Don't leave zombies around, shamelessly stolen from 'man perlipc'<br />use POSIX ":sys_wait_h";<br />sub REAPER {<br /> my $child;<br /> while (($child = waitpid(-1,WNOHANG)) > 0) {<br /> $Kid_Status{$child} = $?;<br /> }<br /> $SIG{CHLD} = \&REAPER;<br />}<br />$SIG{CHLD} = \&REAPER;<br /><br />use DBD::mysql;<br /><br />my $dbh = DBI->connect(<br />'DBI:mysql:database=test;host=127.0.0.1;port=3306',<br />'test', 'test',<br />{ RaiseError => 1, AutoCommit => 0 }<br />);<br /><br /><br />while( 1 ) {<br /> my $row;<br /> eval {<br /> my $sth = $dbh->prepare( "select id from job_queue where state='NEW' li<br />mit 1 for update" );<br /> $sth->execute();<br /> $row = $sth->fetchrow_hashref();<br /> $sth->finish();<br /> };<br /><br /> if( $@ or !$row ) {<br /> # Couldn't lock or lock wait timeout<br /> $dbh->commit();<br /> sleep 10;<br /> next;<br /> }<br /><br /> # Got one, change state, commit, and fork a worker<br /><br /> $dbh->do( "update job_queue set state='WORKING' where id=" . $row->{id} );<br /> $dbh->commit();<br /><br /> # Fork a worker<br /> if( fork ) {<br /> # Parent, let the child have the old connection and reconnect to<br /> # the db.<br /> $dbh->{InactiveDestroy} = 1;<br /> $dbh = DBI->connect(<br /> 'DBI:mysql:database=test;host=127.0.0.1;port=3306',<br /> 'test', 'test',<br /> { RaiseError => 1, AutoCommit => 0 }<br /> );<br /> } else{<br /> # Child<br /> print "Locking\n";<br /> $dbh->do( "select * from job_queue where id=" . $row->{id}<br /> . " for update " );<br /><br /> print "Working\n";<br /> #simulated work here<br /> srand( time );<br /> sleep rand 30;<br /><br /> $dbh->do( "update job_queue set STATE='DONE' where<br /> id=" . $row->{id} );<br /> print "Committing\n";<br /> $dbh->commit();<br /><br /> $dbh->disconnect();<br /><br /> exit;<br /> }<br /> sleep 1;<br />}<br /></pre></pre><br /><br />Now, this isn't bad. But it has at least one thing I don't like about it. If a forked worker dies before the job finishes (and commits), there's no way for the job to be reassigned to another worker. <br /><br /><span class="Apple-style-span" style="font-style: italic;">Ideally each worker would have a lock on his job row inside of a transaction</span>, which it is doing now, <span class="Apple-style-span" style="font-weight: bold;">but</span>, instead of the job being in the '<span class="Apple-style-span" style="font-weight: bold;">WORKING</span>' state first, I'd rather it was '<span class="Apple-style-span" style="font-weight: bold;">NEW</span>' before the transaction started. If that were the case, <span class="Apple-style-span" style="font-style: italic;">if a worker died, it's unfinished transaction would be rolled back</span>, and the job would be unlocked and <span class="Apple-style-span" style="font-weight: bold;">NEW</span> again. <br /><br />However, because of the way SELECT ... FOR UPDATE works, Innodb will <span class="Apple-style-span" style="font-weight: bold;">deadlock</span> waiting for 'NEW' jobs to unlock from the workers (or wait until they are done). This is sub-optimal, since my parent process could be forking new jobs in the meantime. <span class="Apple-style-span" style="font-weight: bold;">What would fix this is a SELECT ... FOR UPDATE that skipped locked rows without blocking. </span><br /><br />If anyone knows a good way to achieve this, please let me know!<br /><br />However, assuming we have no better alternatives, we can create a reaper process like this:<br /><br /><pre class="Apple-style-span" style="font-family:'courier new';"><pre class="Apple-style-span" style="font-size:small;"><br />#!/usr/bin/perl<br /><br />use DBD::mysql;<br /><br />my $dbh = DBI->connect(<br /> 'DBI:mysql:database=test;host=127.0.0.1;port=3306',<br /> 'test', 'test',<br /> { RaiseError => 1, AutoCommit => 0 }<br />);<br /><br /><br />while( 1 ) {<br /> my $row;<br /> eval {<br /> my $sth = $dbh->prepare( "select id from job_queue where state='WORKING' limit 1 for update" );<br /> $sth->execute();<br /> $row = $sth->fetchrow_hashref();<br /> $sth->finish();<br /> };<br /><br /> if( $@ or !$row ) {<br /> # Couldn't lock or lock wait timeout<br /> $dbh->commit();<br /> sleep 10;<br /> next;<br /> }<br /><br /> # Got one, change state, commit, and fork a worker<br /><br /> $dbh->do( "update job_queue set state='NEW' where id=" . $row->{id} ); $dbh->commit();<br /><br /> sleep 1;<br />}<br /></pre></pre><br /><br />This will find the first WORKING row and try to lock it. Since normal jobs will be moved to DONE before they are committed, this should only ever find jobs that are in WORKING and unlocked, which means the worker died. <br /><br />However this isn't perfect. Because SELECT ... FOR UPDATE will try to lock a row already locked, we have to wait until all jobs before our stalled job are complete, getting deadlocks along the way (be sure to set your innodb_lock_wait_timeout fairly low!. Even further, I've seen dead worker processes leave behind idle mysql connections that hold onto their row locks. <br /><br />Is there any smoother way to do this? I'd love to hear other people's advice.Jay Janssennoreply@blogger.com8tag:blogger.com,1999:blog-5412150273415696914.post-69575235757998530032007-10-04T09:10:00.000-04:002007-10-04T13:03:58.190-04:00How safe is setting read_only?We use a lot of <span style="font-weight:bold;">dual master setups</span> for redundancy and recoverability. Typically, most apps can't write to both masters at the same time, so for consistency we keep only one of the masters read-write at any given instant. To switch over gracefully, turn the primary read-only, wait for replication to catch up, turn the secondary read-write. Simple, eh? <br /><br />Well, I've just had a report that setting the primary read-only caused <span style="font-style:italic;">a transaction to be partially replicated to a slave</span>. Wait, <span style="font-weight:bold;">what</span>? If I'm reading the email correctly, they switched masters in the middle of a transaction. The transaction commit correctly failed on the master with a read-only error, but somehow the other master (or a slave) got part of the transaction that failed and had to be <span style="font-weight:bold;">skipped</span>. <br /><br />This was an <span style="font-weight:bold;">innodb</span> table in <span style="font-weight:bold;">MySQL 5.0</span>. My first thought was that <span style="font-weight:bold;">XA was disabled</span> (though we typically use 4.1 here, so I thought of innodb_safe_binlog). I verified that the XA support appeared to be enabled correctly. My other thoughts are maybe the binlog didn't get synced properly or a thousand other possibilities... I'll update if find out more.<br /><br />Now, I'm not panicking too much, this could be a non-issue, bad report, what-have-you. However, it got me thinking. <span style="font-weight:bold;"> How safe *is* it to simply set read-only?</span> Transactionally speaking, is this doing what I want, which is an <span style="font-weight:bold;">atomic</span> <span style="font-style:italic;">"deny writes from now on"</span> change? <span style="font-weight:bold;">Is this safe with binlogging</span>?<br /><br />My user claims that doing a flush tables with read lock before setting read-only is safer, but is it? Does setting read-only somehow muck with the binlog to prevent it from being resynced with the inno transactional state somehow?<br /><br />I figured I'd post it out here and see what people say...<br /><br /><span style="font-weight:bold;">UPDATE: </span>I found out the user isn't using normal transactions, but rather AUTOCOMMIT. I'm following up to try to figure out what the exact nature of the slave error was.<br /><br />However, another related question is: Is there a race condition between applying a write statement and writing the binary log? Is it possible to do one without the other if there's a sudden server crash? With Innodb, I'd expect the XA transactions to handle this event, but what about plain old MyISAM?Jay Janssennoreply@blogger.com1tag:blogger.com,1999:blog-5412150273415696914.post-40355630869883524852007-05-19T10:23:00.000-04:002007-05-19T11:36:23.138-04:00My first databaseI grew up on a farm. Lest anyone automatically load up their preconceived notions of farmers, my Dad was (and is) very interested in computers and purchased the original IBM PC back in the early/mid 80's. He found a lot of practical use in the Lotus 1-2-3 spreadsheet program, and could fly through the keyboard shortcuts (I guess they weren't really shortcuts in those days, as there wasn't a mouse) fast enough to put him on the same level with any Unix geek today with Emacs or Vi.<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_ZvPeZ4SQGyY/Rk8Lbf38W_I/AAAAAAAAAAU/SQX7O1-c--I/s1600-h/steigedonotcopyfromtts7.jpg"><img style="float:right; margin:0 0 10px 10px;cursor:pointer; cursor:hand;" src="http://3.bp.blogspot.com/_ZvPeZ4SQGyY/Rk8Lbf38W_I/AAAAAAAAAAU/SQX7O1-c--I/s200/steigedonotcopyfromtts7.jpg" border="0" alt="these are fun to drive" id="BLOGGER_PHOTO_ID_5066280672855612402" /></a>In the 90's, I remember him buying our 486/66 with 16MB of RAM from Micron and describing it as a computer that would "peel out on the lawn." My dad is a tractor-guy and has always been obsessed with horsepower. He still likes to rev up his <a href="http://www.bigtractorpower.com/steiger4wdtractorhistory.htm">Steiger tractors</a> and listen to the power. This type of personality translates well into the computer field.<br /><br />Anyway, I had been interested in computers since he brought the first one home. Sometime around when he got the Micron, he paid a lot of money for this thing called a database. I'm not even sure which one it was, I think it might have been <a href="http://en.wikipedia.org/wiki/FoxPro_2">FoxPro 2</a>. He wanted to use it to track information on our crop fields: what seed brand had been planted, when it had been worked, fertilizers used, statistics on the yield, stuff like that. He didn't really know how this new software worked, but he understood that it could do stuff like that better than a spreadsheet could. <span style="font-weight:bold;">He was totally right</span>.<br /><br />Unfortunately for him, he was relying on me to make this work (think lazy teenager, I'm not saying all teenagers are lazy, but I was). He knew I grasped computer concepts much more quickly than he did, but I couldn't make heads or tails of it: at least for the 20 minutes I tried. The FoxPro box sat on the shelf, unused, and it probably is still sitting there.<br /><br />I titled this post <span style="font-style:italic;">"My First Database"</span>, and I guess that story doesn't fit the requirement since I didn't really actually create a database. So here's this story:<br /><br />A few years later, after my first two years of college, I went on a <a href="http://en.wikipedia.org/wiki/Cooperative_education">Co-op</a> at <a href="http://en.wikipedia.org/wiki/Pratt_%26_Whitney">Pratt & Whitney</a> as a programmer. That was where I first learned Perl, among other things, and I can remember building a few web applications that were meant to help access these legally retained documents (think plane crash, faulty engine, lawsuit). Incredulously, I basically had to design and build these applications myself (just finished 2nd year of Computer Science). Perl CGI was pretty cutting edge in 1996, but at the time (and not even until after I graduated) I still didn't really have the foggiest notion of a database. Having no other guidance from the more experienced engineers or my manager, I based what was essentially the database on the unix filesystem and a set of storage conventions I coded into my application. <br /><br />This was, in essence, my first database, but I had enough sense at this point to realize that:<ol><br /><li>this didn't feel right<br /><li>there had to be a better way to manage the data<br /><li>this thing was going to break as soon as my co-op ended and no one would be able to fix it. <br /></ol> I never heard what happened. <br /><br />That's right, my first database was flat files. One of my co-workers at Yahoo, Dave, likes to listen to me and Tim, the Oracle Architect, talk about databases and he (Dave) likes to joke that flat files are his database expertise. He would be so proud.<br /><br />What does this have to do with anything? I think about how both of these experiences would have been different if it had been set today. How awesome is it that a) MySQL is freely available on the net (indeed, along with plenty of other good choices) and b) the tools to learn and understand these systems are freely available to <span style="font-style:italic;">anyone</span>. There are so many more opportunities for guys who are interested enough, like my dad, to take these tools and build something without wasting a lot of money. <br /><br />I'm not sure my Dad would get MySQL, he's more into trading stocks nowadays than learning new technologies (though he did just buy his first Mac), or if he would have <span style="font-style:italic;">"gotten"</span> it then. He did have me, and realized I was capable of understanding it, I was just too busy playing <a href="http://en.wikipedia.org/wiki/Star_Wars:_X-Wing">X-wing</a>.<br /><br />I realize now that this post is a gushy open-source loving rant (of which I never bother to read myself anymore once I identify it) but I guess that's at least somewhat reflective of how I feel on the subject after careful analysis. I'm no hardcore zealot, but it is cool to look back just a short 10-15 years and see how much has changed.Jay Janssennoreply@blogger.com0tag:blogger.com,1999:blog-5412150273415696914.post-51979062481557137302007-05-16T23:18:00.000-04:002007-05-17T00:31:35.885-04:00MySQL is to Oracle...I came up with this analogy today comparing MySQL and Oracle and it seemed to fit. Only real Unix geeks will get this, but here goes:<br /><br /><b>MySQL is to Oracle as Vi is to Emacs</b><br /><br />Maybe this has been said before, but think about it. <br /><br />Here's what they all have in <span style="font-weight:bold;">common</span>: <br /><ul><br /><li>All are cool technologies and worthy of respect in their own right<br /><li>All have zealots vigorously defend their technology to the death<br /><li>Each set (Oracle/Emacs and MySQL/Vi) represents a different way of looking at and solving a problem<br /></ul><br /><hr><br />For the <span style="font-weight:bold;">Oracle/Emacs</span> world things work like this: <br /><br /><span style="font-style:italic;">The technology is the be-all/end-all, make it do everything we want</span>. <br /><br />In many ways this is a huge advantage, things are tightly integrated, you don't need to worry so much about system conventions and portability, because new features can easily be added inside the existing framework. This continues until the technology becomes very <span style="font-style:italic;">Operating System</span>-like. <br /><br />The disadvantage (to my mind) seems to be <span style="font-weight:bold;">bloat</span>. What the technology was really made for becomes a little murky with all the extra features and complexity of setup and use.<br /><br />I can't spend as much time explaining this camp here, because I'm not a part of it. I do know that Oracle installations take up multiple Gigabytes by default and Emacs has its own web browser and email client, both attributes that are common with Operating Systems. <br /><br /><hr><br /><br />On the <span style="font-weight:bold;">MySQL/Vi</span> side, things are more like this:<br /><br /><span style="font-style:italic;">Keep this technology focused on its purpose, lean and mean</span><br /><br />In this camp, there's far <span style="font-weight:bold;">less bloat</span>. It's very clear <span style="font-style:italic;">(most of the time)</span> what the technology is supposed to do, and installing/configuring/using the technology is centered around the <span style="font-weight:bold;">core purpose</span> of that technology.<br /><br />What's the benefit? In theory, a well made piece of technology that does its job in an efficient manner.<br /><br />What's the disadvantage? <span style="font-style:italic;">"Is that <span style="font-weight:bold;">all</span> it can do??!"</span> There are less features, harder integrate third-party components, and it's harder to add that stuff in (a pluggable what?!)<br /><br /><hr><br /><br /><span style="font-weight:bold;">Of course</span>, it's not as simple as all that, and there are plenty shades of gray. MySQL has a pluggable storage engine framework (I heard it's drag and drop), and Oracle still has a RDBMS somewhere in there (if I pick on MySQL, I can pick on Oracle).<br /><br />Lest I'm misunderstood, I'm simply saying that these technologies seem to <span style="font-style:italic;">represent</span> a particular way of looking at developing software over time. <br /><br />It seems to be generally true that commercial vendors tend to gravitate towards the "<span style="font-style:italic;">be-all/end-all</span>" way of looking at things, but I'm pretty sure no one made millions licensing Emacs.<br /><br />The <span style="font-style:italic;">pragmatists </span>amongst us (myself included) would probably say:<br /><ol><br /><li>Use what works for you. <br /><p>If you know it, and you have it, use it!<br /><li>Save the baby, throw out the bathwater<br /><p>At the end of the day, keep what you need, ditch the rest.<br /><li>Can't we all just get along?<br /><p>Prejudice works both ways, everyone thinks their technology is the best.<br /></ol><br /><br /><hr><br /><b>Some after thoughts</b><br /><span style="font-style:italic;">(in no particular order, and with no particular point)</span><br /><ul><br /><li>It sure is hard to switch to an alternative technology after you have one under your belt. I've attempted to grasp Emacs a few times, just can't do it. I'd like to learn Python or Ruby, but it's sure hard when I can just use Perl.<br /><li>I really try to be neutral on these holy wars, but hey, everyone believes in something.<br /><li>*flashes the Vi gang-sign to Paul Tuckfield*<br /></ul>Jay Janssennoreply@blogger.com0tag:blogger.com,1999:blog-5412150273415696914.post-40155987929844691542007-05-10T00:09:00.000-04:002007-05-11T16:01:41.907-04:00Where cluster needs to beI'm really excited about <a href="http://dev.mysql.com/doc/refman/5.0/en/mysql-cluster-overview.html">MySQL Cluster</a>. It has a lot of potential to be a good competitor to <a href="http://en.wikipedia.org/wiki/Oracle_RAC">Oracle RAC</a>, and not just in a copy-cat kind of way. <br /><br />The thing that makes MySQL different and a good balance, when you're fortunate enough to have the choice, is the "<a href="http://en.wikipedia.org/wiki/MySQL_Cluster#Shared_Nothing">Share nothing</a>" concept. In RAC (and I'm no expert), you use shared storage, usually a Netapp Filer. While filers are seriously cool, they are also seriously expensive, and, no matter how much you gussy it up, it's still a single point of failure. <br /><br />I was glad I sat in on the Intro to cluster talk at the Users conference, it helped me understand what cluster has to offer, and what it still needs. These needs probably aren't any big secret to the Cluster developers, they openly talked about them and made them very clear. Others are my (admittedly limited) opinions based only on what I heard at the conference and not actually from using cluster mysql (yet).<br /><br /><ol><br /><li>Dynamically <span style="font-weight:bold;">adding and removing</span> Storage nodes<br /><p>Doing a lot of queries on a lot of storage nodes is fine, but until I can dynamically add and remove nodes without cluster downtime, I can't easily recover from failures, I can't add capacity, and I can't get my data to re-balance itself over the nodes.<br /><li>I need to scale <span style="font-weight:bold;">bigger than physical memory</span><br /><p>I know 5.1 has disk-based storage for non-indexed data, but I need to scale my indexes beyond RAM too. RAM is too expensive to scale this way forever. For what cluster should be useful for, like a terabyte or two of data, I'd probably have a hard time fitting all my indexes in physical memory.<br /><li><span style="font-weight:bold;">Replicating</span> a cluster<br /><p>Talk about SPoFs all you want, but until you consider your colocation a point of failure, you're still vulnerable. The replication in 5.1 for cluster is good in some ways, but I can't help feeling like it's a little hacky. Passing the replication data back (upstream) into the SQL nodes from the data cluster just doesn't feel right somehow. <p>I do like the <a href="http://dev.mysql.com/doc/refman/5.1/en/mysql-cluster-replication-failover.html">epoch concept</a>, I hope that makes it upstream into generic replication and isn't just NDB specific forever.<br /><p>From what I heard, even with single threaded replication, cluster is so fast that it can scale quite a ways anyway. With clever partitioning, multiple replication threads can be used, but it wouldn't be easy to implement in normal cases. <br /><li>So how do I <span style="font-weight:bold;">backup </span>this thing?<br /><p>So I know data nodes can be redundant, and I know I can replicate to another colo. But how can I simply get a cumulative data node cluster's data onto a single backup medium in a consistent state? Maybe I missed this at the conference, but is backing up your binary log enough? It's not how I would back up any other MySQL server.<br /><p>Netapps may be expensive, but taking a consistent snapshot of all of your data comes in pretty handy.<br /></ol><br /><br />All of that aside, I'm still looking forward to sitting down and playing with NDB.Jay Janssennoreply@blogger.com2tag:blogger.com,1999:blog-5412150273415696914.post-58759169020331027462007-05-08T21:32:00.000-04:002007-05-08T22:26:24.527-04:00Changing masters<a href="http://dev.mysql.com/doc/refman/5.1/en/replication.html">MySQL replication</a> is cool. Almost everyone, when asked what they like best about MySQL, will mention replication. There can be no doubt that many web companies, Yahoo included, owes much to MySQL replication. How else can you scale the reading capacity of your database so easily? <br /><br />It's not all rosy, replication has its problems too. It's very simplistic (I like the term <i>brittle</i>, if any errors happen when executing the SQL, it just stops), it can be inefficient in some places (think UPDATE that does a table scan to change one value, though 5.1 starts to address this with <a href="http://dev.mysql.com/doc/refman/5.1/en/replication-formats.html">row-based replication</a>), and it is single-threaded. <br /><br /><h3>Single Threadedness</h3><br />Let me stop here for a second. A lot of people spend good money on a nice Master DB, multi-core CPU's, plenty of memory, and good drives/RAID, etc. When it comes to their slaves, any old piece of junk will do. If you have low write volume on your master, this can work reasonably well. Your data is protected on your master, and if you lose a slave, no big deal. But once your writes go up, your bottleneck will become your slave(s) getting further and further behind during peak; I promise you. <br /><br />Since the slave is single threaded, it executes the writes that came into the master serially, writes that probably hit the master in parallel. So, you have hundreds or thousands of writes on your master, but your slave can only do half of that and keeps getting further behind because it has no disk throughput, no CPU, and no memory. <span style="font-weight:bold;">Whoops</span>! <br /><br />The lesson? <br /><br /><span style="font-style:italic;">If you need to scale reads and your write volume is growing fast, get the same hardware (or better) on your slaves as on your master.</span><br /><br /><h3>Back on topic...</h3><br />But I digress. That's one problem with replication. The one that I'm more interested in is what to do with your slave when your master goes down. Let us suppose you have a single master and two slaves. If your master crashes (and isn't coming back anytime soon), you can simply start writing to one of your slaves and life goes on. But wait, you were writing to your master, and reading from both of your slaves. You just went from three servers down to one! <br /><br />Your other slave is sitting there doing nothing, but replication is broken, so how can you get it replicating from your new master? You need to find the precise binlog and position on your new master where your slave left off of from the old master.<br /><br />You have a few options:<br /><ol><br /><li><span style="font-weight:bold;">Start binary logging from scratch</span> on your new master before you start writing to it, and start your slave replicating from file 01, position 0. <p><span style="font-style:italic;">Outcome</span>: not too bad, but you better hope that both your new master and your slave were at <b>exactly</b> the same position on the old master for this to work right.<br /><li><span style="font-weight:bold;">Guess</span> at the replication position. <p><span style="font-style:italic;">Outcome</span>: missing data, or tons of replication errors with "duplicate keys". You probably want to guess conservatively, so you'll be sludging through the writes that already happened and hoping things stay consistent. The <a href="http://dev.mysql.com/doc/refman/5.1/en/set-global-sql-slave-skip-counter.html">SQL_SLAVE_SKIP_COUNTER</a> variable will become your friend, but I only recommend skipping one record at a time.<br /><li>Stop your new master and <span style="font-weight:bold;">get a snapshot</span>. <p><span style="font-style:italic;">Outcome</span>: consistent data, but more downtime for your site. A workaround for this would be the various hotbackup tools that should let you take a consistent backup of your master without any downtime. Just make sure you snag the <a href="http://dev.mysql.com/doc/refman/5.1/en/show-master-status.html">master's binlog and position</a> of the instant of your backup.<br /><li><span style="font-weight:bold;">Scan through your new master's binlog</span> and look for the exact position where you left off from on your slave. <a href="http://dev.mysql.com/doc/refman/5.1/en/mysqlbinlog.html">mysqlbinlog</a> and <a href="http://dev.mysql.com/doc/refman/5.1/en/show-binlog-events.html">SHOW BINLOG EVENTS</a> will be your friend. Start your slave from there. <p><span style="font-style:italic;">Outcome</span>: Consistency, no master downtime, no recopying the data to the slave, but a lot of effort, especially if you do this manually.<br /></ol><br /><br />My idea for this, which is a hack, is to create a <span style="font-style:italic;">"heartbeat"</span> table and consistently update it with an incremented field on your master. This could be done via <a href="http://dev.mysql.com/doc/refman/5.1/en/events.html">Events</a>, or cron. It's important that the actual value of the field is in the INSERT or UPDATE statement, so that it is viewable when you scan the binary logs and see the statements.<br /><br />Once that is running and is trickling down to all of your slaves via replication, make sure all of your slaves are doing 'log-bin' and 'log-slave-updates', so theoretically any of them can be promoted to master at any time. When you are trying to troll through the binary logs on the new master, you have a statement that you know will be in there some place at regular intervals, the value of the increment at each binary log position, and you know what the value of the increment is on your slave. <br /><br />With some clever script-hackery you can write a quick and dirty <a href="http://en.wikipedia.org/wiki/Binary_search">binary search</a> (or the <a href="http://en.wikipedia.org/wiki/Search_algorithm">search algorithm</a> of your choice) that will quickly find the exact binlog and position on your new master where the increment value that updated on your slave is located. Once you have that, you can slowly roll forward through the binary log until you find the precise spot that matches the binary log on your slave and you are have your position!<br /><br />This could be done better, without script-hackery and in the MySQL code. I'm not much of a C hacker, but there's talk of something similar coming soon. MySQL cluster implements this same concept in 5.1 called 'epochs' to support <a href="http://dev.mysql.com/doc/refman/5.1/en/mysql-cluster-replication-failover.html">failing over</a> to different master or slave SQL nodes, but it's only for clusters at this point.<br /><br />Next time your master crashes, get that back up first. But, don't forget about your read capacity in your slave pool, and how much your application is suffering without it.Jay Janssennoreply@blogger.com2tag:blogger.com,1999:blog-5412150273415696914.post-69115907567110157832007-05-07T23:40:00.000-04:002007-05-08T11:22:18.178-04:00Will this stick?<a href="http://flickr.com/photos/pborghino/487129394/"><img style="float:right; margin:0 0 10px 10px;cursor:pointer; cursor:hand;width: 160px;" src="http://farm1.static.flickr.com/223/487129394_af00c4aaef.jpg?v=0" border="0" alt="" /></a><br />I've semi-started a lot of blogs, will this one last?<br /><br />My name is Jay Janssen. Amongst other things I am a MySQL guy (tm) at Yahoo! This is a role that I more fell into, rather than one I specifically applied for. My background is Operations, somewhere between Coding/Developing and a System Admin/Colo Tech. I've done both, to some extent, and what's great about Yahoo is finding a lot of opportunity to set stuff up, see it run and fix it when it breaks.<br /><br />MySQL, of course, never breaks. Unfortunately this only really true most often for the blogger, the guy on the hosted website or on an old P-III 1 Ghz running Debian Sarge off of his cable modem. These are the guys who don't know what hit them when <a href='http://digg.com'>digg.com</a> comes around. In reality, the true test of any software is what happens when you beat the snot out of it. What's a better OS, Linux or BSD? What happens under extreme loads to each? Try it and find out.<br /><br />The same holds true for MySQL. There's a lot of great (and free, as in beer) features, <a href='http://dev.mysql.com/doc/refman/5.1/en/mysql-5-1-nutshell.html'>5.1</a> is badass. But when there's a few million in advertising, brand reputation, and a lot of execs breathing down your neck, you need to think hard about stability. Once you slap a thousand page views <i>a second</i> on a website, and all sorts of <i>User-generated</i>, <a href='http://en.wikipedia.org/wiki/Web_2'>Web 2.0</a> features, you'll really find out where the rubber meets the road. <br /><br />If you don't believe me, ask <a href='http://code.google.com/p/google-mysql-tools/'>Google</a>. There was a lot of head-scratching going on at the <a href='http://www.mysqlconf.com/'>MySQL User's Conference</a> when the <a href='http://www.mysqlconf.com/cs/mysqluc2007/view/e_sess/10820'>Google presenters</a> said they used 4.0. <br /><br />So that's what I think about all day. Yahoo has a lot of sites and sub-sites, acquisitions and in-house builds, small sites and big sites. I get to see how they all work and try to make sense out of how we can best make use of MySQL at Yahoo.<br /><br />This blog might end up with lots of posts of general types of MySQL nuggets that I can talk about, or it might not. I'm not that good at keeping blogs up.Jay Janssennoreply@blogger.com0