Latest / Talk Python To Me / #534: diskcache: Your secret Python perf weapon
Transcript
- 0:00Michael Kennedy: Your cloud SSD is sitting there, bored, and it would like a job.
- 0:03Michael Kennedy: Today, we're putting into work with DiskCache, a simple, practical cache built on SQLite
- 0:08Michael Kennedy: that can speed things up without spinning up Redis or other extra servers.
- 0:13Michael Kennedy: Once you start to see what it can do, a universe of possibilities opens up.
- 0:17Michael Kennedy: We're joined by Vincent Warmerdom to dive into DiskCache.
- 0:21Michael Kennedy: This is Talk Python To Me, episode 534, recorded December 19th, 2025.
- 0:27Talk Python To Me, yeah, we ready to roll.
- 0:29Upgrading the code, no fear of getting old
- 0:32Async in the air, new frameworks in sight
- 0:35Geeky rap on deck, Quart crew
- 0:37It's time to unite
- 0:38We started in Pyramid, cruising old school lanes
- 0:41Had that stable base, yeah sir
- 0:43Michael Kennedy: Welcome to Talk Python To Me, the number one Python podcast for developers and data scientists.
- 0:48Michael Kennedy: This is your host, Michael Kennedy.
- 0:49Michael Kennedy: I'm a PSF fellow who's been coding for over 25 years.
- 0:54Michael Kennedy: Let's connect on social media.
- 0:55Michael Kennedy: You'll find me and Talk Python on Mastodon, Bluesky, and X.
- 0:58Michael Kennedy: The social links are all in your show notes.
- 1:01Michael Kennedy: You can find over 10 years of past episodes at talkpython.fm.
- 1:05Michael Kennedy: And if you want to be part of the show, you can join our recording live streams.
- 1:08Michael Kennedy: That's right.
- 1:09Michael Kennedy: We live stream the raw uncut version of each episode on YouTube.
- 1:13Michael Kennedy: Just visit talkpython.fm/youtube to see the schedule of upcoming events.
- 1:17Michael Kennedy: Be sure to subscribe there and press the bell so you'll get notified anytime we're recording.
- 1:22Michael Kennedy: Vincent, hello.
- 1:23Michael Kennedy: Michael, Michael, we're back.
- 1:25Michael Kennedy: Awesome.
- 1:26Michael Kennedy: Awesome to be back with you.
- 1:27Michael Kennedy: Yeah, this is almost the sequel to the last time you were on the show.
- 1:32Michael Kennedy: So it's going to be fun.
- 1:34Vincent Warmerdam: Yeah, so sequel in this case, not the query language,
- 1:36Vincent Warmerdam: like an actual sequel of events.
- 1:38Vincent Warmerdam: Yes.
- 1:39Vincent Warmerdam: Yeah, you can correct me if I'm wrong,
- 1:41Vincent Warmerdam: but I think what happened is you had me on a podcast a while ago
- 1:45Vincent Warmerdam: to talk about a course that I made,
- 1:46Vincent Warmerdam: and a big chunk of the course that we were very enthusiastic about
- 1:49Vincent Warmerdam: was about this tool called DiskCache.
- 1:51Vincent Warmerdam: And then we kind of came to the conclusion,
- 1:52Vincent Warmerdam: well, we had to cap it off.
- 1:54Vincent Warmerdam: Maybe it's fun to do an episode on just DiskCache.
- 1:57Vincent Warmerdam: since we're both pretty huge fans of it.
- 1:59Vincent Warmerdam: I think that's how we got here.
- 2:00Michael Kennedy: I think that is how we got here as well.
- 2:02Michael Kennedy: And we're going to dive into this.
- 2:05Michael Kennedy: Honestly, it's a pretty simple library called DiskCache,
- 2:09Michael Kennedy: but what it unlocks is really, really sweet.
- 2:11Michael Kennedy: And I'm going to talk about a lot of different angles.
- 2:14Michael Kennedy: And now, even though it's just been not that long since you were on the show,
- 2:18Michael Kennedy: maybe just give us a quick intro of who you are.
- 2:20Vincent Warmerdam: Hi, my name is Vincent.
- 2:21Vincent Warmerdam: I've done a bunch of data machine learning stuff, mainly in the past.
- 2:25Vincent Warmerdam: That's sort of what a lot of people know me from.
- 2:27Vincent Warmerdam: These days, though, I work for a company called Marimo.
- 2:29Vincent Warmerdam: You might have heard from us.
- 2:30Vincent Warmerdam: We make very modern Python notebooks.
- 2:32Vincent Warmerdam: We took some lessons from Jupyter,
- 2:33Vincent Warmerdam: and we take a new spin of it.
- 2:35Vincent Warmerdam: So that's my day to day.
- 2:37Vincent Warmerdam: But I still like to write notebooks
- 2:39Vincent Warmerdam: and do kind of fun little benchmarks and also stuff
- 2:42Vincent Warmerdam: with LLMs.
- 2:42Vincent Warmerdam: And I've just noticed that for a lot of that work, boy,
- 2:45Vincent Warmerdam: disk cache is amazing.
- 2:47Vincent Warmerdam: And I also use it for web stuff.
- 2:48Vincent Warmerdam: And I think that's also what your use case
- 2:50Vincent Warmerdam: is a little bit more of.
- 2:51Vincent Warmerdam: But yeah, in notebook land, you also
- 2:54Vincent Warmerdam: like to have a very good caching mechanism
- 2:56Vincent Warmerdam: And on the Marimo side of things, we are also working on different caching mechanisms, which I might talk about in a bit.
- 3:01Vincent Warmerdam: But just for me, the bread and butter, the thing I've used for years at this point is disk cache whenever it comes to that territory.
- 3:06Michael Kennedy: Yeah, it's funny.
- 3:07Michael Kennedy: This was recommended to me for Python Bytes as a news item over there quite a while ago, like years ago.
- 3:13Michael Kennedy: And I'm like, oh, that's pretty interesting.
- 3:15Michael Kennedy: And then I saw you using it in the LLM Building Blocks course, and it just unlocked for me.
- 3:20Michael Kennedy: Like, oh, my.
- 3:22Michael Kennedy: Oh, this is something else.
- 3:24Michael Kennedy: And so since then, I've been doing a bunch with it, and I'm a big fan.
- 3:27Michael Kennedy: I've been on this, like trying to avoid complexity, but still getting really cool responses, performance, et cetera, out of your apps.
- 3:35Michael Kennedy: And I think this is a really nice way to add multi-process, super fast caching to your app without involving more servers and more stuff that's got to get connected and keep running and so on.
- 3:47Michael Kennedy: But before we get into the details of that, maybe let's just talk about caching in general.
- 3:53Michael Kennedy: Like what types of caching is there?
- 3:55Michael Kennedy: You know, I sort of give a little precursor there.
- 3:57Michael Kennedy: But yeah, dive into it.
- 3:58Vincent Warmerdam: So like in the course, the main example
- 4:01Vincent Warmerdam: I remember talking about was the one--
- 4:03Vincent Warmerdam: you've got this LLM, and you want to do some benchmarks.
- 4:05Vincent Warmerdam: And it might be the case that, I don't know,
- 4:07Vincent Warmerdam: using an LLM for, let's say, classification,
- 4:09Vincent Warmerdam: like some text goes in, we got to know whether or not
- 4:12Vincent Warmerdam: it's about a certain topic, yes, no, or something like that.
- 4:14Vincent Warmerdam: Then it would be really great if, suppose, the same text came
- 4:17Vincent Warmerdam: by for whatever reason, that we don't run the query on the LLM
- 4:21Vincent Warmerdam: Again, it's like wasted compute, wasted money.
- 4:23Vincent Warmerdam: So it'd be kind of nice if the same text goes in that we then say,
- 4:27Vincent Warmerdam: oh, we know what the answer to that thing is already.
- 4:29Vincent Warmerdam: We cached it, so here you can go back.
- 4:31Vincent Warmerdam: And that's the case when you're dealing with heavy compute ML systems.
- 4:35Vincent Warmerdam: But there's a similar situation that you might have, I guess,
- 4:37Vincent Warmerdam: with expensive SQL queries,
- 4:39Vincent Warmerdam: or you want to reduce the load on a database somewhere.
- 4:41Vincent Warmerdam: Then having some sort of a caching layer that's able to say,
- 4:43Vincent Warmerdam: oh, you're querying for something, but I already know what it is.
- 4:47Vincent Warmerdam: Boom, we can send it back.
- 4:49Vincent Warmerdam: I think the classical thing you would do in Python
- 4:51Vincent Warmerdam: is you have this decorator in functools, I think, right?
- 4:53Vincent Warmerdam: The LRU_cache.
- 4:57Michael Kennedy: Yeah, exactly.
- 4:58Michael Kennedy: Yeah.
- 4:58Vincent Warmerdam: That's a hell of a world to that.
- 4:59Vincent Warmerdam: But the downside of that thing is that it's all in memory.
- 5:02Vincent Warmerdam: So if you were to reboot your Python process,
- 5:04Vincent Warmerdam: you lose all that caching.
- 5:05Vincent Warmerdam: So that's why people historically, I think,
- 5:07Vincent Warmerdam: resorted to--
- 5:08Vincent Warmerdam: I think Redis, I think, is the most well-known caching tool.
- 5:12Vincent Warmerdam: It's the one I've always used.
- 5:13Vincent Warmerdam: There's Memcache, I think.
- 5:14Vincent Warmerdam: There's other tools.
- 5:15Vincent Warmerdam: You could use Postgres for some of this stuff as well.
- 5:18Vincent Warmerdam: But recently, especially because disks are just getting quicker
- 5:21Vincent Warmerdam: and quicker, people have been looking at SQLite
- 5:23Vincent Warmerdam: for this sort of a thing as well.
- 5:25Vincent Warmerdam: So that's, I think, the quickest summary
- 5:27Vincent Warmerdam: and also sort of the entryway to how I got started with disk cache.
- 5:31Michael Kennedy: Yeah, and so for this example that you highlight in the LLM
- 5:34Michael Kennedy: Building Blocks course, it's not a conversation.
- 5:38Michael Kennedy: It's like a one-shot situation, right?
- 5:41Michael Kennedy: You come up-- you say, I have some code or some documents,
- 5:43Michael Kennedy: and I have almost like an API.
- 5:45Michael Kennedy: I'm going to send that off to the LLM and ask it, tell me X, Y, and Z about it.
- 5:51Michael Kennedy: And sure, it's got some kind of temperature and it won't always give an exactly the same answer,
- 5:56Michael Kennedy: but you're willing to, you know, you're willing to accept an answer.
- 6:00Michael Kennedy: And at that point, like why ask it again and again and again, which it might take seconds,
- 6:05Michael Kennedy: it might cost money.
- 6:06Michael Kennedy: Whereas if you just remember through caching somehow, you remember it, it's like, boom, instant.
- 6:13Vincent Warmerdam: Yeah, and it tends to come up a lot in when you're doing benchmarks, for example.
- 6:16Vincent Warmerdam: So you have this for loop, you want to go over your entire data set, try all these different approaches.
- 6:21Vincent Warmerdam: And if you've got a new approach, then you want that to run, of course.
- 6:23Vincent Warmerdam: But if you accidentally trigger an old approach, then you don't want to incur the cost of like going through all those different LLMs.
- 6:29Vincent Warmerdam: I should say, like, even if you just forget about LLMs, let's just say machine learning in general.
- 6:33Vincent Warmerdam: Let's say there's some sort of image classification thing you're using in the cloud.
- 6:36Vincent Warmerdam: There also, you would say, like, file name goes in.
- 6:39Vincent Warmerdam: that's an image and if the same file name goes in we don't want the expensive compute cost to happen
- 6:43Vincent Warmerdam: either so it's definitely more general than llms but llms do feel like it's the zeitgeisty thing to
- 6:48Michael Kennedy: worry about yeah i think for two reasons one because they're just the topic du jour and two
- 6:54Michael Kennedy: because they're they're i think a part of computing that most people experience that is way slower than
- 6:59Vincent Warmerdam: they're used to yeah well and especially if you're you know if i suppose that you you have a an
- 7:05Vincent Warmerdam: an attic somewhere and you're a dad and you want to do home lab stuff and you're playing with all
- 7:09Vincent Warmerdam: these open source LLM models, then you also learn that, yeah, they're fun to play with, but they also
- 7:14Vincent Warmerdam: take a lot of time to compute things. So then immediately you get the motivation to do it the
- 7:18Michael Kennedy: right way. Yeah, I built a couple of little utilities that talk to a local LLM. I think it's
- 7:25Michael Kennedy: the OpenAI OpenWeights one, that 20 billion parameter one I have running on my Mac Mini,
- 7:31Michael Kennedy: and it's pretty good, a little bit slow, but, you know, it's fine for what it's being used for. And
- 7:35Michael Kennedy: put-- use your disk cache technique on it.
- 7:39Michael Kennedy: And if I ask it the same question again, it's like, boom.
- 7:41Michael Kennedy: You don't need to wait 10 seconds.
- 7:43Michael Kennedy: Here's the answer.
- 7:43Michael Kennedy: Yeah.
- 7:44Vincent Warmerdam: So that-- and I guess like-- but I guess from your perspective,
- 7:46Vincent Warmerdam: I think your main entry point to this domain
- 7:49Vincent Warmerdam: was a little bit more from the web dev perspective, right?
- 7:51Vincent Warmerdam: Like that's-- and I suppose you're using it a lot
- 7:53Vincent Warmerdam: for preventing expensive queries to go to Postgres,
- 7:57Vincent Warmerdam: or I don't exactly know your backend.
- 7:59Michael Kennedy: You know how-- you won't believe how optimized my website is.
- 8:02Michael Kennedy: There's not a single query that goes to Postgres,
- 8:05Michael Kennedy: because they go to MongoDB.
- 8:06Michael Kennedy: I'm just kidding.
- 8:06Vincent Warmerdam: There you go.
- 8:07Michael Kennedy: No, but your point is totally valid.
- 8:10Michael Kennedy: Go into the database, right?
- 8:11Michael Kennedy: Now, I don't actually cache that many requests.
- 8:15Michael Kennedy: I don't avoid that many requests going to the database.
- 8:17Michael Kennedy: They're really quite quick, and so I'm OK with that.
- 8:19Michael Kennedy: But when you think about a feature-rich database,
- 8:23Michael Kennedy: feature-rich web app, there's just tons of these little edge
- 8:26Michael Kennedy: cases you're like, oh, got to do that thing.
- 8:28Michael Kennedy: And it's not a big deal, but we've
- 8:29Michael Kennedy: got to do it 500 times in a request.
- 8:31Michael Kennedy: Then it is kind of a thing.
- 8:34Michael Kennedy: So let me give you an example.
- 8:35Michael Kennedy: I'll give you some examples.
- 8:36Michael Kennedy: So for example, the good portions of the show notes
- 8:40Michael Kennedy: on talkpython.fm are in Markdown.
- 8:43Michael Kennedy: I don't want to show people Markdown.
- 8:44Michael Kennedy: I want to show them HTML, right?
- 8:47Michael Kennedy: So when a request comes in, it'll
- 8:50Michael Kennedy: say any fragment of HTML that needs
- 8:53Michael Kennedy: to be turned into Markdown instead of just going, oh,
- 8:56Michael Kennedy: let me process that.
- 8:57Michael Kennedy: It just goes, all right, what is the hash of this
- 9:00Michael Kennedy: or some other indicator of the content?
- 9:03Michael Kennedy: And then I've already computed that and stored it in disk cache.
- 9:06Michael Kennedy: So here's the HTML result.
- 9:08Michael Kennedy: Another example is there's a little YouTube icon on each page.
- 9:13Michael Kennedy: And that's actually in the show notes, but then the website parses the YouTube ID out
- 9:17Michael Kennedy: and then embeds it with an, like, there's a bunch of stuff going on there to keep YouTube
- 9:22Michael Kennedy: out of spying on my visitors.
- 9:25Michael Kennedy: But stuff happens, YouTube ID is used.
- 9:27Michael Kennedy: That could be parsed every time.
- 9:29Michael Kennedy: Or I can just say this episode has this YouTube ID.
- 9:33Michael Kennedy: That information goes into a cache, right?
- 9:35Michael Kennedy: And because it's a disk cache sort of scenario,
- 9:38Michael Kennedy: like a file-based one, not an LRU cache.
- 9:42Michael Kennedy: It doesn't change the memory footprint
- 9:44Michael Kennedy: and it's shared across processes.
- 9:46Michael Kennedy: So in like the web world,
- 9:47Michael Kennedy: it's really common to have a web garden
- 9:48Michael Kennedy: where you've got like two or four processes
- 9:51Michael Kennedy: all being like round robin to
- 9:53Michael Kennedy: from some web server manager thing, right?
- 9:56Michael Kennedy: If you don't somehow out of process that,
- 9:58Michael Kennedy: either Redis or SQLite or database or something,
- 10:03Michael Kennedy: then all of those things are recreating that, right?
- 10:05Michael Kennedy: They can't reuse that, right?
- 10:07Michael Kennedy: So there's a lot of interesting components there.
- 10:09Vincent Warmerdam: And I suppose your web deployment,
- 10:10Vincent Warmerdam: you have like a big VM, I suppose,
- 10:11Vincent Warmerdam: and then there's like multiple Docker containers running,
- 10:14Vincent Warmerdam: but they do all have access to the same volume,
- 10:16Vincent Warmerdam: and that's how you access SQLite.
- 10:18Michael Kennedy: Bingo, yeah, exactly, exactly.
- 10:21Michael Kennedy: And how am I doing?
- 10:22Michael Kennedy: Yeah, so what I have done is in the Docker Compose file,
- 10:26Michael Kennedy: I have an external,
- 10:28Michael Kennedy: This is also important for Docker.
- 10:29Michael Kennedy: So I have an external folder on a big hard drive in the big VM that says, here's where
- 10:34Michael Kennedy: all the caches go.
- 10:36Michael Kennedy: And then depending on which app, it'll pick like a sub directory it can go look at or
- 10:40Michael Kennedy: whatever that it's using.
- 10:41Michael Kennedy: And so that way, even if I do a complete rebuild of the Docker image, it still retains
- 10:48Michael Kennedy: its cache from version to version and all that kind of business.
- 10:51Michael Kennedy: You could do that with a persistent VM as well, volume as well.
- 10:55Michael Kennedy: But I've just decided--
- 10:57Michael Kennedy: you can go and inspect it a little easier
- 10:58Michael Kennedy: and see how big the cache is and stuff like that.
- 11:00Vincent Warmerdam: OK, so we're going to get into the weeds of how
- 11:03Vincent Warmerdam: disk cache works exactly.
- 11:04Vincent Warmerdam: But I'm triggered here because it sounds like you've done
- 11:06Vincent Warmerdam: something clever there.
- 11:07Vincent Warmerdam: Because what you can do in disk cache
- 11:09Vincent Warmerdam: is you can say, look, here's a file that's SQLite.
- 11:11Vincent Warmerdam: And then it behaves like a dictionary,
- 11:13Vincent Warmerdam: but it's persisted on disk.
- 11:14Vincent Warmerdam: But what I just heard you say is that you've got multiple caches.
- 11:16Vincent Warmerdam: So am I right to hear that, oh, for some things that
- 11:19Vincent Warmerdam: need to be cached, let's say the YouTube things,
- 11:21Vincent Warmerdam: that's a separate file.
- 11:22Vincent Warmerdam: And then all the markdown stuff, that's also
- 11:24Vincent Warmerdam: a separate file, and therefore if connections need to be made to either,
- 11:27Vincent Warmerdam: it's also kind of nicely split.
- 11:29Vincent Warmerdam: Is that also the design there?
- 11:30Michael Kennedy: Yeah, that is.
- 11:30Michael Kennedy: And actually, before, like, we're going to dive into all the details of how it works,
- 11:33Michael Kennedy: but I'll just go, I'm just to give people a little glimpse.
- 11:36Michael Kennedy: I'll go ahead and show, I've got this whole admin backend here.
- 11:39Michael Kennedy: And I've got different caches for different purposes.
- 11:42Michael Kennedy: Because they're just SQLite files, you can either say, give me the same one,
- 11:45Michael Kennedy: or you can say, this one is named something else,
- 11:47Michael Kennedy: and it has a different file name or different folder or whatever.
- 11:50Michael Kennedy: Right, so I've got one that stores things like that YouTube ID I talked about
- 11:53Michael Kennedy: any markdown, any fragment of markdown anywhere in the web app that it needs to say that needs
- 11:58Michael Kennedy: to go to HTML, like just.
- 12:00Vincent Warmerdam: Yeah, and it's like 8,000 items in that thing.
- 12:03Michael Kennedy: Yeah.
- 12:04Michael Kennedy: In this one, there's 8,970 items, which is nine megs, right?
- 12:08Michael Kennedy: I mean, it's not huge, but it's not too bad.
- 12:10Michael Kennedy: And you can actually even see where it thinks it lives, but that's not really where it lives
- 12:14Michael Kennedy: because there's, you know, the volume redirects and stuff.
- 12:17Michael Kennedy: But I've also got stuff for directly about the episodes that it needs to pull back.
- 12:22Michael Kennedy: And then I do a lot of HTTP caching.
- 12:25Michael Kennedy: And one of the things that I think is really wrong with web development is people say,
- 12:30Michael Kennedy: well, that's like a stale image or that's a stale CSS file or JavaScript, you know,
- 12:33Michael Kennedy: all that kind of stuff.
- 12:34Michael Kennedy: So if you just do like super minor tricks and just put some kind of hash ID on the end
- 12:41Michael Kennedy: of your content, it will, and you teach your CDN or whatever, that that's a different file
- 12:47Michael Kennedy: if it varies by query string, then you never, ever have to worry about stale content ever.
- 12:52Michael Kennedy: right but computing that can be expensive especially for remote stuff like if it's it's on a different
- 12:57Michael Kennedy: it's like a s3 thing but you still want to have it do that so i have a special cache for that and
- 13:01Michael Kennedy: that takes that's like pretty complicated to build up because it's got to do like almost 700 web
- 13:06Michael Kennedy: requests to figure out what those are but once they're done it's blazing fast you don't have to
- 13:10Michael Kennedy: do it again right unless it changes then it doesn't change much and so on so there's that's the way
- 13:14Vincent Warmerdam: that i'm sort of using and appreciating disk cache yeah it works well in your setup because you've
- 13:19Vincent Warmerdam: gone for the VM route. I mean, if you go
- 13:21Vincent Warmerdam: for something like Fly.io or maybe even
- 13:24Vincent Warmerdam: DigitalOcean has like a really, I think
- 13:25Vincent Warmerdam: it's a nice like app service, but that
- 13:27Vincent Warmerdam: all revolves around Docker containers that like
- 13:29Vincent Warmerdam: spin up horizontally. And I
- 13:31Vincent Warmerdam: don't think those containers can
- 13:33Vincent Warmerdam: be configured in such a way they share the volume.
- 13:36Vincent Warmerdam: So in that sense, you could still use
- 13:38Vincent Warmerdam: disk cache, but then
- 13:40Vincent Warmerdam: each individual instance of the
- 13:41Vincent Warmerdam: Docker container would have its own cache, which still could
- 13:43Vincent Warmerdam: work out.
- 13:45Vincent Warmerdam: Not going to be as well
- 13:47Vincent Warmerdam: well functional. It's going to be better with your setup, though.
- 13:50Michael Kennedy: Yeah, absolutely. I agree, though. You could still do it. Or you could go, I'll take the
- 13:55Michael Kennedy: zen of what Vincent and Michael are saying today, and I'll apply that to Postgres, or
- 13:59Michael Kennedy: I'll apply that to whatever data. You could pull this off in a database.
- 14:03Vincent Warmerdam: You would just have to do more work. Yeah. I mean, I've had a couple of, I think
- 14:07Vincent Warmerdam: it was like a Django conference talk I saw a while ago. They were also raving about
- 14:11Vincent Warmerdam: disk cache. But the merits of disk cache do depend a little bit on your
- 14:15Vincent Warmerdam: deployment, though.
- 14:15Vincent Warmerdam: That is, I think, one observation.
- 14:17Vincent Warmerdam: Like in your setup, I can definitely imagine it.
- 14:18Vincent Warmerdam: Interesting.
- 14:19Vincent Warmerdam: Yeah.
- 14:19Michael Kennedy: Yeah.
- 14:20Michael Kennedy: Well, I don't even think we properly introduced this thing
- 14:22Michael Kennedy: yet, so.
- 14:23Vincent Warmerdam: But let's maybe go there.
- 14:24Vincent Warmerdam: Yeah.
- 14:24Vincent Warmerdam: Let's start there.
- 14:25Michael Kennedy: Let's start there.
- 14:26Vincent Warmerdam: It's time.
- 14:26Vincent Warmerdam: OK.
- 14:27Vincent Warmerdam: It's time.
- 14:27Vincent Warmerdam: Yeah.
- 14:29Vincent Warmerdam: I guess the simplest way I usually describe it,
- 14:31Vincent Warmerdam: it really behaves like a dictionary,
- 14:33Vincent Warmerdam: except you persist a disk and under the hood
- 14:35Vincent Warmerdam: is using SQLite.
- 14:36Vincent Warmerdam: I think that's the-- it doesn't cover everything,
- 14:38Vincent Warmerdam: but you get quite close, if that's the way it is.
- 14:40Michael Kennedy: I think there might be--
- 14:42Michael Kennedy: you know, I keep harping on this on the show,
- 14:44Michael Kennedy: but there are so many people that are new to Python
- 14:45Michael Kennedy: and programming these days.
- 14:47Michael Kennedy: Many, many of them, almost half of them.
- 14:49Michael Kennedy: I think it's worth pointing out, just like, what is SQLite?
- 14:51Michael Kennedy: Like, why is it different than any other database?
- 14:54Michael Kennedy: Like, why have I been using the word database or SQLite
- 14:56Michael Kennedy: when SQLite is a database, right?
- 14:57Michael Kennedy: That's weird.
- 14:58Vincent Warmerdam: - So, I never really took a good database, of course.
- 15:01Vincent Warmerdam: I might be ruining the formalism of it.
- 15:04Vincent Warmerdam: But the main, like, for me at least,
- 15:06Vincent Warmerdam: the way I like to think about it is Postgres,
- 15:08Vincent Warmerdam: that's a thing I can run on a VM,
- 15:09Vincent Warmerdam: and then other Docker containers can connect to it
- 15:13Vincent Warmerdam: because it's running out of process.
- 15:14Vincent Warmerdam: There's some other process that has the database somewhere,
- 15:17Vincent Warmerdam: and I can connect to it.
- 15:18Vincent Warmerdam: And I think the main thing that makes SQLite different
- 15:20Vincent Warmerdam: is that, no, you got to run it on the same machine,
- 15:23Vincent Warmerdam: on the same process where your program is running.
- 15:25Vincent Warmerdam: And that's, I think, the main--
- 15:26Vincent Warmerdam: and there's all sorts of little details,
- 15:28Vincent Warmerdam: like how the data structures are used internally,
- 15:30Vincent Warmerdam: and SQLite doesn't have a lot of types.
- 15:32Vincent Warmerdam: There's lots of other differences.
- 15:33Vincent Warmerdam: I think that's the main one.
- 15:35Vincent Warmerdam: Unless, Michael, I forgot something.
- 15:36Michael Kennedy: MICHAEL LUTH: Yeah, no, I think it's--
- 15:38Michael Kennedy: and it's--
- 15:40Michael Kennedy: operationally, it's a separate thing
- 15:43Michael Kennedy: run. It has to have both, it has to be secure because if your data gets exposed, like-
- 15:49Vincent Warmerdam: For Postgres, is it not for SQL? Yes, it's running somewhere. People can SSH in if you're
- 15:54Vincent Warmerdam: not careful. You've got to be mindful of passwords and all that stuff. That's totally true.
- 15:58Michael Kennedy: Right. And it can go down. Like it could just become unavailable because you've screwed up
- 16:02Michael Kennedy: something or whatever, right? It's a thing you have to manage in the complexity of running your app
- 16:07Michael Kennedy: when it's like, well, it used to just be one thing I could run in a Docker container. Well,
- 16:10Michael Kennedy: now I got different servers, they got to coordinate and there's firewalls and there's like, it's just,
- 16:14Michael Kennedy: it just takes it so much higher in terms of complexity that like SQLite is a file.
- 16:19Michael Kennedy: Yes.
- 16:20Vincent Warmerdam: I mean, I do want to maybe defend Postgres a little bit there.
- 16:22Vincent Warmerdam: Cause one thing that's like really nice and convenient in terms of like CI/CD and deployments
- 16:26Vincent Warmerdam: and all that, oh, suppose you want to scale horizontally and there's like Docker containers
- 16:31Vincent Warmerdam: running on the left and there's this one Postgres thing running on the right.
- 16:34Vincent Warmerdam: I mean, you can just turn on and off all those Docker containers as you see fit.
- 16:38Vincent Warmerdam: they're just going to connect to the Postgres instance.
- 16:40Vincent Warmerdam: And I've done this trick for Calmcode a bunch of times
- 16:43Vincent Warmerdam: where I just switch cloud providers,
- 16:45Vincent Warmerdam: because Postgres is running there,
- 16:46Vincent Warmerdam: and I can just move the Docker containers
- 16:48Vincent Warmerdam: to another cloud provider, and it all works fine.
- 16:50Vincent Warmerdam: No migration necessary.
- 16:52Vincent Warmerdam: With SQLite, that aspect is a little bit more tricky.
- 16:54Vincent Warmerdam: You have to be a bit more mindful.
- 16:56Vincent Warmerdam: Although, I should mention, might be worth a Google.
- 16:59Vincent Warmerdam: There's actually this one new cloud provider
- 17:01Vincent Warmerdam: that's very much Python-focused.
- 17:02Vincent Warmerdam: It's called Plash, P-L-A dot S-H, I think.
- 17:06Vincent Warmerdam: Oh, this is new to me.
- 17:07Michael Kennedy: Yeah, so I think--
- 17:08Michael Kennedy: Wow, OK.
- 17:09Michael Kennedy: Look at this.
- 17:09Michael Kennedy: From.py to.com in seconds.
- 17:12Vincent Warmerdam: Yeah, it's the Answer AI, Jeremy Howard and friends.
- 17:15Vincent Warmerdam: I don't know to what extent this is super production ready.
- 17:18Vincent Warmerdam: And SQLite, you've got to be mindful of the production aspect
- 17:22Vincent Warmerdam: for some reasons as well.
- 17:23Vincent Warmerdam: But one thing that is kind of cool about them
- 17:26Vincent Warmerdam: is they give you a persistent SQLite as a database
- 17:29Vincent Warmerdam: and a pipeline process that can just kind of attach to it.
- 17:32Vincent Warmerdam: And they just-- in their mind, that's
- 17:33Vincent Warmerdam: the simplest way that a cloud provider should be.
- 17:36Vincent Warmerdam: take a very opinionated approach.
- 17:38Vincent Warmerdam: So yeah, if you're interested in maybe running this
- 17:40Vincent Warmerdam: as a web service, migrations are a little bit tricky
- 17:43Vincent Warmerdam: in that realm, because you do have
- 17:45Vincent Warmerdam: to download the entire data set due to migration
- 17:47Vincent Warmerdam: and upload it again, I think, if I recall correctly.
- 17:50Michael Kennedy: And for some apps, that's no big deal.
- 17:52Michael Kennedy: Others, that's a mega deal.
- 17:53Michael Kennedy: Depends how big that data is.
- 17:55Vincent Warmerdam: So I'm not suggesting this is going
- 17:56Vincent Warmerdam: to be for everything and everyone,
- 17:58Vincent Warmerdam: but I do think it's cool, which is why I figured I'd mention it.
- 18:00Michael Kennedy: Oh, it's new to me.
- 18:03Michael Kennedy: I'm going to follow up with a Lightstream.
- 18:06Michael Kennedy: Have you seen this?
- 18:07Vincent Warmerdam: Yeah, that is also really neat.
- 18:11Vincent Warmerdam: So basically, what if you want to back up your SQLite?
- 18:13Vincent Warmerdam: Like, how could you do that?
- 18:15Vincent Warmerdam: Oh, it might be nice to do that with S3.
- 18:17Vincent Warmerdam: And I think it's like the guy who made the thing works at Fly.io.
- 18:21Vincent Warmerdam: He's doing a bunch of low-level stuff.
- 18:23Vincent Warmerdam: One thing about that open source package
- 18:25Vincent Warmerdam: is also really interesting, by the way,
- 18:26Vincent Warmerdam: is I think he refuses PRs from the outside.
- 18:30Vincent Warmerdam: He just wants to have no distractions whatsoever.
- 18:33Vincent Warmerdam: He has a very interesting way of developing software.
- 18:35Vincent Warmerdam: You can submit issues, of course.
- 18:38Vincent Warmerdam: I think if you scroll down, there used to be a notice that
- 18:40Vincent Warmerdam: basically said, hey, this is a--
- 18:42Vincent Warmerdam: I'm not running this--
- 18:43Vincent Warmerdam: Yeah.
- 18:44Vincent Warmerdam: There you go.
- 18:45Vincent Warmerdam: We welcome-- yeah, contribution guide.
- 18:48Vincent Warmerdam: We welcome bug reports.
- 18:51Vincent Warmerdam: Yeah, this is a way where you can basically
- 18:53Vincent Warmerdam: stream updates to S3.
- 18:54Vincent Warmerdam: And the main observation there is S3 is actually really cheap
- 18:58Vincent Warmerdam: if all you do is push stuff into it.
- 18:59Vincent Warmerdam: If you never pull it out, usually getting it out
- 19:02Vincent Warmerdam: is the expensive bit of S3.
- 19:03Vincent Warmerdam: So this is like pennies on the dollar
- 19:06Vincent Warmerdam: for really decent backup.
- 19:08Vincent Warmerdam: And you can also send it to multiple--
- 19:09Vincent Warmerdam: you can send it to Amazon and also to DigitalOcean,
- 19:11Vincent Warmerdam: if you like.
- 19:12Michael Kennedy: Yeah.
- 19:13Michael Kennedy: Yeah, because these days, S3 is really
- 19:15Michael Kennedy: a synonym for blob storage on almost any hosting platform.
- 19:20Michael Kennedy: Like, it used to be S3 might go to literally S3 at AWS.
- 19:23Michael Kennedy: But now it's like, or DigitalOcean object spaces,
- 19:27Michael Kennedy: or to you name it.
- 19:29Michael Kennedy: They've all adopted the API, kind of like OpenAI's API.
- 19:32Vincent Warmerdam: Yeah, I will say it's a little bit awkward that you have to--
- 19:35Vincent Warmerdam: like, sometimes you go to a cloud provider,
- 19:37Vincent Warmerdam: and they say, you have to download a SDK
- 19:40Vincent Warmerdam: from a competing cloud provider, and then you
- 19:41Vincent Warmerdam: can connect to our cloud bucket.
- 19:44Michael Kennedy: I know.
- 19:44Michael Kennedy: And it's usually boto3.
- 19:46Michael Kennedy: And boto3 is--
- 19:48Michael Kennedy: if you want to cry because you're using a library,
- 19:50Michael Kennedy: like, boto3 has a good chance of being the first one
- 19:53Michael Kennedy: to make you do it.
- 19:53Michael Kennedy: It is so bad for me.
- 19:55Michael Kennedy: It's so not custom--
- 19:57Michael Kennedy: It's not built with craft and love.
- 19:59Michael Kennedy: It's like auto-generated where you pass these--
- 20:02Michael Kennedy: like, you pass this kind of dictionary,
- 20:03Michael Kennedy: and then the other argument takes a separate dictionary
- 20:05Michael Kennedy: that relates back-- it's just like,
- 20:07Michael Kennedy: could you give me a real API here?
- 20:09Vincent Warmerdam: IAN MCKAYAN: I mean, the one thing
- 20:10Vincent Warmerdam: I can appreciate about Bodo that I do think is honest to mention
- 20:12Vincent Warmerdam: is they do try to just maintain it.
- 20:15Vincent Warmerdam: The backward compatibility of that thing
- 20:17Vincent Warmerdam: also means it can't move in any direction as well.
- 20:19Vincent Warmerdam: And I can't-- there is this meme where Google kills all
- 20:22Vincent Warmerdam: of its products way too early, and Amazon's meme
- 20:24Vincent Warmerdam: that they kill them way too late, sometimes never.
- 20:27Vincent Warmerdam: Right?
- 20:28Vincent Warmerdam: So in that sense, I can appreciate that they just
- 20:30Vincent Warmerdam: try to keep Bodo just not necessarily as user friendly,
- 20:33Vincent Warmerdam: but they do keep it super stable.
- 20:34Vincent Warmerdam: Like, I get there's a balance there.
- 20:36Michael Kennedy: Yeah.
- 20:37Michael Kennedy: I feel like we still haven't really introduced this cache.
- 20:39Michael Kennedy: We've kind of set the stage.
- 20:41Vincent Warmerdam: Anyway, but yeah, SQLite, super cool.
- 20:44Vincent Warmerdam: How does it work under the hood?
- 20:45Vincent Warmerdam: Well, it's really just like a Python dictionary.
- 20:47Vincent Warmerdam: So you can say something like, hey, make a new cache.
- 20:49Vincent Warmerdam: And then you can do things like cache, square brackets,
- 20:52Vincent Warmerdam: string name, equals, and then whatever
- 20:54Vincent Warmerdam: Python object you like can go in. And Python has this serialization method called a pickle.
- 21:00Vincent Warmerdam: Serialization just means, well, you can persist it to disk in some way, and then you can sort of
- 21:05Vincent Warmerdam: get it back into memory again. And that's what disk cache just uses under the hood. So in theory,
- 21:10Vincent Warmerdam: any Python object that you can think of can go into disk cache. The only sort of thing to be
- 21:16Vincent Warmerdam: mindful of is if you have like Python version, if NumPy version 1 in Python 3.6, and you're going
- 21:21Vincent Warmerdam: to inject a whole lot of that into this cache.
- 21:24Vincent Warmerdam: Don't expect those objects to serialize nicely back
- 21:26Vincent Warmerdam: if you're using Python 3.12 and NumPy version 2 or something.
- 21:29Michael Kennedy: Right, because pickle is almost an in-memory representation
- 21:33Michael Kennedy: of the thing.
- 21:34Michael Kennedy: And that may have evolved over time.
- 21:36Michael Kennedy: That's also a true statement about your own classes,
- 21:38Michael Kennedy: potentially.
- 21:39Vincent Warmerdam: Yeah, so if you're dealing with multiple Python versions
- 21:41Vincent Warmerdam: and multiple versions of different packages,
- 21:43Vincent Warmerdam: there's a little bit of a danger zone to be aware of there.
- 21:47Vincent Warmerdam: That said, for most of the stuff that I do,
- 21:48Vincent Warmerdam: that's basically a non-issue.
- 21:50Vincent Warmerdam: But I do get this nice little object that can just store stuff into SQLite and can get it out.
- 21:56Vincent Warmerdam: And it's very general.
- 21:58Vincent Warmerdam: It's going to try to be clever about it.
- 21:59Vincent Warmerdam: Like if you give it an int, it's going to actually store it as an int and not use the pickle format.
- 22:03Vincent Warmerdam: So there's a couple of clever things that it can do.
- 22:06Vincent Warmerdam: And it's also really like a Python dictionary.
- 22:07Vincent Warmerdam: So you can do the square bracket thing.
- 22:09Vincent Warmerdam: You can also do the delete and then cache square bracket thing to delete a key from the cache.
- 22:15Vincent Warmerdam: Just like a Python dictionary, you have the get method.
- 22:17Vincent Warmerdam: So you can say dot get key.
- 22:19Vincent Warmerdam: And if it's missing, you can pass a default value.
- 22:22Vincent Warmerdam: So it's very much like a dictionary.
- 22:25Vincent Warmerdam: I think Bob's your uncle on that one.
- 22:27Vincent Warmerdam: Unless, Michael, I've forgotten something.
- 22:29Vincent Warmerdam: But I think that's the simplest way to do it.
- 22:30Michael Kennedy: Yeah, pretty much.
- 22:31Michael Kennedy: Yeah, I think so.
- 22:32Michael Kennedy: The difference being it's not in memory.
- 22:34Michael Kennedy: It's stored to a file.
- 22:36Michael Kennedy: It happens-- it's not always a SQLite file.
- 22:39Michael Kennedy: But often, it is a SQLite file as its core foundation
- 22:43Michael Kennedy: that it's stored to.
- 22:43Michael Kennedy: So it gives you process restart ability,
- 22:47Michael Kennedy: where it still remembers the stuff you cached.
- 22:49Michael Kennedy: It's not like LRU cache.
- 22:50Michael Kennedy: We got to redo it every single time.
- 22:52Michael Kennedy: And I think, I don't know where it is in the docs here,
- 22:56Michael Kennedy: but the thread safety bit of it and the cross-process safety
- 23:00Michael Kennedy: is really nice about, is it persistent?
- 23:03Michael Kennedy: You've got this whole table here, things like, is it persistent?
- 23:06Michael Kennedy: Yes.
- 23:06Michael Kennedy: Is it thread safe?
- 23:07Michael Kennedy: Yes.
- 23:07Michael Kennedy: Is it process safe?
- 23:08Michael Kennedy: Yes.
- 23:10Michael Kennedy: Compared against other things people might choose.
- 23:13Michael Kennedy: And that, honestly, I think that is the other half of the magic.
- 23:17Vincent Warmerdam: Yeah, so especially for your web stuff, I would say that that's the thing you really want.
- 23:21Vincent Warmerdam: And some of that, of course, is just SQLite itself.
- 23:25Vincent Warmerdam: Historically, one reason why people always used to say, like, use Postgres, not SQLite,
- 23:28Vincent Warmerdam: has to do with precisely this concurrency stuff.
- 23:32Vincent Warmerdam: My impression is that SQLite is really good at reading, but writing can be slow if multiple processes do it.
- 23:37Vincent Warmerdam: Some of that, I think, is related to the disk as well.
- 23:39Vincent Warmerdam: I don't know to what extent that has changed.
- 23:41Vincent Warmerdam: But historically, at least, whenever I was doing Django, hanging out at Django events,
- 23:45Vincent Warmerdam: People are always saying, like, just use Postgres
- 23:46Vincent Warmerdam: because it's better for the web thing.
- 23:48Vincent Warmerdam: But it is safe, the SQLite.
- 23:51Vincent Warmerdam: It might become slower, but it is thread safe if it's--
- 23:53Vincent Warmerdam: MARK MANDEL: Right.
- 23:54Michael Kennedy: There's actually-- they've thought a lot about in this thing
- 23:58Michael Kennedy: about transactions, concurrency, and basically dealing with that.
- 24:02Michael Kennedy: But it is ultimately, for the most part,
- 24:05Michael Kennedy: still SQLite underneath.
- 24:07Michael Kennedy: But the thing with a cache is if you're writing it more
- 24:10Michael Kennedy: than you're reading it, you probably shouldn't have a cache.
- 24:13Michael Kennedy: Yeah.
- 24:13Michael Kennedy: I mean, like...
- 24:15Vincent Warmerdam: That beats the purpose.
- 24:18Michael Kennedy: Exactly.
- 24:18Michael Kennedy: Like, you get no value if you're recreating it.
- 24:21Michael Kennedy: You're only probably just doing overhead and wasting memory or disk space.
- 24:24Michael Kennedy: So it's inherently a situation where it's going to be pretty read-heavy,
- 24:30Michael Kennedy: and SQLite is good at read-heavy scenarios.
- 24:32Vincent Warmerdam: And maybe it's also fair to say, like, the LRU cache that you get with basic Python,
- 24:36Vincent Warmerdam: so also to maybe explain that one,
- 24:38Vincent Warmerdam: so the LRU cache is a little bit different because you decorate a function with it,
- 24:41Vincent Warmerdam: and then given the same inputs, one output goes out,
- 24:44Vincent Warmerdam: you can kind of keep track of a dictionary that's in memory.
- 24:47Vincent Warmerdam: If you don't have a lot of stuff to keep in the back of your mind,
- 24:49Vincent Warmerdam: then maybe you don't have to write to disk, right?
- 24:51Vincent Warmerdam: So there's also maybe a reason to just stick to caching mechanisms
- 24:54Vincent Warmerdam: that use Python memory, because I also think,
- 24:56Vincent Warmerdam: I would imagine it to be quicker too.
- 24:59Vincent Warmerdam: But maybe that's also...
- 25:01Vincent Warmerdam: Probably, yes.
- 25:01Vincent Warmerdam: That should be quicker.
- 25:02Vincent Warmerdam: It's just that if you're capped at memory,
- 25:04Vincent Warmerdam: then you might want to spill to disk,
- 25:05Vincent Warmerdam: and then disk cache becomes interesting too.
- 25:06Michael Kennedy: Right, for example, you have literally zero serialization, deserialization.
- 25:11Michael Kennedy: What you put in LRU cache is the pointer to the object
- 25:14Michael Kennedy: that you're caching, right?
- 25:15Michael Kennedy: If you've got a class or a list that's part of the LRU cache.
- 25:18Vincent Warmerdam: The one thing that is good to mention
- 25:20Vincent Warmerdam: is also a really nice feature of disk cache
- 25:21Vincent Warmerdam: is just like LRU cache has a decorator,
- 25:23Vincent Warmerdam: so you can decorate a function, disk cache also has that.
- 25:27Vincent Warmerdam: And it works kind of interestingly, too.
- 25:29Vincent Warmerdam: So when you decorate the function,
- 25:32Vincent Warmerdam: you do have to be a little bit careful if you use that.
- 25:34Vincent Warmerdam: But then disk cache will--
- 25:36Vincent Warmerdam: I think it will hash the function name and the inputs
- 25:39Vincent Warmerdam: that you pass.
- 25:40Vincent Warmerdam: I don't know if it also hashes the contents of the function.
- 25:45Vincent Warmerdam: Like if you change the function itself,
- 25:46Vincent Warmerdam: I don't know if this cache will actually put that
- 25:49Vincent Warmerdam: in a different slots, if that makes sense.
- 25:51Michael Kennedy: Yeah, you can say @cache_memoize,
- 25:54Michael Kennedy: which is the design pattern speak for just remember this.
- 25:57Michael Kennedy: Yeah.
- 25:57Michael Kennedy: It takes the arguments.
- 25:59Vincent Warmerdam: And then it has like the Fibonacci sequence,
- 26:00Vincent Warmerdam: which is the classic example, of course.
- 26:02Vincent Warmerdam: And like there are some extra things you can set there as well.
- 26:05Vincent Warmerdam: So you can say things like, hey, I think you're able to--
- 26:08Vincent Warmerdam: yeah, you're able to set the expiry.
- 26:10Vincent Warmerdam: So you can say things like, I want to cache this,
- 26:11Vincent Warmerdam: but only for the next five minutes or so,
- 26:14Vincent Warmerdam: which can make a lot of sense if you're doing a front page
- 26:16Vincent Warmerdam: kind of a thing.
- 26:17Vincent Warmerdam: So like the Reddit front page or something
- 26:19Vincent Warmerdam: like that that updates, but not every second.
- 26:20Vincent Warmerdam: It probably updates once every five minutes
- 26:22Vincent Warmerdam: or something like that.
- 26:23Vincent Warmerdam: And then you do want to have something that's cached,
- 26:25Vincent Warmerdam: but then after that, you want the cache
- 26:27Vincent Warmerdam: to maybe basically just reset.
- 26:28Vincent Warmerdam: And that is something you can also
- 26:30Vincent Warmerdam: control with a few parameters here.
- 26:31Michael Kennedy: Right, that's interesting.
- 26:32Michael Kennedy: There's a couple good use cases that come to mind for me.
- 26:35Michael Kennedy: Like one, if I put this on the function that
- 26:37Michael Kennedy: generated the RSS feed for Talk Python,
- 26:39Michael Kennedy: I could just say every one minute
- 26:41Michael Kennedy: and then it might be a little bit expensive to compute
- 26:44Michael Kennedy: because it's got a pars, you know, 535 episodes or whatever.
- 26:48Michael Kennedy: But then for one minute, all the subsequent requests,
- 26:50Michael Kennedy: just here's the answer, here's the answer.
- 26:52Michael Kennedy: And then without me managing anything,
- 26:55Michael Kennedy: it will just automatically the next minute refresh itself
- 26:58Michael Kennedy: by the nature of how it works, right?
- 27:00Vincent Warmerdam: How much traffic do you get on that endpoint?
- 27:01Vincent Warmerdam: Just roughly, like you're asking.
- 27:02Michael Kennedy: One terabyte of RSS a month.
- 27:05Michael Kennedy: Okay, gotcha.
- 27:06Vincent Warmerdam: Okay, but there you go.
- 27:08Vincent Warmerdam: Like then just doing that like once a minute instead of like many times a minute will be a huge cookie.
- 27:13Michael Kennedy: I would say it's probably more than one request a second.
- 27:17Michael Kennedy: And the file size, the response size of the RSS feed is over a meg.
- 27:21Michael Kennedy: And so it's a non-trivial amount of asking, you know.
- 27:24Vincent Warmerdam: Yeah.
- 27:24Vincent Warmerdam: And then like, and how do you fix that with a whole bunch of infrastructure?
- 27:27Vincent Warmerdam: No, with a decorator.
- 27:28Vincent Warmerdam: Like that feels...
- 27:29Vincent Warmerdam: Exactly.
- 27:30Michael Kennedy: Exactly.
- 27:31Michael Kennedy: You pretty much summed up all the reasons why I'm so excited about this,
- 27:34Michael Kennedy: because it's like you could do all of this complex stuff
- 27:36Michael Kennedy: or just like you could just literally
- 27:39Michael Kennedy: in such a simple way,
- 27:41Michael Kennedy: just not recompute it as often.
- 27:43Michael Kennedy: Yeah.
- 27:43Michael Kennedy: Here's the danger.
- 27:44Michael Kennedy: What if there's a race condition?
- 27:46Michael Kennedy: And oh my goodness, two of them sneak in.
- 27:48Michael Kennedy: You know what I mean?
- 27:49Michael Kennedy: Like, okay, so you've done a little bit extra work
- 27:51Michael Kennedy: and you throw it away.
- 27:52Vincent Warmerdam: Who cares?
- 27:52Vincent Warmerdam: I want to use Redis now.
- 27:53Vincent Warmerdam: And Redis is cool,
- 27:54Vincent Warmerdam: but I've never used it before.
- 27:56Vincent Warmerdam: Better buy a book.
- 27:57Vincent Warmerdam: Okay, no.
- 27:58Vincent Warmerdam: With this cache, if you,
- 27:59Vincent Warmerdam: I mean, I'm sure it won't solve everything,
- 28:02Vincent Warmerdam: but this, I make a bit of a joke by saying,
- 28:04Vincent Warmerdam: just use a decorator.
- 28:05Vincent Warmerdam: But it's honestly that feeling that this library really
- 28:07Vincent Warmerdam: does give you.
- 28:08Vincent Warmerdam: You can just use it as a decorator,
- 28:11Vincent Warmerdam: which has a lot of great use cases.
- 28:12Vincent Warmerdam: You can just use it as a dictionary.
- 28:13Vincent Warmerdam: So it still feels like you're writing Python.
- 28:16Vincent Warmerdam: It's just Python with one concern less.
- 28:19Vincent Warmerdam: And that is the magic.
- 28:20Michael Kennedy: MARK MANDEL: And it takes on so many of the cool aspects
- 28:22Michael Kennedy: of these high-end servers like Postgres or Redis or Valkey.
- 28:27Michael Kennedy: Valkey is sort of the shiny new Redis, right?
- 28:29Vincent Warmerdam: I would actually love to do a Redis benchmark.
- 28:31Vincent Warmerdam: I haven't done that yet.
- 28:32Vincent Warmerdam: But one thing I do wonder with disks are getting so much faster.
- 28:36Michael Kennedy: Yes.
- 28:36Vincent Warmerdam: Right?
- 28:37Vincent Warmerdam: And so you can actually at some point wonder like how much faster is Redis really going to be
- 28:40Vincent Warmerdam: and how much money are you willing to spend on it?
- 28:43Vincent Warmerdam: Because if your cache is huge and it allows to go in memory in Redis,
- 28:47Vincent Warmerdam: it could be wrong, but Redis is fully in memory, I think, right?
- 28:50Michael Kennedy: I believe so.
- 28:51Michael Kennedy: There is a database aspect.
- 28:53Michael Kennedy: Redis is weird because it could be so many.
- 28:54Michael Kennedy: Redis is cool.
- 28:55Michael Kennedy: It can do a lot.
- 28:57Michael Kennedy: But they do have, they actually have benchmarks here on.
- 29:00Vincent Warmerdam: Ah, there you go.
- 29:01Michael Kennedy: Compared against memcached and Redis.
- 29:06Michael Kennedy: And it has the get speed and the write speed.
- 29:08Michael Kennedy: And this is smaller is better.
- 29:10Michael Kennedy: Yeah, look at this.
- 29:11Michael Kennedy: Disc cache beats Redis.
- 29:13Vincent Warmerdam: And I imagine that's because of the network hop or something.
- 29:17Michael Kennedy: Yeah, exactly.
- 29:17Michael Kennedy: I bet it's the network, the network connection.
- 29:20Michael Kennedy: So if you're running it on the same machine,
- 29:22Vincent Warmerdam: you would have a different number there.
- 29:24Vincent Warmerdam: Might be good to maybe caveat that.
- 29:25Michael Kennedy: Yeah, it might be.
- 29:26Vincent Warmerdam: I mean, but also that I think that I don't know when they ran this benchmark,
- 29:29Vincent Warmerdam: but I just checked on PyPI.
- 29:32Vincent Warmerdam: This project started in 2016.
- 29:34Vincent Warmerdam: So it might-
- 29:35Michael Kennedy: I bet this is 2016 data right here.
- 29:38Michael Kennedy: If I know how these docs go.
- 29:40Vincent Warmerdam: Yeah, so it could also be that those are old disks
- 29:43Vincent Warmerdam: comparing old memories, right?
- 29:45Michael Kennedy: So then this is one of those weird benchmarks.
- 29:47Vincent Warmerdam: You got to really run them every six months or so
- 29:49Vincent Warmerdam: for them to remain relevant.
- 29:51Michael Kennedy: Yeah, yeah.
- 29:51Michael Kennedy: I mean, the new NVMe, whatever, disks,
- 29:56Vincent Warmerdam: SSD disks are so fast.
- 29:58Vincent Warmerdam: And also they're not memory.
- 29:59Vincent Warmerdam: Memory is expensive nowadays.
- 30:01Vincent Warmerdam: Yes, it is.
- 30:02Vincent Warmerdam: People want to build data centers with them, I've heard.
- 30:04Vincent Warmerdam: Yeah, yeah.
- 30:05Michael Kennedy: And on the cloud there, this is a totally,
- 30:07Michael Kennedy: this is another really interesting aspect to discuss.
- 30:11Michael Kennedy: Probably more of a web dev side of things.
- 30:13Michael Kennedy: But if you do LRU caches or even to a bigger degree,
- 30:17Michael Kennedy: I run a whole separate server,
- 30:18Michael Kennedy: even if it is just a Docker container
- 30:20Michael Kennedy: that holds a bunch of this stuff in memory,
- 30:22Michael Kennedy: that's going to take more memory on your VM
- 30:25Michael Kennedy: or your cloud deployment or whatever.
- 30:26Michael Kennedy: And if you just say, well,
- 30:28Michael Kennedy: I have this 160 gig hard drive. That's an NVMe high speed drive. Like maybe I could just put a
- 30:33Michael Kennedy: bunch of stuff there and you could really thin down your deployments, not just because it's not in
- 30:39Michael Kennedy: memory in a cache somewhere, but if you're not having any form of cache, you might be able to
- 30:42Michael Kennedy: dramatically lower how much compute you need and avoid them. Right. Like there's layers of how this
- 30:48Vincent Warmerdam: could like shave off. And again, it's one of those things of like, oh, I just, can I pay for disk
- 30:53Vincent Warmerdam: instead? Oh, that's a whole lot cheaper. What else do I got to do? You just got to write a tech
- 30:56Michael Kennedy: creator yeah i think i pay five dollars for uh i think i remember exactly but i pay something like
- 31:03Michael Kennedy: five dollars for 400 gigs of disc there you go and do you know how much 400 gigs of ram will cost
- 31:09Vincent Warmerdam: on the cloud um well i mean more there goes the college tuition but the exactly sorry kids yeah
- 31:18Vincent Warmerdam: no but like it's um and again like i vividly remember when i started college people were
- 31:23Vincent Warmerdam: always saying oh keep it in memory because it's way faster than disc but i i think we got to let
- 31:26Vincent Warmerdam: a lot of that stuff just go.
- 31:28Michael Kennedy: Interesting idea. Yeah, I agree, though.
- 31:30Michael Kennedy: I think you're right. But anyway,
- 31:33Vincent Warmerdam: so far, we've mainly
- 31:34Vincent Warmerdam: been discussing the mechanics of it, but
- 31:36Vincent Warmerdam: there's some bills and whistles I think we should maybe also mention.
- 31:38Vincent Warmerdam: The expiry definitely is one of them.
- 31:41Vincent Warmerdam: There's also first in, first out
- 31:42Vincent Warmerdam: kinds of things that you can do.
- 31:45Vincent Warmerdam: So maybe if we could go back to that.
- 31:46Michael Kennedy: Yeah, there's a bunch of features, actually,
- 31:48Michael Kennedy: there.
- 31:50Michael Kennedy: The expiry is interesting already
- 31:53Michael Kennedy: because
- 31:54Michael Kennedy: if you do regular, say, LRU caching
- 31:56Michael Kennedy: like that, you have a natural.
- 31:59Michael Kennedy: It's going to go away when the process
- 32:00Michael Kennedy: restarts, and that's going to happen eventually.
- 32:03Michael Kennedy: Even in a web app, you ship a new
- 32:04Michael Kennedy: version, you've got to restart the thing or something.
- 32:07Michael Kennedy: But when it goes to the disk,
- 32:08Michael Kennedy: it starts to pile up, right? That's why I have this little admin
- 32:10Michael Kennedy: page that has, like, how big is this
- 32:12Michael Kennedy: and a button to clear it.
- 32:14Vincent Warmerdam: In fairness, that can blow up
- 32:16Vincent Warmerdam: to a lot of Palooza as well, if you're not careful.
- 32:18Michael Kennedy: It is a concern. Yeah, it is definitely a concern.
- 32:20Michael Kennedy: And so I think a big way to fix it,
- 32:22Michael Kennedy: like the expiry, we already talked about why it's
- 32:24Michael Kennedy: interesting for stale data.
- 32:26Michael Kennedy: You want it to just like auto refresh,
- 32:28Michael Kennedy: but it's also just a safeguard of maybe we'll just recompute this once a month.
- 32:32Michael Kennedy: It's really quick and easy.
- 32:34Michael Kennedy: Yeah.
- 32:34Michael Kennedy: Maybe just don't let it linger forever.
- 32:37Vincent Warmerdam: I think what you can also do though, if I'm not mistaken,
- 32:39Vincent Warmerdam: is I think you can also set like a max key size.
- 32:41Vincent Warmerdam: So you can say this cache, this particular disk cache can only have 10,000 keys in it
- 32:46Vincent Warmerdam: and use a first in first out kind of a principle.
- 32:49Michael Kennedy: Right, or last accessed or number of times.
- 32:52Michael Kennedy: There's a bunch of metrics there actually for how that works.
- 32:54Michael Kennedy: Yeah, it's pretty interesting.
- 32:56Vincent Warmerdam: I've never had to fiddle around with them too much,
- 32:59Vincent Warmerdam: but it's one of those things where even if I don't need it right now,
- 33:02Vincent Warmerdam: it is just a relief to see that the feature is there
- 33:04Vincent Warmerdam: in case you might need it later.
- 33:06Michael Kennedy: Yeah, yeah, for sure.
- 33:08Michael Kennedy: Let me see if I can find out where that is.
- 33:09Michael Kennedy: I don't know, keep bouncing around the same spot.
- 33:11Michael Kennedy: I've got it, let's just talk through them.
- 33:12Michael Kennedy: So I think the tags and expiries are pretty interesting,
- 33:15Michael Kennedy: but then there's also, I think, something that surprised me a little bit
- 33:18Michael Kennedy: is these different kinds of caches.
- 33:20Michael Kennedy: So there's like a fan out cache.
- 33:23Michael Kennedy: Have you looked at these?
- 33:24Michael Kennedy: These are interesting.
- 33:25Vincent Warmerdam: I remember reading about, I've never used them, but I do remember reading them.
- 33:29Michael Kennedy: So let me give you the quick rundown and then you'll understand instantly.
- 33:32Michael Kennedy: It's super quick.
- 33:32Michael Kennedy: So it uses sharding, which is a database term, right?
- 33:36Michael Kennedy: So sharding is like, if I've got a billion records and it's a challenge to put them all
- 33:40Michael Kennedy: in the same database entry, database table or server, I could actually have 10 servers
- 33:45Michael Kennedy: and decide, well, okay, what we're going to do is if it's a number of the ID of the user,
- 33:49Michael Kennedy: if the first number is one, then they go into this database.
- 33:52Michael Kennedy: If it's two, then they go in that, right?
- 33:54Michael Kennedy: So 2005 goes into the second database and so on.
- 33:57Michael Kennedy: So it does that as well.
- 33:59Michael Kennedy: This is one of the things it does to try to avoid the issues of multiple writers, I believe.
- 34:04Michael Kennedy: So you're less likely to write to the same database.
- 34:07Michael Kennedy: So it doesn't have to lock as hard.
- 34:08Vincent Warmerdam: It's kind of what you do where you say, oh, I've got this for the YouTube link.
- 34:12Vincent Warmerdam: And I've got this for the HTML markdown.
- 34:14Vincent Warmerdam: Except those are like different chunks because of their use case.
- 34:19Vincent Warmerdam: But you can also imagine, well, I've got this long list of users.
- 34:22Vincent Warmerdam: but I still want to benefit from having multiple SQLite instances.
- 34:25Vincent Warmerdam: And I suppose that's when you use this, right?
- 34:26Michael Kennedy: Yeah, I think that is why.
- 34:28Michael Kennedy: So it says, it's built on top of cache, cache fanout, automatically shards.
- 34:33Michael Kennedy: Automatically, you just said how many you want, it figures out what that means.
- 34:36Michael Kennedy: And it says, while readers and writers don't block each other,
- 34:38Michael Kennedy: writers block other writers.
- 34:39Michael Kennedy: Therefore, a shard for every concurrent writer suggests it.
- 34:44Michael Kennedy: This will depend on your scenario, default is eight.
- 34:46Michael Kennedy: So that's pretty cool.
- 34:48Vincent Warmerdam: Yeah, okay.
- 34:48Vincent Warmerdam: And presumably internally does something like hashing to figure out how to like
- 34:53Michael Kennedy: send it around the shards.
- 34:55Michael Kennedy: Right.
- 34:55Michael Kennedy: The keys themselves have to be hashable anyway, probably.
- 34:57Michael Kennedy: So just hashes the key and then shards on like the first couple letters or whatever.
- 35:02Michael Kennedy: Yeah, this is cool.
- 35:03Michael Kennedy: So it avoids the concurrency crashes.
- 35:06Michael Kennedy: The one difference for me, the reason I didn't choose fanout cache is because I want to be
- 35:10Michael Kennedy: able to say, I want to clear all the YouTube IDs, but I want to keep the really expensive
- 35:13Michael Kennedy: to compute hashes.
- 35:15Michael Kennedy: I want to be able to clear stuff if I really have to by category.
- 35:19Michael Kennedy: And I guess you could also do that with tags, but I'm just not that advanced.
- 35:22Vincent Warmerdam: Well, and also keep it simple, right?
- 35:23Vincent Warmerdam: And again, it's one of those things where it's, oh, it's nice to know that this is in here,
- 35:27Vincent Warmerdam: even if you don't use it directly.
- 35:28Vincent Warmerdam: I do agree.
- 35:29Vincent Warmerdam: This is a really nice feature.
- 35:30Michael Kennedy: It is.
- 35:31Michael Kennedy: And I probably will never in my life use it, but it's really cool that it's like, you know,
- 35:34Michael Kennedy: it's one of those things about a library that when you're thinking about picking it, it's
- 35:38Michael Kennedy: like, okay, the core feature is great, but if I outgrow it, what is my next step?
- 35:42Michael Kennedy: Do I have to completely switch to something really different
- 35:45Michael Kennedy: like Redis or Valkey?
- 35:46Michael Kennedy: Or do I just change the class I'm using?
- 35:49Vincent Warmerdam: I had that with this.
- 35:51Vincent Warmerdam: So I actually had that feeling a while ago.
- 35:53Vincent Warmerdam: We're going to get to my example I think a bit later.
- 35:54Vincent Warmerdam: But I was using the Memoize decorator
- 35:57Vincent Warmerdam: to decorate a function to properly cache that.
- 36:00Vincent Warmerdam: But the one issue I had is an input to that function
- 36:04Vincent Warmerdam: was a progress bar.
- 36:05Vincent Warmerdam: It's kind of a remote specific thing.
- 36:06Vincent Warmerdam: I wanted this one progress bar to update
- 36:09Vincent Warmerdam: from inside of this one function.
- 36:10Vincent Warmerdam: But the downside was that every time
- 36:12Vincent Warmerdam: I rerun the notebook, I make a new progress bar object.
- 36:15Vincent Warmerdam: Oh, and that means that the input
- 36:16Vincent Warmerdam: has a different object going in.
- 36:17Vincent Warmerdam: MARK MANDEL: So you never actually hit the cache.
- 36:19Vincent Warmerdam: JOHN MUELLER: So, oh my god, I'm never hitting the cache.
- 36:21Vincent Warmerdam: This is horrible.
- 36:23Vincent Warmerdam: Then it turns out the Memoize decorator
- 36:25Vincent Warmerdam: also allows you to ignore a couple of the inputs
- 36:27Vincent Warmerdam: of the function.
- 36:28Vincent Warmerdam: So you can also--
- 36:28Vincent Warmerdam: MARK MANDEL: Oh, interesting.
- 36:28Michael Kennedy: Like ignore by keyword or something, keyword argument.
- 36:31Vincent Warmerdam: JOHN MUELLER: Precisely.
- 36:31Vincent Warmerdam: And there's a bunch of use cases for it, and this was one.
- 36:34Vincent Warmerdam: And you can just imagine my relief
- 36:36Vincent Warmerdam: after writing the entire notebook to then look at the docs
- 36:39Vincent Warmerdam: and go, oh, sweet.
- 36:42Michael Kennedy: Yeah, that's super sweet.
- 36:43Michael Kennedy: Yeah.
- 36:44Michael Kennedy: Okay.
- 36:45Michael Kennedy: I think there's right below this.
- 36:46Michael Kennedy: Yeah, there's a couple.
- 36:47Michael Kennedy: We can just go down this list here.
- 36:48Michael Kennedy: There's some cool.
- 36:48Michael Kennedy: Oh, Django.
- 36:49Michael Kennedy: So they have a legit Django cache.
- 36:52Vincent Warmerdam: Yeah, yeah, yeah.
- 36:52Michael Kennedy: Straight in.
- 36:53Vincent Warmerdam: Sweet.
- 36:54Vincent Warmerdam: Yeah, I recall a little bit.
- 36:56Vincent Warmerdam: It was like a huge Django following for this thing.
- 36:58Michael Kennedy: Yeah, and I think this is a part why.
- 36:59Michael Kennedy: And when I first saw it, the reason it got sent to me is somebody's like,
- 37:02Michael Kennedy: oh, this disk cache, Django cache is a really cool thing to just drop into Django.
- 37:07Michael Kennedy: And I'm like, that's cool.
- 37:09Michael Kennedy: I'm not using Django, but I admire it.
- 37:11Michael Kennedy: That's why I didn't really look into it until I saw your use case outside of Django.
- 37:14Michael Kennedy: I'm like, oh, okay, I understand how much this can do.
- 37:17Michael Kennedy: So the Django dish cache says it uses the fanout cache,
- 37:20Michael Kennedy: which we just discussed with the sharding,
- 37:21Michael Kennedy: to provide a Django-compatible cache interface.
- 37:25Michael Kennedy: And you just do that in your settings file,
- 37:26Michael Kennedy: and you just say the back end is diskcache.django cache,
- 37:29Michael Kennedy: and you give it a location.
- 37:31Michael Kennedy: Boom, off it goes, right?
- 37:32Michael Kennedy: So really, really nice.
- 37:33Michael Kennedy: Cool.
- 37:34Michael Kennedy: Yeah, and it sounds like you've done more Django than me.
- 37:37Michael Kennedy: How's this sit with you?
- 37:37Vincent Warmerdam: I mean, to be very clear, I do think Django is really nice and really mature.
- 37:42Vincent Warmerdam: I do sometimes have a bit of a love-hate relationship with it because Django can go really, really deep.
- 37:47Vincent Warmerdam: And some of that configuration stuff definitely can be a little bit in your face.
- 37:51Vincent Warmerdam: So the main thing I just want to observe is doing everything manually inside of Django can be very time-consuming.
- 37:57Vincent Warmerdam: So it's definitely nice to know that someone took the effort to make a proper Django plugin in the way that Django wants it.
- 38:03Vincent Warmerdam: That's definitely the thing to appreciate here.
- 38:05Vincent Warmerdam: I've never really used this in a Django app, to be honest.
- 38:08Michael Kennedy: Yeah.
- 38:09Michael Kennedy: You know, it has a lot of nice settings here.
- 38:11Michael Kennedy: Like you can set the number of shards, the timeout, so in case there's a write or read
- 38:16Michael Kennedy: contention, it can deal with that or it can at least let you know you're failing.
- 38:19Michael Kennedy: It even has a size limit.
- 38:21Vincent Warmerdam: Does it say how you can configure what to cache and whatnot?
- 38:24Vincent Warmerdam: Or is like the-- I've never really used caches in Django in general.
- 38:27Vincent Warmerdam: So I don't know if there's a general cache feature in Django itself that it will just
- 38:31Vincent Warmerdam: plug into or if--
- 38:32Michael Kennedy: I think there is a general cache feature in Django.
- 38:35Michael Kennedy: The Django people are like screaming silently.
- 38:38Michael Kennedy: Yes.
- 38:38Michael Kennedy: I apologize.
- 38:39Michael Kennedy: I know, I know.
- 38:40Michael Kennedy: But I'm pretty sure it's just like a built-in Django cache functionality.
- 38:44Michael Kennedy: Exactly.
- 38:45Michael Kennedy: Yeah, okay.
- 38:46Michael Kennedy: It just routes into this thing.
- 38:47Vincent Warmerdam: Exactly.
- 38:48Vincent Warmerdam: So instead of configuring Redis, you just feed it this and you're good.
- 38:50Vincent Warmerdam: That's the idea.
- 38:51Michael Kennedy: Yes, exactly.
- 38:53Michael Kennedy: Exactly.
- 38:53Michael Kennedy: So there's more.
- 38:54Michael Kennedy: The next one, I have to rage against the machine.
- 38:57Michael Kennedy: I'm sure it's the way, but DQ, pronounced deck.
- 39:01Vincent Warmerdam: Yeah.
- 39:02Michael Kennedy: So I don't know.
- 39:03Michael Kennedy: For me, I still say DQ.
- 39:04Michael Kennedy: I don't say deck.
- 39:04Michael Kennedy: like it's spelled D-E-Q-U-E.
- 39:06Michael Kennedy: And I know a lot of computer science people just call that deck,
- 39:09Michael Kennedy: but this cache dot DQ or deck, however you want to use it.
- 39:13Michael Kennedy: It provides, there's,
- 39:14Michael Kennedy: there's a couple of higher order data structures that like operate on what we
- 39:18Michael Kennedy: talked about so far, but to give you data structure behavior, right?
- 39:21Michael Kennedy: Like what we talked about so far is sort of dictionary,
- 39:24Michael Kennedy: but not list or order or any of that.
- 39:26Michael Kennedy: But this, you can actually do go and add a thing.
- 39:31Michael Kennedy: How do we add one?
- 39:32Michael Kennedy: Anyway, you can say like pop left, pop left.
- 39:34Michael Kennedy: over and over to get, I guess you just add it.
- 39:37Vincent Warmerdam: It's kind of like a queue.
- 39:38Michael Kennedy: Yeah, like a queue, exactly.
- 39:40Michael Kennedy: With the goal of taking stuff out of it instead of into it.
- 39:42Michael Kennedy: But you don't normally think of a cache as doing that.
- 39:44Michael Kennedy: But it'd be a cool way actually to fan out work across processes.
- 39:48Vincent Warmerdam: I was about to say, that's a really good, I think,
- 39:51Vincent Warmerdam: I mean, there's people that have made, I forget the name,
- 39:55Vincent Warmerdam: the Python queuing system, Celery.
- 39:59Vincent Warmerdam: So that one is also built in such a way that you can say like,
- 40:02Vincent Warmerdam: oh, where do you have the list of jobs that still need doing?
- 40:04Vincent Warmerdam: And I think also Redis is used--
- 40:06Vincent Warmerdam: MARK MANDEL: Yeah, right, Redis and Qum.
- 40:07Michael Kennedy: Yeah, exactly.
- 40:08Michael Kennedy: Or Rabbit and Qum as well.
- 40:10Vincent Warmerdam: FRANCESC CAMPOY: Yeah, exactly.
- 40:11Vincent Warmerdam: But you can configure SQLite if you want to, though,
- 40:14Vincent Warmerdam: if I recall with those.
- 40:15Vincent Warmerdam: It's just that in this particular case,
- 40:16Vincent Warmerdam: if you don't want to use Celery, you can still kind of roll
- 40:19Vincent Warmerdam: your own by using this cache as well.
- 40:20Vincent Warmerdam: I'm assuming it uses the same pickle tricks,
- 40:22Vincent Warmerdam: so you can do general Python things
- 40:24Vincent Warmerdam: and if the process breaks for whatever reason,
- 40:26Vincent Warmerdam: you still have the jobs that need doing.
- 40:27Michael Kennedy: MARK MANDEL: Yeah, we're still going
- 40:29Michael Kennedy: to have to talk about this serialization thing,
- 40:31Michael Kennedy: These pickles.
- 40:33Michael Kennedy: Yes.
- 40:33Michael Kennedy: Not yet.
- 40:34Michael Kennedy: Let's go through this.
- 40:34Michael Kennedy: Let's go through this first.
- 40:35Michael Kennedy: Let's first.
- 40:36Michael Kennedy: Before we get distracted, because it's a deep.
- 40:40Michael Kennedy: So DEC, I guess we'll go DEC.
- 40:44Michael Kennedy: DEC provides an efficient and safe means for cross-thread, cross-process communication.
- 40:49Michael Kennedy: Like, you would never think you would get that out of a cache, really.
- 40:51Vincent Warmerdam: But it's...
- 40:52Michael Kennedy: You would do that in SQLite.
- 40:54Michael Kennedy: Yeah, exactly.
- 40:55Michael Kennedy: But you would do work to do that, right?
- 40:57Michael Kennedy: You would do like transactions.
- 40:58Michael Kennedy: And you would do sorting.
- 40:59Michael Kennedy: You would figure out, well, what if there's contention?
- 41:01Michael Kennedy: I mean, the fact that it's just kind of a pop, it's pretty nice.
- 41:04Vincent Warmerdam: Yeah, that's definitely nice.
- 41:05Michael Kennedy: No, that's definitely true.
- 41:06Vincent Warmerdam: Although one thing that makes it easy in this case, though, is again, it is all running in like one process.
- 41:11Vincent Warmerdam: So it's not like we've got SQLite running in one place and there's 16 Docker containers that can randomly interact with it.
- 41:16Vincent Warmerdam: No, that can, though.
- 41:17Vincent Warmerdam: Is that the case?
- 41:18Michael Kennedy: Because disk cache itself is already cross-process safe.
- 41:23Michael Kennedy: Like, that's why I was so excited about it.
- 41:25Vincent Warmerdam: But it has to be on the same machine, though.
- 41:27Vincent Warmerdam: Like, that's what I do think.
- 41:28Michael Kennedy: Yes, it's got to at least be accessible.
- 41:30Michael Kennedy: RushRite, that is true.
- 41:31Michael Kennedy: because technically there's nothing that says
- 41:33Michael Kennedy: you can't put the file anywhere.
- 41:35Michael Kennedy: I think there's mega performance issues
- 41:37Michael Kennedy: and locking issues on,
- 41:38Michael Kennedy: it says basically don't use it on network drives.
- 41:40Vincent Warmerdam: Yeah, so that's the thing.
- 41:42Vincent Warmerdam: Some of this is like, okay, you can do the locking.
- 41:44Vincent Warmerdam: You can do all those things.
- 41:45Vincent Warmerdam: You can do it well,
- 41:45Vincent Warmerdam: but the practicality of the network overhead
- 41:47Vincent Warmerdam: is something that usually causes a lot of confuffle,
- 41:50Michael Kennedy: at least in my experience.
- 41:51Michael Kennedy: Yeah.
- 41:52Michael Kennedy: Okay, another one is disk cache index,
- 41:54Michael Kennedy: which creates a mutable mapping and ordered dictionary.
- 41:57Michael Kennedy: So if you kind of really want to lay into the dictionary side,
- 42:00Vincent Warmerdam: Yeah, you can do that.
- 42:01Michael Kennedy: That one's transactions as well.
- 42:04Michael Kennedy: So you can actually, it has like sort of in-place updates
- 42:06Michael Kennedy: and other things you can do.
- 42:08Michael Kennedy: So you can say, I want to make sure that I'm going to get
- 42:11Michael Kennedy: two different things out of the cache.
- 42:13Michael Kennedy: And I want to make sure that they're not changed
- 42:15Michael Kennedy: while I'm doing that, right?
- 42:17Michael Kennedy: Just like you would with threading or something.
- 42:18Michael Kennedy: Yeah, so nothing can happen in between.
- 42:20Vincent Warmerdam: So they both have to come out at the same time.
- 42:22Vincent Warmerdam: So the two values that I get, they were, they were,
- 42:26Vincent Warmerdam: they both existed at the same time in the cache
- 42:27Vincent Warmerdam: at the point in time that I was retrieving it.
- 42:29Michael Kennedy: Yeah, yeah, exactly.
- 42:30Michael Kennedy: So just with cache.transact and you just go to town on it.
- 42:35Michael Kennedy: That's pretty straightforward, right?
- 42:36Michael Kennedy: Yep.
- 42:37Michael Kennedy: Are there any more in here?
- 42:38Michael Kennedy: There's a bunch of recipes for like barriers and throttling and probably semaphore-like stuff,
- 42:44Michael Kennedy: but I don't really want to talk about.
- 42:47Michael Kennedy: But you touched on these eviction policies.
- 42:48Michael Kennedy: Here's where I was looking for.
- 42:49Michael Kennedy: There's these different ones here that are kind of cool.
- 42:52Michael Kennedy: Whoops.
- 42:52Michael Kennedy: I didn't go away.
- 42:53Vincent Warmerdam: Yeah, so you can set a maximum to the cache.
- 42:57Vincent Warmerdam: I think you do that by number of items typically in it.
- 43:00Vincent Warmerdam: It could also be the case.
- 43:01Michael Kennedy: Size or something, yeah.
- 43:02Vincent Warmerdam: Yeah, or like total disk size,
- 43:04Vincent Warmerdam: maybe we should double check.
- 43:06Michael Kennedy: The default for the disk size is one gig.
- 43:08Michael Kennedy: Yeah, there you go.
- 43:09Michael Kennedy: So there's already a built-in one, yeah.
- 43:11Michael Kennedy: Which might catch people off guard.
- 43:14Michael Kennedy: Much of the stuff is cash, but not always.
- 43:15Michael Kennedy: I don't understand.
- 43:17Vincent Warmerdam: Yeah, you've got to be a little bit mindful of that,
- 43:18Vincent Warmerdam: I suppose.
- 43:19Vincent Warmerdam: But it's the same default,
- 43:21Vincent Warmerdam: not to have it go to infinity.
- 43:24Vincent Warmerdam: Agreed.
- 43:26Vincent Warmerdam: Yeah, I guess small screen on my side, but like, yeah, last recently.
- 43:30Michael Kennedy: I'll read them out.
- 43:31Michael Kennedy: I'll read them out for you.
- 43:31Michael Kennedy: Yeah.
- 43:31Michael Kennedy: So we got recent last recently stored as a default, every cache item
- 43:35Michael Kennedy: records the time it was stored in the cache and that adds an index to that
- 43:40Michael Kennedy: field.
- 43:40Michael Kennedy: So it's nice and fast, which is cool.
- 43:41Michael Kennedy: We have, this is, there's some other ones that are more nuanced, like least
- 43:46Michael Kennedy: recently used, not in terms of time, but we've got one that was accessed a hundred
- 43:51Michael Kennedy: times and one that was accessed two times.
- 43:53Michael Kennedy: Even if the one is accessed two times was just accessed,
- 43:55Michael Kennedy: that one's getting kicked out because it's not as useful.
- 43:57Michael Kennedy: I don't know.
- 43:58Michael Kennedy: That's a pretty neat feature.
- 43:59Michael Kennedy: And then the one people would expect is, I don't know,
- 44:02Michael Kennedy: maybe at least recently used.
- 44:04Michael Kennedy: How are you?
- 44:04Vincent Warmerdam: Yeah.
- 44:06Vincent Warmerdam: Yeah, exactly.
- 44:07Vincent Warmerdam: And there's also pruning mechanisms, if I'm not mistaken.
- 44:10Vincent Warmerdam: So there's all sorts of fun.
- 44:11Vincent Warmerdam: You can argue there are bells and whistles until you need them.
- 44:15Vincent Warmerdam: And one thing I have always found is every item that I see here,
- 44:19Vincent Warmerdam: you might not need it right now,
- 44:21Vincent Warmerdam: but for every item you see, you do plausibly go,
- 44:23Vincent Warmerdam: oh, but that might be useful later down the line somewhere.
- 44:25Vincent Warmerdam: Like the transaction thing where you retrieve two things at the same time.
- 44:29Vincent Warmerdam: I don't really have a use case for it, but I can imagine it one might,
- 44:33Vincent Warmerdam: where the consistency really matters.
- 44:35Michael Kennedy: Yeah, I can.
- 44:36Michael Kennedy: I could see using the fan out cache.
- 44:38Vincent Warmerdam: Yeah, definitely.
- 44:39Michael Kennedy: But probably not the transaction.
- 44:41Michael Kennedy: But I'm already talking to MongoDB, which doesn't have transactions effectively.
- 44:45Michael Kennedy: So not really.
- 44:47Michael Kennedy: What about performance?
- 44:48Michael Kennedy: Should we talk about your graphs?
- 44:50Michael Kennedy: You brought pictures.
- 44:51Vincent Warmerdam: Yes.
- 44:51Vincent Warmerdam: So that might be, so when you told me like, hey, let's do an episode on disk cache,
- 44:55Vincent Warmerdam: and I kind of told myself, okay, then I need to do some homework.
- 44:58Vincent Warmerdam: Like, I actually have to use it for something real.
- 44:59Vincent Warmerdam: It's a bit complex.
- 45:01Vincent Warmerdam: So what we're going to try and do is we're looking at a chart right now,
- 45:04Vincent Warmerdam: and I'm going to explain to Michael what it does,
- 45:06Vincent Warmerdam: and I'm going to try to explain it in such a way such that if you're not watching
- 45:09Vincent Warmerdam: but listening, that you're also going to be fairly interested in what you're seeing.
- 45:12Michael Kennedy: And I'll link to the chart, of course, people can.
- 45:15Vincent Warmerdam: Yeah, so this is all running on GitHub pages,
- 45:17Vincent Warmerdam: and the charts that you see here definitely needed a bit of disk cache
- 45:20Vincent Warmerdam: to make it less painful.
- 45:23Vincent Warmerdam: So what I've done is I've downloaded a Git repository.
- 45:26Vincent Warmerdam: What you're looking at right now
- 45:27Vincent Warmerdam: is the Git repository for Marimo.
- 45:29Vincent Warmerdam: And then I just take a point in time
- 45:30Vincent Warmerdam: and I say, okay, let's just see all the lines of code.
- 45:34Vincent Warmerdam: And then I take another point in time.
- 45:36Vincent Warmerdam: And then I basically just do kind of a Git blame
- 45:38Vincent Warmerdam: to see if the line got changed in between.
- 45:41Vincent Warmerdam: So what you're looking at here
- 45:42Vincent Warmerdam: is kind of a chart over time
- 45:44Vincent Warmerdam: where it's basically like a bar chart,
- 45:46Vincent Warmerdam: but it changes colors as time moves forward.
- 45:49Vincent Warmerdam: and the shape that you see is that things that happened early on,
- 45:53Vincent Warmerdam: well, there's a nice thick slab,
- 45:54Vincent Warmerdam: but it gets a little bit thinner and thinner as time moves forward
- 45:57Vincent Warmerdam: because some of those lines of code got replaced.
- 45:59Vincent Warmerdam: But in the case of Marimo, you can see that, you know,
- 46:02Vincent Warmerdam: most of the lines of code actually stay around for a long time
- 46:05Michael Kennedy: and it's kind of like a smooth sedimentary layer every time we move forward.
- 46:10Michael Kennedy: It's compressing a little over time.
- 46:12Michael Kennedy: Like there's the weight of the project has sort of compressed it.
- 46:15Michael Kennedy: So, yeah, it's pretty interesting.
- 46:17Michael Kennedy: So, okay, so that's pretty cool.
- 46:18Vincent Warmerdam: But you can also go to Django.
- 46:21Michael Kennedy: So there's a director on top.
- 46:22Michael Kennedy: MARK MANDEL: So if I go to Django.
- 46:24Michael Kennedy: Oh, you can put this cache in here.
- 46:26Michael Kennedy: This is really different.
- 46:27Michael Kennedy: FRANCESC CAMPOY: Yes.
- 46:28Vincent Warmerdam: MARK MANDEL: What is this telling us?
- 46:29Vincent Warmerdam: FRANCESC CAMPOY: So what you can see here
- 46:32Vincent Warmerdam: is that at some point in time, there's
- 46:33Vincent Warmerdam: a huge shift in the sediment.
- 46:35Vincent Warmerdam: There's a lot of light sand and a lot of the dark sand goes away.
- 46:38Vincent Warmerdam: There's also a button that allows you to show the version number.
- 46:41Vincent Warmerdam: So I've--
- 46:42Vincent Warmerdam: yep, there you go.
- 46:43Michael Kennedy: MARK MANDEL: There we go, yeah.
- 46:44Vincent Warmerdam: FRANCESC CAMPOY: So you can see that right before a new version,
- 46:46Vincent Warmerdam: a bunch of changes got introduced or right after.
- 46:49Vincent Warmerdam: It's usually around the version number
- 46:50Vincent Warmerdam: that you can see that shift.
- 46:51Michael Kennedy: Right, once the feature freeze is lifted,
- 46:55Michael Kennedy: some stuff comes in, PRs come in maybe or something.
- 46:58Michael Kennedy: Yes.
- 46:59Vincent Warmerdam: And one other thing that's actually kind of fun,
- 47:01Vincent Warmerdam: if you go--
- 47:02Vincent Warmerdam: there's this project called scikit-lego
- 47:04Vincent Warmerdam: that you can also go ahead and select.
- 47:05Vincent Warmerdam: And folks--
- 47:06Michael Kennedy: I've heard a pretty cool guy maintains that, yeah.
- 47:09Vincent Warmerdam: Well, so the funny thing is you can see that there's
- 47:11Vincent Warmerdam: a massive shift there at some point.
- 47:12Michael Kennedy: OK.
- 47:13Vincent Warmerdam: That's when we got the new maintainer.
- 47:16Michael Kennedy: Are you on the purple or the green side?
- 47:18Vincent Warmerdam: So there's this dark blue sediment that sort of goes down massively at that point.
- 47:22Vincent Warmerdam: But yeah, no, so but in this case, like the first thing he did is redid all the docs. So we went
- 47:27Vincent Warmerdam: from Sphinx to make docs. And that's like a huge, if you look at the lines of code that changed as
- 47:31Vincent Warmerdam: a result that, you know, that's quite a lot. But if we now start talking about how you make a chart
- 47:36Vincent Warmerdam: like this, you got to imagine like, I take the start of the GitHub history, I take the end of the
- 47:40Vincent Warmerdam: GitHub history, I sample like 100 points in between, and then for every line in every file,
- 47:46Vincent Warmerdam: I do a git blame.
- 47:49Michael Kennedy: MARK MANDEL: I think Django is something like 300,000 lines
- 47:52Michael Kennedy: of code.
- 47:52Michael Kennedy: I mean, that's a lot of--
- 47:54Michael Kennedy: A lot of--
- 47:54Vincent Warmerdam: MARK MANDEL: So that thing took two hours and 15 minutes
- 47:57Vincent Warmerdam: on my M4 Mac.
- 47:58Vincent Warmerdam: And if you go there, you can actually select it.
- 48:02Vincent Warmerdam: That one-- that was a chunky boy, is what I'll say.
- 48:06Vincent Warmerdam: MARK MANDEL: Yeah, 550,000 lines.
- 48:08Vincent Warmerdam: Yeah.
- 48:09Vincent Warmerdam: MARK MANDEL: There you go.
- 48:09Vincent Warmerdam: But you can see that there's one version change, I think,
- 48:12Vincent Warmerdam: where they made a bunch of changes.
- 48:13Vincent Warmerdam: And it could be that--
- 48:14Vincent Warmerdam: MARK MANDEL: Yeah.
- 48:15Vincent Warmerdam: That might have been, again, because I checked the docs
- 48:17Vincent Warmerdam: as well on Markdown files, it might have been a big docs
- 48:20Vincent Warmerdam: change.
- 48:20Vincent Warmerdam: But hopefully by just looking at this,
- 48:22Vincent Warmerdam: you can go like, oh yeah, this is probably a notebook somewhere.
- 48:25Vincent Warmerdam: And there's a huge for loop that does threading
- 48:29Vincent Warmerdam: and tries to do as much in parallel as possible.
- 48:31Vincent Warmerdam: And there's a progress bar.
- 48:33Vincent Warmerdam: Right?
- 48:34Vincent Warmerdam: Yeah.
- 48:35Vincent Warmerdam: And we don't--
- 48:37Michael Kennedy: Now I see why you had this problem with the caching.
- 48:39Vincent Warmerdam: That's right.
- 48:41Vincent Warmerdam: But yeah, but here's also where the threading came in.
- 48:43Vincent Warmerdam: Because the moment you say this point in time, now do all the files, do the git blame, that's definitely something that can happen in parallel.
- 48:49Vincent Warmerdam: But then for every file, for every point in time, you do want to have something in the cache that says, okay, if I have to restart this notebook for whatever reason, that number is just known.
- 48:58Vincent Warmerdam: Don't check it again.
- 48:59Michael Kennedy: Yeah.
- 48:59Michael Kennedy: Yeah, super interesting.
- 49:00Michael Kennedy: Okay.
- 49:01Michael Kennedy: I wonder if this 4.0 in 2022, is that might be when they switched to async?
- 49:08Michael Kennedy: They started supporting async.
- 49:10Michael Kennedy: It could be docs as well.
- 49:11Michael Kennedy: I'm not sure.
- 49:12Vincent Warmerdam: Well, yeah, so that's kind of the hard thing of some of these charts.
- 49:15Vincent Warmerdam: Like, I could expand these charts by saying things like,
- 49:18Vincent Warmerdam: okay, only the Python files, et cetera.
- 49:22Vincent Warmerdam: But, like, the way that this is hosted,
- 49:25Vincent Warmerdam: this is, like, really using the disk as a cache
- 49:27Vincent Warmerdam: because all these charts are Altair charts,
- 49:28Vincent Warmerdam: and you can save them to disk,
- 49:29Vincent Warmerdam: and then you can easily upload them to GitHub pages.
- 49:32Vincent Warmerdam: So I do everything in disk cache to make sure that if I,
- 49:35Vincent Warmerdam: for whatever reason, the notebook fails,
- 49:37Vincent Warmerdam: I don't have to sort of do anything fancy to get it back up.
- 49:41Vincent Warmerdam: But then once it's time to actually put it on the site,
- 49:44Vincent Warmerdam: I could use disk cache to show the charts,
- 49:46Vincent Warmerdam: but then I would need a server.
- 49:47Vincent Warmerdam: So actually using disk to actually just serve some files
- 49:51Vincent Warmerdam: is also just a pretty fine and good idea.
- 49:54Vincent Warmerdam: There are some things on the Marimo side
- 49:57Vincent Warmerdam: where we are also hoping to maybe give better caching tools
- 50:00Vincent Warmerdam: to the library itself.
- 50:02Vincent Warmerdam: It's just that when I was doing this,
- 50:03Vincent Warmerdam: I actually found a bug in our caching layer,
- 50:05Vincent Warmerdam: so then I switched back to disk cache.
- 50:07Michael Kennedy: You know what?
- 50:07Michael Kennedy: Look, that's valuable.
- 50:08Michael Kennedy: That's maybe not the way you will find, but it's valuable.
- 50:11Vincent Warmerdam: It's-- oh, so one thing you learn is that caching is actually
- 50:15Vincent Warmerdam: hard to get right.
- 50:16Michael Kennedy: Oh, it is.
- 50:17Vincent Warmerdam: It is.
- 50:18Michael Kennedy: Very hard.
- 50:18Vincent Warmerdam: It's on par with naming things.
- 50:22Michael Kennedy: It is one of the two things that goes wrong--
- 50:25Michael Kennedy: naming things, cache invalidation, and off by one errors.
- 50:28Vincent Warmerdam: Yes, exactly.
- 50:29Michael Kennedy: It's the middle one.
- 50:30Vincent Warmerdam: Yeah.
- 50:32Vincent Warmerdam: Dad jokes are amazing.
- 50:33Vincent Warmerdam: Anyway, so one thing about this repo, by the way,
- 50:38Vincent Warmerdam: this is all my--
- 50:38Vincent Warmerdam: we're going to add a link to the show notes.
- 50:40Vincent Warmerdam: There is a notebook, so if you feel like adding your own project
- 50:43Vincent Warmerdam: that you want to just add, feel free to spend your compute
- 50:46Vincent Warmerdam: resources two and a half hours to add a popular project.
- 50:49Vincent Warmerdam: I would love to have that.
- 50:50Vincent Warmerdam: One thing I think will be cool with these sorts of charts
- 50:52Vincent Warmerdam: is to see what will change when LLMs kind of get into the mix.
- 50:55Vincent Warmerdam: Do we see more code shifts happen if more LLMs get used
- 50:59Vincent Warmerdam: for these libraries?
- 50:59Michael Kennedy: MARK MANDEL: Very interesting.
- 51:01Vincent Warmerdam: I don't know if more code--
- 51:02Michael Kennedy: old code will get changed, but they are verbose code writers,
- 51:06Michael Kennedy: those things.
- 51:06Vincent Warmerdam: Yes.
- 51:07Vincent Warmerdam: So this, assuming we can do this over time
- 51:10Vincent Warmerdam: and we're going to start tracking this,
- 51:13Vincent Warmerdam: I'm calling this code archaeology.
- 51:15Vincent Warmerdam: I do think it will be an interesting chart.
- 51:18Vincent Warmerdam: As is, I think it's already quite interesting to see differences
- 51:20Vincent Warmerdam: between different projects.
- 51:21Vincent Warmerdam: I think if you go to sentence transformers,
- 51:23Vincent Warmerdam: you can also see when the project got moved from an academic lab
- 51:25Vincent Warmerdam: to hugging face.
- 51:26Vincent Warmerdam: So there are interesting things you can see with these charts.
- 51:30Vincent Warmerdam: But you are going through every file, every line,
- 51:33Michael Kennedy: get blame 100 times.
- 51:35Vincent Warmerdam: Yeah, a lot.
- 51:36Michael Kennedy: You got to do 100 times per line as well.
- 51:39Vincent Warmerdam: Well, so the start of the project with a Git repository,
- 51:42Vincent Warmerdam: and then to make a chart like this,
- 51:44Vincent Warmerdam: you got a sample over the entire timeline.
- 51:46Vincent Warmerdam: And it is a bit cheeky because sometimes you can go like,
- 51:48Vincent Warmerdam: OK, but there's a character that changed because of a linter.
- 51:51Vincent Warmerdam: And then is that really a change?
- 51:53Vincent Warmerdam: Does it really matter?
- 51:55Michael Kennedy: It's whoever decided to run the format on the thing or whatever.
- 52:00Vincent Warmerdam: We're looking at the Django chart.
- 52:01Vincent Warmerdam: It could also just be that black just got an update
- 52:03Vincent Warmerdam: or something like that, right?
- 52:03Vincent Warmerdam: Yeah, exactly.
- 52:04Vincent Warmerdam: It's also possible.
- 52:06Michael Kennedy: It's very possible.
- 52:07Vincent Warmerdam: It's unlikely, but it's not impossible, let me say.
- 52:09Vincent Warmerdam: But yeah, anyway.
- 52:11Vincent Warmerdam: Yeah, this was one of the benchmarks
- 52:13Vincent Warmerdam: that I did with disk cache that I thought was pretty amusing
- 52:16Vincent Warmerdam: and pretty interesting.
- 52:18Vincent Warmerdam: But there's this one other feature
- 52:19Vincent Warmerdam: that I think we should also talk about, which
- 52:21Vincent Warmerdam: is that if you want to, disk cache actually lets you
- 52:24Vincent Warmerdam: do the serialization yourself.
- 52:26Vincent Warmerdam: So normally, what it would do is it would say,
- 52:29Vincent Warmerdam: like, OK, let's do the pickle thing.
- 52:31Vincent Warmerdam: And it's a bit clever, right?
- 52:32Vincent Warmerdam: So if the thing you're storing is like an integer,
- 52:35Vincent Warmerdam: doesn't go through the whole pickle thing and just stores it as an integer there's these native types
- 52:38Michael Kennedy: that sqlite has and then you know it's able to do something clever but right as soon as it becomes
- 52:42Michael Kennedy: like a custom class or a list of weird things then yeah then then it's i personally don't like it
- 52:49Michael Kennedy: pickling i would i would prefer that it makes me do something it's i think it's weird well so the
- 52:53Vincent Warmerdam: thing is you can write your own disk class and then what you can do is you can pass set disk class
- 52:58Vincent Warmerdam: onto uh the disk cache itself and i'm just kind of wondering like when might it make sense to do
- 53:03Vincent Warmerdam: this sort of a thing and if you go to the docs there's actually like a really good example just
- 53:07Michael Kennedy: right there which is jason yeah i i lost your example if i'll get it back if you type disk
- 53:13Vincent Warmerdam: you'll find it so jason has this interesting thing the it is text if you think about it but
- 53:18Vincent Warmerdam: it's text that has a bit of structure and that means that there are these compression libraries
- 53:23Vincent Warmerdam: you can actually run on them and especially if you have like a pattern that repeats itself let's say
- 53:28Vincent Warmerdam: a list of users or something like that and there's always the user key and there's always maybe the
- 53:32Vincent Warmerdam: the email key and those things just repeat themselves all over the place,
- 53:36Vincent Warmerdam: then there is an opportunity to,
- 53:38Vincent Warmerdam: there's this library called zlib where you can just take that string,
- 53:41Vincent Warmerdam: you can compress it and then that compressed representation can go into disk cache instead.
- 53:46Vincent Warmerdam: Yeah, I figured that sounds like a lot of fun.
- 53:49Vincent Warmerdam: You can just grab the implementation there.
- 53:51Vincent Warmerdam: I have this notebooks repository where I have LLMs just write fun little notebooks.
- 53:55Vincent Warmerdam: I always check the results obviously just to be clear on that.
- 53:58Vincent Warmerdam: But one thing that was I think pretty cool to see,
- 54:00Vincent Warmerdam: if you just do the normal data type and you pickle it, then you get a certain size. And if you just
- 54:08Vincent Warmerdam: have a very short, normal Python dictionary basic thing, then it's negligible. You shouldn't use this
- 54:14Vincent Warmerdam: JSON trick. But the moment you get text heavy, there's just a lot of text that you're inputting
- 54:19Vincent Warmerdam: there and there's some repetition of characters. Or if you really do something that's highly
- 54:23Vincent Warmerdam: compressible, it is not unheard of to get like 80%, 90% savings
- 54:30Vincent Warmerdam: on your disk space, basically.
- 54:32Vincent Warmerdam: Now, there is a little bit of overhead
- 54:33Vincent Warmerdam: because you were doing the compression and decompression.
- 54:36Vincent Warmerdam: But if you're doing text-heavy stuff,
- 54:38Vincent Warmerdam: this is something that can actually save you a whole bunch.
- 54:40Vincent Warmerdam: And I can imagine for LLMs, this would also be a win.
- 54:43Michael Kennedy: OK.
- 54:43Michael Kennedy: So this JSON disk, not only does it serialize to and from JSON,
- 54:47Michael Kennedy: which I think is safer.
- 54:49Michael Kennedy: It can be a pain if you had date times.
- 54:51Vincent Warmerdam: You've got to do something about that.
- 54:52Vincent Warmerdam: or JSON or something like that.
- 54:53Vincent Warmerdam: You can.
- 54:54Michael Kennedy: Yeah, yeah, I think.
- 54:55Michael Kennedy: But then it's using Zlib here.
- 54:57Michael Kennedy: You know, I just actually did something like this
- 54:59Michael Kennedy: for just something in my database.
- 55:00Michael Kennedy: It had nothing to do with caching.
- 55:02Michael Kennedy: But these records are holding tons of text
- 55:06Michael Kennedy: for something sort of tangential to the podcast.
- 55:10Michael Kennedy: And I'm like, I don't really want to put 100K of text
- 55:13Michael Kennedy: that I'm not going to query against or search
- 55:15Michael Kennedy: into the database.
- 55:17Michael Kennedy: So I used Python's XZ implementation.
- 55:20Michael Kennedy: And like you said.
- 55:21Vincent Warmerdam: There's a bunch of compression algorithms you could use.
- 55:22Michael Kennedy: It was way fast.
- 55:23Michael Kennedy: So I just store it as bytes now, and it's like a tenth the size.
- 55:26Michael Kennedy: It's great.
- 55:26Michael Kennedy: So I guess this is the same, but for the cache back end, right?
- 55:29Vincent Warmerdam: Yeah.
- 55:30Vincent Warmerdam: And I think-- well, you can see, I think Zlib
- 55:32Vincent Warmerdam: is being used internally.
- 55:33Michael Kennedy: Yeah.
- 55:33Michael Kennedy: I mean, it's not XZ, but it's the same idea.
- 55:36Vincent Warmerdam: Yeah, exactly.
- 55:38Vincent Warmerdam: And there's always new compression algorithms.
- 55:40Vincent Warmerdam: Like, feel free to check whatever makes sense.
- 55:42Vincent Warmerdam: But the fact that you have one very cool example
- 55:45Vincent Warmerdam: to add on the docs, because you can just copy and paste it.
- 55:47Vincent Warmerdam: A lot of people benefit from it.
- 55:48Vincent Warmerdam: But why stop here?
- 55:50Michael Kennedy: Because this is the company you can do for JSON.
- 55:52Vincent Warmerdam: What else can you do?
- 55:53Michael Kennedy: Before we move on, though, if I were writing this,
- 55:55Michael Kennedy: I would recommend using microJSON or ORJSON
- 56:00Michael Kennedy: or whatever, some of the more high performance versions
- 56:03Michael Kennedy: right there.
- 56:03Vincent Warmerdam: Yeah, and I think ORJSON--
- 56:05Vincent Warmerdam: I mean, performance is cool.
- 56:07Vincent Warmerdam: The reason I use ORJSON a lot more
- 56:09Vincent Warmerdam: has to do with the types that it supports.
- 56:10Vincent Warmerdam: So it can accept the NumPy arrays, for example,
- 56:13Vincent Warmerdam: and just listifies it.
- 56:14Vincent Warmerdam: And I think it has a few things with dates as well.
- 56:17Vincent Warmerdam: It just has a slightly better support for a few things.
- 56:19Michael Kennedy: OK, good to know.
- 56:20Michael Kennedy: All right, where are we going?
- 56:21Michael Kennedy: What's next?
- 56:23Vincent Warmerdam: Numpy arrays.
- 56:24Vincent Warmerdam: OK.
- 56:25Vincent Warmerdam: So a lot of people like to do things with embeddings nowadays.
- 56:29Vincent Warmerdam: So like text thing goes in, some sort of array thing comes out.
- 56:31Vincent Warmerdam: And then hopefully, if two texts are similar,
- 56:33Vincent Warmerdam: then the arrays are also similar.
- 56:35Vincent Warmerdam: So you can do all sorts of fun little lookups.
- 56:37Vincent Warmerdam: And I do a fair share of doing things with embeddings.
- 56:40Vincent Warmerdam: And embeddings are also not notoriously expensive
- 56:43Vincent Warmerdam: to calculate, but still pretty expensive to calculate.
- 56:46Vincent Warmerdam: OK, but you can write your full Python thing in there.
- 56:48Vincent Warmerdam: So if you compare storing NumPy as bytes compared to that
- 56:54Vincent Warmerdam: to a pickle, it's actually even.
- 56:55Vincent Warmerdam: There's very little to gain there.
- 56:57Vincent Warmerdam: But one thing you could do is you could say, well,
- 57:01Vincent Warmerdam: let's maybe bring it down to float 16.
- 57:03Vincent Warmerdam: That's a thing you can do.
- 57:04Vincent Warmerdam: You can sort of say, before we save it,
- 57:05Vincent Warmerdam: we actually make it just a little bit less accurate
- 57:09Vincent Warmerdam: on the numeric part of it.
- 57:10Vincent Warmerdam: But that'll save us a whole bunch of disk space.
- 57:11Michael Kennedy: So that's already kind of old.
- 57:13Michael Kennedy: Well, you need it to be super precise
- 57:15Michael Kennedy: when it's involved in calculations.
- 57:17Michael Kennedy: But then in the end, if you're not going to report the numbers
- 57:20Michael Kennedy: to great decimal places, maybe going down is good, yeah.
- 57:23Vincent Warmerdam: Yeah, it depends on the use case.
- 57:25Vincent Warmerdam: But typically, you could argue maybe a 1% difference
- 57:28Vincent Warmerdam: in similarity if we have 100x savings on disk.
- 57:32Vincent Warmerdam: That'll be kind of a win.
- 57:34Vincent Warmerdam: So one thing I was sort of focusing on is just this--
- 57:37Vincent Warmerdam: you can do things like, OK, come up
- 57:40Vincent Warmerdam: with your own little weird data structure where you say,
- 57:42Vincent Warmerdam: OK, let's pretend we're going to quantize the whole thing.
- 57:45Vincent Warmerdam: So we're going to calculate the quantiles of the float values that it can take.
- 57:50Vincent Warmerdam: And we're going to take basically 256 buckets.
- 57:55Vincent Warmerdam: We're going to store the scale.
- 57:57Vincent Warmerdam: We're going to store the mean.
- 57:58Vincent Warmerdam: And then we're going to store in what bucket the number was in.
- 58:01Vincent Warmerdam: And you can turn that into a string representation.
- 58:03Vincent Warmerdam: These things are pretty fun to write.
- 58:05Vincent Warmerdam: Nice.
- 58:06Vincent Warmerdam: And yeah, and then you scroll down into your big notebook and then you find this.
- 58:12Vincent Warmerdam: There you go.
- 58:13Vincent Warmerdam: That's a retrieval time.
- 58:14Vincent Warmerdam: I think I got like a 4x improvement in terms of like disk space being saved.
- 58:20Vincent Warmerdam: It was like a 1% similarity score that I had to give up for doing things like this.
- 58:24Vincent Warmerdam: Mileage can vary, of course, but like, again, these are fun things to sort of start playing with
- 58:29Vincent Warmerdam: because you have access to the way that you write that down.
- 58:32Vincent Warmerdam: So that was also like a fun little exercise to do.
- 58:36Michael Kennedy: Yeah.
- 58:36Michael Kennedy: Could you save NumPy arrays by just putting up, just converting it to bytes?
- 58:41Michael Kennedy: There's probably some efficient.
- 58:42Michael Kennedy: You know what?
- 58:43Michael Kennedy: What about a parquet file?
- 58:45Michael Kennedy: Like in an in-memory Parquet file, then you just say,
- 58:47Michael Kennedy: here's the value in bytes.
- 58:50Vincent Warmerdam: So I tried the bytes thing and compared it to the pickle thing,
- 58:53Vincent Warmerdam: and that was basically the same size.
- 58:55Michael Kennedy: OK.
- 58:55Vincent Warmerdam: That barely led to anything.
- 58:57Vincent Warmerdam: About the Parquet one, I mean--
- 59:00Vincent Warmerdam: You do get compression.
- 59:01Vincent Warmerdam: Well, yeah, but I could be wrong on this one.
- 59:03Vincent Warmerdam: But I think Parquet is optimized to be a disk representation.
- 59:07Vincent Warmerdam: And then once you want to have it in memory,
- 59:09Vincent Warmerdam: it becomes an arrow representation.
- 59:11Vincent Warmerdam: I see.
- 59:11Vincent Warmerdam: Yeah, probably.
- 59:12Vincent Warmerdam: So in that sense, what I would do is, OK,
- 59:14Vincent Warmerdam: if you have something in Arrow, you use this cache
- 59:16Vincent Warmerdam: to make sure it's written as parquet.
- 59:18Vincent Warmerdam: But then you have to be-- you kind of have
- 59:20Michael Kennedy: to know what you're doing if you're going to make parquet
- 59:23Vincent Warmerdam: files.
- 59:24Vincent Warmerdam: And also, the benefit of a parquet file
- 59:25Vincent Warmerdam: is that you have one huge table.
- 59:27Vincent Warmerdam: Because then--
- 59:27Vincent Warmerdam: Right, you can scan it, yeah.
- 59:28Vincent Warmerdam: Yeah, it's a columnar format.
- 59:30Vincent Warmerdam: So then if I were a column, boy, would I
- 59:32Vincent Warmerdam: want to have all the rows in me.
- 59:35Vincent Warmerdam: So in that sense, what you--
- 59:38Vincent Warmerdam: yeah, so in that sense, what I would do instead
- 59:40Vincent Warmerdam: is if you, for whatever reason, you just have a lot of data,
- 59:43Vincent Warmerdam: It's still kind of a cache, but it makes more sense to store all of it in like a huge parquet file.
- 59:47Vincent Warmerdam: In parquet, you can store a partition.
- 59:49Vincent Warmerdam: So you can say this one column that's partitioned, a date would be like a very typical thing to partition on.
- 59:54Vincent Warmerdam: And then if you point polars to like parquet, but you say, I only want to have this date,
- 59:58Vincent Warmerdam: it can sort of do the forward scan and only pick the rows that you're interested in.
- 1:00:03Vincent Warmerdam: And I would imagine that that would beat anything we might do with this cache, especially if the table is big.
- 1:00:09Michael Kennedy: So you don't always want to cache stuff.
- 1:00:11Michael Kennedy: Like I said, I actually don't avoid hitting the Mongo database
- 1:00:15Michael Kennedy: a lot for my projects because it's like the response time
- 1:00:18Michael Kennedy: is quick enough and might as well.
- 1:00:20Michael Kennedy: I want to take two little avenues here.
- 1:00:22Michael Kennedy: But the first one is, what about DuckDB?
- 1:00:25Michael Kennedy: I know at least on the data science side and the analytics
- 1:00:28Michael Kennedy: side, DuckDB is really popular, really well respected,
- 1:00:31Michael Kennedy: really fast.
- 1:00:32Michael Kennedy: Maybe you don't even cache it.
- 1:00:33Michael Kennedy: Maybe you just use DuckDB as a thing.
- 1:00:36Vincent Warmerdam: How do you feel about that?
- 1:00:37Vincent Warmerdam: I mean, DuckDB does solve a very different problem
- 1:00:40Vincent Warmerdam: than SQLite or Postgres in a way.
- 1:00:42Vincent Warmerdam: So I don't believe-- to name one thing,
- 1:00:44Vincent Warmerdam: I believe DuckDB does assume that everything under the hood
- 1:00:47Vincent Warmerdam: is immutable.
- 1:00:49Vincent Warmerdam: So it will never be ACID compliant,
- 1:00:51Vincent Warmerdam: because it doesn't necessarily have to be.
- 1:00:52Vincent Warmerdam: You can still insert rows, if I'm not mistaken.
- 1:00:54Vincent Warmerdam: But the use case is just assumed
- 1:00:56Vincent Warmerdam: to be analytical in general.
- 1:00:58Vincent Warmerdam: That like--
- 1:00:58Vincent Warmerdam: I see.
- 1:00:59Vincent Warmerdam: --it's really designed to sort of fit that use case.
- 1:01:03Vincent Warmerdam: You can insert rows, though.
- 1:01:04Michael Kennedy: So like--
- 1:01:05Michael Kennedy: I mean, you might be caching data science things
- 1:01:07Michael Kennedy: that you're only computing once.
- 1:01:08Michael Kennedy: Like, for example, your charts.
- 1:01:11Michael Kennedy: Once you've computed that, it's not going to change because it's historical.
- 1:01:14Vincent Warmerdam: I mean, I might want to rerun it a month later or something like that.
- 1:01:16Vincent Warmerdam: That's something I might want to do.
- 1:01:18Vincent Warmerdam: And in that particular case, it would be cool if the sampling is the same
- 1:01:21Vincent Warmerdam: and I just want to add one sample at the end that all those samples I had before,
- 1:01:25Vincent Warmerdam: that those are in a cache somewhere.
- 1:01:26Michael Kennedy: Or maybe you want faster, better resolution.
- 1:01:29Michael Kennedy: Instead of going 100, you're going to go to 1,000 points,
- 1:01:31Michael Kennedy: but you could do 10% less because those are done, right?
- 1:01:34Vincent Warmerdam: So stuff like that.
- 1:01:35Vincent Warmerdam: But then what you would never do with a cache is do a group buy
- 1:01:37Vincent Warmerdam: and then a mean, for example.
- 1:01:39Vincent Warmerdam: It's like--
- 1:01:41Michael Kennedy: It's not-- it's outgrown its use at that point.
- 1:01:43Michael Kennedy: That's for sure.
- 1:01:45Vincent Warmerdam: Yeah.
- 1:01:45Vincent Warmerdam: And if it were a part of it, then the docs would say so.
- 1:01:47Vincent Warmerdam: But like-- no, so in my mind, DuckDB really just
- 1:01:51Vincent Warmerdam: solves a different problem, similar to like general SQLite
- 1:01:54Vincent Warmerdam: also solves a different problem than disk cache.
- 1:01:56Vincent Warmerdam: And also Postgres is also solving a slightly different problem.
- 1:01:59Michael Kennedy: Sure.
- 1:01:59Vincent Warmerdam: All right, fair.
- 1:02:00Michael Kennedy: Like the other angle--
- 1:02:01Michael Kennedy: Yeah?
- 1:02:02Michael Kennedy: No, go ahead.
- 1:02:02Michael Kennedy: Finish your thoughts.
- 1:02:03Vincent Warmerdam: Well, I also really love Postgres, I got to say.
- 1:02:06Vincent Warmerdam: Like the thing I really like about it
- 1:02:07Vincent Warmerdam: is that it is like boring, but in a good way, software
- 1:02:09Vincent Warmerdam: where like I have a Postgres thing running there
- 1:02:14Vincent Warmerdam: and whatever SSH thing I need, I can just swap the cloud
- 1:02:17Vincent Warmerdam: provider and it'll just go ahead and still
- 1:02:19Vincent Warmerdam: run without me having to move the data
- 1:02:20Vincent Warmerdam: or do any migration or anything like that.
- 1:02:22Vincent Warmerdam: That is also just like a nice feeling,
- 1:02:24Vincent Warmerdam: but it solves a different problem.
- 1:02:25Michael Kennedy: Yeah.
- 1:02:25Michael Kennedy: Yeah.
- 1:02:26Michael Kennedy: These would very likely be used together, not instead of--
- 1:02:29Michael Kennedy: I mean, Postgres can be used instead of disk cache,
- 1:02:31Michael Kennedy: but disk cache, definitely not instead of Postgres.
- 1:02:34Michael Kennedy: So the other one, the other angle I wanted to riff on, have you riff on just a little bit is
- 1:02:38Michael Kennedy: think people, especially people who are maybe new to this idea of caching, they can end up thinking,
- 1:02:43Michael Kennedy: okay, I'm going to store stuff. We talked a lot about like, I get something back from the database.
- 1:02:47Michael Kennedy: I could store that in a cache. So I don't have to query it again or whatever. And those are
- 1:02:51Michael Kennedy: certainly good use cases. But I think a lot of times an even better use case is if you're going
- 1:02:55Michael Kennedy: to get 20 rows back from a database, do some Python and construct them along with a little
- 1:03:00Michael Kennedy: other information into some object.
- 1:03:03Michael Kennedy: And then that's really what you want to work with.
- 1:03:04Michael Kennedy: Store that constructed thing in the cache.
- 1:03:07Michael Kennedy: You know what I mean?
- 1:03:08Michael Kennedy: Like, as far as you can go down the compute layer, like, don't just stop like, well, it
- 1:03:12Michael Kennedy: comes back from the database, so we cache it.
- 1:03:14Michael Kennedy: Like, if there's a way to say it, like, there's a bunch of work after that, think about how
- 1:03:16Michael Kennedy: you might cache at that level.
- 1:03:18Vincent Warmerdam: Okay, I'm going to pitch you a dream then.
- 1:03:21Vincent Warmerdam: Imagine you have a Python notebook and, oh, you're running a cell, you're running a cell,
- 1:03:26Vincent Warmerdam: you're running a cell, and halfway the kernel dies for whatever weird reason.
- 1:03:29Michael Kennedy: Right.
- 1:03:30Vincent Warmerdam: it'd be nice if I could just reboot the notebook and it would just pick it up again and move further.
- 1:03:33Vincent Warmerdam: Because again, I'm picking something out of a database and I'm doing something little with it
- 1:03:37Vincent Warmerdam: and processing, processing, processing. But wouldn't it be nice if maybe every cell had a caching mechanism?
- 1:03:43Vincent Warmerdam: If only you had some influence. If only we're a company that did this kind of stuff.
- 1:03:51Vincent Warmerdam: You can imagine these are things that we are thinking about. And again, what I'm about to
- 1:03:55Vincent Warmerdam: suggest is definitely a dream. This is not something that works right now. Don't pin me on this.
- 1:03:59Vincent Warmerdam: like we're thinking out loud here.
- 1:04:01Vincent Warmerdam: You can also imagine this being super useful
- 1:04:03Vincent Warmerdam: where an entire team can share the cache.
- 1:04:05Vincent Warmerdam: Yeah.
- 1:04:05Vincent Warmerdam: Right?
- 1:04:06Vincent Warmerdam: So if your colleague already calculated something,
- 1:04:08Vincent Warmerdam: you don't have to recalculate it again.
- 1:04:10Vincent Warmerdam: There's all sorts of use cases like that as well.
- 1:04:13Vincent Warmerdam: But there may be, there are these moments
- 1:04:15Vincent Warmerdam: when you want to have very tight manual control
- 1:04:17Vincent Warmerdam: over what goes into the cache.
- 1:04:19Vincent Warmerdam: That makes a lot of sense and it's great.
- 1:04:21Vincent Warmerdam: But there are also moments
- 1:04:22Vincent Warmerdam: when you just really don't want to think about it at all.
- 1:04:24Vincent Warmerdam: And you just want everything to be cached.
- 1:04:25Michael Kennedy: Could I just use this thing as a checkpoint?
- 1:04:28Michael Kennedy: I can autosave as my code runs.
- 1:04:30Michael Kennedy: Yeah, that's cool.
- 1:04:31Vincent Warmerdam: Yeah.
- 1:04:31Vincent Warmerdam: And again, doing this right is hard,
- 1:04:34Vincent Warmerdam: because there's all sorts of weird Python types.
- 1:04:36Vincent Warmerdam: And I mentioned the progress bar thing.
- 1:04:38Michael Kennedy: And there's all sorts of things that we've
- 1:04:40Vincent Warmerdam: got to be mindful of here.
- 1:04:42Vincent Warmerdam: But if you're thinking about really,
- 1:04:44Vincent Warmerdam: how would you use this in data science
- 1:04:46Vincent Warmerdam: when you fetch a little bit of data and deal with it,
- 1:04:48Vincent Warmerdam: to me, it is starting to feel more natural
- 1:04:50Vincent Warmerdam: than thinking about cells in a notebook,
- 1:04:52Vincent Warmerdam: maybe cache on that level.
- 1:04:53Michael Kennedy: Yeah, that's pretty interesting.
- 1:04:55Michael Kennedy: just sort of cascade them along as a hash of the hashes
- 1:05:00Michael Kennedy: of the prior cells or--
- 1:05:01Vincent Warmerdam: Well, and this is where things become tricky, of course,
- 1:05:04Vincent Warmerdam: because then, OK, I've got this one cell,
- 1:05:06Vincent Warmerdam: and I change one function in it.
- 1:05:07Vincent Warmerdam: Oh, if you're going to cache on the entire cell,
- 1:05:09Vincent Warmerdam: oh, everything has to rerun.
- 1:05:10Vincent Warmerdam: And then, oh, if you're not careful,
- 1:05:11Vincent Warmerdam: your cache is going to be huge.
- 1:05:13Vincent Warmerdam: So like, OK, how do you do this in a user-friendly way?
- 1:05:16Vincent Warmerdam: There's all sorts of--
- 1:05:17Vincent Warmerdam: it sounds easier than it is the one thing I want to say.
- 1:05:19Michael Kennedy: Yeah, well, I think you've got a better chance
- 1:05:21Michael Kennedy: with Marima than with Jupyter, at least,
- 1:05:23Michael Kennedy: because you have a dependency graph.
- 1:05:25Michael Kennedy: So you can at least say, if this one is invalid,
- 1:05:28Michael Kennedy: that means the following three are also invalid,
- 1:05:32Michael Kennedy: being sort of propagated there.
- 1:05:33Vincent Warmerdam: Totally.
- 1:05:34Vincent Warmerdam: But I try to focus a little bit more on the user experience
- 1:05:38Vincent Warmerdam: side of things.
- 1:05:38Vincent Warmerdam: And one thing I've really learned from the notebook
- 1:05:40Vincent Warmerdam: with the progress bar is just there
- 1:05:42Vincent Warmerdam: were moments when I felt like, oh, I just
- 1:05:44Vincent Warmerdam: want this entire thing to be automated.
- 1:05:46Vincent Warmerdam: Don't make me think about this.
- 1:05:47Vincent Warmerdam: And then there were moments where I thought, oh, it's really
- 1:05:49Vincent Warmerdam: nice to have tight manual control.
- 1:05:51Vincent Warmerdam: How do I provide you with both?
- 1:05:53Vincent Warmerdam: Yeah.
- 1:05:54Vincent Warmerdam: That's quite tricky.
- 1:05:55Vincent Warmerdam: But it is a dream, so that's something to keep in mind.
- 1:05:57Michael Kennedy: Yeah, maybe someday there'll be a turn on cash flow checkbox
- 1:06:01Michael Kennedy: something.
- 1:06:01Vincent Warmerdam: Yeah, or well, at least till then,
- 1:06:04Vincent Warmerdam: I do think having something that works on disk instead of memory
- 1:06:07Vincent Warmerdam: these days is also just a boon.
- 1:06:10Michael Kennedy: Right.
- 1:06:10Michael Kennedy: So this works in data science notebooks.
- 1:06:12Michael Kennedy: It works in web apps.
- 1:06:13Michael Kennedy: It works in little TUIs.
- 1:06:15Michael Kennedy: It doesn't care.
- 1:06:16Vincent Warmerdam: It works with LLMs.
- 1:06:19Vincent Warmerdam: And if you have a kind of--
- 1:06:20Vincent Warmerdam: actually, similar to your set of,
- 1:06:22Vincent Warmerdam: If you have multiple processes with your web app running on one VM,
- 1:06:25Vincent Warmerdam: if you have one big VM that you share with your colleagues,
- 1:06:27Vincent Warmerdam: you can also just share the cache, actually.
- 1:06:29Michael Kennedy: Yeah, that's true.
- 1:06:30Michael Kennedy: So that's the reason you can't just point the same file, yeah.
- 1:06:32Vincent Warmerdam: Yeah, and especially if you're doing like big experiments
- 1:06:34Vincent Warmerdam: like grid search results or stuff like that,
- 1:06:36Vincent Warmerdam: that you really don't want to recalculate the big compute thing.
- 1:06:39Vincent Warmerdam: That's actually not too unreasonable.
- 1:06:41Michael Kennedy: Yeah, that's cool.
- 1:06:42Michael Kennedy: Yeah, if you have a shared Jupyter server.
- 1:06:44Vincent Warmerdam: Yeah, and a bunch of universities have that, right?
- 1:06:47Michael Kennedy: Yeah, exactly.
- 1:06:48Michael Kennedy: I don't want to go down this path because we're basically out of time.
- 1:06:51Michael Kennedy: I respect your time.
- 1:06:52Michael Kennedy: However, I do think there's a whole interesting conversation to be had about like, how do you
- 1:06:57Michael Kennedy: choose the right key for your, what goes into the cache?
- 1:07:01Michael Kennedy: Because you can end up with staleness really easy if something changes.
- 1:07:05Michael Kennedy: But if you incorporate the right stuff, you might never run into stale data problems because,
- 1:07:11Michael Kennedy: you know, like for example, I talked about the YouTube ID.
- 1:07:15Michael Kennedy: Basically the cache key is something like episode, episode data, episode YouTube thing,
- 1:07:20Michael Kennedy: colon YouTube, colon the hash of the show notes, right?
- 1:07:25Michael Kennedy: Something like that, where there's no way
- 1:07:27Michael Kennedy: that the show notes are gonna change
- 1:07:28Michael Kennedy: and I'll get the old data because guess what?
- 1:07:32Michael Kennedy: It's constructed out of the source, right?
- 1:07:34Michael Kennedy: Things like that.
- 1:07:35Michael Kennedy: There's probably a lot, especially like in your notebook side.
- 1:07:39Michael Kennedy: There's a lot to consider there, I think.
- 1:07:41Vincent Warmerdam: - Yeah, I mean, so I remember this one thing
- 1:07:43Vincent Warmerdam: with the course where I still wanted it to be cached,
- 1:07:46Vincent Warmerdam: but I wanted to have like text goes in
- 1:07:48Vincent Warmerdam: And then five responses from the LLM go out.
- 1:07:51Vincent Warmerdam: And the way you solve that is you just add another key.
- 1:07:53Vincent Warmerdam: But you have to be mindful of the cache key.
- 1:07:55Vincent Warmerdam: And you can-- oh, and you can use tuples, by the way.
- 1:07:57Vincent Warmerdam: That's also something you can totally use as a cache key.
- 1:07:58Michael Kennedy: Right, right.
- 1:07:59Vincent Warmerdam: So that was easy to fix.
- 1:08:01Vincent Warmerdam: It's just that you have to be mindful.
- 1:08:02Michael Kennedy: Yeah, that's kind of-- I want to give a quick shout out to that.
- 1:08:05Michael Kennedy: I want to leave--
- 1:08:06Michael Kennedy: I don't want to leave on a sour note.
- 1:08:08Michael Kennedy: But I think it's necessary to give this shout out,
- 1:08:11Michael Kennedy: or this like call this out, rather, is the way I should say it,
- 1:08:14Michael Kennedy: is I think this project is awesome.
- 1:08:15Michael Kennedy: You think it's awesome.
- 1:08:17Michael Kennedy: Honestly, I think it doesn't need really very much.
- 1:08:19Michael Kennedy: But if you look at last updated,
- 1:08:22Michael Kennedy: if you look at the last updated date,
- 1:08:24Michael Kennedy: it's really, it hasn't got a lot of attention
- 1:08:27Michael Kennedy: in the last six months or something like that.
- 1:08:29Vincent Warmerdam: Yeah, and if I look at PyPI,
- 1:08:31Vincent Warmerdam: the last release was 2023,
- 1:08:34Vincent Warmerdam: which, yeah, a year and a half ago.
- 1:08:36Michael Kennedy: Yeah, and it's okay to,
- 1:08:37Michael Kennedy: I would like to say that it's okay for things to be done.
- 1:08:40Michael Kennedy: It doesn't have to, things don't have to change,
- 1:08:43Michael Kennedy: but there's also a decent amount of,
- 1:08:45Michael Kennedy: like conversations on the issues and they haven't, you know,
- 1:08:48Michael Kennedy: like a couple of days ago, actually someone asked about this, but you know,
- 1:08:52Michael Kennedy: the last change, I believe the guy Grant who works on it,
- 1:08:57Michael Kennedy: started working at OpenAI about the time changes stopped going on.
- 1:09:02Michael Kennedy: I'm not entirely sure. I feel like I could be confused with another project.
- 1:09:06Michael Kennedy: So Grant, if that's not true, I apologize, but I think that it is.
- 1:09:11Michael Kennedy: Pretty sure that is. Do we have LinkedIn?
- 1:09:13Vincent Warmerdam: I mean, I am comfortable stating is--
- 1:09:16Vincent Warmerdam: let me put it this way.
- 1:09:18Vincent Warmerdam: Yes, my colleague might make a different caching mechanism.
- 1:09:21Vincent Warmerdam: And yes, I might use that at some point.
- 1:09:22Vincent Warmerdam: But at least for where I'm at right now,
- 1:09:25Vincent Warmerdam: this cache needs to break vividly in front of my face
- 1:09:28Vincent Warmerdam: for me to consider not using it.
- 1:09:30Vincent Warmerdam: Because it does feel like it's done in a really good way.
- 1:09:33Vincent Warmerdam: The main thing that needs to happen, I think,
- 1:09:35Vincent Warmerdam: functionally to make sure this doesn't get deprecated too
- 1:09:38Vincent Warmerdam: badly is just you got to update the Python version.
- 1:09:40Vincent Warmerdam: When a new Python version comes out,
- 1:09:42Vincent Warmerdam: you got to update PyPI to confirm,
- 1:09:43Vincent Warmerdam: like, OK, we do support this Python version.
- 1:09:45Vincent Warmerdam: But I mean, most of the--
- 1:09:48Vincent Warmerdam: if you look at the area that needs to be covered,
- 1:09:49Vincent Warmerdam: a lot of that has been covered by SQLite.
- 1:09:51Vincent Warmerdam: And that thing is definitely still being maintained.
- 1:09:54Michael Kennedy: It's getting mega maintained.
- 1:09:56Michael Kennedy: That's right.
- 1:09:57Michael Kennedy: So I also don't see the problem.
- 1:09:59Michael Kennedy: I'm not going to not use it.
- 1:10:01Michael Kennedy: I just want to put it out there on the radar for people
- 1:10:04Michael Kennedy: for whom my co-look is to go, oh, Michael and Vincent
- 1:10:06Michael Kennedy: were so psyched, and I started to use this.
- 1:10:08Michael Kennedy: And now I'm really disappointed because of whatever.
- 1:10:10Michael Kennedy: I saw this.
- 1:10:11Vincent Warmerdam: I mean, the only real doom scenario I can come up with
- 1:10:14Vincent Warmerdam: is if SQLite made like a breaking change.
- 1:10:16Vincent Warmerdam: That's the only thing I can kind of come up with.
- 1:10:18Michael Kennedy: But the odds of that seem very low.
- 1:10:20Michael Kennedy: Yeah, and it's on GitHub.
- 1:10:21Michael Kennedy: You can fork it.
- 1:10:22Michael Kennedy: I fork it.
- 1:10:22Michael Kennedy: Exactly, exactly.
- 1:10:23Michael Kennedy: So, no, I definitely am still super excited about it.
- 1:10:27Michael Kennedy: I just want to make sure that we put that out there.
- 1:10:29Michael Kennedy: I'd intended to talk about it sooner in the conversation,
- 1:10:32Michael Kennedy: but you know what?
- 1:10:33Michael Kennedy: We were just so excited.
- 1:10:34Vincent Warmerdam: Yeah, no.
- 1:10:36Vincent Warmerdam: This is definitely in my top five favorite Python libraries
- 1:10:38Vincent Warmerdam: outside of the standard lib.
- 1:10:40Vincent Warmerdam: Awesome.
- 1:10:40Michael Kennedy: Yeah, I've really, really have gotten awesome results out of it as well.
- 1:10:43Michael Kennedy: So remember the way we opened the show and you talked about this,
- 1:10:45Michael Kennedy: like when we talked about the LLM building block stuff
- 1:10:48Michael Kennedy: on the previous time you were on the show,
- 1:10:50Michael Kennedy: it was like, oh, we better not go too deep on this,
- 1:10:53Michael Kennedy: even though we're both so excited because it's going to derail the show.
- 1:10:56Michael Kennedy: We're now one hour and 15 minutes into it.
- 1:10:58Michael Kennedy: We kind of cut ourselves off.
- 1:11:00Michael Kennedy: I think that was accurate.
- 1:11:02Vincent Warmerdam: Yeah, I mean, you get two dads making dad jokes
- 1:11:05Vincent Warmerdam: and riffing on tools they both like.
- 1:11:06Vincent Warmerdam: It's bound to exceed a barbecue.
- 1:11:10Michael Kennedy: Yes, I know.
- 1:11:11Michael Kennedy: I wonder what would happen if sometime we just removed the time limit,
- 1:11:15Michael Kennedy: just got real comfortable and just riffed on something.
- 1:11:17Michael Kennedy: It could be hours.
- 1:11:18Michael Kennedy: It would be fun.
- 1:11:18Michael Kennedy: But maybe not today.
- 1:11:20Michael Kennedy: Two-hour live streams exist, Michael.
- 1:11:22Michael Kennedy: I know.
- 1:11:24Michael Kennedy: I've listened to some podcasts at over three hours.
- 1:11:26Michael Kennedy: I'm like, how is this still going?
- 1:11:27Michael Kennedy: But you know what?
- 1:11:28Michael Kennedy: Yeah.
- 1:11:28Michael Kennedy: It's all good.
- 1:11:29Vincent Warmerdam: But yeah, this is a good point in time.
- 1:11:32Michael Kennedy: We're both excited.
- 1:11:33Michael Kennedy: That's the summary.
- 1:11:34Michael Kennedy: That is the summary.
- 1:11:35Michael Kennedy: And I think I'm going to let you have the final word on this topic here.
- 1:11:39Michael Kennedy: like maybe speak to people just about caching in general
- 1:11:42Michael Kennedy: and disk cache in particular as we close it out?
- 1:11:45Vincent Warmerdam: I mean, I guess the main thing that I learned
- 1:11:48Vincent Warmerdam: with the whole caching thing in the last couple of years,
- 1:11:49Vincent Warmerdam: I always thought it was kind of like a web thing.
- 1:11:52Vincent Warmerdam: Like, oh, you know, front page of Reddit,
- 1:11:54Vincent Warmerdam: that thing has to be cached.
- 1:11:55Vincent Warmerdam: That's the way you think about it.
- 1:11:55Vincent Warmerdam: Yeah, of course.
- 1:11:56Vincent Warmerdam: And thinking about it too much that way
- 1:11:58Vincent Warmerdam: totally blocked me from considering, like, oh,
- 1:12:00Vincent Warmerdam: but if you do stuff in notebooks and data science land,
- 1:12:02Vincent Warmerdam: then you need this as well.
- 1:12:03Vincent Warmerdam: And I think there's actually a little emerging discovery
- 1:12:07Vincent Warmerdam: phenomenon happening where people that do things
- 1:12:09Vincent Warmerdam: LLM's at some point go like, oh, I need a cache.
- 1:12:11Vincent Warmerdam: And then, oh.
- 1:12:15Vincent Warmerdam: So that's the main thing I suppose I want to say.
- 1:12:16Vincent Warmerdam: Like, even if you're doing more data stuff,
- 1:12:18Vincent Warmerdam: like give this disk cache thing a try.
- 1:12:19Vincent Warmerdam: It's just good.
- 1:12:20Michael Kennedy: Yeah, it's so easy to adopt and try out.
- 1:12:23Michael Kennedy: Like, you can throw it in there.
- 1:12:24Michael Kennedy: Just add a decorator.
- 1:12:26Michael Kennedy: Exactly, see what you get.
- 1:12:27Michael Kennedy: See what you get.
- 1:12:28Michael Kennedy: All right, Vincent, welcome.
- 1:12:30Michael Kennedy: Oh, thank you for coming back.
- 1:12:31Michael Kennedy: I really appreciate it.
- 1:12:32Michael Kennedy: Thanks for having me.
- 1:12:33Michael Kennedy: Always good to talk to you.
- 1:12:34Michael Kennedy: Yeah.
- 1:12:34Vincent Warmerdam: And yeah, see you next time when we, again,
- 1:12:36Vincent Warmerdam: find out there's a cool Python library.
- 1:12:38Michael Kennedy: Yeah, that's going to be the three-hour episode.
- 1:12:41Michael Kennedy: Watch out, y'all.
- 1:12:43Michael Kennedy: Have a good one.
- 1:12:44Michael Kennedy: Later.
- 1:12:45Michael Kennedy: This has been another episode of Talk Python To Me.
- 1:12:48Michael Kennedy: Thank you to our sponsors.
- 1:12:49Michael Kennedy: Be sure to check out what they're offering.
- 1:12:50Michael Kennedy: It really helps support the show.
- 1:12:52Michael Kennedy: If you or your team needs to learn Python,
- 1:12:54Michael Kennedy: we have over 270 hours of beginner and advanced courses
- 1:12:58Michael Kennedy: on topics ranging from complete beginners to async code,
- 1:13:02Michael Kennedy: Flask, Django, HTMX, and even LLMs.
- 1:13:05Michael Kennedy: Best of all, there's no subscription in sight.
- 1:13:08Michael Kennedy: browse the catalog at talkpython.fm.
- 1:13:10Michael Kennedy: And if you're not already subscribed to the show
- 1:13:12Michael Kennedy: on your favorite podcast player,
- 1:13:14Michael Kennedy: what are you waiting for?
- 1:13:15Michael Kennedy: Just search for Python in your podcast player.
- 1:13:17Michael Kennedy: We should be right at the top.
- 1:13:19Michael Kennedy: If you enjoyed that geeky rap song,
- 1:13:20Michael Kennedy: you can download the full track.
- 1:13:22Michael Kennedy: The link is actually in your podcast blur show notes.
- 1:13:24Michael Kennedy: This is your host, Michael Kennedy.
- 1:13:26Michael Kennedy: Thank you so much for listening.
- 1:13:27Michael Kennedy: I really appreciate it.
- 1:13:28Michael Kennedy: I'll see you next time.
- 1:13:41I'm out.