Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Thinking in Actors

Thinking in Actors

Using Erlang style actors to implement a highly concurrent system. Presented on WebCampZg (http://2014.webcampzg.org/).

78d85ea26770975ea8c497d7bd722d55?s=128

Saša Jurić

October 04, 2014
Tweet

Transcript

  1. None
  2. Thinking in Actors @sasajuric

  3. None
  4. None
  5. CPU CPU CPU CPU CPU CPU CPU CPU

  6. bank account balance http request socket database connection

  7. “deposit”, 300 “store”, account_id, ... http request bank account database

    ok ok
  8. bank account balance “deposit”, 300 “withdraw”, 200 “balance”

  9. defmodule BankAccount do use ExActor.GenServer definit do initial_state(0) end defcast

    deposit(amount), state: balance do new_state(balance + amount) end defcast deposit(amount), state: balance do if amount <= balance do new_state(balance – amount) else new_state(balance) end end defcall balance, state: balance do reply(balance) end end
  10. account = find_account(id) BankAccount.withdraw(account,200) account = find_account(id) BankAccount.deposit(account,300) account =

    find_account(id) balance = BankAccount.balance(account) client 1 client 2 client 3 account = BankAccount.start
  11. http http bg job http clients accounts database

  12. > 30 active events > 5,000 simultaneous players > 2,000

    requests/sec
  13. 1.accept TCP packets 2.assemble XML messages 3.update the model 4.store

    to database 5.serialize the model 6.dispatch
  14. tcp listener xml assembler model builder serializer persister dispatcher

  15. tcp listener xml assembler model builder serializer persister dispatcher message

    6 message 5 message 4 message 3 message 2 message 1
  16. tcp listener xml assembler model builder serializer persister dispatcher

  17. persister tcp listener xml assembler model builder serializer dispatcher

  18. <message> <match id=”1” ...> ... </match> <match id=”2” ...> ...

    </match> ... <match id=”n” ...> ... </match> </message>
  19. i model builder serializer dispatcher match 1 match 2 match

    3 match 4 match 5
  20. i

  21. i match 1 match 2 match 3 match 4 match

    5
  22. player match http request subscription notification query new messages

  23. http://www.manning.com/juric/ wcp14twcf