siddhant3030
Suppose I have two tables in my database. I want to build a service in elixir which should pick data from my database and cache each rows in redis keys.
- I want to use genserver and supervisor to do this.
Can anyone tell me how do I go about it?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
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
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
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
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
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
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
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
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
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
(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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #elixirconf-us
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #security
- #elixirconf-eu
- #api
- #forms
- #performance










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
wanton7
Do you really need to use Redis? If it is not mandatory to use it, then maybe just use GitHub - whitfin/cachex: A powerful caching library for Elixir with support for transactions, fallbacks and expirations · GitHub Instead.
lucaong
Hi @siddhant3030,
I think that more information about your case would make it easier for people to help you with this:
Why is
SupervisorandGenServera requirement? Is this an exercise/coding challenge, or you have specific needs with regards to resilience? If it’s about resilience, in order to design the supervision it’s useful to know how you want the system to react to failures (wipe all cached values, keep the cached values, restart the whole system or only specific parts, etc.)What is the expected behavior of the caching service? When should values be purged from the cache? What is the cache key? How are values retrieved from the database?
Is Redis a requirement, or just an idea?
If you just want some starting point, you can have a look at the official Elixir getting started guide, that has a very similar example. The guide walks you through the implementation of a key/value store using various techniques, from
AgenttoGenServer,Supervisor,ETS, etc. Caching services are usually key/value stores with some logic on top, so that might put you on the right track.andreaseriksson
As @wanton7 suggests, cachex is fine. But I think it works best as a short term cache (like memcached). If you really want something that is still (fairly) easy to use would be to use either a new database table where you precalculate data or a materialized view.
I guess the options are endless.
siddhant3030
Okay. Wait
lucaong
Before I start, is this an interview task or homework? If so, make sure that the company is ok with you sharing it here. It is quite likely that employees of companies working with Elixir hang out in this forum.
That said, there are different ways to design such a service and split up responsibilities, depending on specific cases and needs, but a possible way to start could be something like:
A scheduler module, possibly a
GenServer, that schedules the cache update task every 10 seconds. Usually, something like that is implemented with aGenServersending a delayed message to itself withProcess.send_after, and reacting to it withhandle_info, triggering the task and rescheduling the next message. The actual task logic would be in a different module.A
Taskto perform the cache update: it should select newly inserted rows from the DB, serialize the data, and save it in Redis. How to select for newly inserted rows depends on the data model. It seems from your description that data can only be inserted and not changed or deleted: if so, you might assign auto-incrementing IDs and keep track of the latest ID that was cached, so you can select every record inserted after the last check by selecting for greater IDs.The Postgres client, the Redis client, the serialization logic, etc. should probably all be implemented in their own processes, so they can be restarted independently in case of a crash
The supervision tree will depend on the specific needs and dependencies between these processes
Consider that this is just a guess: designing a real system requires a much better understanding of the task at hand, goals, and constraints than we can have from this short description.
If you start implementing this, and run into specific issues, I am sure that people on the forum will be able to help you with them.
EDIT: this response was based on the message it replies to, before it was heavily edited and basically mostly deleted. Without that context, it does not make much sense anymore… What I wrote here is not the way one would design a caching layer.
andreaseriksson
Sounds like it.
lucaong
It might very well be a learning question, I just don’t want people to get in trouble.
siddhant3030
No. This is part of a project. I want to test a POC for this.
lucaong
Then the best would be to describe more clearly the project goals, so people can help you better. What’s the end goal of your POC, or the problem you are trying to solve with it?
I am asking because your description focuses on the what, but doesn’t mention the why, and that makes it difficult to devise a proper solution.
Not knowing what its goal is, the system you describe sounds strange and over-engineered. For example:
Why can’t the golang service write on Redis? That would make the Elixir service unnecessary, simplify the whole system a lot, and the cache would be updated as soon as a write happens.
Alternatively, why should the Elixir service poll the database every 10 seconds, rather than listening to a messaging queue (RabbitMQ, Kafka, etc.)? That would avoid unnecessary database queries, and make the system scalable, as you would be able to add more workers as needed.