Architect periodic and on-demand sync with WorkManager

Tests combining periodic and push WorkManager requests safely. Outline: one Worker class, PeriodicWorkRequest hourly and OneTimeWorkRequest on push, with ExistingWorkPolicy.KEEP plus constraints. Red flag: two Workers, no deduplication, or ForegroundService.
What's really being asked
Whether you understand WorkManager as the unified deferrable background work API and can avoid duplicating logic across periodic and immediate requests. Interviewers want to see that you know how to use enqueue policies, constraints, and input data to keep the architecture DRY and race condition free.
The full answer
First, use a single Worker subclass for both paths so sync logic lives in one place. Second, use setInputData to pass a trigger reason flag so the Worker can log or branch if needed. Third, schedule the hourly sync with PeriodicWorkRequest using a unique name and ExistingPeriodicWorkPolicy.KEEP to avoid stacking duplicate periodic jobs. Fourth, handle the push notification by enqueuing a OneTimeWorkRequest under a different unique name with ExistingWorkPolicy.KEEP or APPEND_OR_REPLACE depending on whether you want to drop an in flight manual sync. Fifth, add Constraints such as NetworkType.CONNECTED to both requests so they do not waste battery when offline. Sixth, mention expedited work for the push path if the user experience requires near immediate execution while respecting App Standby buckets.
The mistakes people make
Creating two separate Worker classes that copy and paste the same sync logic. Using enqueue without an ExistingWorkPolicy, which causes the work queue to grow unbounded every time a push arrives. Using a ForegroundService directly instead of WorkManager, which forces you to manage lifecycle and permissions manually and ignores power management best practices. Using a PeriodicWorkRequest for the push path, which is semantically wrong and introduces unnecessary scheduling overhead. Ignoring constraints entirely, leading to battery drain and ANR risks.
Likely follow ups
How would you handle retry and backoff if the server is rate limiting? What happens if the periodic work fires while the one time push work is already running? How do you test this Worker, including injection of the repository or API client? Would you use a CoroutineWorker or ListenableWorker, and why? How do you observe the work state to show a sync spinner in the UI?
A concrete example
Imagine a mail app that syncs every hour. You define MailSyncWorker that takes input data with key TRIGGER mapping to PERIODIC or PUSH. In Application.onCreate or a dedicated scheduler class, you enqueue PeriodicWorkRequest with a 1 hour repeat interval, constraints requiring an unmetered network, and the unique name mail_sync_periodic. When FCM delivers a new_mail push, the FirebaseMessagingService builds a OneTimeWorkRequest with setExpedited if the user tapped the notification, sets input data to PUSH, and enqueues it under mail_sync_immediate with ExistingWorkPolicy.KEEP. Both paths execute the same doWork block, but the push path might pass a boolean to the repository forcing a full folder refresh instead of an incremental delta.
Interview question
You are architecting a mail app that syncs every hour and also on push notifications. Which WorkManager strategy best prevents duplicated sync logic and unbounded queue growth?
- a.Use a single Worker class, pass the trigger reason via setInputData, schedule periodic work with a unique name and ExistingPeriodicWorkPolicy.KEEP, and enqueue push work as a OneTimeWorkRequest with a different unique name and ExistingWorkPolicy.KEEP.Correct
- b.Create separate MailSyncWorker and PushSyncWorker classes that contain the same repository calls, and enqueue the push worker without an ExistingWorkPolicy.
- c.Use a single Worker for both triggers, but enqueue the push work as a PeriodicWorkRequest with a 1-minute interval and no unique name.
- d.Handle the hourly sync with WorkManager and the push sync with a ForegroundService started directly from FirebaseMessagingService for lowest latency.
Why? this is the answer
This keeps the sync logic DRY in one Worker while using separate unique names and KEEP policies to prevent duplicate periodic jobs and an ever-growing push queue. Option B is tempting because separating workers by trigger feels like clean architecture, but it duplicates logic and omitting ExistingWorkPolicy leads to unbounded work growth.
Just read this? Test yourself on what you have been reading.
Read the original → developer.android.com
- #android
- #workmanager
- #background-work
- #architecture
- #kotlin
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.
See open roles