Database Synchronous vs Asynchronous IO
Key Takeaways
The video discusses the differences between synchronous and asynchronous IO, with a focus on database operations and file systems, covering concepts such as blocking calls, kernel system calls, and IO_uring.
Full Transcript
Why is it slow? Well, it's blocking. It's not allowing me to do other things that I might need to do as a process. I can be doing tons of CPU intensive work, and you just blocked me? How dare you? That is rude. The oldest trick of the book to do asynchronous IO is to offload it to a thread or another process. So, we're still doing synchronous IO, but someone else is blocked, not me. I'm free. I'm free. As a main process, I'm free. I'm just going to spin a process, and I'm going to ask that process to actually do the IO for me. We need to start with the idea of synchronicity versus asynchronicity. I talked about this many times in this channel and my podcast. Essentially, to be synchronous is Think of it as a client, you're calling some sort of an API, some sort of a back end, some sort of a server. Doesn't matter. If your call blocks the execution of the rest of the instructions in your program, that is a synchronous call. We have called this API synchronous because it did not allow you to move to the next instruction unless you get an answer, okay? So, you're blocked. And when you're blocked, uh you don't get to execute the rest of your instructions. Asynchronous, on the other hand, is you run, and you delegate that job to some other party, often, and then you move on. And then, sometime later, you either check if this is ready, or you get a callback, like with Node.js, how Node.js does it, that hey, your result, remember this thing you asked about a few milliseconds ago? It's ready. You can You can get the results here. So, that's the gist of synchronous versus asynchronous execution, okay? And And we can dive into details just just by that. But, we want to kind of move that into the IO concept. So, what does it mean to do asynchronous IO? We're kind of uh giving some concrete examples here. We're doing an IO. Uh that means it's either a read or a write. In this context, since we're talking about Postgres, we're not talking about network, which is its own beast. We're talking about files, right? We're reading and writing to disk, right? And we're dealing with system calls, that is the kernel system calls, that allows us to read from files because a database is just a bunch of files, believe it or not. Yeah. Shocking, right? The data The The kernel provides you with a beautiful system calls that allows you to do IO. Often, all of these methods are synchronous. That means read, for example, is a system call that allows you, given a file descriptor that you have opened for a file on disk, I want to read that file, and I want to put whatever I read, this many bytes, in this pointer, copy the data, and move it to this memory location where it lives in my user space. That's a read call, and that is a blocking call. It's a blocking call. If you call that, your process will switch from user mode to kernel mode because you called the system call, and then your That's it. You cannot move to the next instruction. You cannot. Your process is blocked because it's synchronous, right? And until the kernel take that read, talks to the IO controller, NVMe, or whatnot, does the actual block read, uh uh gets the result. We're talking here about buffered IO, so we're writing into the page cache, to the kernel page cache, and then gets that Get a copy of the of that whatever we read into the page cache, and then we get you now from the page cache to your memory location, and that is a CPU cost. You're copying data from the page cache into your specified buffer, okay? So, that's a read. Write I is identical, right? It just works the other way, right? But, it's synchronous, right? So, I'm blocked when I do that. What's wrong with that? Well, if it's wrong If you're doing a read, you get something, and you read and read and read and think of it You implement a full table scan. You read, get something, process it. Read, block, get something, process it. As it's As opposed to read read read read read, execute, and then let me worry about that later. So, yeah, think about it. Why is it slow? Well, it's blocking. It's not allowing me to do other things that I might need to do as a process. Like, I can have I can be doing tons of CPU intensive work, and you just blocked me? How dare you? That is rude, right? So, synchronous IO does this. Blocking. Asynchronous IO allows you to uh unblock yourself. Well, the the oldest trick of the book to do asynchronous IO is to offload it to a thread or another process. So, we're still doing synchronous IO, but someone else is blocked, not me. I'm free. I'm free. As a main process, I'm free. I'm just going to spin a process, and I'm going to ask that process to actually do the IO for me. Guess who's doing that? Node.js. Yeah, for the longest time, Node.js exclusively worked on that mode for files, for files. Don't yell at me. For files. Network is a different beast. I talk about all of this, by the way, in my Node.js course, if you're you're interested to know the details and the inners the inner the inner workings of this. Go hit to node.win. win, w i n. It will redirect you. Get you a coupon. Worker thread. Beautiful design. Nothing wrong with that. Okay? So, So, let do this asynchronous IO, but let someone else get blocked. So, we're still getting blocked, right? And Node.js, by the way, uses a library called libuv to implement that piece, right? Uh in recent releases, they switched to IO_uring, which is what we're going to about to talk to next, right? But, essentially, the worker pool is a very nice idea to do asynchronous IO. It's just like let someone else be blocked. It's a very simple design, yet we still give the feel of asynchronicity. Okay? So, that's That's asynchronous IO. Now, asynchronous IO, again, I'm talking about file IO here, nothing about network. Different beast, again. Here, asynchronous IO, there is a new interface. I say new, it's been there for years now. It's called IO_uring, which is a shared memory between the kernel and the user space, where the user can submit a job, and the kernel can pick that job, actually does the read, and then writes back the results into the um in into the shared memory space, and then the kernel the the the user can check if the result is ready. So, it's a pure asynchronous model. If you really think about it, someone else is doing the blocking read, and that's the kernel, right? We're just offloaded the game. That's the whole the game, by the way. We just tell someone else to do it. It's just we present it in a in a pretty way, okay? So, everything, at the end of the day, is is synchronous. There's no escape. Someone else has to pay the price, right? Okay. So, that's IO_uring. Of course, I think Node.js supports it, right? I keep referencing Node.js because all Node and all these framework and Postgres, they're all software. They all need to read from disk and do network, and they're all kind of a back end infrastructure, which is the specialty of this channel of this channel. So, synchronous IO, asynchronous IO, briefly.
Original Description
Watch full show here https://youtu.be/yGjGc21mmoU
Fundamentals of Operating Systems Course
https://oscourse.win
NodeJS Internals and Architecture
https://node.win
Backend Troubleshooting Course
https://performance.husseinnasser.com
Fundamentals of Backend Engineering
https://backend.win
Fundamentals of Networking for Effective Backends udemy course (link redirects to udemy with coupon)
https://network.husseinnasser.com
Fundamentals of Database Engineering udemy course (link redirects to udemy with coupon)
https://database.husseinnasser.com
Follow me on Medium
https://medium.com/@hnasr/membership
Introduction to NGINX (link redirects to udemy with coupon)
https://nginx.husseinnasser.com
Python on the Backend (link redirects to udemy with coupon)
https://python.husseinnasser.com
Become a Member on YouTube
https://www.youtube.com/channel/UC_ML5xP23TOWKUcc-oAE_Eg/join
Buy me a coffee if you liked this
https://www.buymeacoffee.com/hnasr
Arabic Software Engineering Channel
https://www.youtube.com/channel/UChWZsjdoRvZ0T9QWZOD6UpA
🔥 Members Only Content
https://www.youtube.com/playlist?list=UUMO_ML5xP23TOWKUcc-oAE_Eg
🏭 Backend Engineering Videos in Order
https://backend.husseinnasser.com
💾 Database Engineering Videos
https://www.youtube.com/playlist?list=PLQnljOFTspQXjD0HOzN7P2tgzu7scWpl2
🎙️Listen to the Backend Engineering Podcast
https://husseinnasser.com/podcast
Gears and tools used on the Channel (affiliates)
🖼️ Slides and Thumbnail Design
Canva
https://partner.canva.com/c/2766475/647168/10068
Stay Awesome,
Hussein
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Hussein Nasser · Hussein Nasser · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Extending ArcObjects (IGeometry) - 01 - Getting Started
Hussein Nasser
Extending ArcObjects (IGeometry) - 02 - The Document, The Map and The Layers
Hussein Nasser
Channel Update - New Book, New Job, New Videos
Hussein Nasser
Learn Programming with VB.NET - 01 - Getting Started
Hussein Nasser
Learn Programming with VB.NET - 02 - Classes and Objects (Part 1)
Hussein Nasser
Learn Programming with VB.NET - 03 - Classes and Objects (Part 2)
Hussein Nasser
Learn Programming with VB.NET - 04 - User Interface
Hussein Nasser
Learn Programming with VB.NET - 05 - By Value v. By Reference
Hussein Nasser
Learn Programming with VB.NET - 06 - Variable size, 32 bit vs 64 bit
Hussein Nasser
Learn Programming with VB.NET - 07 - Conditional Statements
Hussein Nasser
Learn Programming with VB.NET - 08 - Inheritance
Hussein Nasser
Learn Programming with VB.NET - 09 - Strategy Design Pattern
Hussein Nasser
Learn Programming with VB.NET - 10 - How did I learn programming
Hussein Nasser
IGeometry 2016 Retrospective - Channel Update
Hussein Nasser
Javascript by Example - The Vook
Hussein Nasser
Vlog - Keep your servers close and your database closer
Hussein Nasser
Vlog - Client/Server Programming Languages
Hussein Nasser
Javascript By Example L1E01 - Getting Started
Hussein Nasser
Persistent Connections (Pros and Cons)
Hussein Nasser
Javascript By Example L1E02 - Building the Calculator Interface
Hussein Nasser
Happy new Year from IGeometry!
Hussein Nasser
Synchronous v. Asynchronous
Hussein Nasser
Javascript By Example L1E03 - Displaying the Digits on Calculator Screen
Hussein Nasser
Show Your Work. Blog, Vlog, Write, Create and Develop!
Hussein Nasser
Relational Database Atomicity Explained By Example
Hussein Nasser
Javascript By Example L1E04 - Operators, All Clear with Arrow Functions
Hussein Nasser
What Comes First, User Experience or Software Architecture?
Hussein Nasser
Javascript By Example L1E05 - Evaluate the Calculator Expressions with eval
Hussein Nasser
Fastest Way to Learn Programming Language or Technology
Hussein Nasser
Javascript By Example L1E06 - Fix Leading Zero Bug with Conditions
Hussein Nasser
Stateful vs Stateless Applications (Explained by Example)
Hussein Nasser
Javascript By Example L1E07 - Running our Calculator on the Mobile Phone
Hussein Nasser
Advice for New Software Engineers and Developers
Hussein Nasser
Why JSON is so Popular?
Hussein Nasser
Building Scalable Software - SLA, HS, VS
Hussein Nasser
Vlog (Istanbul) - Datacenter Proximity
Hussein Nasser
Should Software Engineers Learn Bleeding-Edge Technologies?
Hussein Nasser
Do Developers Build Bad User Interfaces/Experience?
Hussein Nasser
Learn By Doing.
Hussein Nasser
I Wrote Bad Front-End Code That Broke Chrome
Hussein Nasser
My Story
Hussein Nasser
Vlog - Horizontal vs Vertical Scaling
Hussein Nasser
Can User Experience Help Build Better Rest API?
Hussein Nasser
Reverse engineering Instagram in flight mode
Hussein Nasser
The Benefits of the 3-Tier Architecture (e.g. REST API)
Hussein Nasser
Stateless v. Stateful Architecture (Podcast)
Hussein Nasser
The evolution from virtual machines to containers
Hussein Nasser
Proxy vs. Reverse Proxy (Explained by Example)
Hussein Nasser
Canary Deployment (Explained by Example)
Hussein Nasser
No Excuses
Hussein Nasser
Synchronous vs Asynchronous Applications (Explained by Example)
Hussein Nasser
What is an Asynchronous service?
Hussein Nasser
Difference between Client Polling vs Server Push in Notifications
Hussein Nasser
Software vs. Hardware AdBlockers (Explained by Example)
Hussein Nasser
HTTP Caching with E-Tags - (Explained by Example)
Hussein Nasser
Simple Object Access Protocol Pros and Cons (Explained by Example)
Hussein Nasser
Nodejs Express "Hello, World"
Hussein Nasser
Reverse Engineering Instagram feed
Hussein Nasser
Popup Modal Dialog with Javascript and HTML
Hussein Nasser
MIME and Media Type sniffing explained and the type of attacks it leads to
Hussein Nasser
More on: Database Integration
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Common Next.js Errors (and How I Solved Them)
Dev.to · gary killen
Applying Scalability in Backend (CodeBuddy)
Medium · LLM
Why Every Backend Developer Should Learn Nginx Before Going to Production
Medium · DevOps
Connecting Frontend to Backend: A Backend Engineer’s Reality Check
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI