Urgent.News

What's breaking now, across hundreds of outlets.

Tech

Building WorkManager Jobs That Are Truly Idempotent — Not Just 'Usually Fine'

On Android, background execution is unpredictable. Google's official background processing library, WorkManager , is designed to be resilient: it guarantees that scheduled background tasks will eventually execute, even if the user reboots their phone, kills the app, or travels through cellular dead zones. However, in achieving that resilience, WorkManager makes an important operational…

Android's background execution is unpredictable, but WorkManager is built to be resilient. It ensures scheduled background tasks eventually run, despite phone reboots, app terminations, or cellular dead zones. However, WorkManager guarantees at-least-once execution, not exactly-once. If Android kills a process during a task, WorkManager may retry the same background worker multiple times in succession.

This can lead to problems in applications like personal finance, where a background worker might process recurring monthly rent of Rs. 20,000. If the worker is not idempotent, running it twice would deduct rent twice, spam users with duplicates, and corrupt the database.

FinLedger developed idempotent background workers that can execute a hundred times without duplicating financial records. These workers follow three key design principles:

1. Unique Work Registration: Every background task is registered with a unique, domain-specific string identifier using ExistingPeriodicWorkPolicy.KEEP. This ensures that if the app is launched again or the device is rebooted, the existing schedule is preserved, preventing multiple concurrent schedules which could lead to duplicated work.

2. Idempotency Key (Database State Gating): Each recurring transaction gets a deterministic Idempotency Key based on subscription ID and year-month. The worker checks if a transaction with this key already exists in the database. If it does, the worker skips processing and returns success immediately. This ensures that even if WorkManager retries the job multiple times, the transaction is inserted only once, avoiding duplicate financial records.

3. Atomic State Commit (Database Transactions): The worker performs the actual transaction insertion and writes the idempotency marker within a single withTransaction block. If the device loses power during this process, both the transaction and the idempotency marker roll back, ensuring consistency. When the device restarts, the worker retries cleanly, without encountering orphaned markers.

Additionally, these workers are designed to be hardware-aware. They request constraints that prevent execution when the device is in low-battery mode, idle, or lacks network connectivity. This ensures that background tasks run only under optimal conditions, further safeguarding against failures.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

Why Your Shell Command Breaks on Filenames With Spaces

'No such file' for a folder you can see, or 'too many arguments'? The shell splits on spaces before your command runs. Here's how quoting fixes it.

  • Filenames with spaces cause shell command errors
  • Quoting filenames resolves the issue
  • Escaping spaces or using tab completion as alternatives

Their shop costs ₹0 a month. A finished feature stays off to keep it that way.

Snackly sells dry fruits, nuts and seeds. Their storefront and the admin panel behind it run on Cloudflare, take real payments, and cost them nothing per month to operate.

  • Snackly sells dry fruits, nuts, and seeds online.
  • Cloudflare powers storefront and admin panel, eliminating monthly costs.
  • Free Cloudflare tiers support full store functionality without charges.

More from Sunday, 29 November 2026 →