<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
		xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
	xmlns:media="http://search.yahoo.com/mrss/"
>

<channel>
	<title>That Not So Fresh Feeling &#187; MySQL</title>
	<atom:link href="http://blog.ryantoohil.com/category/mysql/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.ryantoohil.com</link>
	<description>Stuff.</description>
	<lastBuildDate>Thu, 02 Feb 2012 20:04:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<copyright>2006-2007 </copyright>
	<managingEditor>ryan@ryantoohil.com (That Not So Fresh Feeling)</managingEditor>
	<webMaster>ryan@ryantoohil.com (That Not So Fresh Feeling)</webMaster>
	<ttl>1440</ttl>
	<image>
		<url>http://blog.ryantoohil.com/wp-content/plugins/podpress/images/powered_by_podpress.jpg</url>
		<title>That Not So Fresh Feeling</title>
		<link>http://blog.ryantoohil.com</link>
		<width>144</width>
		<height>144</height>
	</image>
	<itunes:subtitle></itunes:subtitle>
	<itunes:summary>Just another place for just another jackass to rant about sports, politics, entertainment, technology, and life.</itunes:summary>
	<itunes:keywords></itunes:keywords>
	<itunes:category text="Society &#38; Culture" />
	<itunes:author>That Not So Fresh Feeling</itunes:author>
	<itunes:owner>
		<itunes:name>That Not So Fresh Feeling</itunes:name>
		<itunes:email>ryan@ryantoohil.com</itunes:email>
	</itunes:owner>
	<itunes:block>no</itunes:block>
	<itunes:explicit>no</itunes:explicit>
	<itunes:image href="http://blog.ryantoohil.com/wp-content/plugins/podpress/images/powered_by_podpress_large.jpg" />
		<item>
		<title>Hey Twitter! Your problems are my problems!</title>
		<link>http://blog.ryantoohil.com/2008/06/hey-twitter-your-problems-are-my-problems.php</link>
		<comments>http://blog.ryantoohil.com/2008/06/hey-twitter-your-problems-are-my-problems.php#comments</comments>
		<pubDate>Tue, 03 Jun 2008 03:33:24 +0000</pubDate>
		<dc:creator>Ryan Toohil</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[Web hosting]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://blog.ryantoohil.com/?p=275</guid>
		<description><![CDATA[If you&#8217;ve been following along at home, you&#8217;ll have noticed I work for a web hosting company. We&#8217;re pretty big, with hundreds of thousands of customers, and we&#8217;ve got some interesting operation efficiencies/differences from traditional hosts that give us some fun advantages. Those same advantages come at the cost of having to do some interesting <a href='http://blog.ryantoohil.com/2008/06/hey-twitter-your-problems-are-my-problems.php'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve been following along at home, you&#8217;ll have noticed I work for a web hosting company. We&#8217;re pretty big, with hundreds of thousands of customers, and we&#8217;ve got some interesting operation efficiencies/differences from traditional hosts that give us some fun advantages. Those same advantages come at the cost of having to do some interesting thinking about scalability and performance. This is where the stuff currently going on at Twitter starts to sound *really* familiar.</p>
<p>But, let&#8217;s start with the basics &#8230;</p>
<p><strong>Typical Web Host</strong><br />
A typical web host has a &#8220;pizza box&#8221; architecture. They pile a bunch of servers in a rack in a data center. Each server is running some MySQL, some Apache (or ISS), some email app (Exim or Qmail), and some FTP. Each server is usually running another instance of Apache to run the customer control panel. The box usually runs off it&#8217;s own local storage. Thus, each box is an individual entity running it&#8217;s own versions of applications, with it&#8217;s own individual issues. Depending on which box you&#8217;re on, you might have MySQL slowness, or Apache slowness, or disk slowness, all depending on what your neighbors are doing. Rolling out upgrades becomes a bit more arduous as you&#8217;ve got to upgrade each box. This is decidedly old-school, but very common and works fairly well. This architecture only starts to be a problem as you grow; each time you add X customers (where X is the number each box can support), you have to add a new box. Each time you add a new box, you need more power, space, backup storage, etc. That gets hard to scale.</p>
<p>So, then there was the idea of not using local box storage, but instead using networked storage. Now you can just add more disks without having to add entirely new boxes, and backups become less of an issue. This means you can grow a little bit more without having to add more boxes.</p>
<p><strong>Atypical Web Host</strong><br />
An <strong>a</strong>typical web host (like us and some of our competitors) might do things a bit different. For instance, rather than having a single LAMP (Linux-Apache-MySQL-PHP/Perl/Python/(P)Ruby) stack per set of customers (on a box), you build a pool of boxes that can all serve up services for a customer. This means that:</p>
<ul>
<li>You&#8217;ve got some redundancy</li>
<li>You don&#8217;t have to scale all of your services as you add new customers</li>
</ul>
<p>In this model, more like a typical web service than a web host, you&#8217;ve got pools of servers that perform certain tasks, and customer data is kind of abstracted away to all just live on big storage arrays. Things scale much easier and more cost effectively.</p>
<p>But &#8230;.</p>
<p><strong>What does this have to do with Twitter?</strong></p>
<p>See, when you&#8217;re running centralized services, you end up with a lot of data sitting in your database, and it&#8217;s getting accessed by Y thousands (tens of, hundreds of) users. The &#8220;pizza box&#8221; model doesn&#8217;t have this issue since the data is distributed across thousands of servers. But that&#8217;s not feasible in something like Twitter. You don&#8217;t only want to be able to see tweets from people who are on the same server as you. You want to see tweets from everybody. That&#8217;s the power of Twitter.</p>
<p>Having lots of data in a single database instance solves that problem. But it introduces a whole new set of issues: database locking, slave synchronization, disk performance.</p>
<p>Let&#8217;s take an educated guess at what Twitter&#8217;s database schema might be like:</p>
<blockquote>
<pre>Table: Users
UserID     int(14) autoincrement
Username   varchar(50)

Table: Tweets
TweetID	   int(14) autoincrement
UserID     int(14)
Tweet      varchar(140)
DateStamp  datetime

Table: Followers
UserID     int(14)
FollowerID int(14)

Table: UserTweets
UserID     int(14)
TweetID    int(14)</pre>
</blockquote>
<p>In a nutshell, we&#8217;ve got a table that keeps track of our users. We&#8217;ve got a table of tweets, which maps back to give us a user based off of the UserID. Right there, you get a pretty easy query to get all of the tweets for a user:</p>
<blockquote>
<pre>SELECT t.Tweet FROM Tweets t INNER JOIN Users u USING (UserID);</pre>
</blockquote>
<p>When Twitter was just a baby, that query&#8217;s pretty darn fast. It&#8217;ll give you all of the Tweets written by any user in particular. Reads will be fast, since UserID will be indexed and unique. Writes will be fast, since we&#8217;re not writing a ton of data and updating the indexes should be pretty quick (since Twitter is still just a baby).</p>
<p>Now, we add the functionality of &#8220;following&#8221; another Twitter user (good thing we thought ahead and added that to our schema!) Now a user can follow another user, which just simply sticks a row in the table with the id of the user and the id of who they are following.</p>
<p>Keeping track of all the tweets flowing into the user from folks they&#8217;re following is easy, since we planned ahead. We made a table that lets us map a bunch of tweets to a user. Every time we add a tweet, we stick a row in that says &#8220;this user has this tweet&#8221;&#8211;it doesn&#8217;t matter if the tweet was by that user, or someone they were following, they all go in that mapping table.</p>
<p>Again, when Twitter was a baby, this was all pretty easy and quick:</p>
<blockquote>
<pre>SELECT t.Tweet FROM Tweets t INNER JOIN UserTweets u USING (TweetID) where UserID = ?;</pre>
</blockquote>
<p>We&#8217;re going to get back all the tweets assigned to that user, whether written by the user or someone they are following. Simple and fast, since again, we&#8217;re hitting some pretty easily indexed and unique fields.</p>
<p>Of course, I&#8217;ve made one assumption here. I&#8217;m assuming that, when a user starts following someone, that their tweets start getting associated to my user (the UserTweets table). I&#8217;m guessing this is the case since when you start following someone all of their tweets don&#8217;t magically show up in your history (conversely, when you stop following someone, they don&#8217;t miraculously disappear &#8212; I don&#8217;t think so, at least). Either Twitter sticks stuff in the mapping table (my guess), or they go look up the date you started following each user, and they build that index on the fly.</p>
<p>In other words, the alternative version is that Twitter, when you view a page that displays your tweets along with those of folks you follow, would have to go lookup the date when you started following someone, then find all of their tweets after that date, bring them together, and put them in order.</p>
<p>Mapping table seems a whole lot more likely. And much faster. Which is kind of how Twitter got big.</p>
<p>Anyway, since we&#8217;ve got this nice mapping table, we need to know how to fill it up. If I&#8217;m following Robert Scoble (like the rest of Western Civilization), every time he writes something, it needs to make it to UserTweets associated with me.</p>
<p>Again, this is pretty easy, if you&#8217;ve got a handy loop. First, get all the followers:</p>
<blockquote>
<pre>SELECT FollowerID from Followers where UserID = SCOBLES_ID;</pre>
</blockquote>
<p>Then, loop through all those folks and post it:</p>
<blockquote>
<pre>INSERT INTO UserTweets (UserID, TweetID) values (FOLLOWER_ID, SCOBLES_TWEET_ID);</pre>
</blockquote>
<p>That loop will run as many times as it takes to update Scoble&#8217;s followers. None of these queries are complex. They&#8217;re all easily indexible. They should all be pretty fast. When Twitter is still in baby-state.</p>
<p>But now Twitter is growing. It&#8217;s a toddler. It&#8217;s got many many users and some of them have loads of followers. Things are starting to slow down. Twitter&#8217;s parents look at it and say &#8220;Well, one obvious reason you&#8217;re starting to slow down is that the database is starting to lock. See, now that we&#8217;ve got some data, lookups are taking a little bit longer, and inserts are taking a little bit longer, and when they happen at the same time, we don&#8217;t want data to get out of sync, so the database locks up. When it locks up rapidly enough and often enough, we run out of threads and our database likes to go down. We&#8217;re going to teach you to read and write at the same time.&#8221;</p>
<p>And that&#8217;s what they do.</p>
<blockquote><p>We currently use one database for writes with multiple slaves for read queries. As many know, replication of MySQL is no easy task, so we&#8217;ve brought in MySQL experts to help us with that immediately. (<a href="http://blog.twitter.com/2008/05/its-not-rocket-science-but-its-our-work.html">blog.twitter.com</a>)</p></blockquote>
<p>Now we&#8217;ve got a master database, where all of our writes (inserts) go, and a couple of replicas where all of our reads (selects) go. Perfect. Things are fast again. Users are happy. Twitter is moving along.</p>
<p>Twitter grows into the tween stage. And it&#8217;s not pretty. Databases are constantly crashing. Things are slow. What the hell? Didn&#8217;t we just fix this?</p>
<p>Well, no. We just hid the problem. Replication isn&#8217;t a pretty solution. MySQL replication is flawed. It&#8217;s not instantaneous. It can fall behind. Further, it breaks. A lot. And when it breaks, we lose half of our read capacity, which then overloads the other server, and everything goes down. Resynchronizing can be painful. Adding more replicas helps, to a point (since each replica adds a little bit of overhead to the master).</p>
<p>Big users (those with lots of followers) cause more load, since now we&#8217;ve got to do a big lookup to get thousands of rows (followers) and then do thousands of inserts every time one of those users posts. That&#8217;s not good for our database.</p>
<p><em><strong>NOTE:</strong> The Twitter folks claim they don&#8217;t copy the tweet around for each follower. I&#8217;m sure they don&#8217;t. But they don&#8217;t say anything about copying the ID around (which, as I&#8217;ve stated, makes a good amount of sense if you were architecting Twitter 18 months ago).</em></p>
<p><em></p>
<blockquote><p>13:03: I ask about how Twitterâ€™s engine works internally and I ask if Tweets are copied for each Twitter message. For instance, do my Tweets get copied 23,000 times? EV answers that the service does NOT do that. (<a href="http://scobleizer.com/2008/05/31/clearing-the-air-with-twitter/">scobleizer.com</a></p></blockquote>
<p></em></p>
<p><em>Also, it&#8217;s what Dare Obasanjo <a href="http://www.25hoursaday.com/weblog/2008/05/23/SomeThoughtsOnTwittersAvailabilityProblems.aspx">posits</a>, building on what <a href="http://assetbar.wordpress.com/2008/02/08/twitter-proxy-any-interest/">Israel says on the Assetbar blog</a>, and they&#8217;re both way smarter than me.<br />
</em></p>
<p>And you can&#8217;t really add a second master database. So writes are going to be as fast as our server can handle them.</p>
<p>This is where we are today (or at least recently). There&#8217;s enough users on Twitter that updates are probably starting to slow since the tables and indexes are so large that there&#8217;s just not much MySQL can do. Reads are slow because replication is fragile.</p>
<p><strong>What do we do? </strong><br />
Without completely re-architecting things, what can we do? There&#8217;s a couple of things, right off of my novice brain:</p>
<ul>
<li>Cache, Cache, Cache</li>
<li>Make the database smaller</li>
</ul>
<p><em>Cache, Cache, Cache</em><br />
The less we have to go to the database, the better things perform, and the better they scale. There are lots of things we can cache, things which don&#8217;t update very often. For instance, the list of followers. If we stored the follower list in memory for each user (as it was loaded), we&#8217;d cut down on a query that gets run every time a user loads Twitter.</p>
<p>That&#8217;s *a lot* of queries.</p>
<p>It sounds like the folks at Twitter are already heading down this path.</p>
<blockquote><p>A: We&#8217;ve mitigated much of this issue by using memcached, as many sites do, to minimize our reliance on a database. (<a href="http://blog.twitter.com/2008/05/its-not-rocket-science-but-its-our-work.html">blog.twitter.com</a>)</p></blockquote>
<p><em>Make the database smaller</em><br />
This isn&#8217;t something it sounds like Twitter wants to try. Basically, this is sharding. You take all the users with a UserID &lt; 100000 and put them in one database, all the users with a UserID &gt; 100000 and &lt; 200000 and put them in a second database, and so on. Then you have a master lookup that lets you know where to find the data you&#8217;re looking for (which you can cache!).</p>
<p>This makes your selects and inserts fast again, since the tables and indexes are smaller. But now you&#8217;ve got to manage the multiple shards, adjust them when they get too big, and deal with the added layer of abstraction.</p>
<p>It&#8217;d help, but it&#8217;s a big task.</p>
<p>If you&#8217;ve been reading my blog, you might be asking yourself:</p>
<p><strong>Why Does This Sound Familiar?</strong><br />
Why does this sound familiar? Because our unknownish web host ran into similar issues. See my post <a href="http://blog.ryantoohil.com/2008/02/thoughts-on-mysql-scalability-from-a-certified-mysql-moron.php">Thoughts on MySQL Scalability From a Certified MySQL Moron</a> from February.</p>
<p>All of the problems Twitter is facing seem to be directly related to building the service in a logical fashion, but not forseeing the problems that massive growth would have on the system (a single tweet spawning tens of thousands of database updates). Quite frankly, it&#8217;s completely reasonable. Frustrating, but reasonable. As Michael Kowalchik (founder of <a href="http://www.grazr.com">Grazr</a>) states:</p>
<blockquote><p>As a startup there&#8217;s only so much energy you have, and you must apportion your resources carefully. The truth is, we like to talk about scaling, but without steady growth and something people find compelling, all the scaling in the world won&#8217;t help you. (<a href="http://www.mathewingram.com/work/2008/06/01/twitter-and-the-importance-of-architecture/#comment-567723">mathewingram.com</a></p></blockquote>
<p>I&#8217;m curious about Twitter&#8217;s solutions both as a <a href="http://twitter.com/ryant">user</a> and as an engineery web dork at a company hitting the same problems. We&#8217;re starting to use memcached, looking at using smarter non-table locking databases, and thinking about utilizing the file system more than a database.</p>
<p>Twitter has some technological mountains to climb to be able to scale to support the rate at which they are growing. I think that, sooner or later, they&#8217;ll have to bite the bullet and use database shards. They&#8217;re also likely going to have to build out memcached clusters large enough to allow them to cache nearly every thing about a user. That&#8217;s loads of data (gigs and gigs and gigs), but machines and memory are cheap for a company like Twitter, and the payoff will be worth it. I have no idea if Twitter&#8217;s growth is slowing during these performance issues, but throwing some funds behind a massive memcached cluster would be well spent now, and would surely be useful even after any sort of re-architecture.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ryantoohil.com/2008/06/hey-twitter-your-problems-are-my-problems.php/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>phpBB is a Piece of Feces and May Be the Bane of My Existance</title>
		<link>http://blog.ryantoohil.com/2008/04/phpbb-is-a-piece-of-feces-and-may-be-the-bane-of-my-existance.php</link>
		<comments>http://blog.ryantoohil.com/2008/04/phpbb-is-a-piece-of-feces-and-may-be-the-bane-of-my-existance.php#comments</comments>
		<pubDate>Sun, 20 Apr 2008 20:20:46 +0000</pubDate>
		<dc:creator>Ryan Toohil</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Web hosting]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://blog.ryantoohil.com/?p=267</guid>
		<description><![CDATA[I haven&#8217;t been posting very much recently because I&#8217;ve sadly been working my tail off. I very much enjoy what I do, but there are just weeks (months &#8230; years &#8230;) where it&#8217;s just a non-stop grind to get everything done. Recently, it&#8217;s been working on launching a new VPS platform, but that was interrupted <a href='http://blog.ryantoohil.com/2008/04/phpbb-is-a-piece-of-feces-and-may-be-the-bane-of-my-existance.php'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>I haven&#8217;t been posting very much recently because I&#8217;ve sadly been working my tail off. I very much enjoy what I do, but there are just weeks (months &#8230; years &#8230;) where it&#8217;s just a non-stop grind to get everything done. Recently, it&#8217;s been working on launching a new VPS platform, but that was interrupted by a breakdown of our customer MySQL infrastructure.</p>
<p>Our setup is a bit different than most. Since we don&#8217;t run a typical box-by-box web hosting architecture, we don&#8217;t simply have a thousand boxes with each one running Apache and MySQL. Instead, we have a really robust pooled architecture for everything <strong>except</strong> MySQL, which just isn&#8217;t something that&#8217;s very <em>poolable</em>. For MySQL, we&#8217;ve got some big boxes with a bunch of memory and some fast disks that handle our MySQL load. But, slowly over time, performance had degraded.</p>
<p>When you&#8217;d hop onto a box and look at the transactions per second or number of queries, nothing looked terribly out of the ordinary. Yet the load would be huge, and performance would be pretty bad. Our team brought up some new boxes, shuffled customers between them to even the load out, moved our backup processing onto the hot spare replicated boxes (to reduce even more load on the disks) and things were better.</p>
<p>But they weren&#8217;t better enough. (I know, awesome English, eh?)</p>
<p>We started just watching the processlist, looking for the culprit. And after about 5 minutes, it was obvious.</p>
<p>Motherfrakking phpBB spam.</p>
<p>phpBB is written in a really shitty way. Not the forum part, necessarily, which works when it&#8217;s not being exploited. But the search part is awful. For every word in every post (unless you&#8217;ve got a smart list of words to ignore), it throws entries in some big tables so that when you search for &#8220;foobar&#8221;, it can tell you every post that contains that work. That&#8217;s a fine design for a small board with a tiny amount of traffic. But as your board grows, even legitimately, that table can become hundreds of thousands of rows long (or more!) and inserts and selects can become extremely slow.</p>
<p>It&#8217;s ten times worse when the only thing putting content into is spammers who are just flooding it with huge wordlists multiple times per second. Now, all of a sudden, you&#8217;ve got this single board showing up in your processlist five times, with each entry running for 30, 40, 50 seconds. One of those boards can cause some extra load on a server.</p>
<p>When you&#8217;ve got ten or twenty, it can bring the server to a halt. Literally. I popped onto a server where the load was near <em>10</em>. I turned off 40 phpBB boards getting spammed. The load dropped to less than <em>1</em> and stayed there.</p>
<p>After some quick thinking, we identified a bunch of boards that were getting spammed and turned them off. One of our engineers built a brilliant little monitoring script that can identify phpBB boards in the processlist and shut them off if they show up at a high enough frequency with those awful queries (you know then when you see them, believe me). All told, we&#8217;ve turned off maybe 12k boards in the past 2 weeks, and haven&#8217;t heard a single complaint.</p>
<p>Why? Because these boards were setup by users who then forgot about them. And there they sat, for months or years, collecting spam, draining resources. Basic negligence on the part of users caused a huge server load, which then caused those same customers to call in and complain.</p>
<p>It feels like we&#8217;ve got this mostly under control, except for the user side. We need to figure out a way to get people to realize that the things they install on their site can be exploited and lead to security issues (on their site), performance issues (for everyone), and can suck up the resources they pay for.</p>
<p>But yeah, it sucks when you work about an 80 hour week because people forget about their phpBB install, and the folks who wrote phpBB decided that they&#8217;d build the most stupidly designed search setup of all time.</p>
<p>So, when on April 8th my Twitter looked like this:<br />
<img src="http://www.ryantoohil.com/images/Twitter___ryant_-_Mozilla_Firefox_3_Beta_5_(Build_2008032619)-20080420-161832.jpg" alt="php is feces" title="php is feces" /></p>
<p>now you know why.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ryantoohil.com/2008/04/phpbb-is-a-piece-of-feces-and-may-be-the-bane-of-my-existance.php/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Word To The Wise</title>
		<link>http://blog.ryantoohil.com/2008/03/word-to-the-wise.php</link>
		<comments>http://blog.ryantoohil.com/2008/03/word-to-the-wise.php#comments</comments>
		<pubDate>Tue, 18 Mar 2008 02:57:08 +0000</pubDate>
		<dc:creator>Ryan Toohil</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://blog.ryantoohil.com/2008/03/word-to-the-wise.php</guid>
		<description><![CDATA[Always, Always, Always start your MySQL update query with the limit and the where clause, even if you&#8217;ve done the query a thousand times before. The one time you screw it up, it will be on a Friday evening, when you&#8217;re just trying to do one last thing for the day before you head home, <a href='http://blog.ryantoohil.com/2008/03/word-to-the-wise.php'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>Always, <em>Always</em>, <strong><em>Always</em></strong> start your MySQL update query with the limit and the where clause, even if you&#8217;ve done the query a thousand times before.</p>
<p>The one time you screw it up, it will be on a Friday evening, when you&#8217;re just trying to do one last thing for the day before you head home, and when most of the office has gone home.</p>
<p>Oh, and it&#8217;ll be a database of 30 million DNS records and you&#8217;ll manage to wipe them all out.</p>
<p>Yep, that&#8217;s why I did last Friday. I was stupid and working too fast, and managed to hit the &#8216; and Enter in quick succession, as they sit next to eat other on the keyboard. Caught my mistake when it wiped out &#8220;only&#8221; 13k records on the master database, but the query had already replicated out and managed to take out the slaves.</p>
<p>My saving grace is that we&#8217;ve got a couple of great folks in our sysadmin group, and they helped me get things back from a backup pretty quickly.</p>
<p>But yeah, that was scary. And not fun. Now I&#8217;ve been on both sides. I&#8217;ve helped people recover from a disaster &#8230; and I&#8217;ve caused a huge disaster. Go me.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ryantoohil.com/2008/03/word-to-the-wise.php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Thoughts on MySQL Scalability From a Certified MySQL Moron</title>
		<link>http://blog.ryantoohil.com/2008/02/thoughts-on-mysql-scalability-from-a-certified-mysql-moron.php</link>
		<comments>http://blog.ryantoohil.com/2008/02/thoughts-on-mysql-scalability-from-a-certified-mysql-moron.php#comments</comments>
		<pubDate>Wed, 13 Feb 2008 22:43:35 +0000</pubDate>
		<dc:creator>Ryan Toohil</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Web hosting]]></category>

		<guid isPermaLink="false">http://blog.ryantoohil.com/2008/02/thoughts-on-mysql-scalability-from-a-certified-mysql-moron.php</guid>
		<description><![CDATA[While I sit here and wait for myisamchk to finish and tell me that the table that various folks have spent the better part of the day trying to restore is either healthy or once again dead (how&#8217;s that for a run-on sentence), I wanted to dump out some of the things we&#8217;ve done to <a href='http://blog.ryantoohil.com/2008/02/thoughts-on-mysql-scalability-from-a-certified-mysql-moron.php'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>While I sit here and wait for myisamchk to finish and tell me that the table that various folks have spent the better part of the day trying to restore is either healthy or once again dead (how&#8217;s that for a run-on sentence), I wanted to dump out some of the things we&#8217;ve done to try to make our MySQL backend scale. It&#8217;s not been pretty, but given that it&#8217;s strung together with some Perl, some MySQL, and a bunch of paper clips, I think the folks around me have proven themselves brilliant (I just sit around and pretend to know what&#8217;s going on).</p>
<p>Oh, and this is all without a cluster. That&#8217;s probably the next step. And &#8220;Oh: part 2,&#8221; myisamchk finished checking 28 million rows. All is good. I&#8217;ve copied the data over to the main server and brought it up and everything is happy. Back to MySQL scalability &#8230;</p>
<p><strong>First issue:</strong> <em>We&#8217;ve got too much data</em></p>
<p>This one was easy to solve. We got rid of it. Sort of. We started archiving off data that we no longer needed chronologically. Old support incidents, logging, anything that had a timestamp that we aren&#8217;t looking at gets sliced off into an archive so that the main tables can be as tidy and fast as we can get them. Which for us is like 8GB of data and not fast at all. But it&#8217;s better than 12GB of data.</p>
<p><strong>Second Issue:</strong> <em>We&#8217;ve got too many connections</em></p>
<p>When you&#8217;re small(ish), it makes sense to throw a bunch of dbs on the same server. As you grow, those connections start to swamp MySQL. MySQL starts to get all panicked, and it doesn&#8217;t know how to handle all of the people asking for data, so it starts to get sloppy about closing old handles. Then it&#8217;s basically like thermal runaway in a transistor. The server can&#8217;t close old connections, new ones open up, adding more overhead, and all of a sudden your nice server has 5000 open connections and is hosed. Again, this was a pretty easy one to fix. Bring up a new box, move some databases it to it, and hope that you&#8217;ve built your code layer to make that swap pretty easy (ours was). Presto. Now both of your servers are happy.</p>
<p><strong>Third Issue:</strong> <em>We lock up the damn tables all the time</em></p>
<p>We&#8217;ve got a lot of customers who are constantly accessing their sites. We&#8217;ve got nearly 1000 support agents across the globe who are using our tools to look at customer configuration to make sure there&#8217;s no issues. This puts a whole chunk of load and repetitive queries on the database. That&#8217;s easily handled.</p>
<p>Except when you add in a bunch of data updates. Agents, customers, new signups adding and editing data in the database. All of a sudden those hundred pending SELECT statements are stuck because one big select locked the data when an UPDATE came in. Now you&#8217;ve got a bunch of web users who think your stuff is slow and/or broken. We&#8217;ve tried to attack this in a few ways:</p>
<ol>
<li>Fix your queries &#8212; We watch our slow queries and try to make them faster. We look at our most often called queries and try to make them faster. Sounds simple, but bad queries are the biggest cause of problems.</li>
<li>Add indexes &#8212; This goes with &#8220;fixing&#8221; queries. Add indexes and make sure you&#8217;re queries use them.</li>
<li>Perform less queries &#8212; Can you cache your data? Can you make less queries and do more in your language of choice? Can you make your users smarter (maybe without them knowing) about when they need to request data? Do it. The less queries you have, the more likely you won&#8217;t lock things up.</li>
<li>Split your reads and your writes &#8212; If you can split your reads and writes at the code layer, then you can shuttle reads off to one (or more boxes) and writes off to the primary box, and you should lock up a lot less. We accomplished this by having a couple of boxes replicate the main database, and having one of our smart engineers subclass Perl::DBI to look for SELECT statements and swap the database handle over to the read replica. It helps more than you might thing (but it&#8217;s not a silver bullet).</li>
</ol>
<p>Most of this sounds like common sense. It is. But it still matters. We&#8217;re trying to do a lot with a little, and every ounce of performance you can squeeze out matters, when your users are super demanding and will use any slowness as an excuse.</p>
<p>There are some other things we should and probably will try:</p>
<ul>
<li>Denormalization to bring data back together and cut down on costly joins</li>
<li>Sharding to split our data up into smaller chunks and this cut down on long table scans and huge indexes</li>
<li>A real MySQL cluster to optimize reads and writes and spread traffic out to many nodes</li>
</ul>
<p>I wish I knew more. I&#8217;m still barely up the curve compared to some of the engineers and admins I work with. Thankfully, they&#8217;ve been able to keep our many million row tables (and many GB data and index files) humming along with few interruptions.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ryantoohil.com/2008/02/thoughts-on-mysql-scalability-from-a-certified-mysql-moron.php/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Filing Away for Future Use: MySQL Stupidity</title>
		<link>http://blog.ryantoohil.com/2007/10/filing-away-for-future-use-mysql-stupidity.php</link>
		<comments>http://blog.ryantoohil.com/2007/10/filing-away-for-future-use-mysql-stupidity.php#comments</comments>
		<pubDate>Fri, 12 Oct 2007 16:38:14 +0000</pubDate>
		<dc:creator>Ryan Toohil</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Web hosting]]></category>

		<guid isPermaLink="false">http://blog.ryantoohil.com/2007/10/filing-away-for-future-use-mysql-stupidity.php</guid>
		<description><![CDATA[We had a database issue the other day. We were losing records in our DNS database. It turned out the table was corrupt and inserts were failing. We used MySQL&#8217;s REPLACE INTO to update the table. So, in essence, this is what was happening: try to insert domain oh, I can&#8217;t insert? that means it&#8217;s <a href='http://blog.ryantoohil.com/2007/10/filing-away-for-future-use-mysql-stupidity.php'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>We had a database issue the other day. We were losing records in our DNS database. It turned out the table was corrupt and inserts were failing.</p>
<p>We used MySQL&#8217;s REPLACE INTO to update the table. So, in essence, this is what was happening:</p>
<blockquote><p>try to insert domain<br />
oh, I can&#8217;t insert? that means it&#8217;s already there<br />
delete the existing record<br />
try to insert again</p></blockquote>
<p>On a happy table, that works just fine. The INSERT fails initially because the table is corrupt, but REPLACE INTO thinks it is because the record exists. So it DELETEs it. Then it tries to futilely INSERT the replacement record, which obviously fails.</p>
<p>Now we&#8217;re down one record. Oops.</p>
<p>Thankfully, it was easy for us to fix the table and replay the changes and get everything back to normal. Yay. Filing this one away in my brain in case I run into it again.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ryantoohil.com/2007/10/filing-away-for-future-use-mysql-stupidity.php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

