Archive for the ‘Twitter’ Category

Aug 06

Twitter Broke the Web

Posted by Ryan Toohil in Twitter, Web, Web hosting

With Twitter being down, and feeling the need to spew out characters to the Twinterweb, I opened up trusty MarsEdit and decided to update my good ol’ blog.

That’s when I realized how much Twitter had taken over.

I hadn’t logged into my web interface in a bit, so I had a WordPress update, 4 plugin updates, and a bunch of pending spam. I cleared that out and then started crafting a post in MarsEdit. As I went to post those fateful words, MarsEdit choked on an XML-RPC error complaining about bad content.

I quickly scanned through recent post, looked for bad characters. I used the Googles to try to find an explanation. Finally, I noticed that each page of my blog was spitting out a PHP warning (because, you know, PHP is pretty dumb, and I’m too lazy to have turned off warnings) that it couldn’t download my latest tweets from Twitter.

One quick click of “Disable” and MarsEdit sprung back to life.

Twitter was breaking my site.

At which point, it dawned on me, that at this point, Twitter being down is like having part of the internet’s routing being down. It’s tied into so many systems, and so much traffic/content flows through it, that when it goes kablooey, all that content has to route elsewhere. Which then floods those systems, and they start to struggle and burst at the seams a bit, and then folks overflow into another system, and so on.

Until, the end result, of course, is that Twitter was breaking my site.

Here’s a graphical representation:

Twitter Down

It’s fixed now.

My site, that is, not Twitter, which is still down.

Aug 06

Twitter’s Down! Oh no!

Posted by Ryan Toohil in General, Twitter

Guess I’ll have to post here.

There’s lots of little things that, if tweaked, would just make life a lot nicer . . .

For instance . . .

The Google Calendar sync tool should support syncing Outlook to secondary Google Calendars, not just your primary calendar. That would mean I could drop SyncMyCal which runs in my Outlook-only Parallels Virtual Machine. I’d have a nifty, no-click workflow to sync my Work calendar to my secondary Work Google Calendar, which get sync’d down to iCal and to my iPhone.

Better yet . . .

The iPhone should support syncing both iCal and Outlook calendars. It’s sort of a dumb thing that if you turn on Outlook calendar and contact syncing, that’s the *only* calendars and contacts you get. Why not keep them separate? If Apple added the ability to have your Outlook calendar sit right along side your iCal calendars, then I wouldn’t even need to sync my Work calendar up. I could just accept meeting invites on my phone and have everything work nice and happy.

Oh, and same goes for Contacts. Just let me sync my Exchange contacts as a separate group. Seriously.

Even better . . .

Apple Mail and iCal should build in real Exchange support. Then I could actually not run Outlook in my virtual machine all day. Instead, I could just use Mail and iCal as Exchange clients and have the most simplified workflow of all. Supposedly, this is going to happen in Snow Leopard. That would be awesome.

Making life even sweeter . . .

Google should fix contact management. I love Gmail. I use it for everything. I hate Google’s Contact stuff. It is awful. They tried to make it better. It didn’t work very well. But they’ve got the keys right in their hands … Social Graph. Imagine you gave Google some information about you that they could use some OAuth or other authentication means to determine that it is really you. So you hand them some keys to Flickr and Twitter and (if they can work out their differences), Facebook. Now, Google pulls all that information together and makes a nifty contact for you. Name, picture, email address, home address, work address, phone number, Twitter, Flickr, etc, etc. You only get as much information as the contact allows (if we’re not friends on Facebook, you don’t get my phone # or address).

Now, Google can do all this behind the scenes and give you a little button to “add so-and-so” as a contact. If I add them, they go into my Contacts section. Then, I subscribe to my Google Contacts like they’re an LDAP source or however else they want to get hooked into your setup.

Boom. All of a sudden I’ve got a real contact solution that’s continually updated.

Ok, sure, that’s a lot like what Plaxo promises. Except Plaxo really doesn’t work all that well.

So. When all this happens, I’ll have my calendars all seamlessly syncing together to my computer and to my phone. On top of that, my contacts (work and personal) will all sync nicely as well. And stay up-to-date, thanks to something like Social Graph or the like. Plus, Snow Leopard will mean I don’t have to run a Virtual Machine just to use Outlook (and don’t tell me to use Entourage … it barely works).

With my work life (and personal contacts) working so seamlessly . . .

I’ve got time to kill. I want to get through some movies or TV while I’m out and about.

Give me Netflix Watch Instantly and Hulu on my iPhone! Now that they’re on my XBox and Mac, I’d love them remotely. When I’m out eating lunch or riding in a car, I don’t need HD video. I just need crappy streaming video that will let me knock off an old episode of Newsradio or watch 15 minutes of a crappy movie.

But, if I don’t want to watch something . . .

Sometimes I just want to listen. The fact that the new version of the iPhone firmware lets you download podcasts is great. By why doesn’t it sync my entire podcast list and let me just quickly scan for a new episode of something I listen to? Even better, why not do something similar to Genius and recommend me a podcast I might like?

All that would be very cool.

Oh, and why can’t I sync stuff over bluetooth. That would be good too.

If you’ve been following along at home, you’ll have noticed I work for a web hosting company. We’re pretty big, with hundreds of thousands of customers, and we’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.

But, let’s start with the basics …

Typical Web Host
A typical web host has a “pizza box” 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’s own local storage. Thus, each box is an individual entity running it’s own versions of applications, with it’s own individual issues. Depending on which box you’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’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.

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.

Atypical Web Host
An atypical 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:

  • You’ve got some redundancy
  • You don’t have to scale all of your services as you add new customers

In this model, more like a typical web service than a web host, you’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.

But ….

What does this have to do with Twitter?

See, when you’re running centralized services, you end up with a lot of data sitting in your database, and it’s getting accessed by Y thousands (tens of, hundreds of) users. The “pizza box” model doesn’t have this issue since the data is distributed across thousands of servers. But that’s not feasible in something like Twitter. You don’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’s the power of Twitter.

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.

Let’s take an educated guess at what Twitter’s database schema might be like:

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)

In a nutshell, we’ve got a table that keeps track of our users. We’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:

SELECT t.Tweet FROM Tweets t INNER JOIN Users u USING (UserID);

When Twitter was just a baby, that query’s pretty darn fast. It’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’re not writing a ton of data and updating the indexes should be pretty quick (since Twitter is still just a baby).

Now, we add the functionality of “following” 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.

Keeping track of all the tweets flowing into the user from folks they’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 “this user has this tweet”–it doesn’t matter if the tweet was by that user, or someone they were following, they all go in that mapping table.

Again, when Twitter was a baby, this was all pretty easy and quick:

SELECT t.Tweet FROM Tweets t INNER JOIN UserTweets u USING (TweetID) where UserID = ?;

We’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’re hitting some pretty easily indexed and unique fields.

Of course, I’ve made one assumption here. I’m assuming that, when a user starts following someone, that their tweets start getting associated to my user (the UserTweets table). I’m guessing this is the case since when you start following someone all of their tweets don’t magically show up in your history (conversely, when you stop following someone, they don’t miraculously disappear — I don’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.

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.

Mapping table seems a whole lot more likely. And much faster. Which is kind of how Twitter got big.

Anyway, since we’ve got this nice mapping table, we need to know how to fill it up. If I’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.

Again, this is pretty easy, if you’ve got a handy loop. First, get all the followers:

SELECT FollowerID from Followers where UserID = SCOBLES_ID;

Then, loop through all those folks and post it:

INSERT INTO UserTweets (UserID, TweetID) values (FOLLOWER_ID, SCOBLES_TWEET_ID);

That loop will run as many times as it takes to update Scoble’s followers. None of these queries are complex. They’re all easily indexible. They should all be pretty fast. When Twitter is still in baby-state.

But now Twitter is growing. It’s a toddler. It’s got many many users and some of them have loads of followers. Things are starting to slow down. Twitter’s parents look at it and say “Well, one obvious reason you’re starting to slow down is that the database is starting to lock. See, now that we’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’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’re going to teach you to read and write at the same time.”

And that’s what they do.

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’ve brought in MySQL experts to help us with that immediately. (blog.twitter.com)

Now we’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.

Twitter grows into the tween stage. And it’s not pretty. Databases are constantly crashing. Things are slow. What the hell? Didn’t we just fix this?

Well, no. We just hid the problem. Replication isn’t a pretty solution. MySQL replication is flawed. It’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).

Big users (those with lots of followers) cause more load, since now we’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’s not good for our database.

