mneumann

mneumann

Sharing single API connection from multiple nodes w/ failover

Hi,

I’ve three mostly identical nodes in an embedded system and roughly every minute, all of them have to make an API call to fetch some more information, and usually they do this pretty much simultaneously with the same query parameters (i.e. exact same HTTP GET query). To save bandwidth, I’d like to designate a single node responsible for performing the HTTP API calls and cache the responses for some short period of time (in case some nodes do the same call 10 seconds later), so that I have to fetch the data only once and not three times.
Unlike a centralized HTTP caching proxy, I also want that when the responsible node goes down, another node is selected to do this job.

How would you do that?

Would you use a library like Elector or swarm for that purpose?

These API calls are done once every minute, so performance really doesn’t matter that much.

How about this simple scheme:

When a node needs to make an API call, it first sends an RPC to node 1. If node 1 does not acknowledge within a short period of time, lets say 1 second, that it has received the request, the node continues to send a request to node 2…and so on.

So if I remove one node from the network, the RPC call will just time out and the next node is tried. Dunno if the Erlang VM will buffer the RPCs and retry once the network in on again…

Or would you use Erlang’s global module? Each node making an API call would first check if the global name “fetch_api” exists. If it doesn’t exist, which either means, we just started up the system or the node that had registered the name previously went down,
it would call :global.register and register itself and then just send a message to the registered “fetch_api” pid.

I think the simplest solution would really be, that each node has it’s supervised gen_server that can fetch and cache the API, regardless of if it’s used or not, just to make the code simpler. The client code of the gen_server would get the pid from a globally registered name OR register it’s own supervised gen_server in case none is found.

That really sounds like 2-3 lines of Elixir code :slight_smile: :slight_smile:

Anyone tried similar things? Are there some pitfalls with that approach? When I just poweroff a node by pulling the plug or network cable, how fast would Erlang notice that the node is down?

Note that, the API calls only GET data. I am also not concerned of net-splits. And normally, all three nodes should be up and running. In case one node is down for longer periods, which is not a real scenario atm, I accept a slight increase in latency.

Marked As Solved

mneumann

mneumann

sorry to answer my own question… :global module is truly amazing!

I can run the following two lines to make an API call regardless on which node:

# if global name is already registered this returns :no (just ignore)
:global.register_name(:api_fetch, pid_of_local_gen_server)

:global.whereis_name(:api_fetch) |> send(request)

Plus, I need some Node.ping code running in the background in order to establish connections to nodes. Of course this does not retry a request in case the node is going down that curently processes a request. In my case, this does not matter, as it is very seldom and one minute later everything works as normal.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement