bdubaut

bdubaut

URL shortener with GenServer & Registry

Hi everyone, in an effort to understand GenServers and Registry, I put together a small URL shortener service, without a database. Each “link” is registered using Registry, with the shortcode as a key to find the process. I am aware that this probably would not be a suitable approach for a production system, but it was fun to mess around with GenServer and Registry.

https://github.com/bdubaut/shortex

I would love some feedback on it:

  • what is good
  • what is bad
  • if my tests are relevant

This actually made me think about how your architecture changes when you bring in OTP patterns in your app design from the start, especially with persistence strategies. If you can store state inside a process, then how do you decide when to persist? What data to persist? I can definitely see a CQRS/ES type of architecture, with the event stores inside a DB, and having the read models in-memory as processes. Would this be a valid approach?

Thanks

Most Liked

petecorey

petecorey

Cool stuff!

My latest application uses an Event Sourcing/Memory Image approach, and I’ve had nothing but pleasant experiences with it.

In my experience, people’s instinctual response to Memory Images (keeping the application’s entire working state in memory) is vehement rejection, but in reality I’ve found it to be a really elegant solution to a lot of hard problems.

It opens up worlds of flexibility for managing read models (that can be reshaped, regenerated, and refactored on the fly), and makes conflict resolution fairly simple when compared to other solutions that rely on complicated consensus algorithms between distributed nodes.


One quick thing I noticed in your project is that there might be a race condition when generating new short codes for links. What would happen if two people simultaneously try to generate a new shortened link, and generate_shortcode returns the same shortcode for each? The first caller to reach Link.start_link would succeed, but the second call to Link.start_link would fail because the name is taken, right?

Instead of doing your Registry.lookup in generate_shortcode to detect used codes, you might want to listen for :already_started failures from Link.start_link instead.

That being said, the problem I described is very unlikely to happen in the real world. Just something to think about. Distributed systems are hard.

Good work!

Where Next?

Popular in Discussions Top

Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
cvkmohan
The upcoming Phoenix 1.6 release looks very interesting. Became a habit to watch the commits - and - what they are bringing in. phx.gen...
New
mmmrrr
Just saw that dhh announced https://hotwire.dev/ Is it just me or is this essentially live view? :smiley: Although I like the “iFrame-e...
New
WildYorkies
It seems that the more I read, the more I find Elixir users speaking about all the ways that Elixir is not good for x, y, and z use cases...
New
arcanemachine
https://nitter.net/josevalim/status/1744395345872683471 https://twitter.com/josevalim/status/1744395345872683471
New
nunobernardes99
Hi there Elixir friends :vulcan_salute: In a recent task I was on, I needed to check in two dates which of them is the maximum and which...
New
IVR
Hi all, I’ve seen a number of related threads in the past, but I’d still be very curious to hear an up-to-date opinion on this topic. I...
New
kostonstyle
Hi all How can I compare haskell with elixir, included tools, webservices, ect. Thanks
New
Markusxmr
Since Drab has been developed for a while in the open, introducing the Liveview functionality in a way it happend appears to undermine th...
New

Other popular topics Top

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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
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
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement