Erlang / Erlang Advanced Interview questions
What is a dirty scheduler and when should a NIF use one?
A regular NIF runs inline on a normal BEAM scheduler thread, which is fine for short operations but dangerous for anything that blocks or takes more than roughly a millisecond, since it stalls that entire scheduler and every process waiting behind it. Dirty schedulers are a separate pool of OS threads reserved for long-running native work, so it doesn't compete with the normal schedulers running lightweight Erlang processes.
There are two dirty scheduler pools:
- Dirty CPU schedulers — for CPU-intensive native computation (heavy math, compression).
- Dirty I/O schedulers — for blocking I/O operations (file access, slow syscalls).
A NIF opts in by declaring itself with ERL_NIF_DIRTY_JOB_CPU_BOUND or
ERL_NIF_DIRTY_JOB_IO_BOUND in its registration table, and the runtime routes calls to that function
onto the appropriate dirty pool instead of a regular scheduler.
More Related questions...