Full Web Apps with FastAPI Transcripts
Chapter: async databases with SQLAlchemy
Lecture: Async for scalability - async version

Login or purchase this course to watch this video and the rest of the course contents.
0:00 If we live in an asynchronous world like FastAPI allows us to do incredible easily. This picture looks much, much better.
0:09 So here we have three requests coming in. Look at this picture now. So Request 1 is gonna come in and guess what
0:16 it's gonna take as long as it takes to compute that. And then we'll get a response back halfway through.
0:21 Remember, what we were doing, maybe a third of the way through in Request 1 is we were waiting on the database.
0:27 Well, anytime we're waiting, if we're using async and await, we're allowed to just
0:31 completely go do other work during that time with almost zero overhead in fact. When this request comes in, we can start on it right away,
0:39 and we pretty much do a couple database queries and just wait. So now we have Request 1 and Request 2 in flight,
0:44 but they're both just awaiting a database response. Request 3 comes in and boom, we can get the answer right back to them.
0:51 How much better experience is this for the user? Yes, it still takes the same amount of time to that database period while we're
0:58 waiting for say, Request 1 or Request 2 or even Request 3. There's no speed up of waiting on that database. Our initial request is the same speed.
1:07 But as the traffic builds, it doesn't slow down the site because most the time
1:11 we're waiting, we can just keep on doing other stuff, serving Request 3 while we're waiting on 1 and 2, for example.
1:17 It's beautiful and we're gonna see how to do this with FastAPI and SQLAlchemy in this chapter. If we zoom in, again, where we had our framework and our
1:26 database and then our code, all this stuff that's green, we can now go process other request because we're just waiting on the external database to
1:34 get back to us. Really, really nice with just simply converting those queries from a regular query to an async query we're good to go.

Talk Python's Mastodon Michael Kennedy's Mastodon