nodejs 背後係 v8
v8 嘅背後係 libuv
喺nodejs 嘅角度
一萬個requests都係由event loop一條 thread處理
喺 libuv 嘅角度每個 IO 都有一條kernel thread睇住
一有 hardware interrupt 返嚟就將串data交返比 event loop
所以 nodejs 行 single thread只係一個錯覺
由於 cpu-bound task will run to its completion
用event loop 嚟run代表新嘅 http request無法被處理
所以nodejs比你特燈開一條worker thread做cpu-bound task
唔好阻住event loop收request
但呢個只係折衝嘅方法
你粒cpu有20個core
20個cpu-bound task就會霸哂
performance會一路下降
最終context switch都拖死你
而且無得scale
解決辦法有好多種
最直接係 microservices
delegate the task to other services on the network
必要時可以spin up as many services as you want
scalability 理論上係無限
本來nodejs真正嘅優勢係:
1. javascript (frontend / backend 易角嘅代價低咗)
2. non blocking IO (快)
但 non blocking IO差唔多所有framework都有而且更快
例如
asp.net core 已經比 nodejs 快而且language features多好多
所以javascript係佢暫時仲受歡迎嘅原因