> ## Content Index
> Fetch the complete content index at: https://blog.netpy.in/llms.txt
> Use this file to discover other available public pages before exploring further.

# Designing MilkPe for a farmer with a ₹6,000 phone and patchy 3G
- URL: https://blog.netpy.in/milkpe-offline-first-dairy-app/
- Published: 2026-12-06T04:36:36.000Z
- Updated: 2026-12-06T04:36:36.000Z
- Description: A field-collection app is decided by the device and the connection, not the feature list. The ledger sync model, FAT/SNF pricing in integer paise, payload budgets in bytes, and the three things that broke first.
- Author: Netpy Technologies
- Tags: MilkPe, AgriTech, Case Study, Engineering

Every dairy app demo looks the same: a clean phone, full signal, a spreadsheet behind it. Then it goes to a village at 6am, and the phone is a ₹6,000 Android with 900MB of free RAM, the signal is one bar of 3G that dies when a tractor passes, and the person holding it has forty farmers waiting in a queue. We built [MilkPe](https://milkpe.netpy.in/?ref=blog.netpy.in) for that second scenario, not the first.

These are the design decisions that came out of it — the arithmetic, the sync model, the payload budgets, and the failure modes that showed up in the field rather than in the emulator. It is not a case study with a happy percentage at the end. It is the list of constraints we would hand to anyone building a field-collection app for rural India.

## The phone is the specification, not the constraint

A ₹6,000 Android is roughly a quad-core A53, 2GB of RAM of which under 1GB is free, 16GB of storage with about 3GB usable, and a 540×960 screen that is dim and slightly yellow. Assume Android 10 Go. Assume the battery optimisation layer will kill your background work and never tell you.

That budget rules out most obvious stacks before you write a line of code. A React Native or Flutter release with a few libraries lands at 40-60MB — a five-minute download at 20 kbps. We set a hard ceiling of 12MB for the install and enforced it in CI, because it decides whether the app ever gets updated. Peak memory stays under 180MB; 400 rows rendered naively with images will blow through it, so rows are text-only and paginated at 50.

The other number that matters is cold start. Past about 2.5 seconds the agent starts the app while the farmer is already standing there, which is how you get a queue at 6:10am. Measure it on the cheapest device in the office, not an emulator: emulators have 8GB of RAM and no thermal throttling and will lie to you about every number on this page.

## Milk is priced in grams, so the app is an arithmetic engine

Dairies do not buy litres. They buy fat and solids-not-fat, weigh the milk, and pay against a two-dimensional rate table. The calculation, in its general form:

```
kg      = litres * density            # ~1.030 cow, ~1.032 buffalo
per_kg  = fat_pct * fat_rate + snf_pct * snf_rate
amount  = kg * per_kg

```

With a fat rate around ₹700/kg of fat and SNF at ₹400/kg of SNF, 10 litres of cow milk at 1.030 density, 4.2% fat and 8.6% SNF gives 10.30 kg and ₹657.14\. Change the fat rate by ₹10 and every entry in the village shifts.

None of that may touch a float. Money is stored as integer paise, percentages as basis points, and the rounding rule is a single function that the client and the server both import — round half up at the paise, then settle the shift total against the sum of entries rather than against the sum of rounded entries. Two different rounding implementations is the bug that generates support tickets you cannot win: a one-paisa difference, multiplied by 400 farmers and 30 days, is a farmer who is certain the software is stealing from him and a plant accountant who cannot find the missing ₹120,000.

The rate table itself is versioned and downloaded, never hardcoded. When a dairy revises its fat rate mid-month, entries already recorded must re-price against the table that was live at that moment, which means every entry carries a `rate_table_id`. Retroactive repricing is then a report, not a data migration.

## Offline-first is a ledger, not a cache

The tempting design is "save it locally, sync when online, last write wins". That works until two collection points record the same farmer in the same shift and the app has to guess which weight is real. With money attached, guessing is not an option.

MilkPe treats every collection as an immutable event: `(device_id, local_seq, shift_id, farmer_id, litres, fat, snf, rate_table_id, captured_at)`. The device increments `local_seq` monotonically and never reuses it. The server assigns an authoritative `server_seq` on acceptance and deduplicates on the pair `(device_id, local_seq)`. Retrying the same outbox batch ten times is therefore free — the eleventh copy is rejected as already applied instead of creating a phantom 12-litre entry.

The outbox is a state machine: `pending → in_flight → acked`, with `failed` only after ten attempts, and a visible count on the home screen. Agents do not trust "saved" — they trust a number that goes down. Backoff is 2, 4, 8, 16, capped at 60 seconds, with jitter, because 60 devices coming back online at the end of a shift will otherwise hit the API in the same 200ms.

Deletes are separate events, not row removals: a mistaken entry is reversed with a reference to the original `server_seq`, so the shift total is a fold over the ledger and the audit trail cannot drift from the current state.

## Patchy 3G decides the payload, so design for bytes

A single collection entry as JSON, field names included, is about 180 bytes. Sixty farmers at one collection point is 11KB raw — but the request spends more time in TLS setup and DNS than in transfer, so batching is the whole optimisation. Compressed, 11KB of that structure drops to roughly 1.2KB. At an effective 2.5 KB/s, the batch lands in under a second once the connection exists, and the value of batching is that you pay the round-trip cost once per shift instead of once per farmer.

Photos are where this falls apart. A 250KB proof-of-delivery image per entry, 60 entries, is 15MB per collection point per shift — impossible on a metered 1GB/month SIM, and impossible on the battery. When we tried it the queue never drained and the app reported "offline" while the phone had full signal, because the queue was four hours long. It is gone. If a photo is genuinely required for a dispute it is captured at 640px, quality 60, about 45KB, and only uploaded outside the shift window.

## Clocks lie, and the 6am cutoff does not care

Cheap Android devices have drifting clocks, users who set the date wrong to dodge something, and batteries that die mid-shift and come back at midnight. If your shift attribution depends on device time, you will file Tuesday's milk into Monday and the reconciliation will be off by a village.

So the server assigns the shift. The agent opens a session with a collection-point code and a server timestamp, every entry references that `shift_id`, and `captured_at` is stored but never trusted for attribution. Ordering within a shift comes from `local_seq`. The UI shows the pending count and the oldest queued entry, never "last synced 4 hours ago" — elapsed time is meaningless when the device may believe it is 2019.

## Three users, one binary

The buyer is the dairy. The user is the collection agent. The person whose money is at stake is the farmer, who installs nothing. All three shapes have to fit inside a 12MB download.

That means the agent's path gets the entire design budget. Recording a collection is one screen, no navigation stack: farmer picked from a recent list at the top, three numeric fields, a total that recalculates on keystroke, and a confirm target large enough to hit with a thumb while holding a milk can. The farmer list is ordered by recency and searchable by the first three characters of a name, because names in a village are not unique and the agent knows who he is looking for.

Contrast is set for sunlight at 200 nits — heavier than the WCAG minimum, and it looks wrong on a designer's monitor. Touch targets are 48dp. Nothing relies on hover, long-press or an undiscoverable swipe. Numbers open a numeric keypad, because a full QWERTY on 540px covers the entry the agent is trying to verify.

## What broke first

Battery optimisation, before anything else. Aggressive OEM power managers kill background sync, so the queue silently stops draining and the agent only finds out at settlement. Foreground service with a persistent notification during the open shift, WorkManager with an explicit expedite request for the close-out, and an on-screen queue count that is impossible to miss.

Reinstall was the second. Wiping app data resets `local_seq`, and a naive server accepts the new numbers as fresh entries. The fix: the device registers an install id, the server refuses any `local_seq` lower than the highest seen for that install, and the app pulls the authoritative ledger to rebuild.

SIM data exhaustion was the third, and it is not an engineering problem — only a design one. The app has to keep working indefinitely with zero connectivity, and say so honestly.

## What we would tell the next team

Start from the device and the connection, not the feature list. Write the money arithmetic in integers on day one and make both ends share it. Model collection as an append-only ledger with an idempotency key, so retries are boring. Then budget your payload in bytes and cut every feature that does not fit — a photo of a milk can is worth less than a queue that drains.

The rest of MilkPe — route sheets, farmer ledgers, plant settlement — sits on that base, and none of it would matter if the first screen did not work at 6am on one bar of signal. If you are building something in that class, [talk to us](https://netpy.in/?ref=blog.netpy.in).

---

*Header photo: "Milkman in Thodupuzha" by* [*Rainer Halama*](https://commons.wikimedia.org/wiki/File:Milkman%5Fin%5FThodupuzha-WUS-06521.jpg?ref=blog.netpy.in)*,* [*CC BY-SA 4.0*](https://creativecommons.org/licenses/by-sa/4.0?ref=blog.netpy.in)*, via Wikimedia Commons. Cropped and resized.*