Aakash Devkar
← All projects

Full-stack · ML · Oct 2025 – Feb 2026

AgriFlow

Farm-to-buyer crop marketplace with ML price forecasts, online payments and live AWS deployment.

faster p95 on admin orders
21×
faster p95 on admin orders
error rate at 50 users
38.7% → 0%
error rate at 50 users
production p95 on browsing APIs
60 ms
production p95 on browsing APIs

01

The problem

AgriFlow is a marketplace where farmers sell crops directly to buyers, with a quality inspection and tracked delivery in between, plus price and demand predictions to help price the crops. A farmer lists a lot with photos, a field agent inspects it, an admin approves and lists it, a buyer orders and pays an advance or the full amount online, and an agent delivers it.

02

Architecture: order and payment flow

  1. 1

    Web or mobile client

    React web app on S3 + CloudFront, or the Expo app; both call one API over HTTPS

  2. 2

    Nginx on EC2

    TLS 1.2/1.3 with Let's Encrypt and HSTS in front of the API container

  3. 3

    Express API

    JWT + requireRole; checks the crop is listed, stock, address and mobile; computes the total on the server

  4. 4

    PostgreSQL on RDS

    Order saved as pending, over verified TLS

  5. 5

    Razorpay order

    getAmountDue() works out the advance or balance in integer paise

  6. 6

    Payment verify

    HMAC signature check, amount re-fetched from Razorpay, idempotent atomic SQL update and a JSONB audit-log row

Price and demand predictions go from the API to an internal Flask service with no public port. Images upload straight from the browser to a private S3 bucket via presigned POST.

03

Design decisions

ML in a separate Flask service, called only by the API

Instead of
Running the models in Node, or calling ML from the clients
Why
The models are scikit-learn and XGBoost pickles. The service has no public port; the API proxies it with JWT auth and returns 503 if it's down.

Presigned S3 POST for images

Instead of
Uploading through the API to local disk (the earlier approach)
Why
S3 enforces the 5 MB limit and the content type, the API never handles file bytes, and files survive container rebuilds.

Money in integer paise

Instead of
Floating-point rupees
Why
No float rounding when comparing the advance, the total and the amount paid.

Deploys through GitHub OIDC + SSM Run Command

Instead of
Stored AWS keys, or SSH
Why
No long-lived keys and no open SSH port.

04

What I built

  • Four roles on web and mobile: farmers list crops, field agents file quality inspections and update deliveries, admins approve listings and manage users, KYC, transport and farmer payouts, and buyers order and pay.
  • Crop lifecycle pending → inspected → approved/rejected → listed → sold, enforced by database CHECK constraints.
  • Express API with 58 HTTP endpoints across 9 role-guarded routers, JWT auth and bcrypt, on PostgreSQL (9 tables).
  • Public marketplace with server-side pagination, search, category and price-range filters, and sorting.
  • Razorpay advance and balance payments: HMAC signature check, server-side amounts, idempotent verification, a payment audit log and admin farmer settlement; mobile checkout through a short-lived checkout token.
  • Price prediction (Random Forest: min, max and modal price) and demand prediction (XGBoost) in a Flask service, proxied by the API.
  • Direct-to-S3 image uploads via presigned POST, limited to JPG, PNG and WEBP up to 5 MB.
  • React + TypeScript web app (22 pages) and Expo React Native app (23 screens).

05

Hard problems

Unpaginated endpoints failing under load

Problem
GET /api/crops returned all 2,000 crops in every status (842 KiB), and the admin orders endpoint returned all 10,000 orders (6.4 MiB). At 50 concurrent users the large responses held database pool connections until the 2-second connect timeout turned the waiting into HTTP 500s.
Fix
LIMIT/OFFSET pagination with total-count headers. The public listing returns listed crops only (default 20, max 100) with server-side search, filters and sort, and the web and mobile apps switched to “load more”.
Verified
k6, 50 users × 60 s, median of 3 on a re-seeded database: admin orders p95 20.5 s → 974 ms and errors 38.7% → 0%; crops p95 3.93 s → 455 ms and errors 47.3% → 0%.

A 208 MB model reloaded on every request

Problem
joblib.load ran on every price request. Each load took 0.646 s and added about 216 MB, and under 20 concurrent users free RAM on a 16 GB laptop dropped to 7 MB.
Fix
An in-process model cache preloaded at startup, later downloaded from S3 into a Docker volume and served by waitress.
Verified
200 sequential requests, median of 3: price p50 738.7 → 183.8 ms (4.0×), demand 15.8 → 5.2 ms (3.0×).

Advance payments were recorded as full payments

Problem
Buyers could choose an advance, but checkout always charged the full total and verification marked the order fully paid. One column held both the advance the buyer chose and what was actually paid.
Fix
A new amount_paid column with a backfill, one getAmountDue() function in integer paise, the amount taken from Razorpay's payments.fetch, an atomic SQL increment with a payment-id idempotency guard, and advance-paid or fully-paid derived in SQL.
Verified
Code change only (commit d65b242); no automated test yet.

06

Results

Admin orders p9520.5 s → 974 ms (21×)
Admin orders throughput / errors4.9 → 81.5 req/s; 38.7% → 0%
Admin crops p954,514 → 247.5 ms
Public crops p95 / errors3.93 s → 455 ms; 47.3% → 0%
Price prediction median738.7 → 183.8 ms (4.0×)
Production load test (3 min)879 requests, 0% errors; browsing p95 60 ms, price p95 198 ms

Before/after numbers are k6 runs on localhost against a seeded database (10,000 orders, 50 users, median of 3). The production run was 3 minutes of light load (10 users plus 1 prediction per second) from one laptop. Payments run in Razorpay test mode.

07

Deployment

ProviderAWS ap-south-1 (Mumbai)
WebReact build on S3, served by CloudFront at agriflow.me
API & MLOne EC2 instance running Docker Compose: Nginx (Let's Encrypt), the Express API and the Flask ML service, with health checks and memory limits
DataRDS PostgreSQL over verified TLS; private S3 buckets for uploads and models
Secrets & logsSSM Parameter Store → env file at deploy time; CloudWatch Logs for all 3 containers
CI/CDGitHub Actions via OIDC: the backend deploys through SSM Run Command, waits for healthy containers and checks /api/health; the frontend syncs to S3 and invalidates CloudFront

08

Stack

Node.jsExpressPostgreSQLReactTypeScriptExpoPythonFlaskscikit-learnXGBoostRazorpayDockerNginxAWSGitHub Actionsk6