highmountaintea

highmountaintea

There is an oracle on the moon, who can give profound answers to all questions. The oracle is all knowing, but requires a minute or two to ponder each question before answering. Luckily he has 4 brains, so he can ponder 4 questions simultaneously. Unfortunately the oracle has very bad memory, so if you ask the same question over and over again, he will have to think about it every time.

How do we write an OracleServer in Elixir that can query the oracle for answers? It needs to do the following:

  • allow multiple people to ask questions at once, because the oracle has 4 brains
  • cache all the answers, to avoid asking repeat questions
  • if person A asks a question, and person B asks the same question while the oracle is still pondering it, OracleServer would smartly avoid sending the same question to the oracle again, but informs both A and B when the answer comes back
  • the answer is not always 42

Obviously it’s a thinly disguised way to describe a well known problem. I am familiar with solutions in other languages, but am having trouble figuring out how to do it in Elixir.

Showing Posts 1 to 10

kartheek

kartheek

Welcome @highmountaintea to elixir.

Cachex can help you adding cache to your app.

One thing I did not understand from your question is you want to write as learning exercise or use it in a project.

GenServers and Agent can hold data in memory. They can be used to store state or data in memory. Best fit if you want to implement a Cache as a learning exercise.

Cachex can be used directly as cache in your project if you are looking to just use a cache library - like fetch some config from source and store in memory, refresh every n seconds, etc.

highmountaintea

highmountaintea OP

Hi Kartheek. It’s not meant for an exercise or an existing project. I am more interested in understanding how things are done in Elixir.

I did consider using Agent for this particular problem, but I am not sure if it would really satisfy my conditions as set above.

I took a look at Cachex you mentioned, but can you give an example of how Cachex would help prevent the situation I mentioned above? if person A asks a question, and person B asks the same question while the oracle is still pondering it, OracleServer would smartly avoid sending the same question to the oracle again, but informs both A and B when the answer comes back

cmo

cmo

You need to keep track of the question and who has asked it. When you have the answer, you send it to the references you kept track of. When a GenServer receives a call, you have the return address. Keep track of it.

awaiting = %{”Why?" => [pid<0.90.0>, pid<0.765.0>]}

Do you want all the questions & answers kept in memory, on disk or in a db?

You’re going to need a worker pool. Do you want to write that too or use a library?

kartheek

kartheek

Is this is the same thing you are looking for ?

As of v3, fallbacks changed quite significantly to provide the guarantee that only a single fallback will fire for a given key, even if more processes ask for the same key before the fallback is complete. The internal Courier service will queue these requests up, then resolve them all with the results retrieved by the first. This ensures that you don’t have stray processes calling for the same thing (which is especially bad if they’re talking to a database, etc.). You can think of this as a per-key queue at a high level, with a short circuit involved to avoid executing too often.

The new Courier service in Cachex v3 will actually queue the second and third calls to fire after the first one, rather than firing them all at once. What’s even better; the moment the first call resolves, the second and third will immediately resolve with the same results. This ensures that your fallback only fires a single time, regardless of the number of processes awaiting the result. This change in behaviour means that the code above would result in "key" having a single value of 1 as the second and third never fire. Although this results in a behaviour change above, it should basically never affect you in the same way as the code above is deliberately designed to highlight the changes.


Cachex takes care of these things automatically.

highmountaintea

highmountaintea OP

Hi cmo, thanks for your answer. So when the answer is available, what mechanism should I use to send the answer back to the requester(s)? I think I can send a plain message, but is there a more OTP way of doing it?

Also, if we are awaiting for answer inside the OracleServer, would it result in blocking, preventing others from asking questions simultaneously?

highmountaintea

highmountaintea OP

Hi kartheek, I am still trying to digest the fallback mechanism fully, but I think it is what I am looking for.

It’s nice to have a caching library that handles it automatically.

highmountaintea

highmountaintea OP

Forgot to answer your other questions…

Do you want all the questions & answers kept in memory, on disk or in a db? memory

You’re going to need a worker pool. Do you want to write that too or use a library? I would prefer to use a library

cmo

cmo

Ok well you’ll probably want to use ETS to cache your answers and there are a few worker pool libraries. Poolboy is a pretty common choice.

No problem just sending the messages.

Elixir In Action is super helpful in exposing you too all this sort of stuff.

highmountaintea

highmountaintea OP

Ha, this question did spring into my mind while I was reading Elixir In Action :slight_smile: I remembered that each handle_call is blocking, so was trying to figure out how to design a non-blocking server that also handles caching.

Maybe I should re-read chapter 7 again, and some of it might become clear to me.

cmo

cmo

See how he keeps track of it in this Port example.

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews