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
0:03 allows us to do incredible easily.
0:05 This picture looks much, much better.
0:08 So here we have three requests coming in.
0:10 Look at this picture now. So Request 1 is gonna come in and guess what
0:15 it's gonna take as long as it takes to compute that.
0:17 And then we'll get a response back halfway through.
0:20 Remember, what we were doing, maybe a third of the way through in Request 1
0:23 is we were waiting on the database.
0:26 Well, anytime we're waiting, if we're using async and await, we're allowed to just
0:30 completely go do other work during that time with almost zero overhead
0:35 in fact. When this request comes in,
0:36 we can start on it right away,
0:38 and we pretty much do a couple database queries and just wait.
0:41 So now we have Request 1 and Request 2 in flight,
0:43 but they're both just awaiting a database response. Request 3 comes in and boom,
0:48 we can get the answer right back to them.
0:50 How much better experience is this for the user?
0:53 Yes, it still takes the same amount of time to that database period while we're
0:57 waiting for say, Request 1 or Request 2 or even Request 3.
1:01 There's no speed up of waiting on that database.
1:04 Our initial request is the same speed.
1:06 But as the traffic builds, it doesn't slow down the site because most the time
1:10 we're waiting, we can just keep on doing other stuff, serving Request 3 while we're waiting
1:15 on 1 and 2, for example.
1:16 It's beautiful and we're gonna see how to do this with FastAPI
1:19 and SQLAlchemy
1:21 in this chapter. If we zoom in, again, where we had our framework and our
1:25 database and then our code, all this stuff that's green,
1:28 we can now go process other request because we're just waiting on the external database to
1:33 get back to us. Really,
1:35 really nice with just simply converting those queries from a regular query to an async query we're good to go.