GenServer

Get introduced to GenServers, which are usually considered the fundamental abstraction in OTP, and learn how to test GenServers.

A GenServer is a process that holds state and optionally provides an interface to read or update that state.

OTP abstracts all of the common parts of a GenServer, like spawning the process, having a receive loop, and sending messages. What’s left to the user is to implement callbacks that implement code specific to a particular GenServer.

Initializing a GenServer

We’ll see how we can use a GenServer in the Soggy Waffle application, but let’s start with a self-contained example, which will make things easier when illustrating some concepts.

We’ll use a GenServer that provides a simple rolling average of the last N numeric measurements. We can start with a simple implementation by looking at the public API exposed by the GenServer:

Get hands-on with 1400+ tech skills courses.