Top 10 Elixir scenario questions
Try each one yourself first, then open the model answer.
1. A worker process crashes occasionally. Should you wrap everything in try/rescue?
Show answerHide answer
Usually not. Put it under a supervisor with a suitable restart strategy and let it restart in a clean state, handling expected errors with tagged tuples.
2. A single GenServer becomes a bottleneck. What would you do?
Show answerHide answer
Move slow work out of the server process, partition across several processes, or use ETS for frequently read data.
3. A process mailbox grows without limit. Why is that a problem and what helps?
Show answerHide answer
Memory grows and latency rises. Apply back-pressure, for example with calls instead of casts, or GenStage or Broadway pipelines.
4. A GenServer.call times out under load. What do you check?
Show answerHide answer
Whether the callback does slow work, calls itself or waits on another busy process. Reply later or use a task for slow work.
5. Which supervision strategy suits processes that depend on each other?
Show answerHide answer
rest_for_one or one_for_all, depending on which processes must restart together. one_for_one suits independent workers.
6. You need to run many independent requests concurrently with a limit. How?
Show answerHide answer
Task.async_stream with max_concurrency and a timeout.
7. Creating atoms from user input is risky. Why?
Show answerHide answer
Atoms are not garbage collected and the table has a limit. Keep input as strings or use String.to_existing_atom.
8. Code has deeply nested case statements for successive steps. How can it be cleaner?
Show answerHide answer
Use a with expression and return consistent ok and error tuples.
9. State is lost when a process restarts. How do you handle important data?
Show answerHide answer
Persist it in a database or another durable store and load it on start. Process memory is not durable.
10. How would you find why a production node is using too much memory?
Show answerHide answer
Inspect processes with observer or recon, check mailbox sizes and ETS tables, and look for large binaries being retained.
