Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Persistent Term

Persistent Term

Lighting talk given at Chicago Elixir 2019-01-23

Avatar for Clark Kampfe

Clark Kampfe

January 23, 2019
Tweet

Other Decks in Technology

Transcript

  1. Hello — Clark Kampfe — Fast Radius — 3D printing

    manufacturing — Phoenix, Nerves in production — Yes we're hiring persistent_term 2
  2. Problem — Global data — Big — Lots of concurrent

    access — Rarely changing persistent_term 3
  3. Solutions Elsewhere — Locks (C, Java, etc.) — Immutable persistent

    data structures (Clojure, etc) — Compile-time ownership enforcement (Rust) persistent_term 5
  4. Elixir/Erlang VM: The BEAM — Error handling with restarts/supervision —

    Concurrency story — Process GC is independent — Everything is copied (some exceptions) persistent_term 6
  5. GenServer defmodule Pterm.MapServer do use GenServer # PUBLIC ###################################### def

    start_link(args) do GenServer.start_link(__MODULE__, args, name: __MODULE__) end def get(pid) do GenServer.call(pid, :get, 30_000) end # CALLBACKS ###################################### def init(%{data: data} = _args) do {:ok, data} end def handle_call(:get, _from, state) do {:reply, state, state} end end ### Client {:ok, server} = Pterm.MapServer.start_link(%{data: %{a: 1, b: 2}}) Pterm.MapServer.get(server) #=> %{a: 1, b: 2} persistent_term 8
  6. GenServer — Like every other Erlang process — Easy, not

    fast — Idiomatic, battle-tested — Built in — Data is copied to caller: keyspace access patterns matter! — Process mailbox can bottleneck reads and writes persistent_term 9
  7. ETS - Erlang Term Storage hosts = %{...} :ets.new(:hosts, [{:read_concurrency,

    true}, :named_table]) :ets.insert(:hosts, {:hosts_data, big_hosts_data}) [{:hosts_data, data}] = :ets.lookup(:hosts, :hosts_data) persistent_term 10
  8. ETS - Erlang Term Storage — Basically, Redis — Big

    mutable hash table — Easy, fast — Built in — Not GC'd — Data is copied to caller: keyspace access patterns matter! persistent_term 11
  9. FastGlobal — Data is compiled into module constant — Easy,

    fast — Compilation slows down with data size — Kind of a hack — Updates require recompilation — External library — Data is not copied to caller persistent_term 13
  10. persistent_term — new in OTP 21.2 (December 2018) — Big

    mutable global hash table — Easy, fast — Not GC'd — Built in — Updates are very expensive — Best for few, larger data — Data is not copied to caller persistent_term 15
  11. persistent_term - Warning! Persistent terms is an advanced feature and

    is not a general replacement for ETS tables. Before using persistent terms, make sure to fully understand the consequence to system performance when updating or deleting persistent terms. — the OTP gods persistent_term 16
  12. persistent_term considered dangerous On term deletion or replacement... All processes

    in the system will be scheduled to run a scan of their heaps for the term that has been deleted. While such scan is relatively light-weight, if there are many processes, the system can become less responsive until all process have scanned their heaps. — the OTP gods persistent_term 17
  13. summary ### GenServer ETS FastGlobal persistent_term difficulty easy easy easy

    easy 1 read perf medium fast fast very fast 1 write perf medium fast very slow very slow many read perf slow fast fast very fast many write perf slow fast very slow very slow builtin? yes yes no yes lookup anything hashtable hashtable hashtable distributed can be no no no persistent_term 24