Bhargav Naidu Gedela

Writing

Copying files between clouds without the phone

Merge Clouds copies files from one cloud to another: Google Drive, MEGA, Amazon S3 and Backblaze B2. In the app you pick files, pick a destination folder, and tap copy. After that the phone only watches. The bytes move between the clouds on my server, so the copy doesn’t use the phone’s data and it keeps going when the app is closed.

This is how that part works today. Most of it went in on 8 November 2026.

The shape of it

app ──REST──▶ Spring Boot backend ──HTTP (rclone rc)──▶ rclone pod ──▶ source cloud
 ▲                   │                                        └──────▶ destination cloud
 └──── WebSocket ────┘  progress events, plus an FCM push when a transfer finishes

rclone runs as its own deployment in Kubernetes, from the same Helm chart as the backend. For cloud-to-cloud copies the backend never touches file bytes itself. It calls rclone’s remote-control API (operations/copyfile, job/status, core/stats, job/stop) and keeps the record of what was asked, what it cost and how far it got. rclone already knows how to talk to all four clouds. My code decides what to copy, who pays, and what the app sees.

One rclone job per file

A transfer is one source cloud, the files you picked, and one destination folder. TransferService.start sizes every file first, holds the credit for the batch, then starts the copies and stores each rclone job id on its item in MongoDB.

Credit is counted in bytes. Every account gets 5 GiB free once. The batch charge is a single conditional $inc, so two transfers started at the same moment can’t both spend the same balance:

Query query = new Query(
        Criteria.where("userId").is(userId)
                .and("balance").gte(bytesConsumed)
);
Update update = new Update().inc("balance", -bytesConsumed);
UpdateResult result = mongoTemplate.updateFirst(query, update, CreditBalance.class);
if (result.getModifiedCount() == 0) {
    // ...
    throw new InsufficientCreditsException("User has insufficient credits.", bytesConsumed, available);
}

If the balance is short, the request fails with a 402 that says how much is needed and how much is there, before any job starts. The app uses that to say what’s missing and send you back to pick files.

So the credit is held up front and settled at the end:

  • A file that fails to start is marked failed, and its share goes back in a finally block once the loop is done.
  • A file that finishes is charged what rclone says it copied. If that’s less than what was held, the difference goes back.
  • A file that fails or is cancelled gets its whole hold back.

Watching the jobs

MonitorService is one scheduler. Every second it polls each running job, in parallel on a small pool, and waits for the round to end so no job is polled twice at once. While a job runs it reads that job’s stats and writes the progress. When the job ends it settles the credit once and stops watching it. Thirty failed polls in a row, about 30 seconds, and it gives up on the job and returns its credit.

Progress goes through TransferTracker, which updates the item in MongoDB and pushes the new state to the owner’s socket. The update only matches items that are still waiting or running, so a late “failed” from rclone can’t overwrite a file the user already cancelled. Percentages are capped at 99 while a job runs. 100% only appears once rclone says the job is done.

The events keep the old {jobId, percentage} fields for older app builds and add {type, transferId, transfer} for current ones. When a current app connects (?v=2), the server first sends a snapshot of every transfer still running. The app reconnects with a backoff that starts at one second and doubles up to 30, and each reconnect starts with that snapshot. A dropped connection doesn’t lose anything; the app just catches up.

When the last file of a transfer settles, the server sends one FCM push. “One” is enforced by claiming a notifiedAt field with a conditional update, so two files finishing in the same second can’t both send it.

What I got wrong first

All three of these were in the code before I caught them. All three were fixed on 8 November 2026, the same day the transfers API went in.

A thread per job. The first monitor started a thread for each copy and slept in a loop, on a pool of five threads. The sixth concurrent copy wasn’t watched until one of the first five ended. Its progress froze, its credit settled late, and sometimes it was marked failed as “stalled” when it was fine. The single scheduler replaced that.

Global stats. core/stats ignores a jobid field. An async job’s stats live in a stats group named job/<id>:

// core/stats ignores "jobid"; an async job's stats live in the group "job/<id>".
// Without the group this returned global stats, mixing in every other running copy.
reqBody.put("group", "job/" + jobId);

Without the group, every progress bar was a mix of every copy running on the server, across users.

Sizing the whole remote. operations/size reads only fs and ignores remote. I passed the file in remote, so it measured the whole cloud. The comment I left in the code: “a 200 MB file on a 10 GB MEGA reserved 10 GB”. Now I operations/stat the item first and only size folders, by their own path.