Database Synchronous vs Asynchronous IO

Hussein Nasser · Advanced ·🔧 Backend Engineering ·7mo ago

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 Extending ArcObjects (IGeometry) - 01 - Getting Started
Extending ArcObjects (IGeometry) - 01 - Getting Started
Hussein Nasser
2 Extending ArcObjects  (IGeometry) - 02 - The Document, The Map and The Layers
Extending ArcObjects (IGeometry) - 02 - The Document, The Map and The Layers
Hussein Nasser
3 Channel Update - New Book, New Job, New Videos
Channel Update - New Book, New Job, New Videos
Hussein Nasser
4 Learn Programming with VB.NET - 01 - Getting Started
Learn Programming with VB.NET - 01 - Getting Started
Hussein Nasser
5 Learn Programming with VB.NET - 02 - Classes and Objects (Part 1)
Learn Programming with VB.NET - 02 - Classes and Objects (Part 1)
Hussein Nasser
6 Learn Programming with VB.NET - 03 - Classes and Objects (Part 2)
Learn Programming with VB.NET - 03 - Classes and Objects (Part 2)
Hussein Nasser
7 Learn Programming with VB.NET - 04 - User Interface
Learn Programming with VB.NET - 04 - User Interface
Hussein Nasser
8 Learn Programming with VB.NET - 05 - By Value v. By Reference
Learn Programming with VB.NET - 05 - By Value v. By Reference
Hussein Nasser
9 Learn Programming with VB.NET - 06 - Variable size, 32 bit vs 64 bit
Learn Programming with VB.NET - 06 - Variable size, 32 bit vs 64 bit
Hussein Nasser
10 Learn Programming with VB.NET - 07 - Conditional Statements
Learn Programming with VB.NET - 07 - Conditional Statements
Hussein Nasser
11 Learn Programming with VB.NET - 08 - Inheritance
Learn Programming with VB.NET - 08 - Inheritance
Hussein Nasser
12 Learn Programming with VB.NET - 09 - Strategy Design Pattern
Learn Programming with VB.NET - 09 - Strategy Design Pattern
Hussein Nasser
13 Learn Programming with VB.NET - 10 -  How did I learn programming
Learn Programming with VB.NET - 10 - How did I learn programming
Hussein Nasser
14 IGeometry 2016 Retrospective - Channel Update
IGeometry 2016 Retrospective - Channel Update
Hussein Nasser
15 Javascript by Example - The Vook
Javascript by Example - The Vook
Hussein Nasser
16 Vlog - Keep your servers close and your database closer
Vlog - Keep your servers close and your database closer
Hussein Nasser
17 Vlog - Client/Server Programming Languages
Vlog - Client/Server Programming Languages
Hussein Nasser
18 Javascript By Example L1E01 - Getting Started
Javascript By Example L1E01 - Getting Started
Hussein Nasser
19 Persistent Connections (Pros and Cons)
Persistent Connections (Pros and Cons)
Hussein Nasser
20 Javascript By Example L1E02 - Building the Calculator Interface
Javascript By Example L1E02 - Building the Calculator Interface
Hussein Nasser
21 Happy new Year from IGeometry!
Happy new Year from IGeometry!
Hussein Nasser
22 Synchronous v. Asynchronous
Synchronous v. Asynchronous
Hussein Nasser
23 Javascript By Example L1E03 - Displaying the Digits on Calculator Screen
Javascript By Example L1E03 - Displaying the Digits on Calculator Screen
Hussein Nasser
24 Show Your Work. Blog, Vlog, Write, Create and Develop!
Show Your Work. Blog, Vlog, Write, Create and Develop!
Hussein Nasser
25 Relational Database Atomicity Explained By Example
Relational Database Atomicity Explained By Example
Hussein Nasser
26 Javascript By Example L1E04 - Operators, All Clear with Arrow Functions
Javascript By Example L1E04 - Operators, All Clear with Arrow Functions
Hussein Nasser
27 What Comes First, User Experience or Software Architecture?
What Comes First, User Experience or Software Architecture?
Hussein Nasser
28 Javascript By Example L1E05 -  Evaluate the Calculator Expressions with eval
Javascript By Example L1E05 - Evaluate the Calculator Expressions with eval
Hussein Nasser
29 Fastest Way to Learn Programming Language or Technology
Fastest Way to Learn Programming Language or Technology
Hussein Nasser
30 Javascript By Example L1E06 -  Fix Leading Zero Bug with Conditions
Javascript By Example L1E06 - Fix Leading Zero Bug with Conditions
Hussein Nasser
31 Stateful vs Stateless Applications (Explained by Example)
Stateful vs Stateless Applications (Explained by Example)
Hussein Nasser
32 Javascript By Example L1E07 - Running our Calculator on the Mobile Phone
Javascript By Example L1E07 - Running our Calculator on the Mobile Phone
Hussein Nasser
33 Advice for New Software Engineers and Developers
Advice for New Software Engineers and Developers
Hussein Nasser
34 Why JSON is so Popular?
Why JSON is so Popular?
Hussein Nasser
35 Building Scalable Software - SLA, HS, VS
Building Scalable Software - SLA, HS, VS
Hussein Nasser
36 Vlog (Istanbul) - Datacenter Proximity
Vlog (Istanbul) - Datacenter Proximity
Hussein Nasser
37 Should Software Engineers Learn Bleeding-Edge Technologies?
Should Software Engineers Learn Bleeding-Edge Technologies?
Hussein Nasser
38 Do Developers Build Bad User Interfaces/Experience?
Do Developers Build Bad User Interfaces/Experience?
Hussein Nasser
39 Learn By Doing.
Learn By Doing.
Hussein Nasser
40 I Wrote Bad Front-End Code That Broke Chrome
I Wrote Bad Front-End Code That Broke Chrome
Hussein Nasser
41 My Story
My Story
Hussein Nasser
42 Vlog - Horizontal vs Vertical Scaling
Vlog - Horizontal vs Vertical Scaling
Hussein Nasser
43 Can User Experience Help Build Better Rest API?
Can User Experience Help Build Better Rest API?
Hussein Nasser
44 Reverse engineering Instagram in flight mode
Reverse engineering Instagram in flight mode
Hussein Nasser
45 The Benefits of the 3-Tier Architecture (e.g. REST API)
The Benefits of the 3-Tier Architecture (e.g. REST API)
Hussein Nasser
46 Stateless v. Stateful Architecture (Podcast)
Stateless v. Stateful Architecture (Podcast)
Hussein Nasser
47 The evolution from virtual machines to containers
The evolution from virtual machines to containers
Hussein Nasser
48 Proxy vs. Reverse Proxy (Explained by Example)
Proxy vs. Reverse Proxy (Explained by Example)
Hussein Nasser
49 Canary Deployment (Explained by Example)
Canary Deployment (Explained by Example)
Hussein Nasser
50 No Excuses
No Excuses
Hussein Nasser
51 Synchronous vs Asynchronous Applications (Explained by Example)
Synchronous vs Asynchronous Applications (Explained by Example)
Hussein Nasser
52 What is an Asynchronous service?
What is an Asynchronous service?
Hussein Nasser
53 Difference between Client Polling vs Server Push in Notifications
Difference between Client Polling vs Server Push in Notifications
Hussein Nasser
54 Software vs. Hardware AdBlockers (Explained by Example)
Software vs. Hardware AdBlockers (Explained by Example)
Hussein Nasser
55 HTTP Caching with E-Tags -  (Explained by Example)
HTTP Caching with E-Tags - (Explained by Example)
Hussein Nasser
56 Simple Object Access Protocol Pros and Cons (Explained by Example)
Simple Object Access Protocol Pros and Cons (Explained by Example)
Hussein Nasser
57 Nodejs Express "Hello, World"
Nodejs Express "Hello, World"
Hussein Nasser
58 Reverse Engineering Instagram feed
Reverse Engineering Instagram feed
Hussein Nasser
59 Popup Modal Dialog with Javascript and HTML
Popup Modal Dialog with Javascript and HTML
Hussein Nasser
60 MIME and Media Type sniffing explained and the type of attacks it leads to
MIME and Media Type sniffing explained and the type of attacks it leads to
Hussein Nasser

The video explains the differences between synchronous and asynchronous IO, and how they impact database operations and file systems, with a focus on Node.js and IO_uring.

Key Takeaways
  1. Understand the concept of synchronous IO and its impact on system performance
  2. Learn about asynchronous IO and its benefits in reducing blocking calls
  3. Explore the use of IO_uring for asynchronous IO operations
  4. Apply knowledge of synchronous and asynchronous IO to database systems and file systems
💡 Asynchronous IO can improve system performance by reducing blocking calls, but it requires a deep understanding of the underlying operating system and file system concepts.

Related AI Lessons

Up next
This Cop Was Held Accountable For His Brutality! #police #lawyer
Hampton Law
Watch →