SoFunction
Updated on 2025-04-08

Introduction to OTP in Erlang

OTP includes a set of libraries and implementation methods, which can build large-scale, fault-tolerant and distributed applications, and includes many powerful tools that can implement H248, SNMP and other protocols. The core concept is OTP behavior, which can be regarded as an application framework that uses callback functions as parameters, similar to a J2EE container. The behavior is responsible for solving the non-functional part of the problem, and the callback function is responsible for solving the function part.
 
Through the gen_server module, the semantics and hot code exchange can be realized.

1)
2)       Writing interface functions
3) Write 6 required callback functions in the callback module

When the server crashes, a mechanism is needed to detect and restart it, using a monitoring tree, that is, to create a monitor to manage the server. There are two types of monitoring trees: one-to-one and one-to-many.

Copy the codeThe code is as follows:

$erl –boot start_sasl

An environment that runs the production system will be created, and the system architecture support library (SASL, System Administration Support Libriaries) will be responsible for error logging and overload protection.
 
Using gen_server, gen_supervisor, application and other behaviors, a system with reliability of 99.9999999 can be built.
Unified erlang message:

1) Abstract the difference between different line protocols
2) Erlang messages do not need to be parsed, and the receiving process does not have to parse the message before processing, while the http server must parse all messages received
3) Erlang messages can contain data types of any complexity, while http messages must be serialized to flattened before they can be transmitted.
4) Erlang messages can be delivered between different processors

Common third-party libraries include rebar (/basho/rebar) and cowboy(/extend/cowboy). Rebar is the de facto standard for managing erlang projects. Users can create new projects, compile projects, package them, and integrate them with other projects while integrating github. Cowboy is a high-performance web server written in erlang and is a popular implementation of embedded web. In addition, library mochiweb2(/mochi/mochiweb) encoding and decoding methods can realize the mutual conversion of json strings and erlang data types.
 
Erlang program runs on multi-core CPU

1) Use a large number of processes
2) Avoid side effects, such as not using shared ETS or DETS
3) To avoid sequential bottlenecks, you can choose pmap instead of map
4) Small messages, big calculations
5) Use mapreduce to parallelize calculations
mapreaduce is a parallel higher-order function, defined as follows

Copy the codeThe code is as follows:

-specmapreduce(F1,F2,Acc0,L) ->Acc
         F1 = fun(Pid,X) ->void
         F2 = fun(Key,[Value],Acc0) ->Acc
         L = [X]
         Acc = X =term()

Mapreduce is defined in the parallel higher-order function (phofs) module.