CommunityNews

CommunityNews

Learning Elixir's `GenServer` with a real-world example

I started learning Elixir a few months ago, mostly through hacking on Papercups. I’m ashamed to say most of my Elixir education has been through trial and error, figuring things out as I go along. So this past week I decided to take some time off from Papercups to go a bit deeper into the language.

In particular, I was itching to learn more about handling concurrency in Elixir. This, of course, led me to GenServers.

Anyone who’s moderately familiar with Elixir has probably at least heard of GenServer. (If you haven’t, that’s ok too!) Strictly speaking, it’s one of those things you can get away with avoiding for a while, and still be reasonably productive while using something like the Phoenix framework.

But it’s an incredible useful feature of the language that you can add to your toolbox! There are so many nice things you can do with it. For example, with GenServers, you can:

  • create a simple cache, buffer, or rate limiter
  • set up recurring tasks to run every few hours (e.g. check out Jose’s answer to “How can I schedule code to run every few hours in Elixir?” on Stack Overflow)
  • handle async processes much more easily, with built-in functionality for tracing, error reporting, and retry logic

…all without any external dependencies!

This thread was posted by one of our members via one of our automated news source trackers.

Where Next?

Popular Backend topics Top

New
First poster: bot
We all know how to teach recursion. We’ve done it for decades. We pick some honored, time-tested examples—Fibonacci numbers and factorial...
New
AstonJ
Not had time to read it yet but this looks like a good interview… Our friend Yukihiro Matsumoto, creator of the Ruby programming langua...
New
First poster: bot
Like, on a scale from c to rust? issue c zig (release-safe) rust (release) out-of-bounds heap read/write none runtime runtime ...
New
First poster: bot
At Grammarly, the foundation of our business, our core grammar engine, is written in Common Lisp. It currently processes more than a thou...
New
elbrujohalcon
Erlang is famous for its introspecting powers. You can get a lot of information about the processes running in your nodes without any ext...
New
chikega
Mark Hoffman, the author of Programming WebAssembly in Rust, is a pretty hilarious lecturer if you like a dry sense of humor.
New
New
First poster: AstonJ
Ruby’s Struct is one of several powerful core classes which is often overlooked and under utilized compared to the more popular Hash clas...
New
GoulvenClech
Hi everyone :wave: I’m excited to share an article detailing how we have reorganized our Elixir/Phoenix project’s directory structure. W...
New

Other popular topics Top

ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
AstonJ
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
New
AstonJ
Biggest jackpot ever apparently! :upside_down_face: I don’t (usually) gamble/play the lottery, but working on a program to predict the...
New
PragmaticBookshelf
Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
New
New
DevotionGeo
I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New
PragmaticBookshelf
Author Spotlight: Peter Ullrich @PJUllrich Data is at the core of every business, but it is useless if nobody can access and analyze ...
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New