Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SQLite Offline Data: Whole File vs Row Level Refresh

· 6 min read · Home Tech

In this article
  1. Background: Why This Question Matters
  2. How Whole-File Refresh Works
  3. How Row-Level Refresh Works
  4. Comparison: Speed, Bandwidth, and Complexity
  5. How to Choose: A Decision Guide
  6. Common Problems and Pitfalls
  7. Costs and Operational Considerations
  8. Alternatives to Consider
  9. Frequently Asked Questions

When syncing SQLite data on a mobile or desktop app, refreshing the whole database file is simpler and often faster for small datasets, while row-level refresh is better for large, frequently changing datasets that need to minimize bandwidth and merge conflicts. The right choice depends on your data size, sync frequency, and how much control you need over conflict resolution.

Background: Why This Question Matters

SQLite stores data in a single file on disk. Apps that work offline (like note-taking apps, fitness trackers, or inventory systems) need to keep that local file in sync with a remote server. The two main strategies are:

  • Whole-file refresh: Download the entire SQLite database file (or a compressed copy) and replace the local file.
  • Row-level refresh: Send only the changed rows (inserts, updates, deletes) between the client and server, applying them incrementally.

Each approach has distinct trade-offs in terms of speed, bandwidth, complexity, and conflict handling. Understanding these trade-offs is key to choosing the right one for your app.

How Whole-File Refresh Works

With whole-file refresh, the server maintains a canonical copy of the database. The client downloads that file (often compressed) and replaces its local copy. This can be done in a few ways:

  • Direct download: The server exposes a URL to the current database file, and the client fetches it.
  • Delta compression: The server sends only the changes needed to transform the old file into the new one (e.g., using binary diff tools like bsdiff or rsync algorithms).
  • File-level versioning: The client checks a version ID (e.g., a hash or timestamp) and downloads a new file only if the version changed.

This approach is simple to implement because you don’t need to track individual row changes. It also guarantees that the local database is always a byte-for-byte copy of the server’s, which eliminates the risk of partial updates or missing transactions.

How Row-Level Refresh Works

Row-level refresh involves tracking changes at the row level. Common mechanisms include:

  • Change tracking columns: Add a last_modified timestamp or a dirty flag to each row. The client sends rows where the flag is set or where the timestamp is newer than the last sync.
  • Sync logs: Maintain a separate table that records every insert, update, and delete operation with a sequence number. The client and server exchange logs to reconcile.
  • Triggers: Use SQLite triggers to automatically record changes to a sync log when data is modified.

During a sync, the client uploads its changes, downloads the server’s changes, and applies them. Conflict resolution is needed when the same row was modified on both sides. Common strategies include “last write wins,” “client wins,” “server wins,” or merging specific fields.

Comparison: Speed, Bandwidth, and Complexity

Here’s a side-by-side look at the key differences:

  • Speed: Whole-file refresh is often faster for small databases (a few MB) because it’s a single download and replace. Row-level refresh requires multiple queries and network round-trips, which can be slower for small changes.
  • Bandwidth: Row-level refresh uses far less bandwidth when only a few rows change. Whole-file refresh always transfers the entire database, even if only one row changed.
  • Complexity: Whole-file refresh is much simpler to implement. Row-level refresh requires change tracking, conflict resolution, and careful handling of transactions.
  • Consistency: Whole-file refresh guarantees a consistent snapshot. Row-level refresh can have transient inconsistencies if changes are applied out of order.

How to Choose: A Decision Guide

Consider these factors when deciding:

  • Database size: If your database is under 10 MB and grows slowly, whole-file refresh is likely sufficient. For larger databases, row-level is more practical.
  • Sync frequency: If you sync infrequently (e.g., once a day), whole-file refresh is fine. If you sync often (e.g., every few seconds), row-level reduces bandwidth and battery drain.
  • Conflict handling: If multiple users edit the same records, you need row-level conflict resolution. Whole-file refresh simply overwrites local changes, which can lose data.
  • Offline editing: If users make changes offline that must be merged later, row-level is essential. Whole-file refresh would discard those local changes.
  • Development resources: If you have limited time, start with whole-file refresh and switch to row-level only if needed.

Common Problems and Pitfalls

Both approaches have known issues:

  • Whole-file refresh:
    • Large downloads can be slow on poor networks.
    • Replacing the file while the app is using it can cause crashes. Use atomic file replacement (write to a temp file, then rename).
    • Loses any local changes made since the last sync.
  • Row-level refresh:
    • Tracking changes adds overhead to every write operation.
    • Conflict resolution is hard to get right. “Last write wins” can silently overwrite important data.
    • Deleting rows requires careful handling (e.g., tombstones) so that deletes propagate correctly.
    • Syncing out of order can violate foreign key constraints.

Costs and Operational Considerations

Beyond development time, consider the ongoing costs:

  • Server storage: Whole-file refresh requires storing full copies or deltas. Row-level requires storing change logs, which can grow large.
  • Bandwidth costs: Row-level reduces bandwidth, which is important for mobile users on metered plans.
  • Battery and CPU: Whole-file refresh uses more battery on mobile devices due to the download and file replacement. Row-level uses more CPU for change tracking.
  • Backup and recovery: Whole-file refresh makes server-side backups trivial (just copy the file). Row-level requires replaying logs to reconstruct state.

Alternatives to Consider

If neither approach feels ideal, there are other options:

  • Hybrid approach: Use whole-file refresh for initial sync or after a schema change, and row-level for subsequent incremental syncs.
  • Third-party sync frameworks: Tools like Couchbase Lite, Realm (now Atlas Device SDK), or PowerSync provide built-in sync engines that handle row-level sync and conflict resolution. These add a dependency but save development time.
  • Using SQLite’s backup API: Instead of replacing the file, use the SQLite Online Backup API to copy the database while it’s in use. This is safer than file replacement but still transfers the whole database.

Frequently Asked Questions

Can I mix both approaches?

Yes. Many apps use whole-file refresh for the initial download or after a schema migration, then switch to row-level sync for ongoing updates. This gives you the simplicity of a full snapshot when needed and the efficiency of incremental changes.

How do I handle conflicts in row-level sync?

Common strategies include last-write-wins (based on timestamps), client-wins, server-wins, or field-level merging. The best choice depends on your data. For example, a note-taking app might merge text fields, while a counter app might use last-write-wins.

Is whole-file refresh ever a security risk?

If the database contains sensitive data, downloading the entire file over the network increases the risk of interception. Always use HTTPS. Also, if the server file is corrupted, you could propagate that corruption to all clients. Row-level sync is more resilient to such issues.

What happens if the app crashes during a whole-file refresh?

If you replace the file atomically (write to a temp file, then rename), a crash will leave the old file intact. If you overwrite in place, you may end up with a corrupted database. Always use atomic replacement.

Do I need to track changes for row-level sync?

Yes, you need a way to know what changed since the last sync. This can be done with timestamps, a dirty flag, or a separate change log. Without tracking, you’d have to scan the entire database each time, which defeats the purpose.

Which approach is better for a small app with a few users?

Whole-file refresh is usually sufficient. It’s simple, reliable, and easy to debug. You can always migrate to row-level sync later if your app grows or users start editing offline.