NOTE: The Twitter folks claim they don’t copy the tweet around for each follower. I’m sure they don’t. But they don’t say anything about copying the ID around (which, as I’ve stated, makes a good amount of sense if you were architecting Twitter 18 months ago).

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. (scobleizer.com

Also, it’s what Dare Obasanjo posits, building on what Israel says on the Assetbar blog, and they’re both way smarter than me.

And you can’t really add a second master database. So writes are going to be as fast as our server can handle them.

This is where we are today (or at least recently). There’s enough users on Twitter that updates are probably starting to slow since the tables and indexes are so large that there’s just not much MySQL can do. Reads are slow because replication is fragile.

What do we do?
Without completely re-architecting things, what can we do? There’s a couple of things, right off of my novice brain:

  • Cache, Cache, Cache
  • Make the database smaller

Cache, Cache, Cache
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’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’d cut down on a query that gets run every time a user loads Twitter.

That’s *a lot* of queries.

It sounds like the folks at Twitter are already heading down this path.

A: We’ve mitigated much of this issue by using memcached, as many sites do, to minimize our reliance on a database. (blog.twitter.com)

Make the database smaller
This isn’t something it sounds like Twitter wants to try. Basically, this is sharding. You take all the users with a UserID < 100000 and put them in one database, all the users with a UserID > 100000 and < 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’re looking for (which you can cache!).

This makes your selects and inserts fast again, since the tables and indexes are smaller. But now you’ve got to manage the multiple shards, adjust them when they get too big, and deal with the added layer of abstraction.

It’d help, but it’s a big task.

If you’ve been reading my blog, you might be asking yourself:

Why Does This Sound Familiar?
Why does this sound familiar? Because our unknownish web host ran into similar issues. See my post Thoughts on MySQL Scalability From a Certified MySQL Moron from February.

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’s completely reasonable. Frustrating, but reasonable. As Michael Kowalchik (founder of Grazr) states:

As a startup there’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’t help you. (mathewingram.com

I’m curious about Twitter’s solutions both as a user and as an engineery web dork at a company hitting the same problems. We’re starting to use memcached, looking at using smarter non-table locking databases, and thinking about utilizing the file system more than a database.

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’ll have to bite the bullet and use database shards. They’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’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’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.

Apr 09

Twitter

Posted by Ryan Toohil in Social Networking, Twitter, Web 2.0

I didn’t know what to make of Twitter. If you’ve sort of missed out on what it is, figure a cross between blogging and updating your IM away message. It’s all about sort of micro-updates about what you’re doing. It’s limited to 140 characters, which forces you to stay concise, and allows you to do updates via a text message, or the web, or any sort of app you want to build that interfaces with Twitter.

Then, people can “follow” you or “friend” you, just like on pretty much any social network, and get updated to what you’re doing. People can have updates sent to their phone, IM, or just over the web (HTML or RSS). It sounds so egotistic and silly: who would want to know what you’re doing all the time? Do people care that much?

It sounded so stupid. I checked out some of the more famous Twitterings (?) online and I wasn’t sold. Then, I went to Vegas. I wanted to send notes back to people at home–you know, just silly stuff. I signed up for Twitter and gave it a shot. I really haven’t stopped using it since. The ability to just send a text message off to a phone number and have it update a little blog is pretty powerful, and can (depending on how many people use Twitter in your little network) be pretty helpful if you’re looking for some quick info from your friends.

I can think of a whole bunch of other uses (some of which I’m going to try to implement at work … we’ll see how that goes), but, fundamentally, it can be a useful way to get some information out when you don’t have enough in your brain to do anything too big (like update your blog …).

I’m guessing it’ll fade out, but Twitter is a handy fad. I’d almost like to sync my Twitter status to my IM status/info, so people can sort of follow me however they’d like.

The other good thing about Twitter is that it’s one way. It’s only outward communication (though you can use it to sort of carry on a conversation) which is nice, particularly these days when there’s just so much data flying at you (for instance, at work, I’m on 4 IM networks, plus two email accounts, plus normal web stuff). Twitter’s unidirectional (monodirectional?): I just spew and I don’t have to listen back. Not very friendly, but sometimes it’s what you need.

I’m going to try to add my Twitter feed to my blog (look that way —->). We’ll see how it goes.