Build a stateful service with Erlang/OTP

8 steps · about 4 h 40 min · Intermediate

  1. Model requests and state with Erlang terms (reference, Erlang documentation, 30 min)

    Tuples, maps, lists and atoms provide a compact vocabulary for messages and service state.

  2. Validate messages with patterns and guards (reference, Erlang documentation, 40 min)

    Function clauses and guards reject malformed requests before they reach state changes.

  3. Expose a small module interface (reference, Erlang documentation, 25 min)

    An explicit export list separates the client API from callback implementation details.

  4. Document request and reply contracts (reference, Erlang documentation, 30 min)

    Type specifications make expected message shapes visible to readers and static analysis.

  5. Understand process links and messages (reference, Erlang documentation, 35 min)

    Process identity, mailboxes and exit signals explain how the service communicates and fails.

  6. Implement state with the gen_server behaviour (guide, Erlang documentation, 50 min)

    Standard callbacks define synchronous calls, asynchronous casts and state transitions consistently.

  7. Report and classify service failures (reference, Erlang documentation, 30 min)

    Error classes and stack traces distinguish invalid requests from unexpected process defects.

  8. Test calls, casts and invalid requests with EUnit (guide, Erlang EUnit documentation, 40 min)

    Focused tests make the server's reply and state-transition contract repeatable.