Latest / Talk Python To Me / #109: MongoDB Applied Design Patterns
Transcript
- 0:00Database design and decisions used to be fairly straightforward.
- 0:02Pick your relational database engine, map out the general entities,
- 0:06apply the third normal form to them, and you're basically done.
- 0:09With the Cambrian explosion of database options and variations created from about 2009 until present,
- 0:15it's way harder to even choose the database,
- 0:17much less follow the well-worn path of third normal form for modeling.
- 0:20On this episode, you'll meet Rick Copeland, a fellow MongoDB master
- 0:24and author of the book, MongoDB Applied Design Patterns.
- 0:28We'll discuss modeling data using documents in a document database such as MongoDB
- 0:33and some techniques and situations that apply particularly to MongoDB's implementation.
- 0:38This is Talk Python To Me, episode 109, recorded April 26, 2017.
- 0:57Welcome to Talk Python To Me, a weekly podcast on Python, the language, the libraries, the ecosystem,
- 1:15and the personalities.
- 1:16This is your host, Michael Kennedy.
- 1:18Follow me on Twitter where I'm @mkennedy.
- 1:20Keep up with the show and listen to past episodes at talkpython.fm
- 1:24and follow the show on Twitter via at Talk Python.
- 1:26This episode is brought to you by Advanced Digital and Hired.
- 1:31Please check out what they're offering during their segments.
- 1:33It helps support the show.
- 1:35Rick, welcome to Talk Python.
- 1:37Thanks for having me on, Michael.
- 1:38Oh, I'm very excited to have you on.
- 1:40Anytime I get to talk about MongoDB, it really makes me happy.
- 1:43So we're going to have a lot of fun doing that.
- 1:45And I think the work that you've done in MongoDB in a couple areas, in your ODM, as well as
- 1:50your book that we're going to talk about, it's super work.
- 1:52So I'm looking forward to sharing it with everyone and talking to you about it.
- 1:55Awesome.
- 1:55Yeah.
- 1:56Well, of course, before we get into that, we've got to hear your story.
- 1:59How did you get into programming in Python?
- 2:01Well, so when I was, I don't know, when I was a kid, my dad got an Apple II, and I guess
- 2:06that dates me a little bit.
- 2:07But I learned how to program on BASIC starting there and then ended up just getting into
- 2:11computer science in college.
- 2:13So I was pretty hardcore in C, C++.
- 2:16Out of college, I did some systems programming and various other types of things with visual,
- 2:23C++, and ended up having a little period when I was exploring new programming languages and
- 2:28I ran across an essay by Eric Raymond about why Python.
- 2:33And it was kind of like his new favorite programming language.
- 2:35And I'd seen a little bit of Python before, like I'd run across Gen 2 and I saw it's got
- 2:40this crazy indentation syntax and I kind of dismissed it.
- 2:42But then when someone credible...
- 2:44This is a weird language.
- 2:45It uses white space.
- 2:46Let's keep going.
- 2:47Yeah.
- 2:47Yeah.
- 2:48So when someone credible said, you know, this is really cool, I figured that I would get
- 2:52into it.
- 2:52And so I kind of taught myself Python, started to introduce it at that time into a enterprise
- 2:58programming environment, which was interesting.
- 3:00They were using mainly C# and Visual Basic.
- 3:04It was kind of one of those things where whenever there was a problem and they needed something
- 3:07solved really quick, I would say, oh, I'll use Python for that.
- 3:10And they said, don't tell me what you're doing.
- 3:12Just fix the problem.
- 3:13So kind of flying it under the radar.
- 3:15And then, you know, discovered that I really loved it.
- 3:17So the next job, you know, I sought out Python programming positions after that.
- 3:22I think I was in...
- 3:23Python was around version 2.3 then.
- 3:26So obviously it's grown a lot of new bells and whistles since then.
- 3:30And just it's a lot more fun to program in even than it was then.
- 3:33Yeah.
- 3:33It's just both in terms of the language and whatnot, but also the ecosystem and all the
- 3:39packages.
- 3:40Right.
- 3:40It just keeps getting cooler.
- 3:41I think it's a fun place to be for sure.
- 3:44Yeah.
- 3:44Well, even back in, you know, 2002 or 2003 when I started doing Python, it was so far...
- 3:51Like what you got when you just downloaded the standard library was so much more than you
- 3:55got in any of the other languages that I was familiar with.
- 3:58So, you know, if you wanted to do anything in C++, you had to go out and find something
- 4:03that you would configure, make and make install.
- 4:06And then, you know, you'd be able to get those development libraries.
- 4:09But with Python, you know, you can download URLs, you can create an FTP server or, you know,
- 4:14things like that.
- 4:15And it's just built in.
- 4:16So that was a nice aspect of the batteries included at that time.
- 4:20Yeah, absolutely.
- 4:20So what are you doing with Python these days?
- 4:23What's your day job?
- 4:24So these days I'm a consultant, which basically means I do kind of a little bit of everything.
- 4:28So sometimes I'm out training different companies in how to do Python.
- 4:34So I've been to D.C. and California in the last two weeks.
- 4:40I sometimes do custom development for folks.
- 4:43I've got, I think, three active projects going on for that.
- 4:46And I'm working on a startup on the side.
- 4:48So, you know, everybody's got to have their little side hustle that they want to eventually
- 4:51make into something.
- 4:52So I guess I've got more of a three side hustles going at this point.
- 4:55Yeah, that sounds fun.
- 4:57It's a challenge to do all these different things.
- 4:59I know.
- 5:00But it's also fun to have a wide variety and not just be doing the one thing, right?
- 5:05Yes.
- 5:05Yeah, it is.
- 5:06It's never boring.
- 5:08Sometimes it can be a little bit overwhelming.
- 5:10Like, what am I supposed to work on today?
- 5:12And the context, which can get a little bit much.
- 5:14But, you know, it's all good.
- 5:16Yeah, it's better than the alternative, I think.
- 5:18Although you're right.
- 5:18It's definitely overwhelming.
- 5:20And you're still using MongoDB for some of these projects?
- 5:23I am.
- 5:23One of the things that I discovered is just it's kind of my go-to at this point.
- 5:28I know most people, they learn relational databases because it's much more widespread use.
- 5:34But I just kind of got used to using MongoDB.
- 5:36In my last business, it was the main database of choice.
- 5:40So, you know, I just, I have all the tooling and I have the familiarity with it.
- 5:44So that's just the first thing I reach for when I'm implementing something for someone.
- 5:47Yeah, I'm with you.
- 5:49I feel like it just has so much more flexibility and whatnot.
- 5:52I feel like a lot of people fall into using relational databases because that's considered the safe choice or that's what they already know or what they were taught in college.
- 6:04Or like the people they're working with, they already know that, but it's not necessarily because it's the best choice.
- 6:08Right.
- 6:08Yes.
- 6:09Yeah, absolutely.
- 6:11So I thought maybe, you know, we're mostly going to focus on sort of advanced MongoDB design patterns and implementation concepts.
- 6:20But not everybody listening to this is totally familiar with Mongo or NoSQL or document databases.
- 6:26So maybe you could give us a quick view just into the summary of what is NoSQL?
- 6:34What is a document database?
- 6:36And I'm also interested to hear your thoughts on what is NoSQL because everybody seems to have a slightly different definition of that.
- 6:42Okay, sure.
- 6:42So I would say, you know, NoSQL is kind of anything besides SQL.
- 6:47So if you want to kind of drop some of the constraints that SQL puts on you if you're programming.
- 6:54So if you look at things like transactions, do you want them to be atomic, consistent, isolated, and durable?
- 7:00If you drop some of those, then maybe you're in the NoSQL land.
- 7:03So a lot of the NoSQL databases, well, I guess I should say they kind of run the gamut.
- 7:09So a NoSQL database could be just a key value store.
- 7:13So you're able to look up things very quickly.
- 7:15It could be something that's more complicated, has an exotic model like Cassandra with a column, you know, column data store or MongoDB, which is a document data store.
- 7:25So, and that's more like the storage model, the programming model, what are you actually putting into this database?
- 7:31So I guess the, how I describe MongoDB is, or its document model is, if you think of the relational databases, you've got tables, which are made up of rows and columns.
- 7:42In MongoDB, we don't call them tables, we call them collections.
- 7:47And what you put in those collections are JSON objects.
- 7:49So if you've done web programming, you've probably run into JSON.
- 7:53So, and if you haven't, then it's Python dictionaries and lists embedded in each other.
- 7:58Basically, that's the data model you're looking at.
- 8:00So you just have a collection of these documents and, and we call them those JSON objects documents.
- 8:06And you can kind of query into those collections.
- 8:10So you can say, give me, say, all of the restaurants that are bakeries in this collection.
- 8:16And so you have some field in each JSON document that says, you know, the type of cuisine and it's a, it's a bakery.
- 8:22So you could do that sort of a query on a MongoDB database, which makes it a little bit different from a key value store because you don't have to just query based on the key of that document.
- 8:32Right. Key value stores are like the most basic, fastest, most scalable, but also the most limiting, right?
- 8:40Because you've got the key, the primary key, like an ID, or maybe you could use an email address or whatever if it's a user.
- 8:46But then it makes it really hard to ask interesting questions.
- 8:50Like the rest of the data is fairly an opaque blob.
- 8:53I know there's ways to kind of like add extra stuff around some of the databases, but still.
- 8:58Yeah.
- 8:58Well, and a lot of the time, those things, you know, you have the key value store and then people will create their manual indexes around that.
- 9:05So, you know, you've got kind of the natural key of something, maybe it's some restaurant ID and you want to look up things by cuisine.
- 9:11So what you have is maybe a whole bunch of things in another collection that is like cuisine is the key.
- 9:18So then those point to the restaurant ID.
- 9:20So they build their own indexes out of these things.
- 9:22MongoDB takes care of that for you in a lot of cases.
- 9:26Right.
- 9:27You know, one of the big distinctions, I think, that it takes people who are new to this idea of document databases.
- 9:33And MongoDB is not the only one, just the most popular and probably the best.
- 9:37But there's, you know, things like Azure DocumentDB, there's CouchDB, there's a variety of them.
- 9:43Right.
- 9:43And I think it's easy to look at these databases and go, oh, it's kind of like a JSON field embedded in another database or like storing a blob of data or something like that.
- 9:55Right.
- 9:56You're like, oh, I could do this and say Microsoft SQL server and just make like a text field or a JSON field and stick a blob in there.
- 10:02But the big difference is you can index deep into these things.
- 10:06You can do rich queries into them.
- 10:08Right.
- 10:08They're like, they're not just hierarchical things you can store.
- 10:12But, you know, even look at some of the object databases, things like ZODB, things like that.
- 10:18Right.
- 10:19Yes.
- 10:19So that is the big difference between this and a key value store, I would say, is that you can index into these things.
- 10:25If you're using something like, like you could take MySQL and throw a, you know, a blob column and an ID column into a table and call it a NoSQL database if you want to.
- 10:36But you don't really get the ability to do these sorts of rapid lookups on something other than the key.
- 10:43And that's what MongoDB gives you.
- 10:45That plus it gives you some scaling advantages as well.
- 10:48But my background, I haven't taken advantage of that as much as I've just taken advantage of the fact that you can do reasonable things very quickly as opposed to doing unreasonable things reasonably quickly.
- 10:59Right.
- 11:00I think that's a really interesting comment you made about the performance.
- 11:03Right.
- 11:03Like one of the things you can do with a lot of these NoSQL databases, Mongo included, is you can do lots of replication.
- 11:11You can do specifically, I'm thinking of like sharding, like I could set up a 10 sharded cluster.
- 11:17And so then when we do inserts, they're like crazy fast and we can parallelize our queries and our aggregation and MapReduce stuff, all those kinds of things.
- 11:24Right.
- 11:25But that is like the thing that draws people to it.
- 11:28They're like, oh, look what we can do with performance.
- 11:30But at the same time, like very few people actually end up needing that much performance.
- 11:35I mean, I've seen a few places where it was really needed, but 99% or more of the use cases don't.
- 11:41But everybody has a, my relational schema is a pain to deal with.
- 11:47It's hard to add columns.
- 11:48It's hard to change the shape of it.
- 11:50It's like a pain.
- 11:51It's slowing me down.
- 11:52It's have a, everybody has a complexity problem with their software.
- 11:56And I feel like modeling in these documents solves that complexity problem for everyone, not just the 1%.
- 12:02Right.
- 12:02To me, the sharding and the ability to scale out horizontally has always been kind of a safety feature.
- 12:09Like if things go really super well, my system's not going to fall over and I'm not going to have to come back and do manual sharding or partitioning in my database and re-architect my whole application.
- 12:19I know that there is a path forward if something like that happens.
- 12:22But for now, I can get things done faster than I could with any of the relational approaches.
- 12:28Right.
- 12:29Yeah.
- 12:29I totally agree.
- 12:30Totally agree.
- 12:30So one of the, there's a bunch of different ways to access MongoDB from Python, right?
- 12:37You've got the official Python driver, PyMongo from the MongoDB folks.
- 12:42You've got this new thing, BSON, NumPy, NumPy, BSON.
- 12:47I don't remember the order of it that goes straight into the data science type structures in NumPy.
- 12:52And then on top of PyMongo, we've got things like MongoEngine, Ming, MongoKit, and all these ODMs, right?
- 13:03Right.
- 13:03And one of these, named Ming, actually, is one that you created.
- 13:08Yes.
- 13:09Maybe give us a quick overview of what are the trade-offs?
- 13:12When would you consider using one of these ODMs?
- 13:15Like, what the heck is an ODM anyway?
- 13:16So it really comes down to the idea.
- 13:19So I said we don't have tables.
- 13:21And one of the other things that you don't really have when you're dealing with MongoDB is you don't have a database-enforced schema.
- 13:29So you have, it's kind of like this big bag of things.
- 13:34And I said that, you know, they're kind of like Python dictionaries.
- 13:37So I'll have to say this very quickly or very carefully.
- 13:40But you don't want to end up with a big bag of dicts when you're working on this.
- 13:45So what you need to actually have is some sort of a schema that tells you the sorts of things that you're going to put in these collections.
- 13:51Because it turns out whatever you're putting into them, when you read them back out, your code's going to have to do something with that data.
- 13:58You can't just say, well, I'm just going to store everything in there and it's magically going to reappear.
- 14:03Your code is making certain assumptions about what fields are in those dictionaries, what keys, and, like, what is the structure of the data that you're storing?
- 14:11So that's really what these ODMs or object document managers do.
- 14:16That tells you, you know, in this collection, we're putting things in that look like this restaurant.
- 14:20So although MongoDB, until very recently, didn't have any form of enforcing schemas, this would be something in your code where you're documenting it.
- 14:28At the very least, you're documenting what sorts of dictionaries or BSON documents you want to be putting into these collections.
- 14:36Right, and so these, if you go through one of these ODMs, object data mappers, layers, you basically go through predefined classes and objects in Python, which themselves have a fixed structure.
- 14:48And so you're kind of, you filter through, like, a known layer of schema, and that works pretty well, right?
- 14:55Yeah, yeah.
- 14:56And that allows you to kind of, you can get a long way without having any kind of a documented schema if you're the only programmer on the project.
- 15:04But once you start having multiple people, you need to have kind of a common understanding of, well, I'm going to write things that look like this to the collection, and I'm going to read things and expect them to look like this other thing.
- 15:14So that's kind of the base level of why you need something like this.
- 15:17You need a library or a data access layer.
- 15:19Sometimes people will write their own.
- 15:20That's a pretty common thing.
- 15:22If you don't use an ODM, then people will typically write Python modules that have, you know, getters and setters for different types of data that they want to put into the database.
- 15:31That's the approach that MongoDB uses on their training materials, I think.
- 15:34They just build this Python module that does these things.
- 15:38So an object document mapper allows you to kind of abstract that out and write those more quickly.
- 15:45So rather than saying, you know, I want to get a restaurant or I want to write a function that calls, you know, this is called get restaurant, then I can have a restaurant class that has a get method.
- 15:56But the get method is not something I have to write.
- 15:58It's something that the ODM provides me.
- 16:00I hope that makes sense.
- 16:01Yeah, I think it does.
- 16:02And a lot of these, I can't speak to me, you'll have to fill us in on the details.
- 16:07I don't remember exactly.
- 16:08But the one that I'm using right now is called MongoEngine, which is also one of the more popular ones.
- 16:13And it has a lot of additional things that it helps you with.
- 16:16Like you define a class and it's much like SQLAlchemy.
- 16:19You say these are the fields that go into the database.
- 16:22And this one's a string and it has to be unique.
- 16:25Like this one's an integer and I want an index on it and things like that.
- 16:29And so it'll actually apply the uniqueness constraints.
- 16:31It'll apply the index.
- 16:33It'll create and enforce the indexes, all those sorts of things as well.
- 16:37That's part of what Ming does.
- 16:38Definitely.
- 16:39You can put these constraints in there, these indexes that you want it to define, and it'll go ahead and create those indexes for you.
- 16:46Another thing that it's helpful or that it helps you with is your schema evolves as you're building your application.
- 16:53So maybe you didn't need a zip code when you first started or you forgot that you were going to need a zip code.
- 16:59And maybe that's a bad example.
- 17:01But there's some fields that you want to add later on and you need a sensible default for the existing documents.
- 17:07Like let's say you have an account class and you eventually want to start verifying that they've verified their email address.
- 17:13And you didn't think of that at first.
- 17:15You don't have a is email verified or something like that, right?
- 17:17Yeah.
- 17:18So maybe you want all of the existing documents to have that be default false.
- 17:22They haven't verified their email.
- 17:24So you can write a validator or a type into your schema that says, you know, when this field is not found, then I want you to populate the Python object with false.
- 17:36It helps you to do these sorts of on-the-fly data migrations.
- 17:41Right.
- 17:41And it can be kind of like you describe it in your book as a lazy, lazy migrations or lazy schema migrations.
- 17:49Because in a relational database, that wouldn't fly very well, right?
- 17:53You'd have to say, well, we're going to have we're going to do a schema transformation and we're going to add a column and it's going to be a type bool and it's going to be default.
- 17:59There's some kind of script you got to run probably.
- 18:01Yeah.
- 18:01And those I guess that's the deal.
- 18:04You when you're going to be changing the schema in a in a SQL database, you do it up front.
- 18:10So you've got to make sure that all of the rows conform to the schema.
- 18:13And, you know, the database is going to enforce that.
- 18:15So you do this alter table statement and it makes sure that everything conforms to it.
- 18:20So that's one approach.
- 18:21And you can do that in MongoDB as well.
- 18:23You just go in and you and you overwrite all of the existing documents.
- 18:27Right.
- 18:27It's maybe as in a SQL script.
- 18:29It's just a JavaScript script or something like that.
- 18:32Right.
- 18:33And you run that.
- 18:33Yeah.
- 18:33Mongo gives you the option of kind of waiting until you actually load a particular document to make sure it conforms to your current schema.
- 18:41So that's something that we built into Ming as well so that you could actually like read a document and then check to see does that document actually conform to my current schema.
- 18:49And if it doesn't, there was actually the ability to fall back and run a migration function on that document.
- 18:54So you could actually bring things forward at the moment when they're loaded out of the database.
- 18:58Oh, yeah.
- 18:58That's really cool.
- 19:00So one of the early success stories from MongoDB, I think, comes from SourceForge, actually.
- 19:07And you were part of this.
- 19:08I remember SourceForge.
- 19:10This is before there's a GitHub or anything like this, right?
- 19:13And SourceForge was used quite frequently.
- 19:14And I remember it was getting painfully slow.
- 19:18And then one day it was fast again.
- 19:20And you were involved in that somewhat.
- 19:23And that's actually partly where Ming came from, right?
- 19:26Do you want to tell us about that?
- 19:27Yeah.
- 19:28So when I came to SourceForge, I remember in the interview when I was about to come to SourceForge, I had worked on building a SQLAlchemy-like library for, I guess you could call it a NoSQL database.
- 19:41It was a private thing that the company that I was working for had developed internally.
- 19:44And so they asked me about that.
- 19:47And they said, well, you know, we've got this thing called MongoDB that we're thinking about working on.
- 19:51And it kind of stores Python dictionaries.
- 19:53So what would your approach be to doing something like that?
- 19:56And so, you know, I kind of talked about it.
- 19:58And I guess my answer was good enough.
- 20:00And they hired me.
- 20:00And they said, you know, we did some performance evaluations.
- 20:03And at that time, it was like 2009.
- 20:05They looked at various different approaches.
- 20:07And they said, MongoDB is going to give us the performance that we need.
- 20:09And we're comfortable with the data model.
- 20:12So we like the idea of storing things that look like Python dictionaries into the database.
- 20:16But we would like to have something like some kind of a schema enforcement layer or an ODM.
- 20:22Although I don't know that that was really a big term at that point.
- 20:25They maybe called it an ORM even.
- 20:27Yeah.
- 20:28I call it an ORM for a non-relational database.
- 20:31Yeah.
- 20:31ORM minus the R.
- 20:32Yes.
- 20:32So, you know, we started working on that.
- 20:36And I was the main developer on Ming.
- 20:40And so Ming formed kind of the data layer of a complete rewrite of all of the SourceForge developer tools.
- 20:47So when you think about SourceForge, there's kind of two sections of it.
- 20:52I mean, if you think about SourceForge these days.
- 20:54But there's sort of the, this is the site for the developers to build their software.
- 20:58And this is the site for users to download software.
- 21:02This portion of Talk Python is brought to you by Advanced Digital.
- 21:06How would you like to build one of the most visited news sites in the U.S.?
- 21:10That sounds fun.
- 21:11The folks at Advanced Digital would love to talk to you.
- 21:13They're primarily a Python shop located in beautiful Jersey City.
- 21:16Just one subway stop from lower Manhattan.
- 21:19Spend your time building an amazing web app with Python.
- 21:22And do it with a small team of developers focused on agile development.
- 21:25Are you going to miss PyCon this year because your company wouldn't fund the travel and expense?
- 21:29If you join this team, they'll cover your conference and training initiatives.
- 21:33It's time to take your Python to the next level.
- 21:35Build an amazing web app.
- 21:37Get started by visiting python.advance.net right now.
- 21:41So we rewrote kind of all of the developer tools.
- 21:44And we rewrote a lot of the download side of things as well.
- 21:49And that was actually a migration from PHP to Python.
- 21:52And a migration from largely Postgres backed to mostly MongoDB.
- 21:58And we kind of did it in stages.
- 21:59But Ming was a big part of that.
- 22:01Being able to kind of come in and say, we've got a group of programmers working.
- 22:05What's our common understanding of the data that we're storing in this weird database that none of us has seen before?
- 22:09Right.
- 22:10And how did it go?
- 22:12I recall that there were some pretty major stats in how much better the site got.
- 22:18How much fewer, how many fewer database servers there were?
- 22:22Things like that.
- 22:23Do you recall?
- 22:23Well, I remember we went from handling, it was something like 13 servers that were running the PHP front end.
- 22:30Our first deployment, we went down to, I believe, four Python servers doing basically the same work.
- 22:37So that was a nice, nice thing for Python.
- 22:40And of course, the PHP was backed by Postgres and the Python was backed by Mongo.
- 22:44And one of the other things in the first version, this is what a lot of people did with Mongo at the time, and I guess still probably do, is when you're introducing this new technology, you kind of take baby steps.
- 22:54So Mongo was not our system of record initially.
- 22:57We would use it as kind of a cache for all of the Postgres data that was coming from the legacy system.
- 23:02So all of that went into Mongo.
- 23:05And then as long as you obey a few little rules like make sure your working set fits into RAM, Mongo behaved, its performance was closer to memcached than it was to a relational database.
- 23:17So, you know, super fast for a read mostly workload.
- 23:22And that's why we were able to do nice things.
- 23:25And then we, like a lot of people who first deploy MongoDB, we think, oh, this is great.
- 23:30It can probably do anything I want it to do.
- 23:33So we wrote a little rate limiter in MongoDB.
- 23:36And we did it in a really stupid way, it turns out, by basically just logging every request.
- 23:41And then every time a request comes in, we would query to see how many requests from that IP in the last X seconds or minutes or whatever a rate limit was.
- 23:50And that worked until it didn't, which was when the index got bigger than our RAM.
- 23:55And you got this nice cliff of performance.
- 23:59So we reworked that.
- 24:01But, you know, for the most part, it was a pretty good rollout.
- 24:04And, you know, a lot of success moving from PHP to Python.
- 24:10And there's still things that run on.
- 24:13Last I heard, there were still things that ran on Postgres at SourceForge.
- 24:17But it was primarily MongoDB later on.
- 24:20Yeah, okay.
- 24:21That makes a lot of sense.
- 24:22That's really cool.
- 24:22Is it still running in Mongo, do you think?
- 24:25Do you know?
- 24:25Well, it is.
- 24:26So the first version that we rolled out was only for the download side.
- 24:31And then we ended up rewriting all of the developer tools in Python and MongoDB.
- 24:34And then that ended up being outsourced, not outsourced, open sourced as the Apache Allura project.
- 24:42So it's now an official Apache Software Foundation project.
- 24:45And anybody can run the same tools that SourceForge is running for developing software.
- 24:51And there's a little bit of setup involved.
- 24:53But it's still out there.
- 24:55It's something that was kind of a goal early on that we wanted to make sure that we gave back to the community with what we were doing.
- 25:01And, of course, Ming was always open source from the beginning.
- 25:03SourceForge has had its moments of evil, but generally has been a good supporter of open source software.
- 25:12Yeah, I say historically, it's probably got a positive grade, all in all.
- 25:16Yeah.
- 25:16All right.
- 25:17So one of the things I really want to dig into while we're talking is your book called MongoDB Applied Design Patterns.
- 25:24But before we get to that, I just want to quickly write an idea by you and maybe make a plea to anyone who is either running or considering running Mongo.
- 25:31I think I'd love to hear your opinion.
- 25:34One of the things, I think Mongo is super great, but I think they've made a few fairly minor decisions that have come back to haunt them in certain ways that get amplified from the early days.
- 25:47And I think one of those is, by default, not running encrypted connections.
- 25:52And another is, by default, not running with authentication.
- 25:55Yes.
- 25:56So their defaults have always been interesting.
- 25:58Maybe I'll use that word.
- 26:00I think they've optimized too much for performance and scalability and not enough for durability and safety nets.
- 26:07I'm thinking of the initial write concern defaults.
- 26:10I'm thinking of the lack of journaling in the early days.
- 26:13You know, all these things.
- 26:15And each one of them maybe made sense in their original world.
- 26:17But I think people have taken these and not knowing they need to be aware of them got themselves in trouble.
- 26:22Absolutely.
- 26:23So when we started out, the default way that you wrote to MongoDB, if you didn't change any of the settings and you do an update or an insert or whatever, basically you got an acknowledgement from the server that, hey, I received your request to write this data to the database.
- 26:39What you didn't have was any assurance that it actually made it onto disk.
- 26:43We didn't even get an acknowledgement.
- 26:44Yeah.
- 26:45Not even into the data set and memory.
- 26:47Just the servers received your socket request, basically.
- 26:50I think we even didn't get that initially.
- 26:52Yeah, I think you might be right.
- 26:53Yeah, you could be right about that.
- 26:54So everybody learned, first of all, that you needed to have this magic argument when you connected called safe equals true.
- 27:01So by default, MongoDB was running an unsafe mode, which is kind of a silly thing to do when you think about it.
- 27:08It's cool.
- 27:08It's fast.
- 27:09Yeah, it was certainly fast.
- 27:10And somebody made a nice web video about web scaleness from that.
- 27:15Dev null is very fast, too.
- 27:16Yeah.
- 27:17I can write an infinite amount of data to it super quick.
- 27:21But so everybody, you know, we moved over to safe equals true.
- 27:23But even then, you just got an acknowledgement that server received your request and maybe it didn't violate any unique key constraints.
- 27:30So, okay, great.
- 27:31That's some progress, but it might not make it to disk.
- 27:33And so they told you, well, you need to really run in replication.
- 27:37So then you could get some, you could say, well, I want to only consider my right to be complete once it's been also written to another server.
- 27:45Okay, fine.
- 27:45Well, that's pretty good.
- 27:46That's if you're actually getting verification of replication, then you're probably running in a slightly safer mode than most people are writing to MySQL.
- 27:53So I would say that's a good place to be.
- 27:56But then they've also got this network issue that by default, you get MongoDB, you fire it up, and it's going to bind to all of the IP addresses on the machine with no authentication and no encryption.
- 28:07And anybody can connect to it, read, write any of the data that's on the database.
- 28:11So that is not really a good default state to be in.
- 28:16And it turns out a lot of people didn't read their docs when they moved to production.
- 28:21And there was a big exploit recently where there were thousands of production MongoDB databases that were compromised because they were running completely wide open to the intranet.
- 28:30So, yeah, be careful.
- 28:32Yeah, absolutely.
- 28:32So basically, I bring this up for two reasons.
- 28:35One is there's a lot of FUD about Mongo involving things about this write concern and the journaling, and all those are changed, right?
- 28:45The defaults are to do the right thing these days.
- 28:48So those are basically phased out.
- 28:50But this last thing about the security is not.
- 28:53If I were king of Mongo, I'm not a king of MongoDB.
- 28:56But if I were, I would make it a change that unless you set up authentication, it will only listen on localhost by default.
- 29:05Right.
- 29:05That would be my rule.
- 29:07And that's kind of safe.
- 29:08Like, if you're running the server next to your web app or for dev, it's fine.
- 29:12And if you want to do something production-wise, you've got to configure it a little better.
- 29:15But that's not how it works.
- 29:17So just if you guys are listening and you want to run Mongo, definitely, we both definitely recommend it.
- 29:22Just make sure you turn on security or you don't listen.
- 29:25Just unprotected on the internet, right?
- 29:28Just take a few steps to enable encryption if you're going to go across networks and security authentication, things like that.
- 29:34Yeah.
- 29:34And I think the latest versions of the RPMs and the Debian packages do bind only to the local host.
- 29:43So at least they're a little bit more secure.
- 29:44But still, if you're just running the MongoDB binary by default, it's going to listen to anything.
- 29:49So, yeah, be careful.
- 29:50Yep.
- 29:51Yep.
- 29:51And that also, it's not just a server production thing, right?
- 29:54Like, that could be a dev issue.
- 29:57Your dev machine could be on the network and you could be running a dev version with live data and it could have the same problem.
- 30:04So just be careful about this.
- 30:05Yes.
- 30:05All right.
- 30:06So let's talk about your book, MongoDB Applied Design Patterns.
- 30:10That's the title, right?
- 30:11Yes.
- 30:11Okay.
- 30:12I didn't copy.
- 30:12It's not a paraphrasing.
- 30:13Okay, good.
- 30:13So this is a book that looks at MongoDB from a Python developer's perspective.
- 30:19And really, I think it's a super book.
- 30:22The idea is to look at a bunch of different use cases and challenges and try to solve them, right?
- 30:28Right.
- 30:28The genesis of the book is MongoDB needed to, or they wanted to have something like a list of different use cases.
- 30:35Like, how do you use MongoDB in this situation?
- 30:38And so I wrote up a bunch of use cases for them and then they said, you know, this would be a really good book.
- 30:43So let's see if we can introduce you to some people at O'Reilly and see if we can kind of flesh these out into a full O'Reilly title.
- 30:51And so that's what we ended up doing.
- 30:53Yeah.
- 30:53And that book came out in 2013, right?
- 30:56Sounds right.
- 30:57Yeah.
- 30:57Yeah.
- 30:57Yeah.
- 30:57And MongoDB 2.
- 31:01Something 2.2, 2.4 sort of time zone.
- 31:04How much of it do you think is still current?
- 31:06And how much do you think is sort of slightly changed with a release like, say, MongoDB 3?
- 31:12There are definitely changes to some of the performance concerns that have to do with the way that the storage engine works since version 3.
- 31:19Because they switched to WiredTiger by default and not mem map files, yeah?
- 31:23Yeah.
- 31:24So the nice thing, nice maybe in quotes here, for programming against MongoDB in the olden days before WiredTiger is it was really easy to understand the memory model.
- 31:34Because what they did is they just took your whole database and they mapped it into RAM.
- 31:38And they used the Linux virtual memory system to decide what was in and what was out.
- 31:44So if you know how to modify memory, then you knew the most efficient way to modify MongoDB.
- 31:49With WiredTiger, that's changed.
- 31:51They have a real storage engine.
- 31:53You know, it has multi-version concurrency control.
- 31:56It has some interesting, interesting in a good way, performance characteristics of being able to, you know, have multiple writers going at the same time.
- 32:05So I would say some of the things that really optimized for in-place modification in my book don't really apply as much.
- 32:14Because there was a huge difference in performance in the old storage engine between writing to something in place on the disk and doing something that, say, changed the size of a document and required MongoDB to write a whole new copy of the document somewhere else on the disk.
- 32:29Right.
- 32:29And the way it works now, it's totally different.
- 32:32So, all right.
- 32:32But my look, when I went through it, I felt like this is really still quite current.
- 32:37I think you're right about probably the considerations around the memory map files and whatnot.
- 32:41But other than that, it looked really good.
- 32:44So let me read a really quick excerpt from the book just to kind of set the stage.
- 32:49So you say, traditionally, relational databases, while familiar, present significant challenges and complications when trying to scale up to big data needs.
- 32:59And into this world steps MongoDB to address the scaling.
- 33:03And around all of this height and excitement, a bunch of sites grabbed a NoSQL database, MongoDB database, and threw it out there and just started working with it without really understanding that it takes a different thinking about it.
- 33:18Right.
- 33:18It's paraphrasing.
- 33:19Right.
- 33:19But it's basically – and some of these things we just talked about around the durability and security were one of the things.
- 33:27But I think more – probably the biggest mind shift that you have to make in this world, and you start – you dedicate a significant part of your book to this right at the beginning, and I think you should, is schema design and document design relative to, say, first normal form and third normal form and all that.
- 33:46Yes.
- 33:47So I would say that the biggest mindset shift that you've got to get through to be effective at MongoDB schema design is to say how you – what happens when you get rid of joins and what happens when you get rid of transactions.
- 34:00So it's kind of the reads and writes.
- 34:02So MongoDB does not support the join operator.
- 34:05Well, there's a way to do it in the aggregation framework.
- 34:07But besides putting that aside, generally, when you do a query in MongoDB, you can get a collection of – or you can get a set of documents in your results set, but you're not going to be talking to two different collections when you do that.
- 34:19You're going to be making a query against a single collection.
- 34:22And you'll get documents from that single collection.
- 34:24And so the question is, how do you actually use that in an efficient way?
- 34:29So if I was building a blog in a relational database, then maybe if I need to render that blog post, I would maybe fetch something from the posts collection.
- 34:37I would fetch something from the authors collection.
- 34:40I would fetch something from – or not collection, but the post table, the authors table, comments table.
- 34:45And I'd do a join of all these things, and you'd end up with all of the data that you need to represent that blog post to a web user.
- 34:54Well, with MongoDB, you can do the same thing.
- 34:56You could have a posts collection and a comments collection and an authors collection, and you can do kind of the join-y work in memory.
- 35:03But you've gotten rid of a lot of the benefits of MongoDB because the nice thing about MongoDB is you can design your schema so that a single document can satisfy that web request.
- 35:12So you could have the post with the embedded author information with all of the comments all in a single document.
- 35:18So basically, you're doing a single fetch, a single round trip to the database.
- 35:22And even on the database, if you're using a disk or you're using an SSD, whatever the case is, you've got all the locality right there.
- 35:28So the whole document is right where Mongo is looking at that time.
- 35:32And so it's able to basically just do things much more efficiently if you design your schema right.
- 35:38Yeah, and I think it's very much a Shakespearean type of thing, like to embed or not to embed.
- 35:44That is the question, right?
- 35:45Like really, every time I sat down to design a new data model for MongoDB, it's like, what are all the pieces?
- 35:51What embeds where and what shouldn't be embedded for various reasons, right?
- 35:55So like, for example, you mentioned you could have your post and it could have the author embedded and it could have the comments embedded and so on.
- 36:04Maybe even there's categories, right?
- 36:07Like categories and things that you could theoretically embed the category data into the post, but then you have to replicate that across all the different posts, right?
- 36:15Sure.
- 36:15Yes.
- 36:16That may or may not be something you want.
- 36:18Yeah.
- 36:18So you still have relationships in your data.
- 36:21That's a logical concern, right?
- 36:22You can do an entity relationship diagram and you can still map that onto MongoDB.
- 36:28The difference is with Mongo, when you have one of these one-to-many relationships, all of a sudden you now have the option, if it makes sense performance-wise, that you could take both of the entities and put them into a single collection.
- 36:39But you can't do that in a relational database, right?
- 36:43Relational kind of first normal form says you don't have multiple entries in a column.
- 36:48But with MongoDB, that's sort of the norm.
- 36:50You're allowed to have these array types that are being stored there.
- 36:54So now you've got to decide, does it make sense to put it there?
- 36:56Or if you've got a many-to-many joint or a many-to-many relationship, the old way of doing it or the SQL way of doing it is you've got to have a join table that's got IDs from table one and IDs from table two.
- 37:07And it tells you which ones match up with which ones.
- 37:09MongoDB, if you're doing a blog, again, it's just an easy example.
- 37:13So you've got tags or categories.
- 37:15A lot of the time, that'll just be a list of strings that you put into the post.
- 37:19And there's no need to actually have that join collection or that join table that you would have in SQL.
- 37:25I think that's totally right.
- 37:26And even if your tag thing was more complicated, right, you can do these many, many relationships and maybe store a list of tag IDs in every post.
- 37:37Right.
- 37:37And then reach back into the other table.
- 37:39Yeah.
- 37:39Yeah.
- 37:40You'd almost never want to have something like a join table in MongoDB.
- 37:44I can't think of a good case.
- 37:46You'll almost always want to either have a list of IDs in collection A or a list of IDs in collection B or both.
- 37:53But you wouldn't want to have a separate collection where the existence of a document means that these other two documents are joined.
- 37:59Yeah.
- 37:59I find that to be almost never.
- 38:01I don't think I've ever seen that in a well-designed case either.
- 38:05I definitely have never made use of it in the apps that I built.
- 38:08That was one of the problems with people coming from the SQL world is they know how to model things there.
- 38:12And they just assume that if I take the same schema that I had in SQL, it's going to be like that but faster if I do it in MongoDB.
- 38:19Yeah.
- 38:20Because I heard Mongo is faster.
- 38:21So it'll be faster if I just put this over here.
- 38:23Exactly.
- 38:24Yeah.
- 38:24It probably is faster, but not because you copied over your schema design from a relational database.
- 38:30Yeah.
- 38:30Or in many cases, it would end up being slower because you're doing all of the logic of the join at that point, but you're doing it in whatever your programming language is.
- 38:39So I love Python, but it's not this super high-performance bare metal language.
- 38:44If you're building a join engine in Python, yeah, you can do that.
- 38:48But you are now talking about introducing network latency to talk to the database.
- 38:52You're talking about it's written in Python.
- 38:54It's not written in C++ like the MongoDB engine is or like C database engines might be in other cases.
- 39:00So you're kind of, if it's faster, then it's an unusual situation.
- 39:05You're usually going to kill yourself performance-wise.
- 39:07This portion of Talk Python To Me is brought to you by Hired.
- 39:11Hired is the platform for top Python developer jobs.
- 39:15Create your profile and instantly get access to thousands of companies who will compete to work with you.
- 39:20Take it from one of Hired's users who recently got a job and said,
- 39:23I had my first offer within four days and I ended up getting eight offers in total.
- 39:27I've worked with recruiters in the past, but they were pretty hit and miss.
- 39:31I tried LinkedIn, but I found Hired to be the best.
- 39:33I really like knowing the salary up front and privacy was also a huge seller for me.
- 39:38Well, that sounds pretty awesome, doesn't it?
- 39:40But wait until you hear about the signing bonus.
- 39:42Everyone who accepts a job from Hired gets a $300 signing bonus.
- 39:45And as Talk Python listeners, it gets even sweeter.
- 39:48Use the link talkpython.fm/Hired and Hired will double the signing bonus to $600.
- 39:53Opportunity is knocking.
- 39:56Visit talkpython.fm/Hired and answer the door.
- 40:00Yeah.
- 40:01So one of the things while we're on this document design stuff is in MongoDB, there's no concept of a foreign key constraint or relationship.
- 40:09Right.
- 40:09I can't have one document with a strict relationship to another document.
- 40:13Right.
- 40:14I'm not really sure how much value you get.
- 40:15There's no joins and things like that.
- 40:17Like, so oftentimes people think that means there's no relationships in MongoDB.
- 40:22Right.
- 40:23Yeah.
- 40:23But I don't think that that's true.
- 40:24I think you can put them into these models.
- 40:27They just don't span documents, right?
- 40:28Right.
- 40:29Yeah.
- 40:29You can have, you know, the relationships can exist within a document and you get atomic updates and things like that.
- 40:35So you get the database to enforce some consistency there.
- 40:38And you can also model the relationships with, I mean, it's not enforced by Mongo, but you can have a foreign key concept where you've got an ID of a different document in another collection and you're storing that ID.
- 40:50Differences that you always have to take into account the possibility that that document might not actually exist.
- 40:55That's right.
- 40:56Yeah.
- 40:56I think of them as two things.
- 40:57I have a slightly different name that I've used over the years for it.
- 41:00Like for the stuff that's within your document, you've got a post and it has a list inside of it of comments.
- 41:06Like that is a super strong relationship.
- 41:08You can't have a comment without the post.
- 41:10It is the same thing.
- 41:11But if you were like reaching back to an author table through just a foreign key constraint, that doesn't really exist, but it's logically there.
- 41:18I call those soft foreign keys or something like that.
- 41:23Like they're not enforced, but they technically, they fill the same role, right?
- 41:26Yeah.
- 41:26They fill the same role.
- 41:27And sometimes people call them references or document references.
- 41:30Way back when I started with MongoDB, one of the patterns that they kind of promoted was storing the collection name along with the ID.
- 41:39I never found that super valuable, but that's another thing that you can do.
- 41:42If you want to have a reference that could go to any collection, then you can just throw the collection name in there.
- 41:46Yeah.
- 41:46Yeah.
- 41:47Interesting.
- 41:47That works well at the low level, at like the PyMongo level.
- 41:50Less good at the ODM level.
- 41:52Right.
- 41:52So let's talk about some of the use cases.
- 41:55So we've kind of set up this, you talk a lot about like, this is what modeling in this world looks like.
- 41:59You also talk about mimicking transactional behavior with compensation models that work well in MongoDB.
- 42:05But let's just kind of leave that as there.
- 42:07So you kind of set the ground with some of these foundational things.
- 42:10And then you say, let's talk about six different use cases, all the performance considerations and how you model it and everything.
- 42:18Right.
- 42:18So do you want to touch on some of your favorite ones there and maybe like what was non-obvious or maybe something like that?
- 42:24Yeah.
- 42:24So the first one is, has some of the more interesting parts, I think, or some of the things that I found really interesting.
- 42:30And I guess that's why I put it first, but that's the operational intelligence chapter.
- 42:34And it's really focusing on analytics and dealing with kind of high volume data that's coming in quickly.
- 42:40There were two different use cases in there, or maybe there were three in there.
- 42:45But two in particular that I remember were, one of them was incremental aggregation.
- 42:49So this is, you've got something coming in, you've got these aggregate statistics that you want to report out immediately.
- 42:56So one approach that you could do for aggregation is you can run a big MapReduce job on a Hadoop cluster, and that'll come back in a few minutes.
- 43:05But if you actually want something that's up to the minute, then how do you do that in an efficient way?
- 43:10And so this relied a lot on the in-place updating, and it was based on MongoDB's own, it's not called Cloud Manager, but their monitoring service, which would actually monitor MongoDB performance for you.
- 43:22And they offered this as a free service.
- 43:23So it was like, how do we deal with this scale?
- 43:25So let me show you how you can build your schema to deal with that kind of scale and how you can keep the performance high, even with an in-map storage engine.
- 43:33No, I just, I think it was, what I found interesting about this was you start from, like, let's start with log file data, like something out of Apache web request or something like that.
- 43:44Let's put that in the database.
- 43:46And then let's start doing, like, processing and analysis of it.
- 43:50And you have some really interesting graphs and various things that say, like, let's look at, if we design it this way, what are the trade-offs?
- 43:58What is the benefits, what are the drawbacks?
- 44:00And there was a number of non-obvious ways in which things kind of slowed down or got out of control.
- 44:08And you ended up with quite an interesting aggregation report database, right, where you pre-computed and pre-allocated a whole bunch of pieces and then used some of the in-place update operations to sort of, like, increment the numbers at the right levels as these things came in, right?
- 44:24Yeah, that was the incremental aggregation one.
- 44:26So that was, the problem there is it was storing the aggregates in these large documents.
- 44:33And sometimes the documents would grow and that would cause performance problems.
- 44:36And then you get into a secondary issue, which is that even though you think of these things as Python dictionaries, which are super fast to access any item in them, physically they're stored as a list of key value pairs on the disk.
- 44:51And so it turns out it takes longer to access something towards the end than it does to take to access something towards the beginning.
- 44:57So how can we mitigate that issue?
- 44:58And those were just some sort of the sorts of things that you can only see when you've actually run some performance metrics against it.
- 45:05Again, just a shout out to Python.
- 45:06I did all this with, at that time, IPython notebook and printed out the graphs and, you know, just threw those into the book right there.
- 45:14So I think those are actually screenshots from IPython, now Jupyter notebook.
- 45:17Yeah, they looked like some Matplotlib graphs or something, which is cool.
- 45:21Yeah.
- 45:21All right.
- 45:22So another thing that people, at least in the early days, were like, oh, you can't use MongoDB for this was e-commerce, which I totally disagree with that statement.
- 45:29But you have a section where you talk about using MongoDB for like an e-commerce site, right?
- 45:34Yeah.
- 45:34So one of the big things or one of the difficulties with existing e-commerce, I guess the big one is Magento.
- 45:42So Magento uses an entity attribute value store.
- 45:46So they're still stuck on SQL, but they use SQL in a way that makes it non-relational.
- 45:51Basically, instead of keeping your products in a products table where each one of the attributes of that product is a column, they just say, I've got one big table that says for this entity, maybe it's a shirt.
- 46:02I have an attribute, which is a size, and it's an Excel.
- 46:05For this entity, which is a drill, it has, you know, some other attribute, and it's, you know, 120 volts or whatever.
- 46:11And so out of that, they're able to get this very flexible schema.
- 46:15So it's kind of like, well, that's not really a fantastic way to map to the relational model.
- 46:20But they kind of have to because you want to deploy to a store that might have all sorts of different items in it that have different attributes that you want to store.
- 46:29Nice thing about MongoDB is not all of your documents have to look like each other inside the collection.
- 46:35So Mongo lets you actually say, well, I want to store drills and shirts in this collection.
- 46:39Can I do that?
- 46:40And it turns out you can.
- 46:41Maybe there's certain attributes that they all have in common.
- 46:44They have an SKU number.
- 46:46They have a price.
- 46:47They have maybe a quantity available.
- 46:50But then they've all, you know, got their other things that are custom to each one.
- 46:54And so you can introduce this polymorphism with MongoDB in a much more natural way, I think, than using something like an entity attribute value schema in a relational database.
- 47:03Yeah, I think that's leveraging a pretty interesting aspect.
- 47:06And you're in some sense implementing inheritance for specialization.
- 47:12Not exactly, but something to that effect, right?
- 47:14And that because the schema is really enforced at the application layer, not in the database layer, that flexibility pretty much just flows through.
- 47:24And you end up with these sparse objects.
- 47:25Like maybe one document has a drill bit size or something.
- 47:29The other one has a shirt size, right?
- 47:31And those don't appear in both records.
- 47:33You don't waste the space.
- 47:34Yeah, exactly.
- 47:34And you can build your ODM to kind of take care of that.
- 47:40I don't think I haven't been doing a lot with Ming super recently, but I'm not sure if we had the ability to kind of discriminate based on the data that it loads out as to which physical type of object it's creating.
- 47:52But that's certainly something that you can do with an ODM.
- 47:55And I know it's something that SQLAlchemy does with relational databases, but it requires you to either do a super complex schema in SQL or it requires you to waste a lot of columns.
- 48:07And those are kind of your two options to do this sort of object oriented polymorphism.
- 48:11Nice.
- 48:11So what are some of the other ones that you cover that you really like?
- 48:15So I did have some fun with the online gaming chapter because that was just, I don't know, games are fun, but kind of like brainstorming out like what are some of the data structures that you might need when you're building this?
- 48:26How do you do these in a, say, it's a massively multiplayer online game?
- 48:31How would you actually store this?
- 48:32How would you scale it?
- 48:33How would you do the sharding?
- 48:35The online advertising networks was also interesting just because it's a very high frequency sort of application.
- 48:42And it's something that I had seen a little bit of at SourceForge.
- 48:45And, you know, one of the things that you mentioned earlier on was, you know, SourceForge got slower and slower and slower.
- 48:52So part of that we can blame on maybe PHP and Postgres, but part of it we just have to blame on the ad networks.
- 48:58Because SourceForge is an advertising supported site, a lot of these ad networks just took a long time to render the ad.
- 49:04And that's kind of slowing down your browsing experience and can cause various other problems.
- 49:09So what if we could speed those things up and deliver contextual advertising to people in a way that doesn't make them want to pull their hair out?
- 49:16So that was also an interesting one.
- 49:18Yeah, that's a fun one to work on.
- 49:20And I know a couple of people working in this ad network space and they're using Mongo and they have some pretty intense requirements around the traffic that they handle.
- 49:31Because if you run ads on a site that gets, you know, a million views a day and that's just one of the places, right?
- 49:37You all of a sudden are getting a million requests a day.
- 49:40You're getting a million requests a day and you're trying to target those ads now based on some content, you know, that's going on in the article.
- 49:47So presumably you've indexed that and you know something about the keywords, but then you probably have some real-time bidding going on for those too.
- 49:54So how do you actually choose the ad inside that request-response cycle?
- 49:58Because you know that your content people that are actually paying or that, you know, you're advertising on their site, they're not going to like it if you slow down the experience for their viewers.
- 50:08No, absolutely not.
- 50:09So, yeah, that's definitely a cool example.
- 50:11So there was a bunch of great examples and I learned a lot from looking at how you implemented them and the trade-offs.
- 50:17And it's a great book.
- 50:19I definitely recommend if people are, they know a little bit of Mongo and they're like, I think I should be using this, but I don't really know how to solve this problem.
- 50:25There's a lot of good stuff to study there around schema design and whatnot.
- 50:28Well, thanks.
- 50:28Yeah, you bet.
- 50:29So there's a couple of options on where you might run your MongoDB server.
- 50:34And I guess it depends on how complicated of a situation you have.
- 50:39on how much you want to think about this or need to think about this.
- 50:42If you're just running a single server and it's just like there on a machine, maybe you can run that on a VM.
- 50:47You still got to deal with backups and whatnot.
- 50:49But there's also like hosted Mongo.
- 50:52They have MongoDB Atlas.
- 50:56What are your thoughts on like if somebody comes to you and says, hey, I want to do the site and run maybe let's say a three node replicated cluster?
- 51:06Like what would you consider?
- 51:07I would, by default, I would hope that their budget would afford them to get Atlas.
- 51:13So Atlas is actually the cloud service by MongoDB.
- 51:15They'll host your Mongo for you.
- 51:17They'll host the latest copy or the latest version, handle your backups and everything.
- 51:23Now, if you're dealing with a large amount of data, the backups can start to get pretty pricey.
- 51:27So that might not be an option.
- 51:29But unless you have strong operations people on your team, I wouldn't immediately jump to saying, oh, I need to self-host.
- 51:37I need to build it.
- 51:38I need to run it on my own VMs.
- 51:40So there's other options that you can go to.
- 51:42You can go to ImLab is one that I've used in the past.
- 51:46I've really enjoyed working with them.
- 51:47They provide, you know, hosted MongoDB, Compose.io, Object Rocket.
- 51:52These are all hosted MongoDB options that you can go with.
- 51:56And then if you are going to decide to self-host, there's actually some MongoDB provided tools to do that.
- 52:03So if you actually go into the MongoDB Cloud Manager, provide them your EC2 account keys, for instance, and you say, I want to use these three servers or these three virtual machines that I've provisioned to make a three node replica set, then they can do that for you as well.
- 52:19So that would probably be, you know, the next step is get your own VM or get your own VMs and then install Cloud Manager and go ahead and have Cloud Manager install that.
- 52:29Okay, cool.
- 52:30And the Cloud Manager, that's from MongoDB themselves?
- 52:32Yeah, that's also from MongoDB.
- 52:33So all of these things kind of run in the same UI on MongoDB.
- 52:37I guess it's .com.
- 52:39I know they have .com and .org both.
- 52:41Yep.
- 52:42That used to be a big confusion.
- 52:43You couldn't find the download link on .com.
- 52:45Yeah.
- 52:45One thing I'd like to say is I have used MongoLab before, MLab.
- 52:52It used to be MongoLab.
- 52:53They renamed it.
- 52:54And I think they're one of the few options that has a free Mongo server.
- 52:59So if you want to just set up a little prototype and get started and play around, they have like a half a gig free server you can set up and use there.
- 53:08And so that's pretty sweet.
- 53:10They're great.
- 53:10I use them.
- 53:12I still use them today.
- 53:13I use Atlas a little bit, but I use MLab as well.
- 53:16One of the nice things about MLab is that there's an integration to Heroku as well.
- 53:20So if you're using Heroku, you can get the MLab plan for free, and then it's just kind of like I'm not running a server anywhere.
- 53:26Somebody else is doing it for me, and I can play around with things and have them work and with authentication enabled as well.
- 53:32Yeah.
- 53:32Yeah, those all come set up correctly, let's say.
- 53:35Yes.
- 53:36Perfect.
- 53:37All right.
- 53:38Awesome.
- 53:38So, yeah, just right now I'm running my own MongoDB server on my own VM, but I've been working with Mongo for six years.
- 53:45So I feel like that's probably a point at where I can go run my own VM and do my own backups daily, things like that.
- 53:51But, yeah, these are all good options, and I know that jumping on one of the hosted ones is pretty nice to get started.
- 53:56So let's talk about some other stuff that you've been up to.
- 54:00First of all, like all of this MongoDB work, you now just came out with a MongoDB course for Python developers, right?
- 54:06Yeah, so I'm working with Pact Publishing, and they wanted to put out some courses on MongoDB.
- 54:12And I just came out with a video course called Developing with MongoDB and kind of a three-hour course that gives you an intro, both of what is MongoDB, how does it work, how is it different from relational databases,
- 54:25takes you through using it with Python, takes you through some schema design.
- 54:30It doesn't get into some of the big data analytics, you know, using it with Hadoop or some of the other things, but it does give you a good foundation in MongoDB.
- 54:40And, you know, I was happy to say that that was just published yesterday, which would be the 25th of April.
- 54:45We're recording on the 26th, so happy to see that out there.
- 54:48Yeah.
- 54:49How's that for timing?
- 54:50Perfect, huh?
- 54:51Yeah.
- 54:51Nice.
- 54:52That's cool.
- 54:53That must have been fun to make.
- 54:55And you also, speaking of ODMs, you wrote a book with the R instead of a D in there as well, the ORM, right?
- 55:00I did.
- 55:01This is prior to my involvement with MongoDB, and the name of the book is Essential SQLAlchemy.
- 55:06It's also an O'Reilly title.
- 55:07So SQLAlchemy, if you are using Python and you are using an SQL database and you are not using SQLAlchemy, then you're missing out, I would say.
- 55:17And you're probably a Django developer because they have a really nice ORM themselves, and it has a lot of other features, if you're using Django, that are nice.
- 55:25But SQLAlchemy is one of the best libraries, object-relational mappers.
- 55:30I mean, it is the best I've ever seen.
- 55:33Yeah, it's really, really good.
- 55:34I've used it a lot, and it's been perfect.
- 55:36Yeah.
- 55:37A lot of the time when you get something like an object-relational mapper, then you give up a lot of the goodness of, like, a lot of the strengths of SQL.
- 55:46And I think that Mike Baer, who is the author of SQLAlchemy, really did a good job of giving you the abstractions of an ORM while still allowing you to get the performance of raw SQL.
- 55:57So I was really happy with that, and a second edition of that came out in the last year.
- 56:01I didn't have a lot to do with the second edition, but because I wrote the first edition, I get to have my name on the cover.
- 56:06Nice.
- 56:07Perfect.
- 56:09Yeah, and I actually had Mike Baer on one of the first episodes, episode five, so dug into that.
- 56:14Yeah, I like SQLAlchemy a lot.
- 56:16He is a smart dude.
- 56:17Indeed.
- 56:18All right, Rick.
- 56:20So we're about out of time.
- 56:21I don't want to take all your day up, but so let me ask you two quick questions before I let you out of here, and then one more thing after that.
- 56:30So if you're going to write some Python code, what editor do you open up?
- 56:34I open up Sublime Text 3.
- 56:35Sublime Text.
- 56:36All right.
- 56:37Definitely a solid one.
- 56:38Do you have extra plugins, or do you use the Anaconda IDE thing that plugs in there?
- 56:43Not the Continuum thing, but something else.
- 56:45No, I pretty much use almost the default install.
- 56:49I mean, package control is in there.
- 56:51Occasionally do some React programming to mention a different programming language, but get the JSX plugin and things like that.
- 56:58But it's Sublime, pretty vanilla for me.
- 57:00Nice.
- 57:01There's a ton, over 100,000 packages on PyPI.
- 57:04Is there one that's kind of notable you think maybe people haven't tried or heard of that you want to recommend?
- 57:08Well, other than things like PyMongo and SQLAlchemy that we've already mentioned, one of the ones that it just comes up over and over, and people may have already, a lot of people have heard of it, is Requests.
- 57:17It's the most un-Google-able package name.
- 57:22But if you're going to do any web programming in Python as a client, you need the Requests library.
- 57:28Yeah, absolutely.
- 57:29So I think it would be un-Google-able if it weren't so popular.
- 57:34Yeah, true.
- 57:35So Python Requests is your best bet.
- 57:37Yeah.
- 57:38Exactly, exactly.
- 57:39All right.
- 57:40Well, that's about all the time we have to talk about Mongo for today.
- 57:43Any final call to actions?
- 57:44People are excited about this stuff.
- 57:46How do they learn more, do more?
- 57:48So MongoDB.org can teach you a lot about MongoDB.
- 57:52You know, obviously the course, which will be in the show notes, but there's also MongoDB World coming up this summer in Chicago.
- 57:58So that might be a good place if you're really interested in this database.
- 58:02It's probably the cheapest education that you can get.
- 58:04And it's, you know, two days of talks and tutorials before that.
- 58:08So I guess those are my calls to action.
- 58:11Yeah, cool.
- 58:11MongoDB World, that's like the PyCon of MongoDB.
- 58:14Yes.
- 58:15That's the big one to go to.
- 58:17It's in Chicago, and that's cool.
- 58:19It used to be in New York City every time.
- 58:21Yeah, this is the first time that they've kind of ventured out of Manhattan.
- 58:24So it'll be interesting to see what goes on there.
- 58:26Yeah, indeed.
- 58:27All right.
- 58:28Well, Rick, thank you so much for being on the show.
- 58:29It's been great to chat about Mongo.
- 58:31All right.
- 58:32Well, thank you.
- 58:34This has been another episode of Talk Python To Me.
- 58:37Today's guest has been Rick Copeland.
- 58:40And this episode has been sponsored by Advanced Digital and Hired.
- 58:43Advanced Digital would love to work with you to build and extend one of the most visited websites in the U.S. in Python.
- 58:51Reach out to them at python.advance.net to see if there's a fit.
- 58:57Hired wants to help you find your next big thing.
- 58:59Visit talkpython.fm/hired to get five or more offers with salary and equity presented right up front and a special listener signing bonus of $600.
- 59:08Are you or your colleagues trying to learn Python?
- 59:10Well, be sure to visit training.talkpython.fm.
- 59:13We now have year-long course bundles and a couple of new classes released just this week.
- 59:19Have a look around.
- 59:20I'm sure you'll find a class you'll enjoy.
- 59:22Be sure to subscribe to the show.
- 59:24Open your favorite podcatcher and search for Python.
- 59:26We should be right at the top.
- 59:28You can also find the iTunes feed at /itunes, Google Play feed at /play, and direct RSS feed at /rss on talkpython.fm.
- 59:37Our theme music is Developers, Developers, Developers by Corey Smith, who goes by Smix.
- 59:42Corey just recently started selling his tracks on iTunes, so I recommend you check it out at talkpython.fm/music.
- 59:49You can browse his tracks he has for sale on iTunes and listen to the full-length version of the theme song.
- 59:54This is your host, Michael Kennedy.
- 59:56Thanks so much for listening.
- 59:57I really appreciate it.
- 59:59Smix, let's get out of here.
- 1:00:01Stating with my voice, there's no norm that I can feel within.
- 1:00:05Haven't been sleeping.
- 1:00:07I've been using lots of RSS.
- 1:00:08I'll pass the mic back to who rocked his best.
- 1:00:11First, developers, developers, developers, developers.
- 1:00:14First, developers, developers, developers.
- 1:00:17First, developers, developers.
- 1:00:20First, developers, developers.
- 1:00:22First, developers.
- 1:00:23you