💧 Elixir Beginner

What is the difference between send/receive and GenServer in Elixir?

Answer

send/receive is the raw, low-level mechanism for inter-process communication. Any process can send any message to any PID: send(pid, {:hello, "world"}); receive do {:hello, msg} -> msg end. You manually handle the message loop, timeouts, and unexpected messages. GenServer is an OTP behaviour built on top of send/receive that provides: a standardized message protocol, automatic handling of timeouts and unexpected messages (via handle_info), synchronous calls (call) vs async casts (cast), process registration and supervision integration, tracing and debugging support, and graceful shutdown via terminate callback. When to use send/receive: short-lived tasks (via Task), fire-and-forget scenarios, or learning/experimentation. When to use GenServer: any production stateful process — GenServer provides the boilerplate and guarantees that raw messaging lacks. Most Elixir production code uses GenServer over manual message passing.