<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/synchronize-mysql-and-indexeddb-in-an-offline-first-app-t7qnof3ch" -->

---
title: Synchronize MySQL and IndexedDB in an Offline-First App
description: A practical walkthrough builds an offline-first note app that queues writes to IndexedDB and syncs them to MySQL. The design centers on a client-owned primary...
canonical: https://daily.dev/posts/synchronize-mysql-and-indexeddb-in-an-offline-first-app-t7qnof3ch
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Synchronize MySQL and IndexedDB in an Offline-First App | daily.dev
og:description: A practical walkthrough builds an offline-first note app that queues writes to IndexedDB and syncs them to MySQL. The design centers on a client-owned primary...
og:url: https://daily.dev/posts/synchronize-mysql-and-indexeddb-in-an-offline-first-app-t7qnof3ch
og:image: https://api.daily.dev/og/posts/T7qNOf3ch.png
og:image:alt: Synchronize MySQL and IndexedDB in an Offline-First App
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Synchronize MySQL and IndexedDB in an Offline-First App

**[Planet MySQL](https://daily.dev/sources/planet-mysql)** · 12 min read · 0 upvotes · 0 comments

## Summary

A practical walkthrough builds an offline-first note app that queues writes to IndexedDB and syncs them to MySQL. The design centers on a client-owned primary key so retries are idempotent, an outbox pattern where a pending marker is deleted only after server acknowledgement, and a service worker background sync with a fallback drain since background synchronization isn't supported in all browsers. Uses Express 5.2.1 and mysql2 3.24.4, with prepared statements via placeholders and an INSERT ... ON DUPLICATE KEY UPDATE pattern to make retries safe. Also covers limitations: no conflict resolution beyond last-write-wins, no authentication, and browser storage eviction risk.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://codeforgeek.com/sync-app-mysql-indexeddb>

## Questions this post answers

### How do I make a retry-safe API endpoint so pushing the same note twice doesn't create duplicate rows in MySQL?

Use a client-owned primary key combined with an INSERT statement that includes ON DUPLICATE KEY UPDATE, so the first push inserts the row and any retry updates the same row instead of creating a new one. This requires the browser to generate the row's identifier before the server has seen it, rather than relying on an auto-increment integer.

_Developers wiring up offline sync logic can check daily.dev for patterns on idempotent write design._

### How should an offline-first app queue writes locally before pushing them to a server database?

Write the change to IndexedDB first, store a pending marker in a separate outbox object store keyed by the same client id, and delete that marker only after the server confirms the write succeeded. A marker that survives a drain attempt means the row did not land, so the next drain retries automatically without losing the edit.

_Anyone architecting offline-first sync flows can browse daily.dev for more on outbox-pattern queueing._

### Why does IndexedDB.open trigger an onupgradeneeded event and when does it run?

The onupgradeneeded handler fires only once, when a version number passed to indexedDB.open is higher than the database's existing version, and it is the only place where object stores can be created or modified. A store keyed on a client id, rather than a separate index, ensures a pending marker can never be queued twice for the same record.

_Developers debugging IndexedDB schema setup can find related guidance on daily.dev._

## Similar posts on daily.dev

- [Offline-first frontend apps in 2025: IndexedDB and SQLite in the browser and beyond](https://daily.dev/posts/offline-first-frontend-apps-in-2025-indexeddb-and-sqlite-in-the-browser-and-beyond-djyieqljy) · LogRocket · 1 upvotes · 0 comments
- [Build a Next.js 16 PWA with true offline support](https://daily.dev/posts/build-a-next-js-16-pwa-with-true-offline-support-iyrolodea) · LogRocket · 5 upvotes · 0 comments
- [Building an Offline-First Spaced Repetition Engine with JavaScript and IndexedDB](https://daily.dev/posts/building-an-offline-first-spaced-repetition-engine-with-javascript-and-indexeddb-s5zdpbzys) · SitePoint · 3 upvotes · 0 comments
- [The Architecture Of Local-First Web Development — Smashing Magazine](https://daily.dev/posts/the-architecture-of-local-first-web-development-smashing-magazine-fi6dhffcw) · Smashing Magazine · 6 upvotes · 1 comments
- [Offline-First Android Architecture: The Complete Engineering Guide](https://daily.dev/posts/offline-first-android-architecture-the-complete-engineering-guide-4w5ysrp8r) · Medium · 0 upvotes · 0 comments

---

Tags: [#webdev](https://daily.dev/tags/webdev), [#nodejs](https://daily.dev/tags/nodejs), [#mysql](https://daily.dev/tags/mysql), [#offline-first](https://daily.dev/tags/offline-first)

[View this post on daily.dev](https://daily.dev/posts/synchronize-mysql-and-indexeddb-in-an-offline-first-app-t7qnof3ch)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Synchronize MySQL and IndexedDB in an Offline-First App","url":"https://daily.dev/posts/synchronize-mysql-and-indexeddb-in-an-offline-first-app-t7qnof3ch","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/synchronize-mysql-and-indexeddb-in-an-offline-first-app-t7qnof3ch"},"datePublished":"2026-09-11T13:08:09.458Z","dateModified":"2026-10-15T02:22:20.302Z","description":"A practical walkthrough builds an offline-first note app that queues writes to IndexedDB and syncs them to MySQL. The design centers on a client-owned primary...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/007902349f7e3c1de05d81296d8baf09?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/007902349f7e3c1de05d81296d8baf09?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Planet MySQL","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Planet MySQL","logo":"https://media.daily.dev/image/upload/s--UKAtmXTq--/f_auto,q_auto/v1780213421/logos/planet-mysql?_a=BAMAMiWQ0","url":"https://daily.dev/sources/planet-mysql"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/synchronize-mysql-and-indexeddb-in-an-offline-first-app-t7qnof3ch","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"webdev,nodejs,mysql,offline-first","timeRequired":"PT12M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Planet MySQL","item":"https://daily.dev/sources/planet-mysql"},{"@type":"ListItem","position":3,"name":"Synchronize MySQL and IndexedDB in an Offline-First App"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/synchronize-mysql-and-indexeddb-in-an-offline-first-app-t7qnof3ch#faq","mainEntity":[{"@type":"Question","name":"How do I make a retry-safe API endpoint so pushing the same note twice doesn't create duplicate rows in MySQL?","acceptedAnswer":{"@type":"Answer","text":"Use a client-owned primary key combined with an INSERT statement that includes ON DUPLICATE KEY UPDATE, so the first push inserts the row and any retry updates the same row instead of creating a new one. This requires the browser to generate the row's identifier before the server has seen it, rather than relying on an auto-increment integer. Developers wiring up offline sync logic can check daily.dev for patterns on idempotent write design."}},{"@type":"Question","name":"How should an offline-first app queue writes locally before pushing them to a server database?","acceptedAnswer":{"@type":"Answer","text":"Write the change to IndexedDB first, store a pending marker in a separate outbox object store keyed by the same client id, and delete that marker only after the server confirms the write succeeded. A marker that survives a drain attempt means the row did not land, so the next drain retries automatically without losing the edit. Anyone architecting offline-first sync flows can browse daily.dev for more on outbox-pattern queueing."}},{"@type":"Question","name":"Why does IndexedDB.open trigger an onupgradeneeded event and when does it run?","acceptedAnswer":{"@type":"Answer","text":"The onupgradeneeded handler fires only once, when a version number passed to indexedDB.open is higher than the database's existing version, and it is the only place where object stores can be created or modified. A store keyed on a client id, rather than a separate index, ensures a pending marker can never be queued twice for the same record. Developers debugging IndexedDB schema setup can find related guidance on daily.dev."}}]}
```

