abhishektiwari / example-services-starlette-sqlalchmey-core

Example Python API project using Starlette and SQLAlchemy Core with asyncpg

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Example Python API project using Starlette and SQLAlchemy Core with asyncpg

This is a template project using Starlette (a lightweight ASGI framework), SQLAlchemy Core (not ORM), asyncpg (database interface for PostgreSQL and Python/asyncio). For fast JSON serioalizaiton project uses orjson.

Prerequisite

Project setup requires Pyenv and Poetry.

pyenv install 3.10.1
pyenv local 3.10.1
pyenv global 3.10.1
poetry env use 3.10.1
poetry install
poetry shell

Running Project

Change directory to project,

cd example-services

Apply migration,

alembic upgrade head

In local run with uvicorn,

uvicorn example_services:app --port 8080 --log-config logger.json --log-level error

In production run with Gunicorn and Nginx as reverse proxy,

gunicorn --log-level error -w 4 -k uvicorn.workers.UvicornWorker example_services:app

Performnce Considerations

Maximum mumber of PostgreSQL database connections seems to be a key performance bottleneck in our load testing (see details below).

Find max number of connections supported by PostgreSQL instance,

SELECT * FROM pg_settings WHERE name = 'max_connections';

You can change the number of max DB connections if running self-managed PostgreSQL instance. If you are using the cloud-based PostgreSQL instance RDS or something similar, max number of connections is dictated by instance size as set by cloud provider.

Find number of connections currently active,

SELECT sum(numbackends) FROM pg_stat_database;

Performance Benchmarks

We ran performance benchmarks on MacBook (Retina, 12-inch, Early 2016) which comes with 1.1 GHz Dual-Core Intel Core m3 and 8 GB 1867 MHz LPDDR3.

We used PostgreSQL(14.1) installed on Macbook and modified max number of connections 200.

About

Example Python API project using Starlette and SQLAlchemy Core with asyncpg


Languages

Language:Python 96.5%Language:Mako 3.5%