concept pool in category elixir

appears as: pool, pools, pools, Pool, The pool
Elixir in Action

This is an excerpt from Manning's book Elixir in Action.

Assuming that the queries can be run independently, you can start a pool of server processes, and then for each query somehow choose one of the processes from the pool and have that process run the query. If the pool is large enough and you divide the work uniformly across each worker in the pool, you’ll parallelize the total work as much as possible.

For example, in the current implementation of the to-do system, you maintain a small pool of database workers, which allows you to have controlled parallelism of database operations. You implemented this pool completely from scratch back in chapter 7. But as it happens, managing a pool of processes is a frequent pattern in Elixir and Erlang, so a couple of third-party libraries offer a solution to this challenge. In this chapter, you’ll replace your naive implementation of the process pool with a proven and battle-tested process pool library.

Next you need to adapt the store and get functions of Todo.Database. Previously these functions selected a worker ID, and then forwarded this ID to the corresponding function in the Todo.DatabaseWorker module. Now these functions need to check out the worker from the pool, make a request to the worker, and return the worker to the pool. All of this can be easily done with the function :poolboy.transaction/2. The code is provided in the following listing.

Listing 11.6 Adapted operation functions (todo_poolboy/lib/todo/database.ex)
defmodule Todo.Database do
  ...

  def store(key, data) do
 :poolboy.transaction(    #1  
      __MODULE__,
 fn worker_pid ->    #2  
 Todo.DatabaseWorker.store(worker_pid, key, data)    #2  
 end    #2  
    )
  end

  def get(key) do
    :poolboy.transaction(
      __MODULE__,
      fn worker_pid ->
        Todo.DatabaseWorker.get(worker_pid, key)
      end
    )
  end
end

#1   Asks the pool for a single worker
#2   Performs an operation on the worker

Here you invoke :poolboy.transaction/2, passing the registered name of the pool manager. This will issue a checkout request to fetch a single worker. Once a worker is available, the provided lambda is invoked. When the lambda is finished, :poolboy.transaction/2 will return the worker to the pool.

In the provided lambda, you get the pid of the checked-out worker and issue a Todo.DatabaseWorker request. This means that you need to slightly change the implementation of Todo.DatabaseWorker. Previously the functions from this module accepted a worker ID and then did a registry lookup to find the worker process. In this version, the discovery of the pid is performed by the pool, so you don’t need to register the workers anymore or perform any lookup to discover the process. The new implementation is provided in the following listing.

The Little Elixir & OTP Guidebook

This is an excerpt from Manning's book The Little Elixir & OTP Guidebook.

You’re going to build a worker pool over the course of two chapters. What is a worker pool? It’s something that manages a pool (surprise!) of workers. You might use a worker pool to manage access to a scarce resource. It could be a pool of Redis connections, web-socket connections, or even GenServer workers.

For example, suppose you spawn 1 million processes, and each process needs a connection to the database. It’s impractical to open 1 million database connections. To get around this, you can create a pool of database connections. Each time a process needs a database connection, it will issue a request to the pool. Once the process is done with the database connection, it’s returned to the pool. In effect, resource allocation is delegated to the worker-pool application.

Starting a pool

In order to start a pool, you must give it a pool configuration that provides the information needed for Pooly to initialize the pool:

pool_config = [
  mfa: {SampleWorker, :start_link, []},
  size: 5
]

This tells the pool to create five SampleWorkers. To start the pool, do this: