This project is focused on building a highly optimized API endpoint for incrementing an integer field stored in a single-row database table. The primary goal is to maximize requests per second (RPS) by exploring various performance optimizations.
- Language: Python
- Web Framework: BlackSheep
- Database: PostgreSQL
- The database contains a single row with one
INTEGERcolumn. - The API provides endpoints to atomically increment this field and retrieve the current count.
- The objective is to push the RPS to its limits through iterative optimization.
| Method | Endpoint | Description | Version |
|---|---|---|---|
| POST | /api/v1/counter |
Increment the integer field | v1 |
| GET | /api/v1/counter |
Get current value of the integer field | v1 |
Clone the repository:
git clone https://github.com/awtb/IncrementCreate an .env file:
touch .envTemplate:
DB_HOST=localhost
DB_PORT=5432
DB_NAME=increment
DB_PASSWORD=ilyas
DB_USER=ilyas
SERVING_PORT=8000Using prebuilt image:
docker compose upBuilding manually:
docker compose up --buildApply manifests:
kubectl apply -f ./k8sMake sure you have UV installed.
Create virtual environment:
uv venvInstall dependencies:
uv sync --frozenRun project:
uv run scripts/prod.shWe use Locust for load testing.
uv run locust -f src/tests/load/counter.py| Metric | Value |
|---|---|
| Total Requests | 281,000 |
| Failures | 0 (0.00%) |
| Average Response Time (ms) | ~215 |
| Median Response Time (ms) | 190 |
| Max Response Time (ms) | 1250 |
| Throughput (req/sec) | ~4630 |
- Stable throughput around ~4600 RPS with no request failures.
- Latency remains consistent across higher percentiles.
- CPU is the main bottleneck.
- Due to the design, we cannot truly scale horizontally since it forces the use of a single core.
A .devcontainer/devcontainer.json is provided for VS Code or PyCharm integration to ensure consistent development environments.