erlang_web.asciidoc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. [[erlang_web]]
  2. == Erlang and the Web
  3. === The Web is concurrent
  4. When you access a website there is little concurrency
  5. involved. A few connections are opened and requests
  6. are sent through these connections. Then the web page
  7. is displayed on your screen. Your browser will only
  8. open up to 4 or 8 connections to the server, depending
  9. on your settings. This isn't much.
  10. But think about it. You are not the only one accessing
  11. the server at the same time. There can be hundreds, if
  12. not thousands, if not millions of connections to the
  13. same server at the same time.
  14. Even today a lot of systems used in production haven't
  15. solved the C10K problem (ten thousand concurrent connections).
  16. And the ones who did are trying hard to get to the next
  17. step, C100K, and are pretty far from it.
  18. Erlang meanwhile has no problem handling millions of
  19. connections. At the time of writing there are application
  20. servers written in Erlang that can handle more than two
  21. million connections on a single server in a real production
  22. application, with spare memory and CPU!
  23. The Web is concurrent, and Erlang is a language designed
  24. for concurrency, so it is a perfect match.
  25. Of course, various platforms need to scale beyond a few
  26. million connections. This is where Erlang's built-in
  27. distribution mechanisms come in. If one server isn't
  28. enough, add more! Erlang allows you to use the same code
  29. for talking to local processes or to processes in other
  30. parts of your cluster, which means you can scale very
  31. quickly if the need arises.
  32. The Web has large userbases, and the Erlang platform was
  33. designed to work in a distributed setting, so it is a
  34. perfect match.
  35. Or is it? Surely you can find solutions to handle that many
  36. concurrent connections with your favorite language... But all
  37. these solutions will break down in the next few years. Why?
  38. Firstly because servers don't get any more powerful, they
  39. instead get a lot more cores and memory. This is only useful
  40. if your application can use them properly, and Erlang is
  41. light-years away from anything else in that area. Secondly,
  42. today your computer and your phone are online, tomorrow your
  43. watch, goggles, bike, car, fridge and tons of other devices
  44. will also connect to various applications on the Internet.
  45. Only Erlang is prepared to deal with what's coming.
  46. === The Web is soft real time
  47. What does soft real time mean, you ask? It means we want the
  48. operations done as quickly as possible, and in the case of
  49. web applications, it means we want the data propagated fast.
  50. In comparison, hard real time has a similar meaning, but also
  51. has a hard time constraint, for example an operation needs to
  52. be done in under N milliseconds otherwise the system fails
  53. entirely.
  54. Users aren't that needy yet, they just want to get access
  55. to their content in a reasonable delay, and they want the
  56. actions they make to register at most a few seconds after
  57. they submitted them, otherwise they'll start worrying about
  58. whether it successfully went through.
  59. The Web is soft real time because taking longer to perform an
  60. operation would be seen as bad quality of service.
  61. Erlang is a soft real time system. It will always run
  62. processes fairly, a little at a time, switching to another
  63. process after a while and preventing a single process to
  64. steal resources from all others. This means that Erlang
  65. can guarantee stable low latency of operations.
  66. Erlang provides the guarantees that the soft real time Web
  67. requires.
  68. === The Web is asynchronous
  69. Long ago, the Web was synchronous because HTTP was synchronous.
  70. You fired a request, and then waited for a response. Not anymore.
  71. It all began when XmlHttpRequest started being used. It allowed
  72. the client to perform asynchronous calls to the server.
  73. Then Websocket appeared and allowed both the server and the client
  74. to send data to the other endpoint completely asynchronously. The
  75. data is contained within frames and no response is necessary.
  76. Erlang processes work the same. They send each other data contained
  77. within messages and then continue running without needing a response.
  78. They tend to spend most of their time inactive, waiting for a new
  79. message, and the Erlang VM happily activate them when one is received.
  80. It is therefore quite easy to imagine Erlang being good at receiving
  81. Websocket frames, which may come in at unpredictable times, pass the
  82. data to the responsible processes which are always ready waiting for
  83. new messages, and perform the operations required by only activating
  84. the required parts of the system.
  85. The more recent Web technologies, like Websocket of course, but also
  86. HTTP/2.0, are all fully asynchronous protocols. The concept
  87. of requests and responses is retained of course, but anything could
  88. be sent in between, by both the client or the browser, and the
  89. responses could also be received in a completely different order.
  90. Erlang is by nature asynchronous and really good at it thanks to the
  91. great engineering that has been done in the VM over the years. It's
  92. only natural that it's so good at dealing with the asynchronous Web.
  93. === The Web is omnipresent
  94. The Web has taken a very important part of our lives. We're
  95. connected at all times, when we're on our phone, using our computer,
  96. passing time using a tablet while in the bathroom... And this
  97. isn't going to slow down, every single device at home or on us
  98. will be connected.
  99. All these devices are always connected. And with the number of
  100. alternatives to give you access to the content you seek, users
  101. tend to not stick around when problems arise. Users today want
  102. their applications to be always available and if it's having
  103. too many issues they just move on.
  104. Despite this, when developers choose a product to use for building
  105. web applications, their only concern seem to be "Is it fast?",
  106. and they look around for synthetic benchmarks showing which one
  107. is the fastest at sending "Hello world" with only a handful
  108. concurrent connections. Web benchmarks haven't been representative
  109. of reality in a long time, and are drifting further away as
  110. time goes on.
  111. What developers should really ask themselves is "Can I service
  112. all my users with no interruption?" and they'd find that they have
  113. two choices. They can either hope for the best, or they can use
  114. Erlang.
  115. Erlang is built for fault tolerance. When writing code in any other
  116. language, you have to check all the return values and act accordingly
  117. to avoid any unforeseen issues. If you're lucky, you won't miss
  118. anything important. When writing Erlang code, you can just check
  119. the success condition and ignore all errors. If an error happen,
  120. the Erlang process crashes and is then restarted by a special
  121. process called a supervisor.
  122. The Erlang developer thus has no need to fear about unhandled
  123. errors, and can focus on handling only the errors that should
  124. give some feedback to the user and let the system take care of
  125. the rest. This also has the advantage of allowing him to write
  126. a lot less code, and letting him sleep at night.
  127. Erlang's fault tolerance oriented design is the first piece of
  128. what makes it the best choice for the omnipresent, always available
  129. Web.
  130. The second piece is Erlang's built-in distribution. Distribution
  131. is a key part of building a fault tolerant system, because it
  132. allows you to handle bigger failures, like a whole server going
  133. down, or even a data center entirely.
  134. Fault tolerance and distribution are important today, and will be
  135. vital in the future of the Web. Erlang is ready.
  136. === Erlang is the ideal platform for the Web
  137. Erlang provides all the important features that the Web requires
  138. or will require in the near future. Erlang is a perfect match
  139. for the Web, and it only makes sense to use it to build web
  140. applications.