- Podcasts
- Programming Throwdown
- Databases
About this episodeThis show covers Databases. Tools of the show: Jason: nvAlt / nvPy Patrick: Arduino. Books of the show: Jason: Negotiating For Dummies…
This show covers Databases. Tools of the show: Jason: nvAlt / nvPy Patrick: Arduino. Books of the show: Jason: Negotiating For Dummies http://amzn.to/1jS9Yem Patrick: Steelheart http://amzn.to/1lsRxBv
★ Support this podcast on Patreon ★Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
Databases
Hey, everyone. So just real quick, I know we promised the episode on Swift. It is coming. Patrick and I are desperately trying to learn Swift, but it's taking a little bit longer than definitely I anticipated. More just because we want to... There's not a whole lot out there, right? And so a lot of it is just understanding how the language works and things like that. So we'll definitely get to that. But in the meantime, we'll definitely... We have a really awesome show here on Databases.
Oh, man. So from the mailbag, Ash Booth wrote in. And he said, 'Hey guys, I discovered the show about a month ago. You know, I'm an AI/machine learning PhD student. And what's it like to go from academia to, you know, industry to working? Well, they're both working. But what's it like to go from working at a university or studying at a university to a full-time job?'
Definitely. Because engineering internships are almost always paid. I've never heard of an unpaid engineering internship. Right? And so you get money. That's cool. And then also, like, it does expose you for, like, you know, going back to school for the remainder of the time and to know what to study and what you're interested in and what real life—I'm doing little quotes here—is all about. But I mean, I think the biggest transition is from the fact that an academic is perfectly to say things like always and never. And in real life, you can't really say those things because you have to bend or break a lot of rules because you may be working on older technology or, like, the style of your team is just this. And even though academically you may be able to show, like, this paper says this is the more optimal way to do this, it's like, well, we already have coded this. We've already invested the time and tested it, and, you know, whatever. We're just kind of realizing. Yeah, that's a good point.
You know, like, we were talking about this today at lunch that in Python, in the Python compiler, there's something called TimSort. And it's named after Tim. And the idea is, you know, they need to do some kind of sorting. And again, I'm not going to go into the details. I don't even really know the details. But to some extent, part of the Python compiler needed to sort something. But they knew something about the data. Like, they knew certain chunks of the data were already in some kind of order. And so TimSort, you know, took advantage of some of the assumptions they could make about the data to sort faster than nLogN. Right? Which, just to recap, like, if you take Computer Science 101, one of the first things you'll learn is that, you know, the two fastest sorts, theoretically, quicksort and merge sort, both run in nLogN. But TimSort, you know, given the data that TimSort works on, runs in faster than nLogN. And so the point is, you know, when you get down to industry, you're not working on, like, theoretically, theoretical data. I mean, you're working on practical data that you can make a lot more assumptions on. And so a lot of the theory kind of goes out the window.
Yeah, I guess my biggest thing is I kind of Look at it this way. Like, going into academia, like, as a student or as a faculty or whatever, as a research assistant or what have you, is a lot like owning your own business. Like, as a student, you're really in charge of your own fate more than you are as an employee. Like, as a student, you can very quickly, within a matter of three or four months, you can fail out of college, right? But, you know, outside of sexually harassing someone or something which is very unlikely, like, which would require, like, you to be a sociopath. Like, outside of those crazy things, you'll almost never get fired within three to four months. Like, for you to be fired, for most companies, for you to be fired for performance reasons, I mean, you have to actually be...
Yeah, you have to be messing up for a long time, right? I mean, you really have to be going down the wrong path and know it for a while. But school, I mean, you could, you know, really just, like, you know, especially when you get to the higher levels, like a PhD or even worse, a professor. Like, if you don't write, you know, if you don't get enough grants accepted and you don't get enough papers accepted, you know, there are people who want... You know, there are conferences that are looking for good papers, but they're also very selective. And if you don't get these things through, then you don't get funding, and then you have to kind of beg your department to float you, and that, you know, ends badly. And it's so easy to fail, just like running your own business, and to just be destroyed in your career. But then on the flip side, what you're given in academia is a lot more freedom. Like, in school, you can study whatever you want. If you want to be a biologist, you be a biologist, you know. So you study anything. But, you know, if you get a job as, say, a software engineer, then obviously it's much more limited, right? So those are sort of the tradeoffs.
Well, there's... I guess I think about two other things. One is that, like, in school, almost everything was whatever we call greenfield work. So, like, everything is completely unwritten, right? You're writing stuff from scratch. And most of the time when you're working on a job, there's already something that exists. Yeah, that's true. It might just be requirements, but it's very rare. I mean, almost all of my time spent coding is working in other people's code, working in legacy code. You know, even when I'm writing new code, it has to fit in with other people's code, right? Yep. There is some of that in school, but it's, you know, not as much. And then the other thing is, like, the continuity of work. Like, you could be working on the same thing. So, at least in, like, if you're doing graduate work and research, it's probably different. But, like, an undergraduate, you know, you're doing little projects. But there's typically a long project would last a semester. And most times projects are multiple per semester, right, in a class. And at work, like, you could easily be working on basically the same thing for years, you know, but easily, you know, months. And you just every night you go home and you come back the next morning and you're doing the same thing you were, like, the night before. And you just keep doing it over and over and over again, you know.
Yeah, right. Yeah, actually, there's this interesting TED Talk, and we'll link to it in the show notes. But it's about what makes us feel good about our work. And the reason why I bring it up is they did this experiment where you—they had a control group where they started off paying them $5 to build this Lego. So they had a box of Lego, and you had to build something. I think it was like a robot or something. And they had the instructions. And after you built it, they put it aside. They took it to another room, and they gave you a new box and told you to build another one. But this time it was only for, I think it was like, $4. And they had some kind of power function, right? And so most people kept building the robots until it was down to like $0.70. Then for the second group of people, they had them build the robot, and then they destroyed the robot in front of them, like, disassembled it, and had them build it again. And that second group only built the robot once or twice and then said they were done. So, like, grad school is a little bit like being in that second group because you do a lot of work. A lot of it is alone. Even being a professor, you know, a lot of the work, you're really like in your own world. I mean, you have students who are part of your team who kind of report to you, but you're really on your own. And a lot of the work—this is my opinion, so take out the grain of salt—but a lot of that work kind of gets destroyed. You know, it goes into some research paper, and then hopefully someone in industry picks it up and does something with it. But I felt like there's a lot of that kind of being in that second group. But when you are in industry, it's true that like, you sometimes can feel like you're a cog in a much larger machine. But on the flip side, it's like, yes, you're part of a larger machine that's doing something pretty amazing. And so that's sort of like the big tradeoff I see. All
News. So this is pretty topical. There's a database, RethinkDB. And the impression I get is I don't know too much about Rethink, but the impression I get is they're trying to, you know, kind of compete with MongoDB and some of these other document stores. And they released a new version that has a bunch of cool new features. So if you've never heard of RethinkDB, after you're done listening to this podcast and you're a database expert, you can definitely check it out.
Yeah, but I had already kind of been using them for a while. And I got kind of nervous because I have, like, a bunch of things tied to that DNS, scripts, things like that. So I just said forget it. I'll pay them. I think it was, like, you know, $20 for the year or something like that. But now I found out about this DHCPIO. I'm going to try it sometime this week. And it looks like it's exactly what DynDNS used to be where it's free. And you could just set up some kind of service that updates your IP automatically and things like that. So I'll check it out. I'll report back next show and let you guys know if it's solvent or not. But at least, you know, it's good that there's other alternatives out there.
I think because it's just, like, we don't do—at least a lot of programmers I know—they don't consider themselves very artistic. But it's kind of something where you turn a little knob, you change a little equation, and you get something kind of beautiful out. Okay. That makes sense. It's like computer-generated, like, procedurally generated things, right? Programmers will build all sorts of—I always see procedurally generated moonscapes and landscapes and treescapes. And people are just like a simple algorithm that creates something complex. Okay. Conway's Game of Life, right? Like, all these things, like, you know, complexity from simplicity. And this article takes an interesting look, which I had never heard about before, for the—I believe it's the Julia Fractal. And it basically talks about the symmetry of it and how, you know, you always hear the typical thing, like, you know, a fractal is something that takes place on the complex plane. So the vertical axis is the imaginary component and the horizontal axis is the real component. I think I'm getting that right. And, you know, you take a point there and you apply—you put it into an equation. You get a result. And then you take the result and put it back into the same equation. You keep doing that. And either the results will stay under some limit or will kind of spiral out to an infinite number. And based on that, you kind of make colorings and whether it's in the set or out of the set. And, you know, these beautiful things arise. And this one, you know, you typically hear about it in this very mathematical description that it's kind of dry, kind of boring, I guess. But it's interesting. And this one, they talk about it in a way of kind of like folding and transformations. And you'll have to read it. They have some kind of cool animations. They talk about it. And it's just a different way of looking at it, but, you know, kind of interesting that even with a different approach, you can kind of arrive at the same thing and kind of have a different insight in how and why the shapes arise like they do.
Yeah, so I was just saying how great it is. And all of a sudden, I was rotating his website around. You actually move the header of his website around in 3D. And then I got an achievement on the website. I got Achievement Unlocked for rotating the logo around. But as part of getting that achievement, I broke the website. Like, I can't scroll anymore. Oh, nice. But I'm looking at the top of his header.
Well, there you go. That makes me a real musician. I'm also mediocre at Guitar Hero. But this is pretty cool. It's basically—it's all JavaScript. So, you know, you can iterate pretty quickly, and you just write some scripts. Basically, they give you a function, and, you know, t comes in where t is, you know, the current time. And what comes out is an amplitude. And they have a few samples where they just do, you know, sine of t, cosine of t. And you can hear the little sine wave mix. And if you do, like, you know, sine of 2t, you can hear a higher frequency sine wave. And then they even have some crazy examples where it's like they've made their own, like, techno, you know, their own electronic music that you can just, like, you can just execute in the browser. So I thought it was pretty cool.
Let's make a deal. How about I go first of Book of the Show, and then you go first Tool of the Show. Okay. Okay. All right. We just had there, ladies and gentlemen, was a negotiation. And I won only because I read Negotiating for Dummies. And I talked about this book a long time ago. I'm getting ready to buy a car. You know, we've talked about this on the show in the past, but Patrick and I both have pretty new families. So... Are you
I don't know. So I kind of think the minivan is kind of cool, especially because... Cool isn't the right word, but I like the minivan because it can hold... It's sort of like having a hatchback. It could hold bicycles on the back and things like that. Mm-hmm. But then I also see the SUV is kind of a nice way to sort of have a bigger car that could hold more people.
think the problem is I think minivans got such a bad stigma. It sort of like has the whole soccer mom connotation. Like, I'm completely indebted to my kids' happiness and my life doesn't mean anything anymore. It's sort of like... I feel like some people attribute minivans to that. To that idea. And it's obviously not true. It's just a car. But, you know, and then so they came up with this SUV where it's like... SUV, I think if I understand correctly, it's like a trunk... A truck chassis. So you could feel like you're driving something with muscle. But like, you know, it's like... It's like, yeah, I have a chassis and a frame that can withstand Arctic tundra, but I'm going to drive, you know, three people, you know, in suburbia, you know?
Yeah, that's the plan. The plan is to just start actually just reading quotes from the book until they give me the price I want. But no, I thought this book was great. You know, and so it's one of the few books that like I actually use as a reference. Like most of the time, you know, we actually, you know, today's day and age, you just look things up online. Like, you know, like we have a lot of books at the show on programming languages and things like that. And they're great, especially if you don't know the language. Like I'm reading the Swift Programming Guide right now. But then
Well, but at the end of the day, if you just forget how to use a function or something, you're not going to use that as your reference. You're just going to Google it. But this is a book where it's like I feel like they keep going back to it. So I figured I would go back to it here and make it Book of the Show, which it hasn't been yet.
My book of the show is a fiction book called Steelheart by Brandon Sanderson. He writes a lot of books. This guy is crazy. I don't actually know how he writes as many books as he does. Some authors, you know, it's like a long time between books. And he actually has multiple series going in this guy. But like... How do
And he comes out with like... It seems... I'd have to go look at his... But it seems like every couple months there's a book coming out. Yeah, I don't know. I don't know how people do it. And some people it's like, you know... I'm reading a trilogy and it's like been like two or three years since the last book came out. And it's like, I really want the next one. The author's saying like it's going to be like another year at least. And it's like, ah.
Twilight, that's right. So the lady who wrote Twilight, apparently she was going to write, I guess like another, like some kind of epilogue book. But then the book got leaked out and so she canceled it. And that kind of blew my mind because it's not like, you know, it's not like, you can't cancel a book. I mean, it takes so long. It just, I thought that was pretty wild. It takes so long and it's such an individual effort that like the idea that, you know, even if someone spoiled my book that I would just throw away a year of my life.
Something happened with J.K. Rowling, right? The person who wrote Harry Potter wrote another book. Oh man, I wasn't prepared for this. But she wrote another book, but she wrote it under a pen name. And I think it kind of got like... Like it was well-reviewed, but not like people didn't get excited. It wasn't like a bestseller. I'm probably butchering this story. And then like kind of came out that she wrote... Someone leaked, like her publisher or something leaked that it was her. And then all of a sudden it was like, you know, through the roof sales. And all of a sudden it was the, you know, great book and all this stuff. Oh, wow. You know, but like she was trying to dodge it, you know, because she didn't need the money. So she just probably just wanted to write and not have people view it in the light of Harry Potter.
Yeah, it's interesting. There was some writer who gave a TED Talk recently who... It was one of these like... I think the topic was like, how do you survive your own success or something like that. And basically she just said that like she had like huge writer's block because she was like consumed with like her own success. Like not that she was like she was gloating over herself every day. But it was more just like she felt like how can she top that, you know. And the short story is that she just decided to write a book. Like she kind of just got the cojones to write the book. And the second book tanked. Everyone was like, this is nothing like the first book. And then that sort of like gave her the freedom to write the third book, which ended up being like her best book or something. Interesting. Yeah, apparently like, you know, again, like I was saying, it's such an individual effort that like... It's sort of like movies, you know. I would imagine like at some point you're going to the movie just to see George Clooney. And it doesn't really matter what he does. It's just kind of a weird dynamic.
Coming full circle. Steelheart by Brandon Sanderson. And this is kind of like an interesting take on the comic book superhero, supervillain theme. Oh, okay. So it's kind of that way, but not like it's not a Marvel Universe. It's not like DC Universe. And it's a book, not a comic book. But kind of in that kind of theme, but with some interesting twists on it. So it was nice. It was a good read. It's a short, rather short
So I get to go first for tool of the show. All right. Mine is Arduino. So we've talked about Arduinos before. And I recently used it for a project. And people at work were giving me a hard time because, you know, everybody's like, oh, Arduino's overpriced. And, you know, you can get this other ARM device, which is 32-bit and 100 megahertz and only $5, you know. And then by the time they finished the discussion, I actually had the project done on my Arduino. And I'm not like, Arduino is the best thing ever. Like a lot of people try to do stuff with it that you should just move to another processor. But if you're trying to do like rapid prototyping, I've realized how wonderful of a tool it is because there's so much example code in libraries and peripherals out there that just work with it. And you just do it and it works. And if you need to... It's the same thing with kind of Linux distributions in a way. It's nice if there's a big community because somebody probably asked a question. So it's good if you can ask a question and eventually someone answers it. It's better if you just look for the question and it's already been asked and answered. Gotcha.
Sort of. I mean, I don't know. I don't do any like applications programming in it, right? So it's all like manipulate some hardware is what I'm typically doing with it. And so I'm normally grabbing some library and some example code and changing it just enough to where it works. And it's really fast for that. And, you know, I install the IDE. I do the thing. And it's not... I couldn't imagine trying to do a complicated project in it. It would probably be terrible. But like I said, in the time that I could have figured out how to get a compiler installed, properly compiling and properly flashing and, you know, the bootloader working on a $5 controller, whatever other ARM board, it probably would have been very, very powerful and would have taken me weeks to like get working before I started. And I was done. And, you know, like a couple hours for the thing I was trying to do.
Yeah. So, I mean, that's one way to look at it, right? Like some people, even some of the Arduinos have this where they can pretend to be basically a keyboard, right? And so there's interesting like data logger Arduino stuff where you just want to like measure the temperature in your office, right? So you could try to go buy some USB thermometer and figure out some SDK, or you can like hook an Arduino up to it and have it type in the numbers into a spreadsheet and push enter after the end of each reading, which is kind of an interesting hack, but in some ways is really straightforward and quick. So if it works, it works. And if you need to go to something more complicated, you can, and you'll know sooner. So, and that's why Arduino is my tool of the show. Even for, I don't consider myself an expert programmer, but someone who does a lot of embedded work. Like if I had a board that was my preferred setup and I went to it all the time, sure, maybe that would be better. But since I don't, I, you know, when I have to do stuff with hardware and I just need it done, Arduino. Yeah.
With Arduino? So I—it's kind of hard to explain. I'll do it to the other one. So I was trying to, I wanted to build something for just entertainment value, to move like a flag back and forth. You can kind of think of it. So I wanted to just attach a servo motor to a flag and have it wave the flag. Right? Okay. You know, very patriotic. I don't want to actually wave the flag. So I built something to wave the flag for me.
robotic flag waver. So it's basically that. Yeah. And so I programmed Arduino and just told it to go from one extreme to the other back and forth, back and forth, back and forth. And it was done in like, I already had a servo. I had an Arduino board, hacked it together, and it was done. Before that, I built an Arduino that moved two stepper motors that were on pulleys for strings, and they were attached to a pen and they moved the pen around a piece of paper and drew pictures.
So you give it an image, and then there are already tools. Like people have done this before, which I guess is why it wasn't that interesting to me because it kind of felt cheap. But like people encode it threshold or whatever, and then you do like traveling salesman problem—like how do you go from all the point cloud basically to form a pattern that looks like the image? Right? Right. And then you send those coordinates for each of the paths. And the Arduino would move the pen to that, move it, move it, move it. And you just keep streaming the commands over.
Okay. That's pretty awesome. My tool is not definitely not as cool, but I was actually talking to a coworker about sort of productivity tools. I have a bunch of like, you know, at work, I have a bunch of email add-ons and all sorts of email hacks to do all sorts of crazy stuff. I like this native base filter that runs on my email and it can actually shave hours off your workday. Like if you can get your productivity up, or at least by up—I mean, like more efficient. Wait, it doesn't
Yeah. Right. This tool is called NV Alt. It's answered. There was originally a tool called Notational Velocity. I think the tool still exists. It's for OS X. It's pretty cool. It uses like a wiki format. So basically you write in this wiki format and it has this like pane off to the right that has, you know, the rendered format that's updated in real time. And the cool thing about it is you just, it just feels very natural. Like you write what you want it, the note to be. Hey, you hit Enter and you start writing the note and it has a search functionality. It has a bunch of cool stuff. The thing is I don't really like the wiki format. I much prefer Markdown, especially like the GitHub Flavored Markdown, like the extended markdown has tables and things like that. And so NV Alt is an open-source alternative to Notational Velocity, which uses Markdown instead of wiki markup. So I use that. I found out, you know, because we were doing the show, I did some research and there's actually one called NV Pi, which also supports Markdown and it's written in Python. So it works on any OS. So you can actually take notes on Linux. Those notes will magically work on OS X or on Windows or what have you. Also it has sort of this, it could be locked with a password. So in other words, you can put all your notes on say like Dropbox or Google Drive or iCloud or what have you and share them among your computers and not have to worry about somebody who doesn't know the password getting access to your notes. So it's got pretty good security there. It has the indexing and all of that. Oh, also you can at any point turn your notes into sort of like a wiki thing. So it'll basically take all of your notes, render the HTML and then you know put them in file names based on the title and create a little index. So it's sort of like your own personal wiki that you could publish to the internet. I've never used that feature, but I just think it's kind of cool. Like one of these days I might need that. But yeah, so NV Alt it's if you need to take notes, whether you're a student or during meetings or something like that, I found that one really useful.
Yeah. I, the SQL syntax for some reason, like I could use it every day. Like there are some times where, you know, let's say over the course of a month, I use SQL every day and I still don't remember the syntax. I just don't know why. But the, yeah, I remember there's like SELECT WHERE SELECT STAR IN TABLE OR FROM TABLE. See, there you go. Anyways. So, you know, before there were databases, which is probably going way back because databases pretty have been around for a long time. Actually how long databases were around in the 1960s. So before that, and up to today, even people use flat files, right? I mean, it makes sense. Like you just have, you know, if you have some file on your computer, you can actually do, you can actually seek in that file. Like most files are random access files. As long as you're not using like a tape or something like that. And so you can actually just seek to any point in the file and read that point or write that point. And so a lot of people will just write out like, you know, text files. Like for example, if you have a web server, every time somebody visits your website, you might append a line to some flat file or raw text file saying who came to your website. And, you know, this is great for a lot of things, but it doesn't do everything. There's a lot of reasons to use a database. One of them is indexing. So the idea here is, you have a lot of data in your database. Let's say you have a list of names. So, you know, you have Patrick, you have Jason, you have Jennifer, Sophia, you have all these names, a column just full of different people's names. So, you know, most of the time you want to look someone up by their name, like give me all the Patrick's or give me all the Jason's. So what you don't want is to have to look through every name in your whole database and then pull out the Patrick's as you see them. Like you'd want to do some kind of pre-processing and build an index such that when you say like, 'Give me all the Patrick's,' it's kind of ready to go. What's the difference between an index and a reverse index? No, no, no, no. I don't know. I think so, you know, a reverse index is where I think it Works like this. I think, you know, let's say you have a bunch of records and an index says, okay, record one has Patrick in it. So if I look for record number one, I want to get Patrick. Or if I look for record number 14, 50,000, that one's Jason. That's an index. Like you're indexing into the database. A reverse index is when you want to go the other way. Like give me all the Jasons or all the Patrick's. So, you know, even a flat file could have indexing because you could just say like every 10 megabytes in the file is a new person.
Yeah. So, it's just reversed versus forward. It's just like a matter of how the data is stored versus how it's looked up, kind of. Right? So, yeah, you said, like if you have a log, right. First, you just start adding lines to the end of the log, but eventually you want to have a way to go straight to a given day when it gets really, really big. Right? And you don't want to have to parse all the days to find out like where a given day starts. So you may put up at the top, like Day One is that bite, this Day Two is that bite, this Day Three is that bite, this. Right? It's like a forward index. Cause it's kind of like the way it's stored. Reverse index would be like if some, for some reason you wanted to go like the other way, like from a given entry to know what day it was in. Right? Like, right. So it's not exactly the way it was stored. You know, that data in this case is kind of a bad example because it's probably stored in that portion of the log. But if it wasn't like, 'Hey, I'm at some part of a file for some reason, I want to go back to what day it was that,' you know, like going the other way.
Similarly, as the discussion is, as it typically comes, as data grows, right? So like it starts off simple. You do something simple. That's fine. Then as it grows, you start having a problem like caching. So if the file gets really big and you're accessing it in the same way a lot, if this is a flat file, you know, depending on your RAM and your cache, maybe it's getting caching at like the CPU level. But as it grows like bigger and bigger, right? Like you want a more sophisticated caching scheme that understands the access patterns you're likely to have.
Yeah, exactly. I mean, the CPU will cache in its own, like, you know, L1 and L2 cache, but these are very small. Right? But a database can, you know, cache like 200 megabytes of information, you know, in main memory. And it does like the caching between like your hard drive and the main memory. And again, the way that most databases do the caching is very sophisticated. So you don't want to be writing that yourself. Yeah.
Yeah, definitely. Another one is redundancy. So, you know, you want all your data to be backed up in real time. Like imagine if you're processing orders, like you're a furniture online furniture store or something. You know, if your computer dies or even if, you know, just the power goes out and you have to, your computer hard resets, you know, if somebody is in the middle of an order and you double charge them or you forget to charge them or something, it could be a big deal. Right? Or if you lose the entire, all of your data and you have no way to recover, that's a huge disaster. Right? And then it gets really tricky because the way you back things up, I mean, you could have infinite loops. Like you'd have a student record who points to the classroom they're in and the classroom points to the students that are in that class. So you can have these sort of like circular references and things like that. And so being able to, you know, back up something like that, some kind of structured data and to recover it, these things are really tricky and you don't want to be implementing them yourself.
Similarly, trying to scale them. So running on one computer is all well and good, more memory, more CPU. Eventually that runs out. And now you have to scale to multiple computers, also called distributed databases. Right? And now you have a whole host of other problems you got to handle about: Do you put one table on one computer and another table on another computer, or some users can data on one computer and some on another? And then how do you split up that? Right? And it starts to play in a lot of different choices about how you want to scale the data and understanding the problem set so that you're scaling in a way that's going to be beneficial for you.
Yeah, exactly. I mean, most of these databases, you know, if you go to Amazon or Microsoft or Google or any of these like big services, I mean, there's just a ton of computers that are all working together to store one collection of information. And yeah, trying to implement that again is really hard, very hard to get right. Even the experts don't get it right all the time, right? There's a lot of heuristics, a lot of guessing and hoping that certain conditions aren't met and things like that. And there are a lot of fingers crossed, so it's definitely not like a solved problem. And you don't want to be in the business of trying to solve it. I mean, unless you're getting a PhD or something in databases, but for the rest of us, we should leave it to the experts. Also segmentation, right? I mean, most people access data in certain patterns and they might only need—if you're looking at say like SQL, which we'll talk about later—you might only need one column at a time, or you might not select all the columns. And so SQL can actually say, even though this one row of data is kind of one atomic thing, I'm going to break it up into pieces. Or as Patrick said earlier, I'm going to break each day and put each day in a different place. And it can look at sort of your access patterns and also some hints that you can give the database. And it can divide your data, segment your data into different pieces such that it doesn't have to do a lot of extra reading. It can just read the parts that you want and do that very efficiently.
Yeah. But in some ways, like a database is a theoretical design thing, right? Like, you know, for a relational database, I'm defining, you know, here's my schema. Here are my relations. Here are my constraints. And like, you know, I want some physical implementation of that. But you could arguably have different front ends that handle it differently based on how stuff is getting put in or taken out. But in reality, I don't know of anyone who does that for any practical reasons. Typically, you have one database management system, which is also managing kind of like the database, the back end. But a database management system is where things like what Jason was talking about—like observing usage patterns and deciding what to go ahead and pull out a priori. You know, say like, oh, I know you might be going to look at this next. That would be something that the management system would be handling versus like the database itself may have less of an influence on that.
I don't know. That may be arbitrary. The other thing that is really good for a database is like, so initially you start off, you know, kind of storing data, retrieving data. But then as you have more users and more data, you want to do analysis. You want to say like, what kind of trends do I have? How many users are visiting my website each day? Is it growing or shrinking? What regions? And the more ways you want to slice and dice and analyze your numbers, the more you're going to want them to be in a database, which is understood how to write queries to do this. Otherwise, you end up re-implementing a lot of that yourself over whatever your other implementation of storing your data is.
Yeah, that makes sense. Yeah, a lot of the database driven languages are designed for people who want to extract meaningful information from their data. And yeah, you have to be re-implementing that, which is not fun. Yeah, another one we'll just kind of round this out is validation. So, you know, there's a lot of database management systems support triggers. So, in other words, if say an entry is written for a credit card transaction and it links to some credit card, but then that credit card isn't there. Then what that means is clearly some system has made some mistake because it's tried to reference a credit card that doesn't exist. So, you could have some trigger that says, you know, when I see a credit card ID for a credit card that's not in the credit card part of the database, then send an email or page somebody or something like that. Also, you could do client-side. Well, I don't know if you'd really call this client-side, but you could do sort of immediate validation, right? So, if somebody tries to insert a record without inserting its sister record that it's joined with or some other kind of validation, if somebody puts a negative age in the age column or something like that, then it just won't accept that operation. It says, hey, you know, if you're going to put an age, it has to be at least zero. It has to be a non-negative number. So, a lot of these types of validation, this type of validation logic is very easy to do. They make it very easy and practical, which is these are all things that you'd have to write manually in C++ or something like that if you're writing your own database.
So, we're going to start talking about the different types of databases. And one thing to mention as a side note, and it's a very detailed subject, so maybe we'll just kind of gloss over it. So, this is the issue of consistency. This is when a single person is updating or a single user computer process is updating your database. You typically don't run into this problem, but it's when you have multiple users that you begin to have an issue. And consistency—one, the kind of gold standard, I guess, is ACID compliance, which is Atomicity, Consistent, which is I kind of grouped them all in together, but I guess Consistency is one part of it. Isolation and Durability. And we won't go through what each one means. You can read about it. And they have very precise definitions. But these are basically that what happens, what are the guarantees I can say about when I successfully insert a row or update? What do I know has happened or not happened? So, like if you think about a bank, both my wife and I can go use the same ATM card, or, you know, my ATM card and her ATM card, which link to the same account, and we can try to draw money at the same time. And the system needs to be very carefully set so that we aren't able to draw both the full amount, or the bank will end up giving us too much money. And so, in that case, you want to make sure that you understand what happens when there are parallel transactions occurring.
Right. I mean, you know, in Patrick's case, the transactions are happening very slowly. But here, I mean, there's just, there could be millions, maybe billions of people accessing your database. And so, it becomes very likely that two people try to delete the same thing at exactly the same time. And so, that's that could be very difficult to handle, right? Or two people try and insert the same thing at the same time. Or you're in the middle of an operation and the power goes out. These are all just very difficult things to handle. And a lot of thought has gone into sort of how to deal with that. So, yeah, there are many different types of databases. Going getting back to Patrick's point, this is sort of more—we're going to talk about the actual database side. And then we'll get to some implementations of management systems later on. But the two that I think most people will be familiar with are relational databases and then everything else, which, everything else, you've heard maybe NoSQL databases. But basically, that's sort of a catch-all for just a varied assortment of other databases. And it just happens that because of changes in computation and networking and in sort of the—you know, Murphy's, or not Murphy's Law, the Moore's Law kind of fading and things becoming more distributed, the NoSQL databases have become much more important. And so, we'll cover those, we'll cover those in general. So, why don't you go first? What's a SQL database?
I mean, I think if you know databases, any kind of database, this is the one you're likely to know. So, SQL is the Structured Query Language. This is where you see SELECT star FROM the table name WHERE some, you know, limits you put on the query. And the query runs. And similarly, there's a schema associated with it. So, when you create tables and you have constraints. And some of this is particular to a database management system. And some of it is part of, like, a standards-based language that you can use. And there are all sorts of things which support SQL-like languages. So, you can write a query, like, where I just described SELECT star FROM users WHERE creation date is yesterday. You know, something like that. It's a very common way. A lot of things use this because it's a very straightforward notion. But ultimately, underlying it, the relational part says, how do I provide links between two tables? Right. And it gets into what you'll hear the term normalized and denormalized data and patterns for that. So, you want to be efficient in saying, like, if I have a teacher and a teacher has a bunch of students. The students also have classes, right? You can try to model, you know, kind of each row contains all the data necessary. But that's, you're replicating a lot of information and you're not representing the relationships there. That there is a teacher entity. And a teacher entity is linked to student entities and linked to class entities. And those relationships can be one-to-one, many-to-one, one-to-many, many-to-many. Right. You can kind of have constraints about what those things are like and keys that index into other tables. So, like, a student ID is the key. And that might be what you would store in a class roster, a teacher students. And then there might be, like, a student record, which is keyed by student ID and contains, like, the student's address and their billing status. That kind of stuff. And that is the traditional relational database structure. And you use SQL to interface and do what, you know, joins between these tables to construct the information necessary to get the query to run and the information that you are looking for out. Whether it be to look up a specific set of information or do analysis.
Yeah, definitely. Yeah, so a lot of SQL and these kind of things, as Patrick was saying, around this concept of IDs and doing joins. So, for example, you might have, say, a student's table. And it has, you know, a record for every student. But then that record also has what's called a unique ID. So, you know, Patrick and all of his grades and things like that might be in the student table. And it might be given an ID of, like, 123. And then in, say, the classroom table, you know, you might have each classroom also has its own unique ID. And the classroom has a list of students. But instead of putting the whole student record there, you could just refer to them by ID. So, you know, you could say, okay, in this class, there is student 123, student 456, so on and so forth. And then you could take that list of IDs and go back to the student table and say, okay, who is 123? And so this sort of breaks a lot of these, like, circular dependencies or having to, you know, have the same information twice is by storing a lot of these almost like pointers. And there's very efficient ways of looking up things by their ID and things like that.
So, yeah, NoSQL databases have really taken over for a variety of reasons. There's several of them. One is key value store. And this is the oldest. I mean, this might even predate SQL databases. But this is a very simple idea. You know, imagine, like, if you had a SQL table with just one column, right? Well, you had, I guess, two columns, the ID and then some other column. So that's all a key value store is. You know, you give it the key and it spits back the value. And so you could think of this almost like a cache. Most caches are key value stores. And so even though it's very intuitive, it's one of the oldest, you know, types of databases, it actually proves to be very effective. There's something called a column family database, which is similar to a key value store. But the idea is for one key, there may be many values. So, again, if you look at, say, a SQL database, you have, you know, a student record. And you might have the key for that record and then a bunch of other columns with information, just student name, you know, grades, so on and so forth. A column family is very similar where you have, you know, like a student ID. And then you also have, you know, some more, a set of values about that student. The only difference is in SQL, the idea is that the ID itself is, doesn't contain any information. So, in other words, you know, like Patrick might be given an ID of 123, but that, like the application shouldn't know or care about that ID. It's just a number that is used in the database to, you know, look up Patrick. And it's even, most of the time, it's given by the database. The application can't assign that unique ID. And it's, the IDs are chosen in a way which make indexing and things like that really easy. With a column family or a key value store, the assumption is that the key itself is important. So, in other words, I'm not going to say Patrick is 123. You know, Patrick, the name, could be the key. And then the value is all the information on Patrick. And so, you can actually have multiple keys with the same name. And, you know, there could be collisions there that have to get sorted out. You know, depending on how the management system works, the key might have to be unique. But, yeah, the idea there is with the column families at the key and the key value stores at the key matters. Thirdly, there's a document store. And this is something that's becoming much more common recently. But the idea with a document store is that it's much more unstructured. So, if you were to think of, say, XML or JSON or something like that, you can represent a variety of things in XML or JSON. And you could even, you could have a collection of JSON objects which have different topologies, right? So, in other words, like, I could have one document in a document store which is the Patrick document and has, like, a list of Patrick's grades. But then I could have another document in the same database which has, you know, JSON and has, doesn't have grades. That's something completely different because for whatever reason. So, in the case of document store, it's much looser. And just through advances in the technology and also computation power growing, you know, we're able to have these more sophisticated database management systems that can do all the things we talked about, the validation, the indexing, the caching, without having such a tight schema as we do with SQL. So, even with such a loose schema as you have in a document store, you know, given the right kind of resources and things like that, you can still do a lot of the things that we said at the beginning.
All right. So, now, quickly as like an overcap, you know, we'll talk about because you may be thinking of a given database and like oh, I wonder what it is. Or maybe we just mentioned some databases you might be interested in looking up. We're going to kind of go through some breakdown of the various implementations of the databases and some examples if we can give and what kind they are. So, first kind of section is the lightweight, embedded, you know, probably only runs on one computer database. And the first example that is very popular is SQLite, S-Q-L-I-T-E, SQLite. And it is a SQL implementation relational database, but it can be even embedded in your program and is super simple to use. And, you know, kind of one of those things, if you start to get any sort of complicated data you're trying to store in your program, you should consider using this instead because it may make your life, you know, easier. And it's quite efficient. It doesn't take, you don't have to run a separate server and connect to it. It kind of reduces the complexity of that that's necessary.
that. Yeah, Berkeley DB is pretty cool. Actually, I'll list another one on here. MapDB. And so these are also embedded. So as Patrick was saying, you know, let's say you have some program that you're writing and you need to store some kind of configuration. Like, do people, you know, what sounds do people want when they log in? Or, you know, it's some kind of music app, you know, or sorry, some kind of like music creation program. You know, what settings do they want their piano set at? You know, these kind of configuration things. These embedded databases are great for that because you could just load it from a file store to a file. It could be part of your program. You don't have to go phone home or something like that. You know, contact a server or anything. So SQLite is a relational database. So, you know, it uses SQL with the structure tables and things like that. If you just want a key value store, so again, like, you give it, you look up a key, you get a value. Then you can use Berkeley DB or MapDB. Berkeley DB is written in C++, but there are bindings for a bunch of different languages. MapDB is a lot better, but it's written in Java. So if, you know, the quick answer is if you're using Java, use MapDB. If you're using any other language, use Berkeley DB.
Next, we move up the scale to something that you would probably be running on a server. But if you wanted something really fast and, you know, your data size was kept under control, but you wanted it to run in memory, you get to your next set of databases. The one that you see all the time on, like, Hacker News and other websites is Redis. Is that how you say it? I actually don't know. I
This is another one you hear, and this is often when you hear about somebody having a performance problem with their SQL database, right? We'll talk about some in a second. But, you know, they're having problems because there's some specific data that it just has a higher access rate or different pattern than what SQL stuff was built to handle. And you'll hear about people talking about this as a way to store data in memory and have very fast access times and kind of alleviate some of the slowness that can happen if you have too many concurrent users to an SQL database.
Right. Like, in contrast with Redis, Memcached, when you shut down the machine, it does destroy the database. So the idea with Memcached, as Patrick was saying, is to use to augment another database. And it's for when that database is too slow. So you can imagine things that would be good for Memcached should be, you know, the front page of your website, you know, things like that. And there's a lot of, you know, if you use, like, Ruby on Rails or if you use, you know, a lot of these, like, content management systems, you know, most of them will support Memcached. And they will, you know, things like the front page of your website, they will put in main memory.
I have no idea why I've heard people say memcached before, because it looks like it's just Memcached. You're right. Oh, did you actually find a pronunciation? So, well, I was trying to look for it. So I see other people saying that they hear it as Memcached. I don't know. I can't find a definitive answer.
Yeah, definitely. There's a whole art to making MySQL run the way you want it to, given your data. And Patrick and I are very happy to let people much more qualified than we are do that. That is true. Yeah, so those are in the SQL family. If you're interested in the column family, which, again, just a reminder, that's the key value where there could be multiple values, right? So, you know, column family, if you have just one column in the column family, then you have a key value database. So think of the column family as being a superset of what a key value database can do. So in that category, there's two big ones. One is Cassandra and the other is HBase. The short story is the reason why there's two, they use kind of different technologies for scaling. So in other words, when you have to split the, to shard the database among, say, 10 or 100 computers, both each of those two, HBase and Cassandra, handle that differently. And let's just say the trade-off there is, you know, Cassandra is easier to set up. Definitely if you're running on one machine, Cassandra is way easier to set up. But even as you scale out, the setup and the maintenance is pretty easy. But on the flip side, if you do have performance issues, it can be kind of a nightmare. That's just been my experience. HBase is harder to set up. It actually requires a lot of Apache libraries and services that you have to install manually, like Zookeeper. So you actually have to, you know, install Zookeeper, start a Zookeeper server, and then install HBase and install and get an HBase server. So this is actually two servers. So it's got overhead. But on the flip side, it just seems like tuning and understanding what's going on is a lot easier with HBase. So that's column family. And then for document store, there's also a bunch of them. My favorite is MongoDB. I really think MongoDB is very clever in their API. I'm a big fan. I felt like... So they have this kind of cool sort of REST-based JSON API. They also have a good Java API. They are now... They've recently added full text indexing, which is pretty cool. So you can actually say this column, you know, if I want to be able to do, like, fuzzy text searches on this column. You can just annotate the column, and boom, you get fuzzy text searches. So just to recap, like, if the column was, you know, say, a web page, let's say one column of your database or one document of your database is a website, all the XML or all the HTML for a website. You could just turn on this full text indexing, and now you have a search engine over the whole web. Now, of course, you know, I mean, it's probably not optimized for that much data, but you get the idea. It's still pretty powerful. You know, if you're building your own wiki or something like that, it's pretty cool to kind of get full text searching for free. There's also RethinkDB, which we talked about at the beginning of the show. They're definitely, like, new guys on the block, so, you know, they are kind of iterating very quickly. And, you know, I think it'll be pretty cool, but we'll have to sort of wait and see, you know, what they come out with in the future. It's still kind of early.
All right. Well, that about wraps it up. I'm hoping that we didn't repeat too much of what we said when we did query languages, but that was, like, a long time ago. It's, like, 14 episodes ago. So, hopefully it's not too much of a repeat. And thank you guys for all your emails. Get lots of on our Google Plus page or community. I guess it's a community now. People post on there, and that's cool. We see them. Even if we don't respond, we do see them. And emails to us are always appreciated.
Yeah. Thanks, Ash, for your question. That kind of got the whole episode kicked off. So, yeah, definitely, you know, post on the community if you have any questions or any feedback or anything like that. We do read it. And it does give us a lot of inspiration for the show and things like that. We will definitely do Swift as the next episode. We'll have to beef up on our Swift between now and then. But, yeah, I'm looking forward to that one. Cool.
