バックエンド開発のためのJavaScript : Javascript for backend development

バックエンド開発のためのJavaScript : Javascript for backend development

※本稿は弊社のCTO、Slavi Pantaleevによる記事です。

Our previous series of articles (the current state of web-development: #1, #2, #3) talked about Javascript and its popularity in frontend web development.

In this article, we’re going to talk about the role of Javascript in another area: server-side web development.

前回はWEB開発のフロントエンド側のJavaScriptについて、いかに多くのシーンで利用されているかをお話ししました。(2014年WEBの世界で起こっていること(1)(2)(3): The current state of the web)

今回は、JavaScriptのもう一つの使われ方:「WEB開発時のサーバ側」における役割についてお話ししたいと思います。

Node.js

The most important change in the Javascript world that stemmed from Google’s V8 engine is the appearance of the Node.js platform in 2009.

Node.js is an environment for running Javascript on your server. The Node.js platform is a combination of Google’s V8 Javascript engine and a set of APIs (ways to do networking, filesystem operations, process management, etc.) that help with the development of server-side programs.

Node.js enables developers to use Javascript to write server-side (backend) programs. Not only that, but it’s highly-performant, scalable and ideal for creating distributed network services.

The Node.js environment functions in a similar way to how Javascript works in the browser. Programs are simple to write and understand. This allows frontend web developers that already know Javascript to easily transition into writing backend code.

Node.js

GoogleのV8エンジン由来のJavaScriptの世界において最も重要な変化といえば、2009年のNode.jsプラットフォームの登場です。

Node.jsはサーバ上でJavaScriptを動作させる環境です。Node.jsプラットフォームは、GoogleのV8 JavaScriptエンジンといくつかのAPI(ネットワーキングを行う手段、ファイルシステム操作、プロセス管理など)の組み合わせでできており、サーバ側のプログラムの生成を助けます。

開発者がJavaScriptでサーバ側(バックエンド)のプログラムを記述できるのは、Node.jsがあるからです。それだけでなく、高性能で拡張性があり分散型ネットワーク·サービスを作成すには最適です。

Node.jsの環境機能はJavaScriptがブラウザ内で動作するのに似ています。プログラムはシンプルで理解しやすく、書きやすい。すでにフロントエンドでJavaScriptを知っているWEB開発者なら、バックエンドのコードに遷移する書き込みが簡単にできます。

Node is good for real-time services

If you are to write a real-time chat system (like Facebook’s chat or Omegle) in a language like PHP (or Ruby or Python), you will face many challenges. To make it real-time, you need to push messages from the server to your users as fast as possible. There are 2 ways to do that:

  • frequent polling – you make the user’s browser ask your server “are there new messages for me” often (multiple times a second)

  • comet-streaming (long-polling) – you keep an open connection between the user’s browser and your server at all times and push new message over it (as soon as your server is made aware that there are new messages)

Both approaches don’t work well under scale.

Frequent polling leads to many re-connections and a lot of overhead – each request takes significant time to process.

Long-polling wastes a lot of memory – each open connection takes some megabytes of memory – you can’t service many users with a single server.

The modern web requires that you can handle tens of thousands of concurrent users at the same time. This is also known as the c10k problem.

Node.js is designed to be great at handling a lot of concurrent users and their data in real-time. It can keep thousands of client connections open and periodically do small amounts of work for each one.

(For completeness, we need to mention that there are ways to achieve the same result in other languages, albeit in a more complicated way compared to Node).

リアルタイムのサービスに適したNode

もし、あなたがPHP(あるいはRubyやPython)などの言語でリアルタイムのチャットシステム(FacebookチャットやOmegleなど)を記述しようとした場合、多くの課題に直面するでしょう。
リアルタイムにするためには、メッセージをサーバからできるだけ速くユーザーにプッシュする必要がありますが、それには次の2つの方法があります:

  • 頻繁なポーリング – 「私あての新規メッセージはありますか?」という、ユーザーのブラウザからあなたのサーバへの問合せ回数を多くする(1秒に複数回)
  • コメット・ストリーミング(ロングポーリング) – ユーザーのブラウザとあなたのサーバ間のコネクションを常時オープンな状態にしておき、新規メッセージをプッシュする(あなたのサーバが新規メッセージに気付いた段階ですぐに送る)

どちらのアプローチも、アンダースケールではうまく機能しません。
頻繁なポーリングでは、再接続が何度も必要になりオーバーヘッドも非常にかかります – 各リクエストごとにプロセスにかかる時間が大きいのです。
(訳注:オーバーヘッドとは、ある処理の作業そのものに必要な処理時間・必要メモリー量などの正味コストとは別にかかる、作業準備、管理、後処理に必要な2次的コストのこと。)

ロングポーリングでは、メモリーを多く使います – 各オープンコネクションにつき何メガバイトかのメモリーが必要です – ひとつのサーバで多くのユーザーにサービスを提供することはできません。

最近のWEBは、何万もの並列ユーザーに一度に対応できなくてはなりません。これはc10k問題とも呼ばれています。

Node.jsは多数の並列ユーザーとそのデータをリアルタイムで処理するのに優れています。何千ものクライアントとのコネクションをオープン状態でキープでき、さらに各ユーザーに向けてボリュームの小さい作業を定期的に行うことが可能です。

(厳密には、他の言語を使っても同じ結果を出すことができるということに言及しておく必要があるでしょう、とはいってもNodeに比べるとより複雑な方法になります)。

Node provides concurrency in a simple way

In most other languages, developers either perform tasks serially or use threads to parallelize work. Threads, however, are a dangerous feature – one that often leads to many hard-to-find bugs.

Node’s asynchronous execution model enables developers to parallelize certain types of work (work that involves input/output operations) in an efficient and safe way.

Thanks to Node’s simple concurrency model (asynchronous execution in a single thread), developers can perform many tasks at once, without the danger of using threads. Many customers can be served at the same time in an optimal way.

Nodeなら並行処理をシンプルに提供できる

他のほとんどの言語では、開発者はタスクを連続的に実行するか、作業を並列化するスレッドを使用するかのどちらかです。しかしながらこのスレッドというものには危険な特性があります – 多くの発見困難なバグにつながる特性です。

開発者はノードの非同期実行モデルを使うことで、効率的かつ安全な方法で特定種類の作業(インプット/アウトプット動作を含む作業)を並列化することが可能です。

Nodeのシンプルな並列モデル(シングルスレッドでの非同期実行)のおかげで、開発者たちはスレッドを使うリスクを冒すことなく多くのタスクを一度にさせることができます。そして多数のカスタマーが同時に、最適なサービスを受けられるのです。

Node is good for small web services

Node.js can be used to create small and useful backend programs.

They don’t even need to be full-fledged servers that do a lot. It could be a simple API server or just a small component that is part of your service-oriented architecture.

Node‘s power can be leveraged without throwing away all your other code. Modern web-development is diverse – many languages and frameworks, each offering a set of benefits. If you’re not afraid of mixing technologies (and you shouldn’t be), you can use your favorite web-development language for most of the code and only use Node where it makes sense.

小規模WEBサービスに適したNode

Node.jsは、小型かつ便利なバックエンドプログラムを作成するのにも使用できます。

なんでもこなせる本格的なサーバの必要すらありません。シンプルなAPIサーバか、あるいはサービス指向型アーキテクチャの一部分の小さなコンポーネントで事足ります。

(訳注:service-oriented architecture:ビジネス・プロセスの業務機能サービスに基づいたシステム設計構造◆【略】SOA(出典:アルク辞書))

Node’sの能力を活用するのに、他のコードの使用をあきらめる必要はありません。現代のWEB開発は多様です – 多くの言語、フレームワーク、それぞれに複数の利点があります。あなたがテクノロジーのミックスを恐れなければ(そしてまたそうであるべきなのですが)、ほとんどのコードについてはあなたお気に入りのWEB開発言語が使えるでしょう、そして理にかなっている部分のみにNodeを使用すればよいのです。