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

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
First poster: bot
In a previous post we talked about implementing a simple video chat with WebRTC and Elixir. This update will touch on some of the API cha...
New
First poster: bot
One of my favourite programming languages in the last few years has been Crystal. While the language has not yet reached its 1.0 version,...
New
First poster: bot
Over the last few years, due in large part to the hype surrounding blockchain and cryptocurrencies, decentralized applications have gaine...
New
First poster: Exadra37
Summary: I describe a simple interview problem (counting frequencies of unique words), solve it in various languages, and compare perform...
New
First poster: brennan
The perspective of an ignorant computer science undergrad It’s likely that you read the title of this post and thought “what is this guy ...
New
CommunityNews
Have you ever wanted to write a structurally typed function in Rust? Do you spend a lot of time and effort getting your Rust struct s jus...
New
CommunityNews
This thread was posted by one of our members via one of our news source trackers.
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
StuntProgrammer
In building lofi.limo, media storage and distribution naturally came up. I have songs, announcements, and background image loops which I ...
New

Other popular topics Top

AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
AstonJ
There’s a whole world of custom keycaps out there that I didn’t know existed! Check out all of our Keycaps threads here: https://forum....
New
AstonJ
I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl: The Ser...
New
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New
New
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New
PragmaticBookshelf
Author Spotlight: Karl Stolley @karlstolley Logic! Rhetoric! Prag! Wow, what a combination. In this spotlight, we sit down with Karl ...
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