Debian Codegraph @

doc/src/guide/erlang_web.asciidoc

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