case study

Kosh

A B2B procurement platform that watches warehouse stock, sends RFQs to suppliers, ranks their quotations and generates invoices automatically when stock is dispatched.

Type
College group project · Software Incubator
My part
Backend APIs (Django REST), deployment and monitoring
Stack
Django REST · PostgreSQL · Redis · Celery · Channels · Docker · AWS · OpenTelemetry
Links
Live ↗ GitHub ↗
Kosh landing page

01 / problem

Problem

Small businesses reorder stock by hand: checking levels, messaging suppliers, comparing quotes and typing invoices. It's slow, and mistakes are easy to miss.

To avoid the hectic manual paperwork and repititive tasks, we created Kosh, focused on automating procurement between businesses: one central warehouse, suppliers on the other side, and as few manual steps as possible between "stock is low" and "stock has arrived".

02 / architecture

Architecture

architecture
API
Django REST Framework (APIView), Gunicorn behind Nginx with HTTPS, all in Docker Compose on EC2.
Background jobs
Celery worker and Celery Beat in their own containers, with Redis as the broker. Beat runs the stock checks and reminders.
Real-time
Django Channels on a separate Daphne container that only serves /ws/, with Redis as the channel layer.
Data
PostgreSQL on AWS RDS, media on S3, and Redis also used for OTP tokens, cooldowns and caching.
Deploys
GitHub Actions: pushing to the production branch deploys over SSH and rebuilds the containers.
Observability
OpenTelemetry traces to Tempo, metrics to Prometheus, both in Grafana, as a separate Compose project.

03 / flow

Procure-to-pay flow

  1. Stock check

    Celery Beat compares warehouse stock with each product's reorder point.

  2. Low-stock alert

    Products below their reorder point get an alert.

  3. Automatic RFQ

    An RFQ goes to suppliers in the same supply category, optionally including verified suppliers from the platform pool.

  4. Quotations

    Suppliers quote from their own panel.

  5. Ranking

    Quotations are ranked by a weighted score, and the owner sees them in suggested order.

  6. Purchase order

    Accepting a quotation creates a purchase order.

  7. Advance payment

    The order is confirmed only after the server verifies the payment.

  8. Dispatch + invoice

    When the supplier dispatches, a shipment is recorded and the invoice is generated automatically.

  9. Goods receipt

    The goods receipt note (GRN) records what actually arrived, and discrepancies are found automatically.

  10. Ledger + balance

    Stock is updated through the ledger, alerts close, and the remaining balance becomes payable.

04 / decisions

Key decisions

Ledger-based stock

Current stock is the sum of StockMovement rows, each with a reason: goods received, dispatch, damaged, adjustment or opening balance. Product.quantity is only a cached total, and only one function, record_movement(), can change it.

Why: every change in stock can be traced back to a reason, and the cached total can always be checked against the ledger.

Reorder point with z-score safety stock

Safety stock comes from how much daily dispatches vary (their standard deviation), scaled by a z-score for the service level. Until a product has enough dispatch history, its manual threshold is used instead.

inventory/reorder.py
safety_stock  = z * std_daily_dispatch * sqrt(lead_time_days)
reorder_point = avg_daily_dispatch * lead_time_days + safety_stock

# not enough history yet → product's manual threshold

Why: products with unsteady demand get more buffer than steady ones, and it still works on day one.

Weighted quotation ranking

Each quotation is scored on price (50), delivery (25), payment terms (10) and supplier reliability (15). Reliability comes from past goods-receipt history, and the owner can pass different weights. Scores are calculated per request, not stored.

Why: the owner gets a clear suggested order but still makes the final choice.

Idempotent, server-verified orders

Accepting a quotation locks the RFQ row inside a database transaction. confirm_order() runs only after the server has verified the payment, using the advance amount stored in the database, and running it twice does no harm.

Why: a repeated webhook or a double click can't create a second order or confirm the wrong amount.

05 / hard parts

The hard parts

Real-time notifications without touching the HTTP server

Problem

Owners and suppliers needed live updates (low-stock alerts, quotations, orders), but the API runs on Gunicorn, which doesn't handle WebSockets.

What I did

I added Django Channels on a separate small Daphne container that serves only /ws/, so the HTTP server stayed unchanged. A connection authenticates with a one-time ticket from a normal API call (valid for 30 seconds), and each user gets their own group. Pushes are sent only after the database transaction commits, so nobody is notified about data that was rolled back.

Adding observability without risking the live API

Problem

I needed traces and metrics on a production server that was already running the API, without any downtime.

What I did

I put the monitoring stack (OpenTelemetry Collector, Tempo, Prometheus, Grafana, node-exporter) in its own Compose project, with memory limits and ports bound to localhost. Tracing is switched on and off by an environment variable, with about 10% sampling, so it rolled out in stages: host metrics first, then the tracing code with tracing off, then tracing on.

Automatic RFQs that didn't fire

Problem

In the demo, the Celery task skipped most low-stock alerts. It only created RFQs when the reorder point came from the formula, and most demo products didn't have enough history yet.

What I did

I changed the rule so every low-stock alert gets an RFQ automatically, whether it came from the formula or a manual threshold. The owner keeps one setting: whether verified platform suppliers are included.

Invited employees locked out

Problem

Roles aren't stored. They come from records (an Employee record makes you an employee). An invited employee who logged out before accepting had no record, so login returned no role, and the app had no way to know where to send them.

What I did

Shifted the role picker to after Login, instead of after SignUp. Got to know that this is a common mistake many beginner developers do.

One small server, many processes

Problem

The API, Celery worker, Celery Beat, Redis, Daphne and later the monitoring stack all share one EC2 instance, which started with 1 GB of RAM.

What I did

I kept PostgreSQL on RDS instead of on the instance, ran the worker (concurrency 2) and Beat as separate containers, set memory limits on every monitoring container, and resized the instance from 1 GB to 2 GB, then 4 GB, as services were added.

07 / screenshots

Screenshots

Kosh owner dashboard
owner dashboard
Kosh quotation comparison screen
quotation ranking