McElfresh Blog

Go, PostgreSQL, MySQL, Ruby, Rails, Sinatra, etc.

Go vs Postgrest vs Sinatra vs Rails

Posted at — Apr 6, 2024

Introduction

Here are four JSON APIs which offer the same functionality – an endpoint that retrieves 10 items, connected with randomly selected users via user_items, in JSON format.

The Database

The APIs are backed by a PostgreSQL database items_api_development that has three tables. The database, tables, and data are created from within postgrest-items-api, as noted below.

Here is the schema:

users

1+------------+--------------+------+-----+---------+----------------+
2| Field      | Type         | Null | Key | Default | Extra          |
3+------------+--------------+------+-----+---------+----------------+
4| id         | bigint       | NO   | PRI | NULL    | auto_increment |
5| email      | varchar(255) | NO   | UNI | NULL    |                |
6| created_at | datetime(6)  | NO   |     | NULL    |                |
7| updated_at | datetime(6)  | NO   |     | NULL    |                |
8+------------+--------------+------+-----+---------+----------------+

user_items

1+------------+-------------+------+-----+---------+----------------+
2| Field      | Type        | Null | Key | Default | Extra          |
3+------------+-------------+------+-----+---------+----------------+
4| id         | bigint      | NO   | PRI | NULL    | auto_increment |
5| user_id    | bigint      | YES  | MUL | NULL    |                |
6| item_id    | bigint      | YES  | MUL | NULL    |                |
7| created_at | datetime(6) | NO   |     | NULL    |                |
8| updated_at | datetime(6) | NO   |     | NULL    |                |
9+------------+-------------+------+-----+---------+----------------+

items

1+------------+--------------+------+-----+---------+----------------+
2| Field      | Type         | Null | Key | Default | Extra          |
3+------------+--------------+------+-----+---------+----------------+
4| id         | bigint       | NO   | PRI | NULL    | auto_increment |
5| name       | varchar(255) | NO   | UNI | NULL    |                |
6| created_at | datetime(6)  | NO   |     | NULL    |                |
7| updated_at | datetime(6)  | NO   |     | NULL    |                |
8+------------+--------------+------+-----+---------+----------------+

The Applications

Install / run instructions are in the READMEs at the root of all four apps. Once you install all four, you can run performance tests yourself; the Go app has a performance tester that fires up 10 concurrent Go http clients, does 1000 total requests against all four APIs, and prints performance results. Your results will vary from mine.

The same PostgreSQL database is the backend for all four. It is created, migrated, and seeded inside postgrest-items-api.

I’m using these four apps as a starting point to discuss performance, simplicity, extensibility, and ease-of-use for these and potentially other frameworks and languages. This post is just an introduction; in future posts, I’ll talk about issues around

Initial Results / Observations

1000 requests @10 concurrent workers

app elapsed (sec) req / s
Go 0.28 3,571
Postgrest 0.33 3,030
Sinatra 0.62 1,613
Rails 6.08 164

Here is a laundry list of mixed pluses / minuses for each app:

Go
Postgrest
Sinatra
Rails

In future posts, I’ll be tuning all three of these apps, introducing more features and performance monitoring, and demonstrating various upgrade paths. Stay tuned!