dimamik

dimamik

Vault - a lightweight process-scoped global data storage with immutability guarantees

Vault is a lightweight Elixir library for immutable data storage within a process subtree.

Due to Elixir’s actor model nature, it’s common for a process to have global context that is valid for every function call inside the process and its children.

For example, this context can include:

  • A user when processing a request
  • A tenant in a multi-tenant application
  • Rate limiting buckets/quotas
  • Cache namespaces
  • API or client versions
  • And many more, depending on your application domain

Vault.init/1 provides you a guarantee that the context can only be defined once per existing process subtree, so you won’t override it by accident. This makes it easy to reason about your context origination.

# Initialize vault in parent process
Vault.init(current_user: %{id: 1, first_name: "Alice", role: "admin"})

# Access data from any descendant process, even these not linked!
spawn(fn ->
  Vault.get(:current_user) # => %{id: 1, first_name: "Alice", role: "admin"}

  Vault.init(current_user: :user) # => raises, because the ancestor already has vault initialized
end)

# Access data from the parent process itself
Vault.get(:current_user) # => %{id: 1, first_name: "Alice", role: "admin"}

In my case, repeatedly passing the user from the connection and GraphQL context into lower-level functions became hard to maintain.

A typical flow involved extracting the user from the Absinthe resolution context, performing substantial business logic, and only at the end persisting data or writing to the audit log - both of which also required the user. Maintaining this plumbing was cumbersome.

Because the user is immutable for the lifetime of the request and retrieved only once, storing it in the process dictionary is an elegant way to eliminate redundant parameters and simplify the overall flow.

This approach does introduce an implicit dependency - the need to understand where the value originates - but since it’s initialized exactly once, the trade-off is acceptable. In most cases, callers can simply read the value without needing to think about its source.

Properties

  • Immutability guarantees. Initializes only once per process tree - will raise if one of ancestors already has vault initialized.
  • Familiar API - API is the same as for Elixir’s Map module, except for Vault.init part.
  • Any child process will have access to the parent’s Vault. We’re using ProcessTree library by JB Steadman, which does all the heavy lifting of traversing process trees and propagating data back. You can read more about how ancestors are fetched in this amazing blog post by the library’s author.
  • Once the vault is found on one of the parents, it’s cached (set in the child’s process dict), so next fetches are faster.
  • We have a set of unsafe_* functions to perform updates on already initialized vault. These updates won’t propagate to the children that already initialized the vault.

I’m really curious what you guys think!

Most Liked

LostKobrakai

LostKobrakai

I’m not sure using links is a great idea here. Links connect processes in all manner of configurations. There’s no clear parent/child hierarchy there. For that you’d rather want $ancestors or $callers (Task — Elixir v1.18.4). Then you’re also no longer traversing a graph, but just a list of parents.

dimamik

dimamik

I’ve just released Vault v0.2.1.

Thanks to @Asd, @jswanner, and other folks suggestions, we’re now relying on ProcessTree library to traverse process tree in an efficient and inclusive way. It relies on Process.info(pid, :parent) and fallbacks to processes $ancestors and $callers if OTP<=24 or if parent process is dead, which is exactly what we need in this case. Big kudos to JB Steadman, the author of ProcessTree.

Since ProcessTree does all the heavy lifting for process traversal now, I was going back and forth on the need to have Vault as an abstraction layer above it in the first place. And I think it still has value in guaranteeing the immutability for the process subtree and defining a clean API on how to initialize and access this data.

My primary use-case was for propagating across a single process, so I definitely still see some value being added, but I would love to hear what you guys think!

garrison

garrison

Contexts have a very particular use-case in a React-style engine, namely they allow components to re-render based on dependencies through a memoization barrier, effectively turning the dependency tree into a DAG. You can get pretty far with a tree but the DAG models certain types of dependencies better (theme styling is a common example).

LiveView doesn’t have memoization (though maybe you can do something similar with LiveComponents?), but even if it did the engine has to actually understand the dependency DAG for things to update. If you just write your assigns into the process dictionary you can access them in distant children, but they won’t be able to re-render when things change which breaks the entire declarative model.

Surface actually tried to hack Contexts onto LV and eventually gave up for this reason.

jswanner

jswanner

I’m pretty sure ProcessTree already does that

LostKobrakai

LostKobrakai

I’d consider $callers as well. It will open the door for a lot of tooling, which makes use of it.

Where Next?

Popular in Announcing Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
isaias-dias-machado
IEx’s h macro is great but it lacks a pager, so I built a small tool that caches documentation and lets you fuzzy search through it in yo...
New
rodloboz
I’ve started working on a new library to run SQL queries and do basic business intelligence. Think “Blazer for Elixir.” Currently it fe...
New
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API. Docs are at OpenaiEx User Guide — ope...
New
dimamik
Vault is a lightweight Elixir library for immutable data storage within a process subtree. Due to Elixir’s actor model nature, it’s comm...
New
LostKobrakai
I’ve recently created a small library phoenix_vite integrating the vite build tooling with phoenix. It provides an igniter.installer t...
New
fhunleth
Elixir Circuits is a set of libraries for interacting with hardware. We previously announced Circuits.UART, and now we’re ready to announ...
New
sevensidedmarble
Announcing Live Toast: a replacement toast/flash component for Phoenix LiveView, heavily inspired by the look of Sonner (the amazing toas...
New
taro
I took lessons from the last discussion and cobbled together an example as a proof-of-concept. Mar demonstrates a Flask-like web dev int...
New
trisolaran
Hi! :waving_hand: I would like to present LiveSelect, a little library that I wrote to easily add a dynamic selection input to your LV f...
194 10054 106
New

Other popular topics Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement