Ecto adapter for Mnesia Erlang term database

Deps StatusHex.pm DownloadsLatest VersionLicenseBuild StatusCoverage StatusEbert

Ecto 2.X adapter for Mnesia Erlang term database. In most cases it can be used as drop-in replacement for other adapters.

Supported features:

Planned features:

Not supported features (create issue and vote if you need them):

Why Mnesia?

We have production task that needs low read-latency database and our data fits in RAM, so Mnesia is a best choice: it’s part of OTP, shares same space as our app does, work fast in RAM and supports transactions (it’s critical for fintech projects).

Why do we need adapter? We don’t want to lock us to any specific database, since requirements can change. Ecto allows to switch databases by simply modifying config, and we might want to go back to Postres or another DB.

Clustering

We don’t recommend to use distributed Mnesia, because it’s neither AP, nor CP database. (And there are no such thing as AC DB.) Mnesia requires you to handle network partitions (split brains) manually.

So clustering should be an option only when you absolutely sure how you will recover from split-brains. In general, if you don’t sure what is network splits, don’t use it.

Mnesia configuration from config.exs

config :ecto_mnesia,
  host: {:system, :atom, "MNESIA_HOST", Kernel.node()},
  storage_type: {:system, :atom, "MNESIA_STORAGE_TYPE", :disc_copies}

config :mnesia,
  dir: 'priv/data/mnesia' # Make sure this directory exists

Notice that {:system, [TYPE], ENV_NAME, default_value} tuples can be replaced with any raw values.

They tell adapter to read configuration from environment in run-time, so you will be able to set MNESIA_HOST and MNESIA_STORAGE_TYPE environment variables, which is very useful when you releasing app in production and don’t want to rebuild all code on each config change.

If you want to know more how this tool works take look at Confex package.

Storage Types

Table Types (Engines)

In migrations you can select which kind of table you want to use:

  create_if_not_exists table(:my_table, engine: :set) do
    # ...
  end

Supported types:

Ordered Set Performance

Ordered set comes in a cost of increased complexity of write operations:

Set

Operation | Average | Worst Case ———-|———|———- Space | O(n) | O(n) Search | O(1) | O(n) Insert | O(1) | O(n) Delete | O(1) | O(n)

Ordered Set

Operation | Average | Worst Case ———-|———-|———- Space | O(n) | O(n) Search | O(log n) | O(n) Insert | O(log n) | O(n) Delete | O(log n) | O(n)

Installation

It is available in Hex, the package can be installed as:

  1. Add ecto_mnesia to your list of dependencies in mix.exs:
```elixir
def deps do
  [{:ecto_mnesia, "~> 0.7.1"}]
end
```
  1. Ensure ecto_mnesia is started before your application:
```elixir
def application do
  [applications: [:ecto_mnesia]]
end
```
  1. Use Ecto.Adapters.Mnesia as your Ecto.Repo adapter:
```elixir
config :my_app, MyRepo,
  adapter: Ecto.Adapters.Mnesia
```

The docs can be found at https://hexdocs.pm/ecto_mnesia.

Thanks

We want to thank meh for his Amnesia package that helped a loot in initial Mnesia investigations. Some pieces of code was copied from hes repo.

Also big thanks to josevalim for Elixir, Ecto and active help while this adapter was developed.