Erlang / Erlang Basics Interview questions
Which is better for state management, ETS or process dictionaries, and why?
A process dictionary (put/2, get/1) is per-process mutable key-value
storage that bypasses Erlang's usual immutability, private to that one process only. ETS is a separate
in-memory table that can be shared and accessed concurrently across many processes.
ETS is generally the better choice whenever more than one process needs the data, since the process
dictionary is invisible outside its owning process and can't be shared without message passing anyway. Even
within a single process, most experienced Erlang developers avoid the process dictionary except for narrow
cases (some tracing/debugging helpers, or caching a value purely local to that process's own logic), because
it silently breaks the functional, traceable style the rest of the language encourages — a function
reading get(some_key) has a hidden dependency that isn't visible in its arguments.
So: ETS for anything shared or sizeable; process dictionary only for small, genuinely process-local state where passing it explicitly through function arguments would be needlessly awkward.
More Related questions...