Fl4m3Ph03n1x

Fl4m3Ph03n1x

How to use ETS as a global cache?

Background

I have an Elixir application that has 2 machines. Each machine receives requests on a round robin base.

Both machines are connected to a single Redis instance, which works as a global cache.
If machine A receives a request, it caches the request/response in Redis. Should machine B get the same request, it won’t need to re-calculate the answer.

Questions

I know that ETS is usually used for caching.

However this cache is local to machine A and local to machine B.

My goal here would be to replace Redis with an ETS instance and achieve a global cache for my elixir applications using ETS. Thus far I was not able to find any article detailing configurations for such, so I wonder:

  • Is it possible to use ETS as a global cache via HTTP, the same way Redis is being using in the above example?
  • If so how can I do it?
  • If it is possible, is it worth the effort, or does the community advice I use Redis instead? (maybe because it is easier to setup, for example)

Marked As Solved

cevado

cevado

yes, for sure.

If machine A and machine B are both elixir machines, you could use either Cachex or Nebulex, both takes advantage of a cluster of machines to distribute ets tables across nodes

I particularly think that if you have an elixir application is more simple and less expensive clusterizing your nodes and use ets than use redis.

Also Liked

tangui

tangui

This is what http_cache_store_disk and http_cache_store_memory do when cluster_enabled is set to true: they exchange cached responses by using distributed erlang and store them either in ETS table or on disk (and in this latter case, metadata is still stored in memory).

They take into account that nodes can have different requirements such as available disk space or memory and therefore they handle cached response autonomously. A cached response can be discarded from one node and still be available on another.

If you want to cache HTTP responses from Phoenix / Plug, you can take a look at plug_http_cache.

D4no0

D4no0

What about running separate caches on both instances? You will waste a little bit more ram, however you will get rid of a lot of complexity, which IMO is perfect, as ram is dirt cheap these days.

LostKobrakai

LostKobrakai

It’s possible. You could always build a http endpoint around ets and server http requests that way. But none of that comes “built in” and I doubt it’s worth it in the general case.

On the beam you also have mnesia which allows you to share kind of an ets table across the cluster, but it’s not quite straight forward to use. That’s local state synchronized across the cluster and not accessing one shared resource.

Without much detail on why you need a global cache I’d probably suggest staying with redis, lacking any reasonable means of doing a tradeoff with other options.

Last Post!

dimitarvp

dimitarvp

Oh I meant distributed caches in general, plus @tangui’s libraries. Thanks for the link, I’ll read through it.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New

Other popular topics 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
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement