ElixirConf 2023 - Isaac Yonemoto - State: A Necessary Evil

Learn how to handle state in Elixir with confidence, avoiding common pitfalls and misuses, from ElixirConf 2023 talk "State: A Necessary Evil".

Key takeaways
  • State is a necessary evil in programming, but it’s often misunderstood and misused.
  • Beam languages, including Elixir, are not designed to handle mutable state.
  • Mutation can lead to unintended consequences, such as unhandled messages, process crashes, and data corruption.
  • Persistent term, a high-level abstraction, is a safe way to store state.
  • ETS (Erlang Term Storage) is a highly optimized tuple storage engine, but it’s not a good fit for storing state.
  • Gen servers and agents are built on top of ets, but they’re not designed for stateful interaction with the outside world.
  • Atomic operations are a low-level abstraction that can be used for safe concurrent access, but they’re not suitable for most use cases.
  • Sockets, including gen TCP and gen UDP, are used for network communication, but they can be error-prone and require careful handling.
  • NIF (Native Implemented Function) code is C-tier, as it’s difficult to use and prone to errors.
  • Process dictionary is a local KV storage for each process, but it’s not designed for concurrent access.
  • Persistent term tables are a way to store state, but they’re not recommended for production use.