We try to have fun together at Famigo.  Every Wednesday we have Taco Twednesday, in which we all leave the office to eat at and review various taco establishments.  (We're also logging these reviews, taking steps towards the world's first big taco data powerhouse.)  I also regularly invite everybody over to my house to make homebrew beer. (Ask about our not-quite-award-winning ale, Electric Brewgaloo.)  Finally, we devote one afternoon a month to Mandatory Fun Day, where one rotating employee plans an afternoon of fun for the rest of the company.

What's the point of all this camaraderie?  Sure, it's fun to get out of the office, it's great for retention, and it helps with recruiting.  We're doing something more important than all of that though when we're having fun, though.  We're doing something that every software organization needs help with: we're learning to listen to each other.

Writing software is an isolating experience.  Many years ago**, I had a job where I'd sit down, I'd put on my headphones, I'd bang away at the keyboard, and I'd go home 10 hours later.  That was all day, every day, for a few years.  Actually, there was an occasional exception.  Sometimes, I'd hit a hard problem and I'd spend 20 or 30 hours at the keyboard before I went home.  I wasn't the exception; the rest of the team was like that too.  And let me tell you, we made some crappy, crappy software.

It was a little surprising at the time, because, individually, we were all smart.  As a group then, shouldn't we have been very smart, making very good software?  Spoiler alert: no.  No one listened to anyone else, and so each person on the team was left on his own to make crucial mistakes.  These mistakes compounded over the years until the product crashed.

Why weren't we talking?  I can only speak anecdotally here.  My teammates were friendly enough, but there were differences when it came to age, outside interests, background, and politics.  All we had to discuss were the typical programming debates (vim vs. emacs, etc.) or company rumors, both of which were charged conversation topics.  In short order, everyone was still talking, but no one was listening.

We had important questions for each other, things like "What do you think of Feature X?", "Do these requirements make sense?", and "What the hell is it we're actually building?"  Even at the time, I knew I should be asking them, but not enough to get me to participate in an awkward conversation with people I didn't know very well.  Collectively, we could've answered those and built something important.  Instead, each of us charged ahead with a deeply flawed idea of what we were building.

Would tacos, beer making, or a go cart outing have fixed that?  Maybe so!  At the very least, it's a shared experience.  It's a starting point.  We could begin having agreeable conversations, starting with questions like "How about these tacos?"  or "Wow, we made some crappy beer, didn't we?" These simple conversations build rapport, and I would now trust my teammate's opinion on something.  All of that is crucial if I want to feel comfortable asking harder questions like, "I can't figure this out; will you help me?" or "Are we building the right thing?"  That is why it's only partly a joke when I say we practice taco-driven development.

**The job in question is wayyyy back in time, and has nothing to do with any recent employers.

I was recently at the DevOpsDays conference, where I got into a conversation about build automation. I mentioned how we practice continuous deployment, so we may deploy to production 20 times a day. The guy replied, "That sounds great for some tiny startup, but what would happen if you had actual users?"

Allow me to respond in 2 parts. First, ouch. Second, continuous deployment is not at odds with a great user experience or high uptime requirements.

Between our website and our API at Famigo, we handle hundreds of thousands of HTTP calls every day. We've practiced continuous deployment for 2 years. You know how many complaints we've had about a cruddy user experience due to frequent deployments? Zero. Why were these deployments essentially transparent to all of our users? That's a requirement for our build process, and so we've focused on that part as much as the actual act of building and deploying.

How Does It Work?
First, let's talk about what our production environment looks like. We have a few different VMs hosting our web app; these are all based off of the same original image. Our load balancer distributes traffic across these instances evenly. Since all of our web and API is based upon Django, we use virtualenv to manage all of our Python dependencies on each instance. Each instance also runs Jenkins, which does the heavy duty work of building and deploying.

All of the important data comes from MongoDB or Redis. I point that out to just to note that, with this backend, we rarely do schema migrations. Big honking ALTER TABLE statements can cause serious downtime; just ask the guy in the Oracle shirt crying into his keyboard right now.

How Do We Build?
We have one instance that's constantly polling our github repo for changes. When a change is found, it pulls down the repo. Our environment dependencies are part of that repo, so we make a call to virtualenv to ensure the environment is up to date. Then we run all of our tests; there are around 900 of these. When that's done, we rsync the files over to our production directories and restart our fcgi process. We then make a call to the next instance's Jenkins remote access API to kick off a build, and the whole process starts again.

Downtime?
The only portion of the build process that involves any downtime is when we rsync and then restart fcgi. Those steps take maybe a second or two. Since we build and deploy one instance at a time, that second of downtime rolls from machine to machine; in other words, we never have one second of downtime for all users on all instances.

One thing to keep in mind here is that our load balancer constantly pings our instances to ensure they're up. (After all, that's the whole point of these load balancer thingies.) If, for whatever reason, our downtime is longer than a few seconds, the load balancer will stop distributing traffic to that instance until it's back up.

As you can see, you have to be a little bit lucky (unlucky, rather) to ever see downtime here. You need to hit one particular instance with a request during its 1 second of downtime while the load balancer is sending traffic there with the load balancer not having realized the instance is down.

Does That Downtime Even Matter?
Please break out your slide rule, as we're going to do some math. Per instance, if we do 20 deployments with 1 second of downtime for each, that's 20 seconds. There are 86400 seconds in a day. 20/86200 is, in purely mathematical terms, teensy weensy. (I don't know how to calculate downtime across all instances because of the load balancer and its outage detection, so I'm just sticking with one instance here.)

Now, if we were processing credit cards or something like that, 20 seconds of downtime per day due to deployments would be unacceptable. (Note: we don't do that.) On the contrary, if your traffic is largely mobile, as ours is, then 20 seconds a day is nothing. In fact, we expect far worse. The reason is that, in the land of mobile, you get in the habit of trying and retrying everything related to the network, because the coverage can be so spotty.

Conclusion
Continuous deployment does not necessarily mean giant swaths of downtime throughout the day. In fact, as you scale up in environment infrastructure, deployment smarts, and hopefully users, you gain tools that can make this downtime negligible. Now, back to my actual users.

Code Like Clarkson

| No TrackBacks
Like anyone else with a brain and a heart, I love Top Gear.  In fact, I love it so much that I find myself borrowing wisdom from the show and applying it other domains entirely.

As you may know, there's a portion of show where they put a celebrity in a reasonably priced car, which the celebrity then races around the Top Gear track.  We then watch the celebrity watch their lap.  Often, the celebrity will say something how it looks as if they're going really slow.  Jeremy Clarkson always has the same retort: if you look like you're going slow, you're probably going quite fast.

This idea maps nicely to software.  To the uninformed, it looks like we're going slow when we write tests.  It looks like we're going slow when we learn and utilize new tools like Hadoop or Cassandra.  It looks like we're going slow when we perform A/B tests.  It looks like we're going slow when we pair program.  And yet, all of these "slow" activities are tremendously helpful to going fast in the long run.

Conversely, if it looks like you're writing software quickly, you're probably not.  Yes, you can get all cranked up on caffeine, code for 20 hours, and write a thousand lines of code.  You can do that day after day, just coding, releasing gigantic features constantly.  I think you'd quickly encounter a day of reckoning.  Things would burst into flames, and you'd realize it's more trouble than it's worth to fix this mountain of code.  You'd realize that it looked as you were going fast, but you were really going slow.

Bytes Matter

| No TrackBacks

I love to profile applications, because I always learn something that surprises me.

Initial Profiler Surprise: Client Side
Case in point, I was recently profiling our Android application, the Famigo Sandbox. This app sends a lot of data back and forth with our API, as we try to determine which of the apps on your phone are safe for your kids. I always assumed that, if app performance suffered during some of the chattier features, it was probably due to slow cell reception.

The profiler told me that I was wrong; the transfer time was almost always negligible. What wasn't negligible was the amount of CPU time it took to parse the JSON coming from the API into native types. (Note that I'm measuring JSON parse time across an average app session, not just for one call.)

