Erlang / Erlang Advanced Interview questions
What is a gen_event and when would you choose it over gen_server?
gen_event is the OTP behavior for a pub/sub-style event manager: one process holds a list of
independent handler callback modules, and any event sent to the manager is dispatched in turn to every
registered handler, each with its own private state.
{ok, Pid} = gen_event:start_link(), gen_event:add_handler(Pid, logger_handler, []), gen_event:add_handler(Pid, metrics_handler, []), gen_event:notify(Pid, {order_placed, OrderId}).
Choose gen_event when you have one stream of events that multiple, independently pluggable
consumers need to react to — logging, metrics, and alerting handlers all reacting to the same application
events, added and removed at runtime without touching the code that raises the events. A plain
gen_server is the better fit when there's a single consumer with one coherent piece of state,
rather than a fan-out to many independent listeners.
More Related questions...