世界で最も大きなトラフィックを捌いているサービスの1つであるTwitterはそのバックエンドをScalaで独自開発したフレームワーク(Finagle)を採用していましたが、ここ数ヶ月の間にこれをNode.js(Express)に置き換えたそうです。
以下はTwitterのエンジニアであるNicolas Gallagherさんのツィートより
Today we moved all of Twitter's mobile web traffic (that's like, a lot) to our new web stack – Node.js, Express, React PWA.
— Nicolas (@necolas) February 8, 2017
本日をもってTwitterのモバイルWeb版のトラフィック(たくさん)を僕たちの新しいWebスタックに移行した。つまり、Node.js、ExpressそしてReact Progressive Web Appだ。
This JavaScript stack has been in production for 9 months, powering the logged-in mobile web app.
— Nicolas (@necolas) 2017年2月8日
このJavaScriptのスタックは今から9ヶ月前から実運用に入っていた。ログイン済みのモバイルWebユーザー向けにね。
For the last 2 years, the stack serving logged-out mobile web has been Scala, Google Closure Templates, and a sprinkling of JavaScript.
— Nicolas (@necolas) February 8, 2017
2年の間、ログインしていないユーザーのトラフィックはScalaとGoogleのClosureテンプレートを中心に捌いていた。
A few months ago @CharlieCroom started an experiment to move a fraction of the logged-out traffic over to the JavaScript stack.
— Nicolas (@necolas) February 8, 2017
数ヶ月間にCharlie Croomがログインしていないユーザーのトラフィックの一部をJavaScriptのスタックに置き換える実験を始めた。
This week and last, @jbell (our team's Node.js tsar) moved the rest of the logged-out traffic from the Scala app to the JavaScript app.
— Nicolas (@necolas) February 8, 2017
今週にはJames Bellenger(僕たちのチームのNode.jsの帝王)が残りのログインしていないユーザーのトラフィックをScalaからJavaScriptに移行した。
個人的な意見ですが、Scalaは美しい仕様を持つ素晴らしい言語ですが、関数型+オブジェクト指向という特徴から、学習コストが高すぎると感じました。そして実運用をするには、それに加えてJavaのライブラリの知識やJVMのパフォーマンスに気を配る必要があります。つまり、面倒すぎる言語なのでしょう。
一方でNode.jsはチープなマシンでも高いパフォーマンスが出せるような設計になっており、何よりJavaScriptであることからフロントエンドと使用する言語を共通化できるという利点があります。
これについてはTwitterのエンジニアでもある James Bellengerさんも
@chaseadamsio @dstevensio @necolas node, so far, hasn't been much of an issue, and the benefit of being able to run the same code on the server has been huge
— James Bellenger (@jbell) 2017年2月8日
Node.jsは今まで致命的な問題が発見されたことはなく、そしてサーバーと同じコードで動作させることができるという利点はとても大きい。
これには、Twitterの特殊事情がありそうで、REST APIがjson形式でデータをやり取りしていることから、そのパースにかかる計算コストが大幅に削減されたことがありそうです。JSONはJavaScriptのデータ型そのものですからね。
@chaseadamsio @dstevensio @necolas The highest yield things we've done have been improvements to the twitter rest apis used by node. Still plenty of low-hanging fruit there.
— James Bellenger (@jbell) February 8, 2017
Node.jsの導入による最高の収穫はTwitterのRest APIの性能の改善だ。それに加えてたくさんの収穫がある。
@chaseadamsio @dstevensio @necolas Generally, reducing the amount of json parsed on the server has been very effective. We're looking at graphql down the road.
— James Bellenger (@jbell) 2017年2月8日
総合すれば、JSONのパースにかかる時間の削減がとても効果的だった。graphQLの導入も視野に入れているよ
とにかく秒間10万アクセスにも及ぶ大量のトラフィックを捌いているTwitterがNode.jsを採用したことで、またNode.jsの優位性を証明する結果になりました。