Podcast Search

Developing Perspective

#126: Scaling.

Select a time to play from there. The line being spoken is highlighted as the audio plays.

0:00:00Hello and welcome to Developing Perspective. Developing Perspective is a podcast discussing

0:00:03news of note and iOS development, Apple and the like. I'm your host, David Smith. I'm

0:00:07an independent iOS and Mac developer based in Herndon, Virginia. This is show number

0:00:11126 and today is Thursday, May 23rd. Developing Perspective is never longer than 15 minutes,

0:00:16so let's get started.

0:00:17All right, so I'm going to talk about scale today. And more specifically, probably the

0:00:23verb scaling. So this is something that I've been obviously spending a lot of time working

0:00:28on in Feed Wrangler is to try and have a reasonable approach

0:00:32to increasing capacity for back end things.

0:00:36And there's some amount of front end work that goes into this,

0:00:39but a lot of it is just about how do you plan for,

0:00:43and then how do you actually execute

0:00:45on having something that can scale

0:00:47to a large number of users.

0:00:49There's kind of a couple of phases that this goes through,

0:00:51and so I'll kind of walk through this process.

0:00:53And this is kind of my experience.

0:00:54I'm not an expert at it.

0:00:55And if anything, the experience of Feed Wrangler

0:00:57definitely gave me a lot of respect for the people who do, who work for these big kind

0:01:02of crazy VC funded startups, where their goal is just users and, you know, so like a free

0:01:07service with who's just designed and, you know, sort of built on the premise on the

0:01:12premise that within a few hours of launching, you'll have hundreds, if not thousands, if

0:01:15not millions of users, and how difficult that must be, in many ways, just technically and

0:01:22emotionally and so on to handle, you know, I'm working through a lot of issues around

0:01:27have worked through a lot of issues that have,

0:01:32at least have the advantage of having a paywall

0:01:34in the front of it.

0:01:35So while my user base is good and solid and growing

0:01:38and kind of what I was hoping for it to be,

0:01:40it's nowhere near, there's this nice big barrier

0:01:43between what people are, the number of users

0:01:46that I can handle, the number of users that will

0:01:48at any one time jump onto the service.

0:01:50So definitely mad respect to those people who can handle

0:01:54and work through those issues.

0:01:55And obviously there's a lot of that

0:01:56that's just throwing money at it,

0:01:57by having people who've done this before,

0:01:59by who are specialists in very specific attributes of it,

0:02:02and they can kind of stack those all up.

0:02:04You know, so you have a database guy, a web guy,

0:02:06a front-end guy, a network guy,

0:02:08you can do all those kinds of things,

0:02:09rather than obviously me just being one guy working on it.

0:02:11So, first I'm gonna talk a little bit about the planning side.

0:02:16And planning is kind of a tricky thing.

0:02:18When you're launching something new,

0:02:20you never really know what the demand for it is gonna be.

0:02:24And you can guess, you can hope,

0:02:26You can kind of maybe back of the napkin kind of guesstimate it, you know, oh, well, there's

0:02:31so many, you know, so many that the potential user base is this size, and I, you know, I

0:02:36think I'll have this much of a reach and so on.

0:02:38But for the most part, you're just guessing.

0:02:41And so what I tended to do for this is that my goal was to, before launch, to have a system

0:02:46that could scale pretty widely, but who, which could also scale down fairly easily.

0:02:53And so that I mean, rather than at every point in the architecture, when I'm sort of building

0:02:58how it was going to work and how it's structured, my goal was always to make it so that if I

0:03:02needed to, I could add, I could essentially I could throw money at the problem, which

0:03:06is essentially what you want, you want to be able to say, if things are going crazy,

0:03:11awesome, that you can just like, okay, I'm gonna throw money at the problem, I'm going

0:03:13to get more servers, I'm gonna get bigger servers, I'm gonna get faster servers, whatever

0:03:17it is, and you know, your capacity and your ability to work with that will just increase,

0:03:22you know, sort of, if not linearly, but will solidly

0:03:26increase solidly with that.

0:03:29So a lot of that is making sure that, like in my case,

0:03:32it's so that I could probably handle my web traffic on one

0:03:37big, beefy web front end.

0:03:40But instead, I chose to put a load balancer in front

0:03:43of a couple, or I think a trio, of web application servers.

0:03:49I didn't necessarily need to do that.

0:03:51I could have just put one big beefy server in the front,

0:03:53but then all of a sudden I have these issues

0:03:55if I need more capacity.

0:03:56Then I have to insert the node balancer in live in the front

0:03:59and redistribute traffic and those kinds of issues.

0:04:02It's like, well, I'll just start with that from the beginning.

0:04:04So now if I need to, I can add more servers on the front end,

0:04:08and my general capacity will increase.

0:04:11I can scale it down if I need to.

0:04:12If I find that I really don't need as many servers as I have,

0:04:15I can just easily just unprovision them,

0:04:18remove them from the pool, and we're fine.

0:04:21similarly on the back end, you know, the database, you want to

0:04:23set it up so that if you need to distribute it, that database, if

0:04:27you need to increase its size, you want to have the ability to

0:04:30do that. And it's a little bit trickier with databases, because

0:04:33really, you only have a little bit of a few options for doing

0:04:36that. But you know, it's making sure you're having a researcher

0:04:38that can support for example, having multiple read slaves or

0:04:41those kinds of things. And then, you know, I have a worker pool

0:04:44that doing all the scraping and asynchronous processing for the

0:04:47system. And same thing, it's designed around that, rather than

0:04:51putting all that on one machine and having that kind of be my big beefy worker bee, I've

0:04:55got a whole swarm of those that I can easily add new servers to or remove servers from

0:05:01in a way that makes sense.

0:05:03And I found that to be pretty, pretty, you know, worked out pretty well in terms of if

0:05:07I was able to launch and I was like, "Ooh," I totally underguessed the number of workers

0:05:10I'd need.

0:05:11I think I started with two and I'm now to, I don't even know, I've been as high as I

0:05:15think eight or nine and I think right now I'm settling in around four, which seems to

0:05:20be work pretty well, but it was good to have the infrastructure in place and to have practiced

0:05:25but we're pulling up and bringing down servers and so on.

0:05:29And then in terms of actual scale, this is something that I talked about a little a few

0:05:33episodes ago where I talked about performance. It's a lot of scale isn't necessarily about

0:05:40volume of machines and those kinds of things. It's one of these things that I wish everyone

0:05:46wishes there was a way that you could just kind of take a dial

0:05:48and turn it up, and your capacity

0:05:51would increase exactly with that.

0:05:53And this is kind of the lie or the--

0:05:57I don't know, the impressions in like Heroku,

0:06:00one of these managed services gives you, where it's like,

0:06:02oh, you just up the workers and it'll happen.

0:06:05Whereas the scaling seems far more-- it's a much more subtle

0:06:07problem in that really what you're trying to do

0:06:10is you have to find the bottleneck and work on it.

0:06:12And so much of what I've been doing now

0:06:14is just constantly working up and down the stack from top

0:06:17to bottom, finding a bottleneck, killing it, crushing it,

0:06:20and moving on to the next one.

0:06:23And that has dramatically increased your scale.

0:06:25You'll find these weird database indexes

0:06:27that you miss that you-- turns out

0:06:29that there's this one call that needs to do it

0:06:31and is taking forever that you didn't really plan for.

0:06:34And I found that that's incredibly helpful to have

0:06:37as a tool for that is to be able to just work through my stack

0:06:44in a methodical way.

0:06:47So I have my database machine.

0:06:48And actually, this is-- I've got professional help.

0:06:50I've got a DBA to kind of help me tune and optimize that.

0:06:54It's kind of the crazy thing.

0:06:56And that's probably a fair point.

0:06:57If you don't know how to do something,

0:06:59either spend the time to learn it,

0:07:02or just find someone who does, and hire them

0:07:04for a short contract to help you and kind of nail

0:07:06out the actual issues.

0:07:09It's kind of remarkable.

0:07:10I had these database issues.

0:07:11I couldn't quite track them down.

0:07:13I found I was able to locate somebody who's a Postgres DBA,

0:07:17who was able to take the machine,

0:07:18and was like, oh, you need to do this, this, this, and this.

0:07:20And he's just working through it,

0:07:22which is probably the same kind of experience

0:07:23that I would have if someone shows me their iOS project

0:07:26and they're having some problem.

0:07:27I could be like, oh, here it is.

0:07:29Fix this, this, and this.

0:07:31And not being too proud to be like, oh, no, I

0:07:34need to find and fix it myself.

0:07:35You do, however, want to make sure you learn all of that,

0:07:38learn what the problems are.

