Prev Next

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.

What is the core structural idea behind gen_event?
When is gen_event a better fit than gen_server?

Invest now in Acorns!!! 🚀 Join Acorns and get your $5 bonus!
Acorns Logo

Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!

Earn passively and while sleeping

Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.

Robinhood Logo

Invest now!!! Get Free equity stock (US, UK only)!

Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.

The Robinhood app makes it easy to trade stocks, crypto and more.


Webull Logo

Webull! Receive free stock by signing up using the link: Webull signup.

More Related questions...

What is the difference between linking and monitoring a process? Why would you choose monitor over link for a client process? What is a dirty scheduler and when should a NIF use one? Explain the internal working of Erlang's per-process garbage collector? Why does Erlang use generational (per-process) garbage collection instead of a single global GC? What is EPMD and what role does it play in distributed Erlang? Why do distributed Erlang nodes require a shared cookie? Explain the internal working of the Erlang distribution handshake between two nodes? What is the two-version code loading rule and what happens when a third version is loaded? Explain the internal working of code:purge/1 and code:soft_purge/1? What is a gen_event and when would you choose it over gen_server? What is the difference between error, exit, and throw in Erlang? Why is exit/1 different from exit/2? What is an OTP application (.app file) and how does it differ from a single module? Explain the execution flow of application:start/1 and its dependency resolution? What is a release in OTP and how does it differ from an application? Explain the internal working of a relup-based hot upgrade? What is the code_change/3 callback used for in gen_server? How does the global module handle process name registration across a cluster? Why can global name registration cause a network partition ("split brain") problem? What is the pg (process groups) module used for? What are Erlang maps and how do they differ from records? When should you choose a map over a record for structured data? What is the difference between ets:match, ets:select, and ets:foldl? How can you use match specifications to filter ETS data efficiently? What do the write_concurrency and read_concurrency ETS options optimize for? How can you optimize binary pattern matching for parsing variable-length network protocols? Why is copying a large list between processes more expensive than copying a large binary? What is tail call optimization in Erlang and why does it matter for long-running loops? How do you write a properly tail-recursive accumulator-based function? Explain the internal working of selective receive and why message order in the queue matters? What is erlang:process_flag(priority,...) used for? Why should high-priority processes be used sparingly in Erlang? What is rpc:call/4 and how does it work across distributed nodes? What is the difference between rpc:call and simply sending a message to a remote PID? How does erlang:send/3 with the nosuspend option change message-sending behavior? What is Dialyzer and how does success typing differ from static typing? Why doesn't Dialyzer catch every type error the way a traditional type checker would? How do you define and use a custom behavior with the -callback attribute? What is the difference between a behavior callback module and a plain library module? Explain the internal working of exception propagation through nested try/catch blocks? When should you use throw instead of returning an {error, Reason} tuple? How do you troubleshoot a memory leak caused by large binaries not being garbage collected? What is the significance of the binary reference count and off-heap binary garbage collection? Why is the Erlang distribution protocol not encrypted by default, and how can you secure inter-node traffic? Why is Mnesia's two-phase commit necessary for distributed transactions? How do you troubleshoot slow ETS lookups on a heavily-used table? Why might increasing the number of BEAM schedulers not improve throughput linearly?
Show more question and Answers...

#

Comments & Discussions