
Server - recieves Request and sends Response
Inside Server there consists of Threads.
Single-Thread or Multi-Thread

In Single Thread , one Thread takes care of all the requests

파란색 부분이 실행하는 부분이고 회색 부분이 아무 실행이 없는 부분이다.
만약 파란색 부분이 겹쳐있다면 보기와 같이 위에 파티 실행권이 우선이다.
그만큼의 시간을 파티2는 걸쳐야한다
싱글 스레드에서 파란 부분, request를 받는은 CPU: active time 이라고 한다 (Thinking part)
회색 부분은 I/O라고 한다. Input/Output : inactive time (waiting time to take input and send output)
I/O includes reading from filesystem making network requests and even waiting for time to pass
여기서 중요한 부분은 inactive라고 사실 가만히 있는게 아니다 CPU 부분은 프로그래밍 일을 하고 I/O 부분은 데이터를 읽고 하는 일은 한다. 그 시간이 걸리는게 바로 inactive타임이다.
그리고 이러한 시간을 non-blocking I/O 는 파티1 과 2를 오가는 작업으로 시간을 단축한다.
Non-blocking I/O 는 Thread is not stuck waiting for the request to fininsh.
Blocking I/O 는 waits for I/O to finish before proceeding.

Blocking I/O 많은 시간 낭비를 한다
그럼 왜 전부 non-Blocking I/O 를 사용하지 않는것일까
많은 언어들이 이 방법을 제한하고 ( 언어마다 단점인 부분이 있다고 한다 ) 트래픽을 줄이기위해thread를 늘린다.
자바 스크립트의 callback 함수는 이런 non-blocking I/O를 이행 시키기 위해 너무 잘 맞는 언어였고
이 장점을 위해 태어난 것이 바로 node js다
장점
I/O work > cpu work 에 가장 어울리는 프레임워크다.
최대한 활용하는 코드를 간단하고 리팩토링에 집중해야한다는 소리인듯하다
network request 에 있어서 최고라고 한다
머신런닝 처럼 cpu 에 과한 일은 노드에 어울리지 않는다.
멀티 스래드의 설명은 다음 과 같다 :
In multi-threaded servers, multiple threads take care of requests simultaneously, allowing for better performance and more efficient use of resources. Each thread handles a specific request, and if one thread is busy, another can take over. This can reduce the amount of time a user has to wait for their request to be processed. However, managing multiple threads can be more complex and require more resources than a single-threaded server.
https://www.youtube.com/watch?v=wB9tIg209-8
이 영상을 보고 메모한 것이다 비전공자인 자에게는 너무 쉽게 풀어진 내용으로 다른 사람들도 꼭 한번 봤으면 좋겠다
EX.
Blocking I/O
만약 텍스트 파일이 크다면 console.log 까지 가기 힘들것이다
let text = fs.readFileSync(...., 'utf-8');
console.log(text)
fs.writeFileSync(..., text)
NonBlocking I/O
텍스트 파일이 커도 async 로 인해
let txt= fs.readFile(...,'utf-8', (err,data)=>{
console.log("done")
})
console.log('running in background')
running in background
done
순서로 실행된다.
MultiThread도 꾸준히 성장하고 많은 곳에서 사용되며 아주 효율적으로 실행될수도 있다.
프로세스 없는 Thread가 낭비 라고 생각할수도있고 Thread가 비싸니 별로라고 생각할수도 있다.
하지만 대기업일 경우 수많은 프로세스가 끊임없이 실행되고 Thread가 그 값을 하니 낭비라고 할수없다. 그리고 기술은 꾸준히 성장한다. SingleThread를 보며 아 이게 효율적이구나 왜 굳이 멀티쓰레드를 사용해야하지? 라는 생각은 버려야한다. 목적에 맞는 쓰레드를 사용하는것이 개발자가 중요한 이유다. 싱글 쓰레드는 아직 inner Process 가 많은 CPU에 의존하는 목적에는 적합하지 않다.
너무 재밌다.