Offline-first and sync

How do apps work without internet?

Apps work without internet by keeping the data and logic they need on the device. They read and write a local database, queue the changes that must reach a server, and sync when a connection returns. Some can also exchange data directly with nearby devices over Bluetooth or a local network.

Learning objectives

After reading this article you will be able to:

  • Explain why thin-client apps fail when the network drops
  • Describe how a local database and an outbox keep an app working offline
  • List the design habits that make offline support easier to build

What breaks when an app depends on the network

Many apps are thin clients. Each tap sends a request to a server, and the screen waits for the answer. When the connection drops, the app shows a spinner, then an error, and anything typed into a form may be lost. The app has not failed; it never had the data it needed in the first place.

An app that works offline is built the other way round. It assumes the network may be absent and treats a working connection as something to use when it is there.

Keep the data on the device

The first step is a local copy of the data the person needs. Phone apps usually store it in an embedded database such as SQLite, which runs inside the app with no server. Web apps can use IndexedDB in the browser. The app reads from this local copy, so screens load at once whether or not there is a connection.

Deciding what to keep is a design question. A field technician’s app might hold today’s jobs, the manuals for the equipment on them, and the forms to fill in, but not the company’s entire history. The local copy has to fit on the device and be refreshed when a connection allows.

Write locally, then send

When the person makes a change, the app writes it to the local database straight away and shows it as done. This is sometimes called an optimistic update. The change also goes into a queue, often called an outbox, of things that must reach the server.

When a connection returns, the app works through the outbox. Each change carries a stable identifier, so if the app sends it, loses the connection before hearing back, and sends it again, the server can recognise the repeat and apply it once. This property is called idempotency, and offline apps depend on it, because retries are normal rather than exceptional.

Bring copies back together

While a device is offline, other people may change the same data. When it reconnects, the app has to merge what it did with what happened elsewhere. Sometimes the changes touch different things and combine cleanly. Sometimes they collide, and the app needs a rule for which change wins, or a way to merge both.

How does offline sync work? and How are sync conflicts resolved? cover this in detail.

Talk to nearby devices directly

Syncing with a server only helps once the server is reachable. If two people are side by side in a basement, on a ship, or at a site with no coverage, neither can reach the server, but both may need the same information now.

For that, apps can exchange data directly between devices, over Bluetooth Low Energy or a local network, and sometimes relay through other devices in a mesh. The server still becomes the system of record when it is reachable again; the direct link covers the time in between.

Design for it from the start

Offline support is hard to add to an app built around constant requests, because the assumption that the server answers is spread through every screen. It is easier when it is part of the design:

  • Show sync state honestly. Let people see what is saved on the device, what is waiting to send, and what has been confirmed by the server.
  • Separate local success from final success. Saving a record on the device is not the same as the backend accepting it. The app should know the difference and handle a later rejection.
  • Decide what needs a live answer. Payments, unique bookings, and permission changes may need to wait for a connection. Most other work does not.
  • Test without the network. Use airplane mode, slow and flaky connections, and reconnection after long gaps, not just a working connection that happens to drop.

Frequently asked questions

Can a web app work offline?

Yes, within limits. A service worker can cache the app's files and answer requests when there is no connection, and IndexedDB can store data in the browser. Browsers do not let web pages open Bluetooth links freely in the background, so device-to-device work usually needs a native app.

What should still require a connection?

Anything where one central authority must decide at the moment it happens, such as charging a card or claiming the last unit of stock. An offline app can record the request and complete it when it reconnects, but it should not pretend the decision has been made.

Sources

  • About SQLite. The embedded database most phone apps use for local storage
  • IndexedDB API. Local storage for web apps
  • Service Workers. The W3C specification that lets web apps load and handle requests offline
  • What the SDK handles. Offline Protocol's split between the SDK, your application, and your backend

Build it with Offline Protocol

Offline Protocol adds the device-to-device part to an offline-capable app, messages, shared documents, and service calls between nearby devices, while your backend stays the system of record. The docs explain what the SDK handles and what stays with you.

Read what the SDK handles