Like most JSON decoders, we parse everything, regardless of whether we use it or not. I took another look at our API responses and learned that our app actually didn't need half of what we were sending.

Now, we weren't doing anything too crazy on any individual API call. We consistently returned too much data everywhere, though, across many API calls. In aggregate, these bytes mattered. Once we learned this, we streamlined the data returned from our API and quickly saw our JSON parsing bottleneck go away.

Subsequent Profiler Surprise: Server Side
Here, I was profiling our website, which is essentially an app recommendation engine for families. We consistently see some calls take a long time, and I assumed it was the complexity of the queries. For example, our queries to find and sort the best iphone apps or free android apps take into account a lot of disparate data from our own reviewers, the app stores, and all of our family users.

When I profiled these calls again, I was shocked. The queries were actually well-tuned (as of the author of these queries, yes, this is shocking); the slowness was coming from the ORM (pedantic note: it's really an ODM - shakes TI85 threateningly) we use to turn our MongoDB documents into our lovely Python models.

This problem was actually very similar to the problem seen in our Android app. MongoDB documents are encoded in BSON, which is very similar to JSON, and our ORM is responsible for parsing that BSON into usable types. On almost all of these queries, we were asking our db drivers to parse the entire document when we really only needed a small subset (1/3 or 1/4) of the fields. That's hardly noticeable when you're dealing with a few documents, but it becomes quite a bottleneck with thousands of documents. Again, I realized that bytes matter.

Once I figured out the problem, the fix was easy. Instead of asking for every field on every document in the query, I simply specified the fields I wanted. When this change went live, the bottleneck disappeared and we got an easy 40% improvement in average render time.

Let Us Conclude
I don't think I need to restate this, but I will, because it's my website and we hammer points into the ground 'round these parts. The lesson is that the more data you return, the more you must process.

This is so basic that it's often easy to ignore entirely. However, once you have real users and real data, bytes matter, and they matter more and more as you scale. Use them wisely.

Understanding-Driven Development

| No TrackBacks

I have a weird idea. What if, with every change we made to our codebase, we tried to increase our understanding of it a little bit?

Entropy Tries to Thwart Us
This is challenging because codebases always go in the opposite direction. As you make more changes and new people join the team, everybody understands less and less of what ought to be happening; the fact the code works at all is nearly miraculous! Soon, everyone who touches the codebase adopts an "If it ain't broke, don't fix it" attitude.

Success depends on understanding, though. We have to understand the code to add new features, fix important bugs, refactor, and bring new teammates aboard. Not only that, but problems that are deeper than code, like architecture and scalability, can't be addressed without first understanding.

Understanding Must Be Widely Distributed
One person understanding isn't enough. After all, what happens if that one person gets eaten by a komodo dragon?

There are deeper problems than that, though. Imagine that your brain becomes tightly coupled with a bit of code. The first problem is that your brain is faulty, and you will forget. The second (scarier) problem is that, if you're the only person who understands a piece of code, you own it and you'll maintain it. Forever. It doesn't matter what you else you progress to, when a problem arises with that code, it's your problem. It encourages context switching, and lots of tiny, strange code silos.

How to Create Understanding
How do you increase understanding on a large scale, then? Let's go through a few approaches, none of which are earth shattering.

  1. Automated tests. When you have simple, isolated tests that are run often, it means anyone can learn about the code, make a change, see the effects, and feel good about the work they just did; you are creating understanding. Unit tests, BDD-style tests, integration tests? All of these work.

  2. Refactoring. As you are adding features or fixing bugs, you can create understanding if you're constantly working to make the code as clear as possible. The great thing about these changes is that they can be trivial. One technique is just to revisit the names used in a chunk of old code. If a variable contains sales invoices and you change its name from temp to sales_invoices, you have succeeded. Make more changes like that!

  3. Documentation. Yes, documentation can create understanding, but only if it accurately reflects the current state of your code. The most effective way to do this is to generate it dynamically based on the code itself: method signatures, assertions, url routes, the requirements stated in your BDD tests.

  4. Environment automation. There are probably a lot of magical bits in your environment. Maybe your build process doesn't work unless this one particular directory is owned by this one particular user, or your CDN occasionally serves up old assets and you have to poke around in the Amazon Web Service dashboard to fix it. These weird workarounds are often simple, but you encounter them infrequently enough that no one remembers exactly what's happened or why. Do your brains a favor: automate all of this. Once it's written, it can be understood.

How to Create Misunderstanding
You can easily abuse all of the methods I just said, and actually use them to create misunderstanding.

  1. A test creates misunderstanding if it depends on data that's changed by other tests. If your tests don't repeatably succeed, regardless of order, you're causing confusion.

  2. Refactoring can create misunderstanding if you take well-understood code and change it dramatically, without also writing tests.

  3. Documentation often causes more harm than good. Think about the nearest gigantic, outdated Word doc, or the comments in your code you fail to revise as you refactor. At some point, someone will read that and get confused.

  4. Environment automation causes misunderstanding if it doesn't accurately reflect the state of your enviroment. Maybe you have some disaster recovery scripts lying around. Do they work, or would looking at them only give you misconceptions about the way your environment used to look?

Conclusion: Be Smarter.
Ultimately, software development is really, really hard. We have to think in terms from single bits to clusters of super-powered VMs. The best (only?) way to work effectively together and build great things is to constantly and collectively work towards a better understanding of our code.

Things Get Weird at Scale

| No TrackBacks

Something scary happened on Saturday. At Famigo, we have several different monitoring systems for our production environment. At about 3 AM, they all collectively went nuts. I happened to be up then because my son had a coughing fit, so I checked the site, verified that nothing weird was happening, and chalked it up to code gremlins.

When I woke up a few hours later, I saw roughly 38 gazillion more alerts. Based on the amount of load we were seeing, you'd think Barack Obama had interrupted all network programming to give a Famigo plug (note to Barack: you should do this). Still, I didn't actually see many users on the site, just a lot of load. Also, we're load balanced in production, and each traffic-handling node was under heavy load; it wasn't just one machine.

Maybe It's Not a Problem?
Saturday mornings are a key time for us at Famigo because we send out a report to each activated user, showing what apps their kids played along with some personalized app recommendations. When I saw the load, I immediately thought something was wrong with the email generation process. Maybe we dropped an index somewhere and the queries were suddenly taking a long time? I resolved not to worry any more about this until the emails were done.

Here's the problem: the emails wouldn't finish. They were going so slowly due to the load that we'd be sending these emails for days. I now began to worry more. I canceled our email task and began troubleshooting in earnest.

Bizarre Facts Emerge
As I SSHed into our various boxes, I noticed, via a top command, that it was the Python process serving our website and API that was consuming 100% of the CPUs. That was intriguing. I restarted all of the usual suspects (MongoDB, lighttpd, our web app), only to see things quickly begin to degrade again. Within a few minutes, Python was once again consuming 100% of the CPU.

Like most web apps, we don't really do much work that's CPU-bound. In fact, one of the laws of performance I've learned is that if you do have work that's CPU-intensive, always do it in the background. And yet, we were clearly taxing the CPU. Maybe we accidentally pushed a commit that attempts to generate pi to 1 million digits every time someone made a web request?

It seemed clear that the issue was with our code. Here's another weird thing: we hadn't changed much lately. I went through all of the commits for the past 2 days, and it was all pretty boring stuff. Just to verify, I pointed my dev instance to our production database (don't try this at home, kids) and began to actually navigate through these recent changes. Like I initially thought, there was nothing earth-shattering; it was all pretty standard web stuff.

At this point, I began to think I was hallucinating. I hadn't seen Girl Interrupted, but I imagined that Winona Ryder got committed in that movie because of the mental strain of debugging in production.

A-ha! (Or, How to Solve Problems Through Random Mouse Clicks)
I didn't know what to do. I just began clicking around our site on my dev instance, monitoring page load time in Chrome. Everything looked just fine on my dev instance at first, and then I began to notice one trivial view taking a little bit longer than it should've. The page itself should've rendered in a second or so, and it was taking closer to 1.5 seconds. The more I reloaded, the longer it took. This was particularly interesting because this view was our application view (here's an example). We can render over 30,000 apps with that view, so while the logic is very simple, it's constantly being rendered because of all the traffic.

Now, we get to the funny part. As I mentioned, we analyze tens of thousands of apps and, if you knew the right app slug, you could actually render any app, even dirty ones, with that view. While we didn't advertise this fact, you could get to stuff like famigo.com/app/super-sexy-sex-time/. Those are clearly not the apps we want families to see, even progressive European families. So, I had recently put in just a bit of work to keep sex apps from rendering. Every time someone requested an app, we'd check to make sure it was in the set of allowed, non-sex apps before we rendered anything.

It's really just one line of code that does the no-sex-app check. The no-sex-app check wasn't being done in the database; I was basically saying, in Python, 'raise 404 if app not in good_apps'. That's so simple! On my development instance, it worked fine. It didn't slow down our unit tests. However, when I wrote a quick bash script to request that URL 10 times simultaneously, things began to explode. In production, when we regularly have 25+ visitors requesting that URL at all times, everything truly burst into flames. It turns out that this particular view was CPU-bound, I just didn't know it until it encountered some scale.

Scale Drives You Mental
I think this is a fascinating bug. It's very simple logic that would work fine if we had 3 or 5 people on the site at any moment. With 10x that traffic, it was catastrophic to the rest of the platform. Imagine how crazy this gets at 100x or 10,000x that traffic. That's what makes scalability fun: gigantic issues at scale often come from very innocuous code. At least for me, the root cause is never what I expect.

How do we prevent this from happening in the future? I'm not entirely sure. It's not really something that fits into a unit test or an integration test. As a stopgap solution, we wrote a Python decorator that wraps all of our views and logs how long they took to render. Based on that, we can calculate how long a view should take to render, and alert ourselves if the render time is outside a reasonable span of time. It's not perfect, but it's a start. Anybody have a better idea?

I am on the record as being a MongoDB fan, admirer, and devotee. I never quite felt the same way about Redis, though. My friends would talk excitedly about Redis and I'd say, "But I have a perfectly good key value store in memcached and a perfectly good document oriented database in MongoDB. Between those two, I will solve all hard problems, excluding global warming!" Slowly, though, I began to run into harder problems.

A Harder Problem
At Famigo, we do many, many distinct, complex queries when it comes to recommending apps for families (eg, give me the top 1000 puzzle games for young adults that are free on the Amazon App Store, sorted by user rating). Doing all these queries on demand proved to be a little bit slow (average query time is about a second), so I decided to cache the results of each distinct query for 8 hours. That's slightly more complex, but it wasn't like I was writing an Erlang compiler in Visual Basic.

Initial Approaches
Take 1 of cache implementation: Use memcached for the cache. Ten minutes later, curse memcached for not having an ordered datatype.

Take 2 of cache implementation: Use MongoDB for the cache. Many minutes later, celebrate success (prematurely).

What did our cached query results look like in MongoDB? Each document in the cache had a cache key (eg, most-popular-puzzle-games-for-young-adults), an expiration date, an ordinal, and a reference to the application document that we wanted to render.

Warning Signs
There were already hints that I was doing it wrong. Case in point: I had to manage all of the cache expiration myself. In MongoDB, you can specify a maximum number of documents that a collection can store (which I was doing; I specified a max of 500k docs), but that's not at all the same thing as caching these results for exactly 8 hours. Speaking of which: hey 10gen, we want TTL collections!

Another sign I was doing it wrong: I had to do a lot of index tuning to make my interactions with the MongoDB cache fast. Every time I checked the cache, I had to specify the cache key, expiration date, and sort by the ordinal; for that to be fast, all of those needed to be covered by an index. While the index sped up my finds, it slowed down my inserts. I had a hell of a time finding the right balance.

Unfortunately, I'm not yet done listing the signs that I wasn't doing it right. You can't delete from a MongoDB capped collection. That's no problem if you're just collecting logs, but from time to time, we must invalidate our cache. Since I couldn't delete these documents from the cache, I had to add another column that stored an Active status, which also required an index, since we had to query by it every time.

How Did It Work? (Spoiler Alert: Not So Great.)
We ended up running in production on my MongoDB app query cache for a month or two. It was definitely faster than performing all of the complex queries in real time (~300ms instead of 1s), but there was a new delay when we had to add results to the cache (~200ms). As both app data and users scaled up by an order of magnitude, it was clear that this would just burst into flames at some point.

A New Solution Emerges!
I decided to try something new. I knew that Redis had a sorted set datatype, so I started to play with that. Rather than cache these app query results in MongoDB, I created a sorted set of app ids for each query. I let Redis handle all of the cache expiration business by setting a TTL value for each key. When I wanted to pull from the cache, I did so, then did a find in MongoDB using the $in operator with all of the app ids, then I reordered that in Python based on the app ordering in Redis. I knew it wasn't as pretty, but was it effective?

For my first test, I merely timed how long it took to add a few hundred results to my Redis-backed cache. That was regularly around 200ms; it was now down to 1 or 2ms. Impressive... but then that should be fast. I refused to be impressed until I started pulling from the cache.

Was it faster to pull the app ids from Redis, use that to pull the documents from MongoDB, then use Python to reorder everything? Actually, yes. Thus far, getting from the cache takes 1/3 of the time that it did before. Meanwhile, adding to the cache is essentially free.

How Not to Do MongoDB, or Any Other Datastore
It turns out that, technically, I was correct. I could use MongoDB as a key-value store for caching, much like I could use my Mazda 3 as an amphibious assault vehicle. In practice, neither would be optimized for those use cases.

A key part of determining your architecture is understanding the strengths and weaknesses of your technology choices. The primary strength of MongoDB is how it allows you to simplify and decouple your data modeling via document-orientation. What about Redis? Its primary strength is how it enables very fast access to a few key data structures, like sets and dictionaries. With both of those stated, it becomes clear the situations in which you can combine MongoDB and Redis to build delightful software.

If there's one thing that the earth has enough of, it's social media professionals. If there's another thing that the earth has enough of, it's software development analogies. Regardless, I'm going to spin one here. You know what building software is like? It's like escaping from prison.

Think back to your favorite prison break movie, whether it's the Great Escape, Shawshank Redemption, the cleverly-titled TV show Prison Break, or the Rock (which, granted, is about breaking into prison - further proof that Nick Cage doesn't play by your rules). What was involved?

