Puma is a fast, concurrent web server designed to serve Ruby on Rails applications efficiently by handling multiple requests at the same time.
Click to reveal answer
beginner
Where do you typically configure Puma settings in a Rails project?
Puma settings are usually configured in the config/puma.rb file inside a Rails project.
Click to reveal answer
intermediate
What does the workers setting in Puma configuration control?
The workers setting controls how many separate OS processes Puma will spawn to handle requests, improving concurrency by using multiple CPU cores.
Click to reveal answer
intermediate
Explain the purpose of the threads setting in Puma configuration.
The threads setting defines the minimum and maximum number of threads each Puma worker can use to handle multiple requests concurrently within a single process.
Click to reveal answer
advanced
Why is it important to set preload_app! in Puma configuration when using workers?
Setting preload_app! loads the application before forking workers, which saves memory by sharing code between workers and speeds up worker boot time.
Click to reveal answer
Where is Puma configuration usually found in a Rails app?
Aconfig/routes.rb
Bconfig/database.yml
Cconfig/puma.rb
Dapp/controllers/application_controller.rb
✗ Incorrect
Puma settings are placed in config/puma.rb to control server behavior.
What does the workers setting do in Puma?
AControls logging level
BSets number of OS processes to handle requests
CDefines maximum request size
DSets number of database connections
✗ Incorrect
workers controls how many OS processes Puma uses for concurrency.
Why use preload_app! in Puma config?
ATo load the app before forking workers for memory efficiency
BTo disable threading
CTo increase logging verbosity
DTo enable SSL encryption
✗ Incorrect
preload_app! loads the app once before workers fork to save memory.
What does the threads setting specify?
ANumber of database threads
BTimeout duration
CNumber of workers
DMin and max threads per worker for handling requests
✗ Incorrect
threads sets the thread pool size per Puma worker.
Which Puma setting helps utilize multiple CPU cores?
Aworkers
Bthreads
Cport
Denvironment
✗ Incorrect
Using multiple workers lets Puma use multiple CPU cores.
Describe how Puma uses workers and threads to handle multiple requests.
Think about how a restaurant uses multiple kitchens (workers) and multiple cooks (threads) inside each kitchen.
You got /4 concepts.
Explain why setting preload_app! is beneficial in Puma configuration.
Imagine preparing ingredients once before making many dishes to save time and resources.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of configuring the threads setting in a Puma server for a Rails app?
easy
A. To control how many requests Puma can handle concurrently within a single worker
B. To set the number of worker processes Puma will spawn
C. To specify the port number Puma listens on
D. To define the Rails environment (development, production, etc.)
Solution
Step 1: Understand the role of threads in Puma
Threads allow Puma to handle multiple requests at the same time inside one worker process.
Step 2: Differentiate threads from workers and ports
Workers are separate processes, ports define network access, threads are about concurrency inside a worker.
Final Answer:
To control how many requests Puma can handle concurrently within a single worker -> Option A
Quick Check:
Threads = concurrent requests per worker [OK]
Hint: Threads = concurrent requests per worker process [OK]
Common Mistakes:
Confusing threads with workers
Thinking threads set the port
Mixing threads with environment settings
2. Which of the following is the correct syntax to set Puma to listen on port 3000 in the configuration file?
easy
A. bind 'tcp://3000'
B. set_port 3000
C. listen 3000
D. port 3000
Solution
Step 1: Recall Puma configuration syntax for port
Puma uses the simple directive port followed by the port number.
Step 2: Check other options for correctness
set_port and listen are not valid Puma config commands; bind requires a full URL, not just a port number.
Final Answer:
port 3000 -> Option D
Quick Check:
Use port to set listening port [OK]
Hint: Use 'port' keyword followed by number [OK]
Common Mistakes:
Using 'listen' instead of 'port'
Omitting quotes in 'bind' URL
Using non-existent 'set_port' command
3. Given this Puma config snippet:
threads 5, 5
workers 2
port 4000
How many total threads can Puma handle at once across all workers?
medium
A. 5
B. 10
C. 2
D. 20
Solution
Step 1: Understand threads and workers relationship
Each worker runs with 5 threads, and there are 2 workers.
Step 2: Calculate total threads
Total threads = threads per worker * number of workers = 5 * 2 = 10. But note the config uses 'threads 5, 5' which means min and max threads are 5, so max threads per worker is 5.
Step 3: Re-examine the question carefully
Actually, the question asks total threads Puma can handle at once across all workers. Since each worker has 5 threads, and there are 2 workers, total is 5 * 2 = 10.
Final Answer:
10 -> Option B
Quick Check:
Threads per worker * workers = 10 [OK]
Hint: Multiply threads by workers for total concurrency [OK]
Common Mistakes:
Confusing min and max threads
Ignoring number of workers
Assuming threads is total, not per worker
4. You added preload_app! in your Puma config but your Rails app crashes on worker boot with a database connection error. What is the most likely fix?
medium
A. Increase the number of workers
B. Remove preload_app! from the config
C. Add a on_worker_boot block to reconnect ActiveRecord
D. Change the port number to 8080
Solution
Step 1: Understand what preload_app! does
It loads the app before workers fork, so connections like database must be re-established in each worker.
Step 2: Fix database connection error on worker boot
Use on_worker_boot block to reconnect ActiveRecord to the database after forking.
Final Answer:
Add a on_worker_boot block to reconnect ActiveRecord -> Option C
Quick Check:
Use on_worker_boot to fix DB connections [OK]
Hint: Reconnect DB in on_worker_boot after preload_app! [OK]
Common Mistakes:
Removing preload_app! instead of fixing connections
Changing unrelated settings like port
Increasing workers without fixing connections
5. You want Puma to run with 3 workers and a thread pool between 2 and 6 threads per worker. You also want to ensure your Rails app reconnects to the database properly after forking. Which Puma config snippet correctly achieves this?
hard
A. threads 2, 6\nworkers 3\npreload_app!\non_worker_boot do\n ActiveRecord::Base.establish_connection\nend
B. workers 3\nthreads 6, 2\npreload_app!\non_worker_boot do\n ActiveRecord::Base.connection.disconnect!\nend
C. threads 2, 6\nworkers 3\nport 3000\non_worker_boot do\n ActiveRecord::Base.establish_connection\nend
D. workers 3\nthreads 2, 6\npreload_app!\non_worker_boot do\n ActiveRecord::Base.connection.reconnect!\nend
Solution
Step 1: Set threads and workers correctly
Threads should be set as threads 2, 6 for min and max, and workers as 3.
Step 2: Use preload_app! and reconnect DB properly
preload_app! must be present, and on_worker_boot should call ActiveRecord::Base.establish_connection to reconnect DB.