“do you know event loop?” Asked by my interviewer

LORY
7 min readOct 15, 2023

Let’s explore every aspect where the concept of an event loop excels in this one single post.

The story

“During yesterday’s interview, my interviewer asked me which web server I am using, I told him Apache, PHP” my friend told me.

“Then what he said?” I asked him.

“He said that was a bad choice. Web server a better choice is using nodejs” He said.

“And did you ask him why”.

“Yes, He said it is an ‘async web server’, super efficient for I/O operation”. he told me

“Did you mention nginx?” I asked.

“Yes, and he said they are based on the same design thinking”.

“What design, did you ask?”.

“Event loop.”

“He is right, and then he asked you about the event loop?”

“Yes, but I couldn’t explain the full picture because I am not very sure how exactly event loops make web servers efficient, and I heard it in many places. can you brief me on the details?”

“Sure, Let’s start with a real-life sample, you are seeing it and using it every day if you notice carefully.”

“Really?”

“Yes, I will make sure you won’t be confused again.”

Real-life sample

Do you still remember we talked about multi-processing, multi-threading, and coroutine in the earlier post? Yes, it is all about the async process. let’s continue with the same sample story.

Problem: 3 Friends go to McDonald’s. order of the “I/O Model” is blocked, and your friends want to get the food ASAP.

A Quick recap

Multi-threading: It looks like a mess, every thread racing each other.

Multi-threading+GIL: It looks better, but it is more like a “patch” to the problem. As we discussed here: versioning for better performance, locking for strong consistency

Coroutine: A cheap yet efficient solution, that uses yield+send to for “function communication” + event loop(within the process) to archive async processing, but bottlenecked in a single thread.

--

--

LORY

A channel which focusing on developer growth and self improvement