adoptingerlang / service_discovery

Adopting Erlang service discovery project
Apache License 2.0
52 stars 11 forks source link

[question] Why use `persistent_term` instead of simply calling `application:get_env/2` in `service_discovery_storage`? #13

Closed toraritte closed 4 years ago

toraritte commented 4 years ago

From sds_storage.erl:

% [...]
-define(STORAGE_KEY, {?MODULE, storage_module}).
-define(STORAGE_MOD, (persistent_term:get(?STORAGE_KEY))).

configure_storage(StorageMod) ->
    persistent_term:put(?STORAGE_KEY, StorageMod).

-spec create(service_discovery:service()) -> binary() | {error, term()}.
create(Service) ->
    ?STORAGE_MOD:create(Service).
% [...]

I couldn't figure out why this is necessary (or more beneficial) instead of just putting

-define(STORAGE_MOD, (application:get_env(service_discovery, storage_module)).

Does it have to do with this being an umbrella project? Is it because of convenience because application:get_env/2 returns a tuple?


Thank you so much by the way for putting together all these amazing online materials and libs! Fiddled with Erlang and Elixir for years, but this time I have a serious project going into production, and putting the pieces together without Adopting Erlang (to just name one work) would have taken exponentially longer. Again, your efforts are deeply appreciated!

tsloughter commented 4 years ago

It is simply because of performance. Application environment is backed by an ETS table while persistent term is optimized for reading, so it makes sense to use in cases where the value should never be changed but needs to be dynamic at runtime.