DEV Community

akashkore5
akashkore5

Posted on Originally published at khetisetu.com

Building KhetiSetu: An Offline-First Farmland Marketplace for Rural India

Building KhetiSetu: An Offline-First Farmland Marketplace for Rural India

Designing a marketplace for urban audiences is a well-trodden path. You deploy a React app to a CDN, plug in a CMS, and assume your users have stable fiber or 5G. However, when your target user is a smallholder farmer in rural Karnataka or outside Bhatinda, those assumptions collapse instantly.

In these regions, "4G" is often just a marketing term. The reality involves high latency, frequent packet loss, and devices with limited storage. When we began building the farmland rental marketplace at KhetiSetu, we quickly realized that a standard Next.js setup would fail the moment a user drifted 500 meters away from the nearest cell tower.

The core engineering challenge wasn't just matching landowners with tenants; it was ensuring that listings could be uploaded and viewed on a 50kbps connection without the browser timing out.

The Bandwidth Paradox: Performance as Accessibility

In the context of rural tech, performance isn't a luxury feature—it is the baseline for accessibility. If a page takes ten seconds to load, the user assumes the service is broken. We had to abandon heavy client-side hydration in favor of a strategy that prioritizes "Time to Content."

Our architecture leans heavily on Next.js’s Incremental Static Regeneration (ISR). Since land listings don't change by the second, we pre-render pages at the edge. However, the most significant hurdle is the "Listing Creation" flow. A farmer typically needs to upload four to five high-resolution photos of their soil and irrigation setup. On a spotty connection, a 15MB upload will fail nine times out of ten.

We solved this by implementing a client-side compression pipeline before the data ever hits the network.

// A simplified version of our client-side image processor
async function processFarmImage(file) {
  const options = {
    maxWidthOrHeight: 1200,
    useWebWorker: true,
    initialQuality: 0.6,
  };

  try {
    // We use browser-image-compression to shrink the file locally
    const compressedFile = await imageCompression(file, options);

    // We generate a tiny BlurHash for the loading state
    const blurDataURL = await generateBlurHash(compressedFile);

    return {
      file: compressedFile,
      preview: blurDataURL
    };
  } catch (error) {
    console.error("Compression failed, falling back to original", error);
    return { file, preview: null };
  }
}
Enter fullscreen mode Exit fullscreen mode

By shifting the heavy lifting to the user’s device (which is often more powerful than the network it's connected to), we reduced our average payload size by 85% without sacrificing the visual clarity needed to judge land quality.

Designing for Intermittent Connectivity

A farmer might start listing their land while in the field but lose signal before hitting "Submit." If the page refreshes and they lose their data, they won't try again. We treat the local device as the primary source of truth using an "Offline-First" sync pattern.

We use IndexedDB via the Dexie.js wrapper to persist form state. This ensures that even if the browser is closed or the phone reboots, the progress remains intact.

The Sync Lifecycle

State Action UI Feedback
Draft Saved to IndexedDB "Saved locally" icon
Pending Network request initiated "Syncing when online..."
Retrying Exponential backoff on 5xx or timeout "Weak signal, retrying..."
Synced Confirmed by KhetiSetu API "Listed Successfully"

This lifecycle is managed by a background Service Worker. When the browser detects the onLine event, it triggers a sync of any records sitting in the "Pending" store. This allows the user to click "Submit" while offline and put their phone back in their pocket, knowing the platform will handle the rest once they reach the village square.

Localized State and Vernacular Routing

Language is another major barrier. A marketplace for India cannot be English-only. While Next.js provides built-in i18n support, we had to optimize it for low-memory devices. Loading a massive JSON translation file for five different languages is a waste of resources.

We split our translation bundles by page and use a custom hook to fetch only the required strings. Furthermore, we found that "Transliteration" is often more useful than pure translation. Many farmers use a mix of local dialect and English technical terms (e.g., "Borewell," "Registry," "Lease").

At KhetiSetu, we built a glossary that maps these terms to ensure that a search for "Pani" or "Water" yields the same irrigation-related results, regardless of the user's primary language setting.

The Infrastructure: Edge over Origin

To keep latency low, we can't afford a round-trip to a central server in Mumbai for every request coming from Bihar. We utilize a multi-layered caching strategy:

  1. Edge Caching: Static assets and ISR pages are cached at edge nodes closest to the user.
  2. Stale-While-Revalidate: We serve the cached version of a land listing immediately while fetching the update in the background.
  3. Regional Data Sharding: While our primary DB is centralized for consistency, we use read-replicas to speed up regional queries.

The following Mermaid diagram illustrates how we handle a listing request in a low-bandwidth scenario:

sequenceDiagram
    participant U as Farmer Device
    participant SW as Service Worker
    participant E as Edge Node (Next.js)
    participant API as KhetiSetu Backend

    U->>SW: Request Listing Page
    SW->>SW: Check Cache (IndexedDB)
    alt Is Cached?
        SW-->>U: Return Stale Content (Immediate)
    else Not Cached
        SW->>E: Fetch via Edge
        E-->>SW: Return Compressed HTML/JS
        SW-->>U: Render Page
    end
    SW->>API: Background Revalidate (if online)
    API-->>SW: New Data
    SW->>U: Update UI silently

Lessons from the Field

If you are building for the next

Top comments (0)