Elixir Phoenix

38
Phoenix a Rails successor INTRODUCTION TO ELIXIR AND ITS POWERFUL WEB FRAMEWORK Tanuj Soni | [email protected] 1

Transcript of Elixir Phoenix

Page 1: Elixir Phoenix

Phoenix a Rails successorINTRODUCTION TO ELIXIR AND ITS POWERFUL WEB FRAMEWORK

Tanuj Soni | [email protected]

1

Page 2: Elixir Phoenix

What is Phoenix?

It’s a web framework that is written in Elixir programing language and runs on Erlang virtual machine.

2

Page 3: Elixir Phoenix

How does it came to existence

Chris Mccord (the creator of Phoenix) was working on a realtime application in which clients had to open long connections with the server and needed to have data flowing full duplex mode.And developing scalable realtime applications was hard because the clients will maintain long connections with server and will block the resources of the server till the connection ends.While he was trying to figure out the problem he heard WhatsApp case. WhatsApp guys were maintaining 2 Million connections on a single machine.

Blog post describing the case..http://blog.whatsapp.com/index.php/2012/01/1-million-is-so-2011/

3

Page 4: Elixir Phoenix

WhatsApp Server configuration

Intel(R) Xeon(R) CPU X5675 @ 3.07GHz 24 CPU Cores96 GB RAM

Only 40% of RAM and CPU were utilized…..

4

Page 5: Elixir Phoenix

● It was designed by Ericsson for its telecommunication systems in 1986.

● It’s Distributed, Fault-tolerant and Hot swappable language. These features met all telecommunication requirements.

5

Page 6: Elixir Phoenix

● Elixir is a functional, concurrent, general-purpose programming language that runs on the Erlang virtual machine (BEAM).

● José Valim is the creator of the Elixir programming language, a R&D project of Plataformatec. His goals were to enable higher extensibility and productivity in the Erlang VM while keeping compatibility with Erlang's tools and ecosystem.

● José added a lot of syntactic sugar to make look similar to Ruby.

Ref- https://en.wikipedia.org/wiki/Elixir_(programming_language)

6

Page 7: Elixir Phoenix

7

Page 8: Elixir Phoenix

8

Page 9: Elixir Phoenix

9

Page 10: Elixir Phoenix

10

Page 11: Elixir Phoenix

What is new in that?Isn't that’s similar to Node or EM Websocket Ruby?

11

Page 12: Elixir Phoenix

The difference is about scaling!

● The clock speed of newer CPUs are not getting faster instead they having multiple cores.

● Ruby and Node both are not able to utilize the resources in a way these are utilized by Elixir.

● As we have seen the WhatsApp case we know the Elixir can scale vertically and it scales very well.

12

Page 13: Elixir Phoenix

So what happens if we can not further scale vertically?

13

Page 14: Elixir Phoenix

14

Page 15: Elixir Phoenix

We start scaling it horizontally

● If a single powerful machine is not capable, we can simply turn on another machine and tell this new machine how to find other machines.

● Erlang is distributed so is Elixir as the system is designed around message passing, the Erlang VM abstracts the message passing in a such a way that it doesn’t matter if the recipient of the message is in the same machine or in another node in the same network.

15

Page 16: Elixir Phoenix

Scaling vertically by adding more powerful hardware was a thing of the past. We scaled horizontally, by adding more commodity hardware. With the coming of age of mega-core architectures, we have the choice of either adding more hardware or more cores, or both. Erlang style concurrency puts Elixir/Phoenix ahead of the game when it comes to scaling with both approaches.

16

Page 17: Elixir Phoenix

17

Page 18: Elixir Phoenix

18

Page 19: Elixir Phoenix

● Whenever a socket is initiated a new process is created to handle it. Don’t worry the are Erlang virtual machine processes not operating system processes.

● And for every channel that is created within this channel separate processes are created.

● Erlang VM processes are very isolated that means a buggy or faulty channel will not affect any other thing in the system.

● All the processes are concurrent so none of the process is going to block anything in the system.

19

Page 20: Elixir Phoenix

Problem with Node

Despite its asynchronous event model, by nature it is single threaded. When you launch a Node process, you are running a single process with a single thread on a single core. So your code will not be executed in parallel, only I/O operations are parallel because they are executed asynchronous. As such, long running CPU tasks will block the whole server and are usually a bad way to do things.

20

Page 21: Elixir Phoenix

Problem with Rails 5 ActionCable

One of Rails screencast suggested that “You should not do any processing in action cable channels because they are not concurrent , if you do they will block everything.” and that is true.What Rails actually does is that it puts the actions in a queue after some time it will be fetched, processed and broadcasted afterwards.

21

Page 22: Elixir Phoenix

This was a brief introduction about how it came to existence.

22

Page 23: Elixir Phoenix

With Elixir Phoenix we can be as much productive as we were with Rails best part is that the developed things are going to be multiple times efficient.Let’s see other benchmarks and features of Phoenix.

23

Page 24: Elixir Phoenix

The Road to 2 Million Websocket Connections in Phoenix

● Phoenix guys started to benchmark the framework developed on a  128GB RAM and 40 cores.

● And they were able to open 2 Million connections using 80 GB of RAM. CPUs were ideal as none of the data was flowing.

● They broadcasted a Wikipedia article to 2 million connection and it took only 3 seconds.

Ref-http://www.phoenixframework.org/blog/the-road-to-2-million-websocket-connections

24

Page 25: Elixir Phoenix

25

Page 26: Elixir Phoenix

26

Page 27: Elixir Phoenix

How regular applications benefit from Elixir Phoenix?

Phoenix allocates a separate dedicated Erlang VM process for every http request and serves them concurrently.

27

Page 28: Elixir Phoenix

28

Page 29: Elixir Phoenix

● Data for each request is isolated.● Garbage Collector is per process based. ● Most of the time it is not required to Garbage collect as once the

request is served the data is scraped, no GC is required and this also saves the CPU cycles.

● Node, Ruby, Python have global GCs and when the servers starts getting heavy load the GC starts running very frequently that increases the response time of requests.

29

Page 30: Elixir Phoenix

● Ref- https://github.com/mroth/phoenix-showdown

30

Page 31: Elixir Phoenix

Productivity

➢Short-term Productivity- Documentation/Guides- Workflows/Generators

➢Long-term Productivity- Introspection- Maintainability

31

Page 32: Elixir Phoenix

32

Page 33: Elixir Phoenix

Generators

mix phoenix.gen.htmlmix phoenix.gen.jsonmix phoenix.gen.channel

These generates highly documented code.

33

Page 34: Elixir Phoenix

Some more development addons.

● Live Reloads.● ES6 as default.● Pretty error pages.● Concurrency test tools.● (HEX) Packages and dependency resolution tool.● Uses all cores for compiling code.● Uses all cores to run tests.● Debuggers: IEx.pry, Debugger(GUI).

34

Page 35: Elixir Phoenix

35

Page 36: Elixir Phoenix

Observer Tool

Observer allows us to open multiple windows with information on the whole system's statistics or even an individual process of that running system.It makes it easy to identify the bottlenecks of the application just by looking in to the stats of resources consumed by multiple processes.We can simulate an error by manually killing process or by killing DB connection pools.

36

Page 37: Elixir Phoenix

Related links

● https://github.com/h4cc/awesome-elixir● https://hex.pm/● https://github.com/doomspork/elixir-companies● http://elixir-lang.org/docs.html● https://hexdocs.pm/phoenix/Phoenix.html

37

Page 38: Elixir Phoenix

Tanuj [email protected]://www.linkedin.com/in/tanujsoni

38

Contact me on the below details for any question or suggestion.