Your SlideShare is downloading. ×
1 hour dive into Erlang/OTP
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

1 hour dive into Erlang/OTP

8,307
views

Published on

Introduction to Erlang and OTP

Introduction to Erlang and OTP

Published in: Technology

2 Comments
6 Likes
Statistics
Notes
No Downloads
Views
Total Views
8,307
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
25
Comments
2
Likes
6
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. 1 hour dive into Erlang/OTP@jvalduvieco @jordillonch
  • 2. Problem domain
  • 3. Lots of users
  • 4. Lots of users
  • 5. 24x7x365
  • 6. 24x7x365
  • 7. Lots of critical concurrent transactions
  • 8. Lots of critical concurrent transactions
  • 9. Hardware or software breaks
  • 10. Hardware or software breaks
  • 11. Lots of code changes
  • 12. Lots of code changes
  • 13. UnscalableUnmaintainable } code
  • 14. UnscalableUnmaintainable } code
  • 15. Maintenance/Debug inproduction system
  • 16. Maintenance/Debug inproduction system
  • 17. Does it sound to you?
  • 18. The Erlang solution
  • 19. Simplicity...
  • 20. Minimize defensive programming
  • 21. Typeless variables
  • 22. Develop by contract
  • 23. t-shirt function size If
  • 24.  a
  • 25.  function
  • 26.  does
  • 27.  not
  • 28.  fit
  • 29.  on
  • 30.  your
  • 31.  t-shirt
  • 32.  it
  • 33.  is
  • 34.  too
  • 35.  long!
  • 36. Single responsibility principle
  • 37. No shared statebetween entities
  • 38. High Concurrency
  • 39. High ConcurrencyHigh concurrency
  • 40. Light threads
  • 41. Light threads hundreds of thousands of threads in one machine with a good cpu scheduler
  • 42. Light threads hundreds of thousands of threads in one machine with a good cpu scheduler Lt Lt Lt
  • 43. Message passing Lt Lt Lt
  • 44. Message passing A shared nothing architecture that communicates through message passing Lt Lt Lt
  • 45. Message passing A shared nothing architecture that communicates through message passing Lt Lt Lt
  • 46. Process Mailbox Lt Lt Lt
  • 47. Process Mailbox Every process has a mailbox with incoming messages, process take on convenience Lt Lt Lt
  • 48. No shared state Lt S Lt S Lt S
  • 49. No shared state Every process its own internal state stored in a variable avoiding lock contention Lt S Lt S Lt S
  • 50. Soft realtime
  • 51. Soft realtimeYou have no strict guarantees onlatency but language is designed to have low latency
  • 52. High availability
  • 53. High availability High availability
  • 54. Supervised processes Pa Pb
  • 55. Supervised processes processes can be monitored by other processes, handling its termination Pa Pb
  • 56. Fail early Pa Pb S
  • 57. Fail early Fail as soon as possible and let someonehandle bad data, someone will restart you Pa Pb S
  • 58. Fail early Fail as soon as possible and let someonehandle bad data, someone will restart you Pa Pb2 S
  • 59. Hot code updatePb Pa v1 S Pc
  • 60. Hot code update processes code and data can be replaced without loosing servicePb Pa v1 S Pc
  • 61. Hot code update processes code and data can be replaced without loosing servicePb Pa v1 v2 S Pc
  • 62. DistributionNode NodeP P P PP P P PP P P P
  • 63. DistributionProcesses run on nodes and can be located wherever they areNode NodeP P P PP P P PP P P P
  • 64. How does it look like?
  • 65. Show me the code!
  • 66. Demo 1
  • 67. Hands on
  • 68. The shell
  • 69. Type on your console: erl
  • 70. You should see:Erlang R16B (erts-5.10.1) [source] [64-bit][smp:2:2] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]Eshell V5.10.1 (abort with ^G)1
  • 71. Variables
  • 72. Variables start Uppercase
  • 73. Variables are immutable
  • 74. Can contain any type
  • 75. You can do things like...
  • 76. 1 Foo = 1.12 Foo = 2.** exception error: no match of right handside value 2
  • 77. Foo
  • 78.  is
  • 79.  bounded
  • 80.  to
  • 81.  11 Foo = 1.12 Foo = 2.** exception error: no match of right handside value 2
  • 82. Foo
  • 83.  is
  • 84.  bounded
  • 85.  to
  • 86.  11 Foo = 1.1 Foo
  • 87.  ==
  • 88.  2
  • 89.  ?2 Foo = 2.** exception error: no match of right handside value 2
  • 90. 1 Foo = 1.12 Bar = 2.23 Foo = Bar.** exception error: no match of righthand side value 2
  • 91. This is GREAT!
  • 92. Now you have yourbasic error checkingsystem implemented
  • 93. Advanced types
  • 94. [List]
  • 95. [1,2,3,4,5,6]
  • 96. You can do things like...
  • 97. Iterate sequentially
  • 98. 1 MyList = [1,2,3,4].[1,2,3,4]2 [Head|Tail] = MyList.[1,2,3,4]3 Head.14 Tail.[2,3,4]
  • 99. 1 MyList = [1,2,3,4].[1,2,3,4]2 [Head|Tail] = MyList.[1,2,3,4]3 Head.14 Tail.[2,3,4]
  • 100. 1 MyList = [1,2,3,4].[1,2,3,4]2 [Head|Tail] = MyList.[1,2,3,4]3 Head.14 Tail.[2,3,4]
  • 101. Do something to all or some items on a list(list comprehensions)
  • 102. 1 MyList = [1, 2, 3, 4].[1,2,3,4]2 [X + 2 || X - MyList, X 2].[5,6]
  • 103. [Strings]
  • 104. A list
  • 105. You can do things like...
  • 106. 1 MyString = Erlang is not Ruby.Erlang is not Ruby
  • 107. 1 MyString = Erlang is not Ruby.Erlang is not Ruby2 [Head2|Tail2] = MyString.Erlang is not Ruby3 Head2.69 ASC
  • 108.  =
  • 109.  “E”4 Tail2.rlang is not Ruby
  • 110. {Tuples}
  • 111. {1,2,3}
  • 112. Basic data container
  • 113. random access
  • 114. matcheable
  • 115. You can do things like...
  • 116. 1 Mytuple = {1,2,3,4}.{1,2,3,4}2 A = 1.13 B = 2.24 {A,B,C,D} = {1,2,3,4}.{1,2,3,4}5 C.36 D.4
  • 117. 1 Mytuple = {1,2,3,4}.{1,2,3,4}2 A = 1.1 A
  • 118.  and
  • 119.  B
  • 120.  are3 B = 2. bounded
  • 121.  variables24 {A,B,C,D} = {1,2,3,4}.{1,2,3,4}5 C.36 D.4
  • 122. 1 Mytuple = {1,2,3,4}.{1,2,3,4}2 A = 1.1 A
  • 123.  and
  • 124.  B
  • 125.  are3 B = 2. bounded
  • 126.  variables24 {A,B,C,D} = {1,2,3,4}.{1,2,3,4}5 C. pattern
  • 127.  matching3 A==1?6 D. B==2?4
  • 128. 1 Mytuple = {1,2,3,4}.{1,2,3,4}2 A = 1.1 A
  • 129.  and
  • 130.  B
  • 131.  are3 B = 2. bounded
  • 132.  variables24 {A,B,C,D} = {1,2,3,4}. C
  • 133.  and
  • 134.  D
  • 135.  are
  • 136.  {1,2,3,4} unbounded
  • 137.  5 C. pattern
  • 138.  matching variables3 A==1?6 D. B==2?4
  • 139. functions
  • 140. functions are types
  • 141. single exit point
  • 142. You can do things like...
  • 143. foo_bar_func(Foo, Bar) - Result = Foo + Bar, Result.
  • 144. This
  • 145.  is
  • 146.  a
  • 147.  arity
  • 148.  2
  • 149.  function
  • 150.   (2
  • 151.  parameters)foo_bar_func(Foo, Bar) - Result = Foo + Bar, Result.
  • 152. This
  • 153.  is
  • 154.  a
  • 155.  arity
  • 156.  2
  • 157.  function
  • 158.   (2
  • 159.  parameters)foo_bar_func(Foo, Bar) - Result = Foo + Bar, Result. means
  • 160.  that
  • 161.  more
  • 162.   code
  • 163.  is
  • 164.  to
  • 165.  come
  • 166. This
  • 167.  is
  • 168.  a
  • 169.  arity
  • 170.  2
  • 171.  function
  • 172.   (2
  • 173.  parameters)foo_bar_func(Foo, Bar) - Result = Foo + Bar, Result. means
  • 174.  that
  • 175.  more
  • 176.   code
  • 177.  is
  • 178.  to
  • 179.  come means
  • 180.  the
  • 181.  function
  • 182.   code
  • 183.  has
  • 184.  ended
  • 185. This
  • 186.  is
  • 187.  a
  • 188.  arity
  • 189.  2
  • 190.  function
  • 191.   (2
  • 192.  parameters)foo_bar_func(Foo, Bar) - Result = Foo + Bar, Result. means
  • 193.  that
  • 194.  more
  • 195.   code
  • 196.  is
  • 197.  to
  • 198.  comeSingle
  • 199.  exit
  • 200.  pointReturns
  • 201.  Result means
  • 202.  the
  • 203.  function
  • 204.   code
  • 205.  has
  • 206.  ended
  • 207. 1 FooBar = fun(Foo, Bar) -1 Foo + Bar1 end.#Funerl_eval.12.829309122 Result = FooBar(1, 2).3
  • 208. atoms
  • 209. A constant with name
  • 210. lowercase
  • 211. You can do things like...
  • 212. 1 ok.ok2 ko.ko3 ok =:= ko.false4 ok =:= ok.true
  • 213. 1 MyFuncOK = fun() - {ok, 33} end.#Funerl_eval.20.829309122 MyFuncFail = fun() - {error, bad_arg} end.#Funerl_eval.20.829309123 {ok, Result} = MyFuncOK().{ok,33}4 {ok, Result2} = MyFuncFail().** exception error: no match of right handside value {error,bad_arg}
  • 214. atoms1 MyFuncOK = fun() - {ok, 33} end.#Funerl_eval.20.829309122 MyFuncFail = fun() - {error, bad_arg} end.#Funerl_eval.20.829309123 {ok, Result} = MyFuncOK().{ok,33}4 {ok, Result2} = MyFuncFail().** exception error: no match of right handside value {error,bad_arg}
  • 215. atoms1 MyFuncOK = fun() - {ok, 33} end.#Funerl_eval.20.829309122 MyFuncFail = fun() - {error, bad_arg} end.#Funerl_eval.20.82930912 }3 {ok, Result} = MyFuncOK().{ok,33} error4 {ok, Result2} = MyFuncFail(). checking** exception error: no match of right handside value {error,bad_arg}
  • 216. atoms1 MyFuncOK = fun() - {ok, 33} end.#Funerl_eval.20.829309122 MyFuncFail = fun() - {error, bad_arg} end.#Funerl_eval.20.82930912 }3 {ok, Result} = MyFuncOK().{ok,33} error4 {ok, Result2} = MyFuncFail(). checking** exception error: no match of right handside value {error,bad_arg} do
  • 217.  they
  • 218.  match?
  • 219. modules
  • 220. basic name spacing
  • 221. public/private functions
  • 222. You can do things like...
  • 223. On your ${EDITOR} create cool_func.erl-module(cool_func).-export([foo_bar_func/2]).foo_bar_func (Foo, Bar) - Result = Foo + Bar, {ok, Result}.
  • 224. On erlang shell type:1 c(cool_module).{ok,cool_module}2 {ok, Result3} = cool_module:sum(33, 44).{ok,77}3 Result3.77
  • 225. Too easy? Let’s add some patternmatching on parameters!
  • 226. area({square, Side}) - {ok,Side * Side};area({circle, Radius}) - % almost :-) {ok, 3 * Radius * Radius};area({triangle, A, B, C}) - S = (A + B + C)/2, {ok, math:sqrt(S*(S-A)*(S-B)*(S-C))};area(Other) - {error, invalid_object}.
  • 227. area({square, Side}) - {ok,Side * Side}; means
  • 228.  that
  • 229.  there
  • 230.  is
  • 231.  area({circle, Radius}) - another
  • 232.  matching
  • 233.   % almost :-) posibility {ok, 3 * Radius * Radius};area({triangle, A, B, C}) - S = (A + B + C)/2, {ok, math:sqrt(S*(S-A)*(S-B)*(S-C))};area(Other) - {error, invalid_object}.
  • 234. area({square, Side}) - {ok,Side * Side}; means
  • 235.  that
  • 236.  there
  • 237.  is
  • 238.  area({circle, Radius}) - another
  • 239.  matching
  • 240.   % almost :-) posibility {ok, 3 * Radius * Radius};area({triangle, A, B, C}) - S = (A + B + C)/2, {ok, math:sqrt(S*(S-A)*(S-B)*(S-C))};area(Other) - {error, invalid_object}.mymodule:area({square, 10}).mymodule:area({circle, 10}).mymodule:area({triangle, 2, 2, 3.5}).
  • 241. Want more?Let’s add some recursion!
  • 242. Want more?Let’s add some recursion! Erlang does not use loops
  • 243. factorial(0) - 1;factorial(N) - N * factorial(N-1).
  • 244. OTP
  • 245. Oh, This is Perfect!
  • 246. OTP is a set of rock solid architecture patterns, practices and tools
  • 247. Improves the design, fault tolerance anddeployment of your app
  • 248. Erlang is just a language,OTP makes it great for the real world
  • 249. Basic architecture patterns
  • 250. Supervisor SP P P S P P
  • 251. Supervisor SP P P S P P
  • 252. Supervisor spawns processes and handles its death
  • 253. gen_serverPP gsP
  • 254. gen_serverPP gsP
  • 255. Generic single threaded server
  • 256. gen_fsmPP gf{S}P
  • 257. gen_fsmPP gf{S}P
  • 258. Generic Finite State Machine
  • 259. gen_event PP P emP P P
  • 260. gen_event PP P emP P P
  • 261. Event handler, you can subscribe to events
  • 262. More on OTP...• File hierarchy• Applications• Releases• Basic abstraction libraries (logging, system...)
  • 263. Let’s see a gen_server in action!
  • 264. IntroducingSimple Key Value Store
  • 265. A server that is able to store {Key,Value} and retrieve them by Key
  • 266. Demo II
  • 267. skvs diagram SP gs ets
  • 268. skvs diagram SP gs ets
  • 269. Starting skvsGo to skvs source directoryCompile./rebar compileStart the Erlang nodeerl -pa ebinStart the appapplication:start(skvs).
  • 270. Some commandsSet a value skvs_server:set(“Key”,”Value”). skvs_server:set(“Key2”,”Value2”).Get a value skvs_server:get(“Key2”).
  • 271. Code on client: skvs_server.erlstart_link() - gen_server:start_link({local, skvs},skvs_server, [], []).get(Key) - gen_server:call(skvs, {get, Key}).set(Key,Value) - gen_server:call(skvs, {set, Key, Value}).set_fire_and_forget(Key,Value) - gen_server:cast(skvs, {set, Key, Value}).
  • 272. Code on client: skvs_server.erl start
  • 273.  gen_serverstart_link() - gen_server:start_link({local, skvs},skvs_server, [], []).get(Key) - gen_server:call(skvs, {get, Key}).set(Key,Value) - gen_server:call(skvs, {set, Key, Value}).set_fire_and_forget(Key,Value) - gen_server:cast(skvs, {set, Key, Value}).
  • 274. Code on client: skvs_server.erl start
  • 275.  gen_serverstart_link() - gen_server:start_link({local, skvs},skvs_server, [], []).get(Key) - gen_server:call(skvs, {get, Key}). sync
  • 276.  call
  • 277.  to
  • 278.  serverset(Key,Value) - (waits
  • 279.  for
  • 280.  reply) gen_server:call(skvs, {set, Key, Value}).set_fire_and_forget(Key,Value) - gen_server:cast(skvs, {set, Key, Value}).
  • 281. Code on client: skvs_server.erl start
  • 282.  gen_serverstart_link() - gen_server:start_link({local, skvs},skvs_server, [], []).get(Key) - gen_server:call(skvs, {get, Key}). sync
  • 283.  call
  • 284.  to
  • 285.  serverset(Key,Value) - (waits
  • 286.  for
  • 287.  reply) gen_server:call(skvs, {set, Key, Value}).set_fire_and_forget(Key,Value) - gen_server:cast(skvs, {set, Key, Value}). async
  • 288.  call
  • 289.  to
  • 290.  server (fire
  • 291.  
  • 292.  forget)
  • 293. Callback code: skvs_server.erl%% Client Callbacks on server%% Sync calls, expects responsehandle_call({set, Key, Value}, _From,State) - {reply, set_value(Key,Value), State};handle_call({get, Key}, _From, State) - {reply, get_value(Key), State}.%% Async calls, do not expect responsehandle_cast({set, Key, Value}, State) - set_value(Key,Value), {noreply, State}.
  • 294. Callback code: skvs_server.erl%% Client Callbacks on server%% Sync calls, expects responsehandle_call({set, Key, Value}, _From,State) - {reply, set_value(Key,Value), State};handle_call({get, Key}, _From, State) - {reply, get_value(Key), State}.%% Async calls, do not expect responsehandle_cast({set, Key, Value}, State) - set_value(Key,Value), {noreply, State}. return
  • 295.  value
  • 296. Callback code: skvs_server.erl%% Client Callbacks on server%% Sync calls, expects responsehandle_call({set, Key, Value}, _From,State) - {reply, set_value(Key,Value), State};handle_call({get, Key}, _From, State) - {reply, get_value(Key), State}.%% Async calls, do not expect responsehandle_cast({set, Key, Value}, State) - set_value(Key,Value), {noreply, State}. Survives
  • 297.  between
  • 298.   return
  • 299.  value requests
  • 300. Actual work...: skvs_server.erl%% Private functionsset_value(Key, Value) - % Insert Key, Value into ETS. % Fail early {ok,Result}=case ets:insert(data_store,{Key,Value}) of true - {ok, {Key,Value}}; {error, Reason} - {error, Reason} end, Result.
  • 301. Actual work...: skvs_server.erl%% Private functionsset_value(Key, Value) - % Insert Key, Value into ETS. % Fail early {ok,Result}=case ets:insert(data_store,{Key,Value}) of true - {ok, {Key,Value}}; {error, Reason} - {error, Reason} end, ETS
  • 302.  is
  • 303.  a
  • 304.  Key
  • 305.  value
  • 306.  store
  • 307.   Result. in
  • 308.  RAM
  • 309. Use cases
  • 310. Gaming Industry
  • 311. Gaming Industry
  • 312. Messaging / IM
  • 313. Messaging / IM
  • 314. Payments
  • 315. Payments
  • 316. Databases
  • 317. Databases
  • 318. Others
  • 319. Others
  • 320. Some day...
  • 321. Every highly interactive website? ;)
  • 322. Pay Hello Ouch! Every highly interactive website? ;) Would you... HeyNO! ! Hi Buf
  • 323. Internet of things
  • 324. And now what?
  • 325. Install Erlang/OTP!https://www.erlang-solutions.com/downloads/download-erlang-otp
  • 326. Use a great IDE• IntelliJ + Erlang (plugin)• Sublime Text + SublimErl• Emacs
  • 327. Read some books
  • 328. RTFM• http://www.erlang.org/doc.html• http://erldocs.com/
  • 329. Join mailing lists• Official Erlang mailing list • http://erlang.org/mailman/listinfo/erlang- questions• Join Barcelona Erlang user group on google groups
  • 330. Find know-how• Check http://www.erlang-factory.com/ videos• Search for Erlang conferences on slideshare• Pray for an http://erlangcamp.com/ near your city
  • 331. Read some code
  • 332. This is the result of 6 months of fun learning Erlang/OTP
  • 333. We are looking for awesome projects http://es.linkedin.com/in/jvalduvieco http://es.linkedin.com/in/jllonch
  • 334. Thank you!https://github.com/jvalduvieco/betabeers_201303
  • 335. Thank you! https://github.com/jvalduvieco/betabeers_201303we
  • 336.  hope
  • 337.  you
  • 338.  feel
  • 339.   like
  • 340.  him
  • 341.  :) (we
  • 342.  did)
  • 343. Erlang, The Moviehttp://www.youtube.com/watch?v=uKfKtXYLG78
  • 344. Images sourceshttp://diromero.wordpress.com/2011/10/07/home-automation/http://iffergan.com/http://technode.com/2012/05/14/internet-of-things-not-just-a-concept-for-fund-raising/http://modernherstory.wordpress.com/2011/05/18/open-minds-open-hearts/http://www.i-m.co/eviemouse/problemsolved/http://strategicsalesmarketingosmg.wordpress.com/2012/06/25/who-are-twitter-users/http://www.secureworks.com/it_security_services/advantage/soc/http://labourlist.org/2011/11/my-biggest-worry-about-labour%E2%80%99s-future/broken-chain/http://prothemedesign.com/how-to/why-you-should-keep-wordpress-and-your-plugins-up-to-date/attachment/matrix-code-update/http://thegapbetweentwoworlds.com/category/career-transitionhttp://www.wingweb.co.uk/Images/697/KC-10_Extender_SR-71_Blackbirdhttp://www.tonywoodtraining.com/2010/11/29/motivation-to-exercise-and-eat-right/http://portalmie.com/blog/6/2011/09/kushimoto-diving-park-tokushima-revista-no-7/http://www.bcsagroup.com/http://www.losemyaccent.com/tag/hand/http://articulo.mercadolibre.com.ar/MLA-450970296-cortaplumas-victorinox-huntsman-15-usos-navaja-origen-suizo-_JMhttp://openclipart.org/detail/4112/http://blog-en.coaching-go.com/2012/02/learn-to-say-no-and-know-how-to-say-yes/ok-2/http://en.wikipedia.org/wiki/File:We_Can_Do_It!.jpghttp://triniblog.wordpress.com/tag/laberintos/http://www.catskillsnyrealestate.com/Want-More-Info-Catskill-Real-Estate.phphttp://www.thelovelyplanet.net/lush-green-images-planet-earth/http://www.growthmax.com/how-exactly-does-growthmax-plus-make-you-grow-taller/http://www.ayurvedalive.in/keep-your-eyes-healthy-beautifulhttp://my.mmosite.com/510619/blog/item/lord_of_the_rings_online.htmlhttp://scifistorm.org/2010/03/24/lord-of-the-rings-blu-ray-review/http://www.123rf.com/photo_15612022_3d-cartoon-bug.htmlhttp://www.airliners.net/photo/1134244/L/http://www.muypymes.com/2009/12/04/moleskine-mucho-mas-que-una-agenda-personalhttp://learnyousomeerlang.com/the-hitchhikers-guide-to-concurrencyhttp://planetpooks.com/one-more-reason-to-love-hermiones-handbag/