erlang_web.asciidoc 8.6 KB

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