josefrichter

josefrichter

Mnesia simple use case questions

I have a simple todo app where users can create and share various todo lists (this thread). It’s a phoenix live view app with default ecto/postgress right now.

I am thinking of using Mnesia mainly for learning purposes, no other strong reasons. (Maybe also to avoid usual limits on free-tier postgres as a bonus.) I have some elementary questions:

  • in postgres I’m storing all todos in single table and retreive them with select/where list_id (even though I have no lists table at all). in mnesia I am thinking of simply creating separate table for each todo list - does that even make sense and does it bring some additional benefits like loading to RAM only the currently used tables?
  • every now and then I want to run a task that deletes old unused todo lists (i.e. mnesia tables in my intended schema) - does the mnesia table have some timestamps by default, based on which I could delete it, or do I need to take care of that separately?
  • does mnesia have some autoincrement unique id, or do I need to handle it myself? I’ve noticed Amnesia wrapper takes care of that, but I’m thinking of going wrapper-free for those learning purposes… And don’t see that in mnesia docs
  • can I mix different structures in same table? I.e. I would have todos with ids 1.2,3… but then maybe :created_at key with timestamp and :table_owner with user info - then to show just todos I’d do probably some pattern matching, does that make sense and does it work?

Thank you very much

Most Liked

Qqwy

Qqwy

TypeCheck Core Team

I can at least answer that: Using ‘just’ a timestamp caps you to a certain number of maximum inserts per second, and requires you to pay good attention to your clock. However, there certainly are common approaches to create timestamp-base IDs. A common one is the Snowflake ID, for which there exists an Elixir implementation.

As to when it makes sense to use UUIDs vs auto-incrementing IDs: personally I agree with the arguments of this blog post that boil down to UUIDs are larger than traditional SQL auto-increment ID fields, but they have several benefits such as better scalability, they still work in a distributed setting, you can move to a different underlying database without having to create new ids, etc.

Yes and no. You cannot vary what fields exist in a Mnesia table. You can however vary what data is stored inside, and this data might be any Erlang/Elixir term including (nested) lists, maps (including structs), tuples, etc. This gives you flexibility to store varying data in a certain field.

The main disadvantage of storing denormalized data is that you cannot (at least not easily) query this data. Depending on what you want to store vs. what you want to search on, this is usually not a problem.

I’d say: start with the simplest thing that works and only make it more complex if you hit against its limits.

Exadra37

Exadra37

I am also building Todo app with Mnesia, no Postgres at all from day 1.

If you have not read yet I would recommend you to read the excellent resource Mnesia and the Art of Remembering.

You can also take a look to Why isn’t mnesia the most preferred database for use in Elixir/Phoenix? - #20 by Exadra37 where you will see an interesting discussion and a summary of this video:

I know you said that you prefer to use Mnesia directly for learning purposes, but if at any time you decided to switch to use a wrapper, then you have:

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

We're in Beta

About us Mission Statement