0:07:39But it's a nice process to go through.

0:07:43And the reality is now I could probably run my system on a much fewer servers than I ever

0:07:47could have before.

0:07:49But one of the things you always have to fight in this process is premature optimization.

0:07:54Optimization is going to be essential.

0:07:56It's going to be necessary.

0:07:57It's going to be something that you have to do.

0:07:58But if you optimize too soon, I find that you really kind of struggle because you'll

0:08:03end up adding complexity to your application in a way that you don't necessarily get a

0:08:09payback for.

0:08:10So a lot of bottlenecks, removing bottlenecks

0:08:13is almost always, unless it's just like a silly mistake

0:08:16or something that you really shouldn't be doing,

0:08:18you're going to add some additional complexity

0:08:20into your application to get a performance benefit out of it.

0:08:24There's usually something like that, some kind of trade-off

0:08:26that you're making in terms of your training speed

0:08:30for something else.

0:08:31And often I find that that's complexity,

0:08:32that you're creating--

0:08:34you're taking tasks and rather than doing them synchronously,

0:08:36you're moving them into an asynchronous queue

0:08:38where you then have to manage what

0:08:40happens if they succeed and fail and so on.

0:08:42There's all these kind of other things that you're doing.

0:08:45And so if you add that complexity too soon,

0:08:49you are just, you're becoming much more,

0:08:52it's much more complicated than it needs to be.

0:08:54And if you obviously, if you add it too late,

0:08:56your system falls down and doesn't work.

0:08:58And so typically what I've been doing,

0:09:00and it's lovely that I've actually been able

0:09:01to round this out and I think I'm back onto the features,

0:09:04sort of sprint leg of it, which you just kind of sit through

0:09:06and you just methodically work it through.

0:09:08I mean, I have this long list of like,

0:09:09here's things that aren't quite working right,

0:09:11here's things that are too slow, here's whatever.

0:09:14You just work through it.

0:09:15And it's a little bit of a drudge,

0:09:17it's a little bit of kind of you're just working

0:09:19your way along.

0:09:20But the nice thing is, every time you,

0:09:22it has this lively cumulative effect that you,

0:09:24you'll often find things that have knock-on effects

0:09:27to other problems.

0:09:28So you find, I found some weird issue in my worker queue

0:09:32setup, and it's like, okay, well, let me fix that.

0:09:35And now that actually makes a whole bunch of other issues

0:09:36go away or are mitigated dramatically.

0:09:39I find something bad in the way that I'm doing feed processing,

0:09:42and I can fix it, and it actually

0:09:43fixes six bugs at a time.

0:09:46And I think that's been very motivating and helpful for me

0:09:49as I go through this process to be encouraged by the fact

0:09:56that there's usually a lot less--

0:09:58problems are far less severe than you probably

0:10:00fear at the front.

0:10:02And just dive in there, tackle it,

0:10:03get your arms around the problem, and work on it

0:10:06almost always you get a result. There's definitely some times in the last couple of weeks where

0:10:10I've been just like I'm up at three in the morning trying to work on some weird service

0:10:12bug and I'm like, "Oh my goodness, what am I doing? This is crazy."

0:10:18And what I found is just stay calm. It's kind of like the always talk about with I guess

0:10:24like soldiers, right? The reason you train is so that when you are actually in the situation

0:10:29that you're prepared for, you'll just act. You don't have to sort of think. You can just

0:10:33just going to do.

0:10:33And it's just like, rely on your training and go with it.

0:10:35And that seems to actually have been working.

0:10:37That it's like, being well prepared before

0:10:39helps a lot on the back end to be

0:10:41able to deal with issues and things down the road.

0:10:44And that's kind of how I've been scaling it.

0:10:46And the nice thing is if you take this approach,

0:10:50you become fairly well prepared, and then

0:10:52you just kind of methodically work your way up and down

0:10:53the stack, fixing things and improving things.

0:10:56You very quickly get to a point that you

0:10:57can-- I think that you can round the corner

0:10:59and really start accelerating onto features.

0:11:01And this is where I've been loving this last couple

0:11:03of few days.

0:11:04This week, I'm much more feature-oriented

0:11:06than bug fix-oriented or stack-oriented.

0:11:08Things are tuned and humming along

0:11:10in a way that works really well.

0:11:12And the last thing I wanted to say around that

0:11:14is, as things are humming along, I definitely

0:11:16recommend that if you do any kind of web service work,

0:11:18that you need to have some kind of monitoring

0:11:21service attached to it.

0:11:22For me right now, I use Pingdom, which is just a monitoring

0:11:25service that basically, they'll hit a URL on a regular basis

0:11:30and send you a push notification, a text, an email,

0:11:34however you want to configure, whenever something about that

0:11:37doesn't meet your criteria.

0:11:39And then at first blush, you're like, OK, well,

0:11:40I just wanted this up for some useful URL,

0:11:46and it'll tell me if my servers are down, which is useful.

0:11:49The thing I wanted to just talk about a little bit

0:11:51is you can go far farther with that in a way that

0:11:54is much more useful if you are selective and careful

0:11:57and creative about what URL you have it hit.

0:12:00And so this is something that I've started doing

0:12:02that I think has been really helpful for me,

0:12:04is you can, rather than just hitting a main URL,

0:12:06like going to feed wrangler.net and telling me if it's up,

0:12:10I create custom URLs in the app that customers will never see,

0:12:14that aren't useful in that way, but that

0:12:16are parts of the application that exercise the whole stack

0:12:20and that examine exactly what's going on in the system

0:12:23and then report back if there's something there that

0:12:26isn't quite right.

0:12:27And so this is making sure that I-- it hits the web server,

0:12:30it goes through the load balancer to the web server,

0:12:33hits the database, comes back, and then gets presented

0:12:36to the user.

0:12:37Or on my worker side, it's like goes and looks and hits

0:12:39the web server, goes and looks at the worker queue.

0:12:42Is the worker queue too high?

0:12:43Is it above what I think it should be?

0:12:45If it is, send it back.

0:12:47And in the process, it's also making sure

0:12:49my Redis server's up, it's handling all the workers,

0:12:51it's handling-- make sure the workers are up.

0:12:54And you can create these kind of interesting URLs

0:12:56And then they send back basic messages, which you can hit directly.

0:13:00And you can then include just status information.

0:13:03So I have an alert that it hits my worker counter.

0:13:06And if the worker counter is above 4,000, which is a number-- there should never be

0:13:10that more than 4,000 in queue jobs at any one time, then it sends me a push notification.

0:13:18And the thing that's kind of funny but subtle about that is making sure you have those things

0:13:21gives you tremendous peace of mind.

0:13:26Because I can stop kind of compulsively checking on things

0:13:29in a way that I would have to otherwise,

0:13:33where I have to be constantly SSHing into machines

0:13:36and kind of looking at things and playing with stuff.

0:13:38It's great to be able to just know that

0:13:41if something goes wrong at any of these,

0:13:43like these four parts of the system,

0:13:45then this particular alert will fire.

0:13:46If anything goes wrong with these parts of the system,

0:13:49these lists alert will fire.

0:13:47And once you have developed a fair bit of trust with that,

0:13:51once it really seems to work, it's

0:13:52great to be able to just push that, put that aside,

0:13:55and focus on features, and focus on making the product better.

0:13:58One other random note that I want to make

0:14:00is if you are doing this kind of work,

0:14:01if you're working on this kind of stuff,

0:14:03there's a great app called Prompt, which is by Panic,

0:14:05which is an SSH client.

0:14:07And configuring that so that you can SSH into your machines

0:14:10from your phone is amazing in terms of-- there's definitely

0:14:13once so far I was out to dinner with my wife,

0:14:15And again, I get this alert that the servers are down.

0:14:17Something was funny.

0:14:19It was great to just pick up my phone,

0:14:21SSH into the machine, found the little thing, which

0:14:24is some worker had an error and needed to be restarted.

0:14:26I restarted it.

0:14:27And boom, I have a backing business.

0:14:29Come back and enjoy dinner.

0:14:31So definitely a great tool to have.

0:14:33You can configure it with your private key

0:14:35and all that kind of stuff.

0:14:36So it's really convenient.

0:14:37All right, hopefully that was helpful.

0:14:38It's a little bit into the network administrator

0:14:42side of things.

0:14:42So I'll be back more into the iOS side, which

0:14:45I think most of you listen to this for soon,

0:14:47but it's what I've been thinking about what I've been doing.

0:14:49All right, so that's it.

0:14:50If you have questions, comments, concerns, complaints,

0:14:52compliments, I'm _DavidSmith on Twitter, DavidSmith@appnet.

0:14:56And otherwise, have a great week and happy coding.