Let’s say I have a process that reads from a file and saves data into an ETS table. This ETS table must be publicly available to all processes so they can read it (only read access is necessary).
The issue here is that reading from a file is a complex topic. It may go well, or it may fail, you never know. Our app depends on this module reading this file and placing it’s information on an ETS table.
After reading this article:
It is clear to me that the module that reads the file and fills the ETS table, let’s call it
Reader can’t rely on the fact everything will go as expected, nor should MyApp, using this module, rely on that assumption either.
So the best practice here would be to have
Reader in MyApp supervision’s tree, as a process that simply returns :ok on the
init function. After that it would try to populate the table, which may fail or not. If it succeeds, processes will be able to read from the table, if it fails, it tries again every 30 seconds or so (it tries forever).
The challenge here is “How will my client, which depends on the data on the ETS table, know the table is ready?”
Two options come to my mind:
- Ask the
Readerprocess every time you need to make a read. This will, unfortunately make this process a big bottleneck because all requests must ask him something.
- Have a special value in the ETS table that says if said table is ready for usage or not.
Readerwould be the only process capable of updating this value.
Overall solution 2 would be more scalable, but I am not sure about having special values in data tables. It doesn’t feel right to mix accessibility with data.
How do you solve this problem?
What patterns or solutions do you use?