Building Pools of Common Resources with Poolboy

Let’s learn how to use the poolboy dependency which enables us to share processes in a project.

Properties of poolboy

Poolboy is an Erlang application for sharing pools of common resources. Sometimes, programs need to use processes that take a while to start. In such instances, it’s best to have a pool of processes to share across a whole project. It’s ideal for situations where we need to throttle many requests down to a smaller number of resources. For example, most web servers run thousands of concurrent jobs through a handful of database connections, because those connections often consume a good amount of memory and take a while to start.

Since Poolboy is an OTP application, we already know how to use it. Just add a dependency, configure it in application.ex, and create an OTP server to do some work.

Creating a poolboy project

We’ll make a new project, poolboy_app, through the following command:

mix new poolboy_app --sup

In the configuration in /lib/poolboy_app/application.ex, we can specify the number of permanent and temporary workers like this:

Get hands-on with 1400+ tech skills courses.