Search code examples
optimizationerlangmnesia

Erlang: optimizing mnesia tables for read/write concurrency


I am working on a new Erlang project using mnesia. When I previously worked with ets, I could set up ets tables optimized for read concurrency, write concurrency, or both. I initially assumed that I could do the same thing with mnesia tables, but thus far I have not found how to do it by looking through the documentation (which surprises me, since mnesia is based on ets/dets from my understanding).

Anyone know if this is possible?


Solution

  • From docs:

    {storage_properties, [{Backend, Properties}]. Forwards additional properties to the backend storage. Backend can currently be ets or dets and Properties is a list of options sent to the backend storage during table creation. Properties may not contain properties already used by mnesia such as type or named_table.

    For example:

    mnesia:create_table(table, [{ram_copies, [node()]}, {disc_only_copies, nodes()},
                    {storage_properties,
                     [{ets, [compressed]}, {dets, [{auto_save, 5000}]} ]}])
            
    

    But it is nor really so simple. Key to Mnesia is fact that this is distributed database, with features like replication or peristalsis. But this also introduces overhead of things like replication and overhead. Tinkering with those could be main source of your optimizations. This is especially important with concepts like transactions and dirty operations in mnesia.

    In Ets all operations are atomic, which is more or less easy to achieve since all data sits in one place (and makes write read optimizations also easier). In mnesia to make write atomic (ensure it's correctness) you need to create lock on possibly many nodes.

    But still it doesn't mean that all interactions with mnesia table have to be wrapped in strict transaction. Maybe you have some dirty reads or writes there.

    Tt all depends on your data, What it means, if there are any interleavings, and how (with how much certainty?) will you use it. So it depends much more on your domain, than on any mnesia setting.