A cast of quirky characters, drawn together by a shared goal.
On a prison break team, you might have a group of guys digging the tunnel, another group laying down the track inside the tunnel, and then some more guys securing fake IDs and afro wigs for once you've escaped. There's always (ALWAYS!) a scene earlier in the movie showing how none of these guys liked each other originally. Once they settle on the idea of a prison break, they quickly become inseparable and ready to lay down their lives for each other.

Between developers, marketing, biz dev, management, and investors, the typical software organization is a collection of people who'd never gather together for any other reason. It rivals the most dysfunctional family in the world. And yet, the end goal, be it world domination, billions of dollars, or just happier users, is enough get this odd bunch of folks to put in thousands of hours in extremely stressful situations. Weekends are skipped, holidays go unobserved, and your kids come to refer to you as 'That smelly guy who drops by occasionally to swear at us and change underwear', all for the sake of the software.

A group of antagonists, out to thwart the escape.
In the prison break movie, there's always a group of guards charged with preventing the prisoners from escaping. We might get a scene showing what happens when some other group tried and failed to escape. We come to learn that these guards are some rough hombres.

Similarly, your competition is there to keep you from the rich rewards you'll gain upon your glorious software release. This competition could be a competing company in this market, or it could be another department in your organization. Regardless, an inordinate amount of energy is spent worrying about these antagonists.

