A FastAPI-based service that buffers person records in memory and flushes them to SQLite when reaching a configurable buffer size (defaults to 100).
- Features
- Prerequisites
- Setup
- Running the Application
- Testing
- CI/CD Workflow
- API Endpoints
- Configuration
- Cleanup
- REST API with
/submit
endpoint - In-memory buffering with automatic flush to SQLite
- Configurable buffer size
- Docker and Kubernetes support
- Comprehensive test suite
- Python 3.11+
- Docker Desktop (for containerized deployment)
- Docker Compose (for containerized deployment using
docker-compose
only) - kubectl and Minikube (for Kubernetes deployment)
git clone https://github.com/Yikuan-Tu/api-buffering-system.git
cd api-buffering-system
# Create the .env file with the required environment variables
echo "DB_PATH=./data/database.db
BUFFER_SIZE=100
LOG_LEVEL=INFO" > .env
# Create and activate virtual environment
python -m venv venv
source venv/bin/activate # Linux/MacOS
venv\Scripts\activate # Windows
# Install dependencies
pip install -r requirements.txt -r requirements-dev.txt
# Start the FastAPI server
uvicorn app.main:app --reload
# Access API at http://localhost:8000
# In another terminal, run test suite
pytest tests/ -v
# Run the load test script
python tests/test_submit.py
# Build and start api server
docker-compose up api
# Access API at http://localhost:8000
## In another terminal
# Run test suite in container
docker-compose run tests
# Run the load test script against container
python tests/test_submit.py
# Build the image
docker build -t api-buffering-system --target production .
# Run the container
docker run -p 8000:8000 api-buffering-system
# Access API at http://localhost:8000
# In another terminal, run the load test script against container
python tests/test_submit.py
# Start Minikube
minikube start --profile=dev-cluster
# Build image in Minikube's Docker context
eval $(minikube --profile=dev-cluster docker-env)
docker build -t api-buffering-system:latest --target production .
# Deploy to Kubernetes
kubectl apply -f k8s/namespace.yaml
kubectl apply -f k8s/
# Access the service
kubectl port-forward svc/api-buffering-system 8000:8000 -n api-buffering-system
# In another terminal, run the load test script against cluster
python tests/test_submit.py
pytest tests/unit/ -v
pytest tests/integration/ -v
python tests/test_submit.py
The GitHub Actions workflow (/.github/workflows/ci.yml
) performs:
- Code checkout
- Python 3.11 setup
- Linting checks (Black and Ruff)
- Automated Unit and integration tests
GET /
: Welcome messageGET /health/
: Health checkGET /docs/
: Swagger page for API referenceGET /count/
: Current record count in databasePOST /submit/
: Submit person records (JSON array)
Example request:
curl -X POST http://localhost:8000/submit/ \
-H "Content-Type: application/json" \
-d '[{"first_name":"John","last_name":"Doe"}]'
Environment variables:
DB_PATH
: Database file path (default:/data/database.db
)BUFFER_SIZE
: Records before flush (default:100
)LOG_LEVEL
: Logging level (default:INFO
)
docker-compose down -v
kubectl delete -f k8s/
minikube stop --profile=dev-cluster
minikube delete --profile=dev-cluster