Erlang: developing a service component that is immutable but slow to get started

I am developing a web service with Erlang. Low latency is the goal. The service provides several views on a set of files. Building these representations takes time, since I have to read in files, analyze them as CSVs, store their fields in records, etc.

A naive approach would be to re-read these files and make the necessary conversions for each request, so that the web application is completely stateless. But I'm concerned about the delay.

Another approach would be to create a server (implementing the gen_server behavior) that prepares these views at startup and stores them in memory as LoopData , and then returns the views as needed to web workers. But this reduces concurrency, as this server processes one request at a time.

Is there a design pattern in Erlang that supports costly initialization at startup and yet allows simultaneous access to initialized data? (The key is that this data is immutable, so I am not interested in mutual exclusion.)

+6
source share
2 answers

You can use one or more ETS tables to store your data.

Fill in the tables during the initialization phase, then read each process.

You can abstract the initialization and reading from tables into modules to make sure you can enable the implementation later (for example, using mnesia or something else later).

Note that ETS tables are stored in memory (by default), and if you need more complex queries, there is mnesia.

+3
source

1.generate xx.csv to xx.erl

2.complie xx.erl xx.beam

3.run erl

4. request(From, SomeKey) -> spawn(fun() -> Reply = xx:get(SomeKey), to_reply(From, Reply) end).

+2
source

Source: https://habr.com/ru/post/1013109/


All Articles