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

Donovan
Hello everyone, I’m so glad to have discovered this awesome community. Thanks for creating it! This is my second post, and apologies for...
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
Fl4m3Ph03n1x
Background This question comes mainly from my ignorance. Today is Black Friday, one of my favorite days of the year to buy books. One boo...
New
PragTob
Hey everyone, this has been brewing in my head some time and it came up again while reading Adopting Elixir. GenServers, supervisors et...
New
rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
hazardfn
I suppose this question is effectively hackney vs. ibrowse but we are at a point in our project where we have to make a choice between th...
New
PragTob
Hey everyone, this has been on my mind for some time and I’d love your input on it! TLDR: I feel like maps are superioer for storing and...
New
chulkilee
Here are the list of HTTP client libraries/wrappers, and some thoughts on HTTP client in general. I’d like to hear from others how they w...
New
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

skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127089 1222
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New

We're in Beta

About us Mission Statement