Fortunately, the antagonists aren't very smart.
Remember how, in all of those movies, the prisoners empty one handful of dirt at a time into the yard, so no one realizes they're tunneling out? And how they put a dummy made out of socks in their beds each night so the guards don't notice their absence? If the guards were actually intelligent and engaged, they'd probably notice that kind of thing. They never do.

Your software group's antagonists are probably the same. You'll spend a ton of time worrying that they've figured out what you're up to, based on a few sentences on your website or a line in some PR piece that slipped out. Oh God, maybe they even signed up for a beta account! And then you'll worry that once they figure this out, they'll beat you to market and steal the money, fame, and silk snuggies that accompany both of those. This will probably never happen either.

Months and months of drudgery.
Unsurprising fact: it's a lot of work to tunnel through a building, under the prison yard, and out to safety. Especially when all you have is a spoon.

Along the same lines, it's a lot of work to write software. No matter how many practices, methodologies, and tools we use, it is ultimately just a hell of a lot of typing. Not all of this is exciting; you'd probably rather dig through a wall with a spoon than revisit your password reset logic. Nonetheless, the goal depends on this, so we type and type and type.

Followed by short bursts of brain-melting terror.
What happens when the prisoners finally escape? All hell breaks loose: somebody realizes the fake IDs never showed up, there's a police-looking-guy in front of the rally point, and the prisoners can hear the guards and their dogs a few hundred yards away.

A software release is quite similar. What should be a triumphant moment quickly turns sour, as the production environment goes down, Google de-indexes us for some reason, and we discover a bug that's led us to charging some people 100x more than we ought to.

Hooray, we escaped from prison and everything's great! Except for when it isn't.
Here's the surprising part: for all the cliches, prison break movies diverge when it comes to the conclusion. Some end happily, with everyone free. In some, no one escapes. And then some end on a mixed note, with a few prisoners escaping and the rest being captured/shot/nibbled on by guard dogs. In general, the endings aren't anything to be depressed over, because there's always a high chance of a sequel.

Software works the same way. There are endings that are happy, non-happy, and all emotions in between. If that's the case and we're not guaranteed success regardless of the sacrifices, then we should focus on the practice itself of software development. Let's have fun, treat each other excellently, and learn. Let's understand that there's an element of luck in these results, but that shouldn't detract from our satisfaction of doing a job really well and building something wonderful.

If we take over the world in the process, that's all the better. Even if we don't, we'll still be in fine position for the sequel.

Many thanks to everybody who helped me think of prison break cliches on Twitter.

MongoDB Is a Tool, Not THE Tool

| No TrackBacks

There's been a lot of angst (exhibit A, B) and counter-angst (exhibit C, D) directed at MongoDB lately. We're enthusiastic users of MongoDB at Famigo but we're not zealots, so our approach towards Mongo may be instructive to others.

Our take on MongoDB: it's a great tool, but it's not the only tool. MongoDB is fast, easy to administer, and has a great API for many different use cases. When you find yourself in a situation where those 3 no longer apply, you should use a different database. Let's consider these, point by point.

MongoDB is fast
This has long been one of Mongo's selling points; if you're gullible enough to believe database benchmarks, there's proof scattered about the web. Lately, there's been much debate about how some of 10gen's design decisions could potentially kill db performance, particularly Mongo's global write lock. Under a write-heavy load (as many Mongo instances are), Mongo could become CPU-bound, which would be catastrophic for performance.

That sounds terrifying (global write lock omgwtfbbq?!?), but we have never experienced this. Under a reasonable load, you're unlikely to run into this if you follow Mongo's one guiding performance principle: your working set must fit into physical RAM. I cannot overemphasize this point. Mongo relies on memory mapped files for performance, and you'll see a gigantic degradation in response time if it must go to the hard drive to read or write.

Remember, for outstanding performance, your working set must fit into physical memory, not virtual memory. If your data is too large for that, you should either shard it so that it will fit into physical memory on multiple machines, or choose another database.

MongoDB is easy to administer
It's quite simple to configure MongoDB for replication; if you know the difference between bash and batman, you could do it in less than 30 minutes. The same goes for sharding. Compared to the amount of time I've spent configuring and freaking out over Oracle clusters of similar size, my MongoDB administration time is a rounding error's rounding error.

There are scary stories here too, particularly with rebalancing shards and the availability of all the requisite services on a large, sharded databases under heavy load. Again, I wonder if MongoDB is the right choice. If you're looking for scalability and high availability via replication, I would try Cassandra. (Random asides: Cassandra's performance actually scales linearly as you add instances, which sounds like magic. Neato graph and other stuff here.)

MongoDB has a great API for many different use cases
Considering that Mongo uses a JSON-like encoding for all its data, the query language is simply amazing (awesomeness ahoy!). Not only that, but there's built-in support for map/reduce across your collections. When it comes to standard CRUD work or ad-hoc querying (via its querying language or map/reduce), Mongo delivers nearly everywhere.

Where isn't it so great? One example is full text searching. You can technically kinda do it, but it lacks basic functionality like stemming. Given the sheer number of simple, powerful full text search engines, you should just supplement Mongo with something like Solr for searching. That's what we do.

Conclusion
Okay, so MongoDB doesn't work superbly for all problems in all deployments at all levels of load. What does?

I like it that Mongo doesn't solve all my problems. One of the great aspects of the NoSQL movement is the sheer number of amazing tools available. I love that that, in the course of building great software, I get to work with Mongo, Redis, Solr, and others. It's fun, and I learn; these are good things.

As you may know, Dennis Ritchie died last week. He was the creator of C and co-creator of Unix, and a true luminary in the world of computer science. I'll leave the eulogizing to others, but I must share a funny, little story about my own interaction with Dennis Ritchie.

When I was 17 or 18 (think late 1990s), I really liked technology and writing code, but I wasn't sure if computer science was the right major for me. I was apprehensive because I'd had an internship that wasn't always a great experience. It sure seemed like I could use some advice from an expert on the matter, but I didn't know any experts. I had a flash of insight: hey, why not email somebody incredibly accomplished and see what they thought about my situation?

To this day, I have no idea why I chose Dennis Ritchie. There were roughly 500 million other programmers in the world who would've had more time to answer my my inane questions, but I chose him. I guess I assumed that language design, O/S internals, and career planning for high schoolers were all similar sorts of expertise. I got his email from his Bell Labs page, then I typed up a note on my predicament and sent it off to him.

Surprisingly, he answered! It wasn't some perfunctory response, either, it was actually quite encouraging. Unfortunately, the email disappeared into the ether long ago, but I can paraphrase what he said.

He said he was happy to offer me fatherly, no, probably more like grandfatherly advice (I recall that part distinctly). He said he himself didn't study cs as an undergraduate, and he didn't think it had limited his options. He went on to say that the field of software advances very quickly, but as long as you understood the fundamentals, you could do a lot of great work.

In retrospect, it was an incredibly nice gesture from an esteemed figure to a clearly deranged teenager. Thanks again, dmr.

About the Author

The Art of Delightful Software is written by Cody Powell. I'm a dad, husband, software developer, and entrepreneur. I'm a cofounder at Famigo, where we make mobile work for families.

Twitter: @codypo
LinkedIn: codypo's profile
Personal blog: Goulash
Email: firstname + firstname lastname dot com

Recent Comments

  • Theodore Brisentine: I am selling the domain and site www.microscopiccolitis.org, please visit read more
  • Joe User: I tend to agree with avoiding indentation, though I'm not read more
  • Leif Mortensen: I'm reminded of a technique we frequently used in my read more
  • Cody: Right now, we're storing around 5000 documents per day and read more
  • Tom: Can you give some specifics. Like how many objects did read more
  • Daniel Molina Wegener: Writing documentation sometimes is a boring task, but it's read more
  • Cody: Murph, you're probably right that you can't get quite this read more
  • murph: this is rad. what if you work in a place read more
  • Cody: Definitely. I think the reasons for rolling my own app read more
  • DasIch: You do realize that Sphinx exists? read more

Categories