How to build forever loop server in elixir?

Hi, all,

sorry for my english,
i’m elixir beginner,

i want build a non-stop Thread for maintain some data state,

in other language,
i will create a forever loop Thread to do this, like

var array = [];
while ( true )
    var needUpdate = array.filter( a => === 1 );
    if( needUpdate.length >= 1 )
         //do something
    thread.sleep( 5 * 60 * 1000 );

this loop thread in every 5 minutes, check the need to update logic data, then thread sleep

like this use, in Elxiir, what should I do is better?

1 Like

Well the direct translation would just be:

def blah(array) do
  |> Enum.count(&(& === 1))
  |> if do
    # do something
  Process.sleep(5 * 60 * 1000)

Though it would process the same things every loop as you don’t have it receiving messages to update the array or so. ^.^

However, a better way for something so ‘temporal’ would be some kind of crontab-like scheduler, like I prefer using Quantum. :slight_smile:


wow, thanks for suggest,
Quantum look very good :heart_eyes:

after look this
i plan use Cachex to cache data,
i will update the cache in other thread, like from websocket connection thread,
then use Quantum to maintain Cahcex data

do you think this plan is make sense?


Sounds good yeah, but if your cache doesn’t need to expire then just using ETS straight should be fine (Cachex basically adds functionality for elapsing cache’s and such from ETS).

However, if your data is time elapsing anyway, forgo Quantum and just have Cachex purge data when it’s X minutes old or as needed. :slight_smile:


thanks very much :laughing:

yes, in my plain,
data is never expire,
if application die, i’ll take previous state from database in initialize

thanks for perfect solution again :blush:

1 Like

Yeah I’d probably just use ETS straight then (although using Cachex to setup a default fallback to the database is a very nice helper). :slight_smile:

Also, are you sure you need to cache? Database are VERY fast nowadays.


:open_mouth: you mean i can just use database, forget a cache?

i’m building Game server,
current my application using ECTO with Postgres,

all user will access same “Map” data,
if i don’t use cache,
the pressure of all operations will be concentrated in the database

i just feeling this is scare, is not?

1 Like

Put all access to the database in it’s own module, use just a database first. If it is shown to be an actual bottleneck later then you can put something like Cachex access in that module without changing the interface. Remember, you can’t really have too many modules. :slight_smile:

But yeah, this is all the Don't prematurely optimize kind of thing. ^.^

Instead just abstract it out behind more functions so you can change the inner implementation as you need. :slight_smile:


Okay, Yes, i understand what you mean,
Thank you very much for your suggestion,
I will follow your suggestions to implement, and then decide to add Cache or not

Thanks very much again :blush:

1 Like

What about genserver?

Is the usual way indeed yep.