This article talk about the recent hot Elixir, and say, at the choice of the programming language view. At the same time as the Erlang enthusiast, Elixir must be mentioned. Even with so many programming languages Elixir also worth contacting.
Elixir is not a recent language. But recent Eco Elixir of gradual improvement, more and more experts are beginning to focus on the language, and give praise Elixir.
Come now Elixir
A small example of Elixir
Parallel processing JSON string input, and parses into usable variables per second speed calculation processing and output.
Elixir with entop monitor application status
Programming language selection view point
Behind the different language groups are very different style library, technology stack, ecological and tool chain. Different languages for different types of problems. Some languages solve some problems of the cost will be much lower than in other languages. Return to nature, to learn a programming language or a cost-efficient solution for real-world business problems.
Personal favorite programming language style
Near real-time updates can change
The best do not need a long time to execute compiled applications fast start.
Java, C compiler is very slow, not suitable for frequent changes of the project. But PHP, Node.js modification can see, you can greatly improve development efficiency. It would also be hot-reload as many front-end tools, as long as there is little to change the source code, do not need to refresh the page automatically reflected in the browser. Play framework similar function can also be loaded automatically.
Furthermore, it is possible to load the better the heat in a production environment, update the code does not affect users. On this point, many people laughed, PHP is such ah default, after deployment to refresh the APC cache can be achieved.
This is stateless, the advantages of short links HTTP applications, though followed by a more performance relative decrease TCP overhead, but the problem becomes much simpler. But it is difficult to do in many other languages, such as most Java applications.
Elixir, Erlang can be done under any circumstances driving a sports car really change the wheel.
On heat load, see another article: Thermal loading commonly used programming development tools.
It supports concurrent execution
People are more accustomed to the idea of order execution, and most of the business logic is executed sequentially. But in order to reduce latency and improve performance, it is best able to support concurrent execution at the language level. For example, an operator returns the result has not yet started, you can begin another operation.
Such remote API calls or remote RPC, the slowest time consuming that a time-consuming operation. From this point of view, most of the popular languages can do concurrent calls, but PHP is difficult to achieve.
Lightweight execution processes or threads
With certain limitations, some business logic inevitably because a lot of computing, networking, disk IO and so occupied perform a process or thread. I hope this body can perform as much as possible lightweight, small memory footprint, fast startup time, consume little switch, preferably at the time of execution of the IO automatically give up computing resources.
Concurrency and parallelism
We pay more attention to concurrent, parallel but less attention. Because by increasing the number of machines withstood a large number of user requests simpler and more urgent than saving machine.
Which is a lot of Internet companies frequently present situation of hundreds of thousands of servers. Number of users and the volume of requests is difficult to compare because of the different business logic, we can only compare the number of machines.
The complicated process model
PHP is both typical of this model. I have seen an asynchronous PHP framework CS remains high, even more than business logic CPU usage higher.
Threading model of concurrency
This model with respect to the process model is a lot better than many threads as lightweight process, create, switching is also much faster.
Question: Relationship between threads and kernel threads for many-kernel threads is limited. Finite scheduling user threads can not take full advantage of multi-core performance. Create a new thread consumption is very large. IO blocking not release computing resources.
Each CPU core can run a thread, we need to switch the scheduler (CS) between multiple threads. If it is CPU-intensive type of computing, with little or no IO operation, it is best to start CPU core number of threads.
But if there are IO operations, such as disk or network, the extra CPU core number of threads effective because when the IO operation can be switched to a different thread of execution CPU operation.
Concurrent Fork-join the lightweight process model:
Fork-join the process of creating your own pool to perform the task of a small particle size.
With respect to the Erlang that truly preemptive scheduling VM operating system or implement preemptive scheduling, Fork-join model is very simple, also means that a relatively low contrast efficiency.
Fork-join operation designed for compute-intensive, which means I can not tell F / J frame because you released IO wait a while computing resources. Therefore, the general need to be asynchronous IO operations into another thread pool, FJ deal only with pure calculation.
Scala based Akka both this model. So, if handled properly, Akka's Actor easily blocked thread of execution, if the thread execution thread pool is used up, the entire application will be dead in there. The Erlang is not the problem.
The Erlang concurrency lightweight process model:
VM dispatching thread will be calculated into a very small execution units. It can support a lot of process. IO blocked automatically release resources. Truly preemptive scheduling.
Static type can avoid a lot of mistakes. Dynamic type often appear unpredictable results, which is contrary to the principle of Least Surprise UNIX style.
Dynamic type lets developers more quickly. Strong static type system performs quickly, such as Java, but can also be used in reflection when necessary, such as many RPC framework implementation (of course there are further bytecode modification technique).
Each language's type system has its own characteristics.
Built Structure rich or containers
Better able to distinguish between Interface, Struct and Implementation. A relatively uniform pattern can easily define the structure they need.
Unless Erlang unparalleled lightweight thread-level GC. Otherwise, you will either need to remember and understand complex GC tuning parameters, or the like PHP process that over time will kill again.
DSL and metaprogramming scalability
In the syntax level of abstraction and encapsulation can enhance the development efficiency. Elixr how to achieve DSL.
Execution speed and performance
This is closely related to concurrent and parallel mode, as well as multi-core utilization.
It simply is modular; the completion of each module relative to a single function, replication tasks completed by the combination of a plurality of modules. Project design is like building blocks. Different input and output modules can be spliced before.
The other is the minimalist style.
Dependency and database management systems
This is the best Node.js npm dependency management system, and this has led to a number of outbreaks Node.js community library. Because creating and publishing a library is too easy to find the required libraries are also very simple.
Greatly improve development efficiency.
Packaging and distribution system
Preferably packaged into a single file, easy to distribute and deploy. For example, Java applications packaged into execution everywhere Fat Jar package, or as Golang compiled into a single document.
Real-world projects, the log is very important. Previous articles have mentioned the importance of the log. So good built-logging system or a more unified and efficient log mode is very important.
The best support for screen printing and writing papers and more. This feature may not be considered a programming language, and this language is not to look good log library.
Java's SLF is a good log system libraries.
Build, compile, test tools more perfect.
Such as Java, maven Scala project, sbt. Erlang programs can use rebar, but Elixir of friendly mix many times.
Another good REPL command-line tool is very important because it can be easily invade application debug, or test a code fragment.
For example, the PHP php -a, sbt, Clojure's lein, Erlang's erl, Elixir of iex like.
This is a big advantage of scripting languages. Small task to create a script to execute immediately, without the need to modify, compile, deploy existing applications running.
This is very important for small tasks. Erlang and Elixir support this operation, escript or Elixir script. For example, connecting to a cluster, or a one-time reading state of data manipulation, and then disconnect.
Desirable to have a standard of comparison unit test model. Such as Java, Node.js, Scala, Elixir, and so on.
Having said that, back to Elixir. First, there is no difference Elixir implementation and Erlang. Erlang advantage Elixir fully equipped. For example: True preemptive scheduling; take full advantage of multi-core parallel execution; Actor model; monitoring tree; transparent distributed;
Extremely high stability; heat update code deployment; functional programming; pattern matching; and so on. And many Erlang lower tool also can be used directly. For example entop.
Also Elixir more than Erlang benefit is more friendly syntax toolchain community. Before beginning to write a lot of writing Ruby Elixir, because they are closest to the syntax.
Elixir meta-programming (meta programming) and DSL
1. quote the code into AST, like LISP syntax.
quote do: 1 + 2
2. Perform quote expression
Code.eval_quoted (quote do: 1 + 2)
3. unquote quote used to refer to variables outside the scope of
number = 13
Macro.to_string (quote do: 11 + unquote (number))
Elixir mature tool chain
mix: Create a project, build tools
hex: can npm comparable dependency and database management systems https://hex.pm/
iex: similar to the erl Erlang is both EPRL application startup command
exunit: unit testing tools
Tip: (Erlang \ Elixir \ Akka need to be careful not to let one too many messages Actor backlog of Queue monitoring Queue length becomes the system bottleneck is necessary.)
Erlang, Elixir some useful tools and libraries
: Observer.start ()
More useful Elixir / Erlang libraries, such as library Web Phoenix Webframework, common MySQL, Redis, MongoDB library