My whitelist bot stores every application and every vote in one JSON file. No database, no server, just data/applications.json. For a bot with five voters that is a reasonable choice: it survives restarts, it is easy to back up, and you can open it in a text editor.
It also has a bug that only shows up when two people are fast.
The lost vote
Every change follows the same pattern: read the whole file, change one application, write the whole file back.
const applications = await readAll(); // read
applications[index].votes.push(vote); // change
await writeAll(applications); // write
Each of those awaits gives other code a chance to run. Two voters click "approve" within a few milliseconds of each other:
- Vote A reads the file. It has 1 vote.
- Vote B reads the file. It also has 1 vote.
- Vote A writes 2 votes.
- Vote B writes 2 votes, built from its own stale copy.
Both clicks "succeeded", but the file now has two votes instead of three. Nothing crashed and nothing was logged. On a whitelist where three votes decide, that is the difference between a decision and a stuck application.
A queue in four lines
JavaScript is single-threaded, but async code still interleaves. The fix is to make sure only one change to the file runs at a time. You do not need a lock library for that, just one promise that every change is chained onto:
let mutationQueue = Promise.resolve();
function queueMutation(operation) {
const run = mutationQueue.then(operation, operation);
mutationQueue = run.catch(() => undefined);
return run;
}
Each new operation starts only after the previous one has finished. Two details matter:
.then(operation, operation)runs the next operation even if the previous one failed. Otherwise one error would block every write after it.- The queue itself swallows errors (
run.catch(...)), but the caller still gets the originalrunpromise, so the caller sees its own error.
Every update now goes through the queue:
export function updateApplication(id, update) {
return queueMutation(async () => {
const applications = await readAll();
const application = applications.find((a) => a.id === id);
const result = await update(application);
await writeAll(applications);
return { application, result };
});
}
Checks belong inside the queue too
The same race exists for rules. A player may have only one pending application, and must wait after a rejection. If the check and the save are separate steps, two quick submissions can both pass the check before either is saved.
So the check and the insert run as one queued operation:
export function createApplicationIfAllowed(application, cooldownMs) {
return queueMutation(async () => {
const applications = await readAll();
if (applications.some((a) => a.userId === application.userId && a.status === 'pending')) {
return { kind: 'pending' };
}
const until = cooldownUntil(applications, application.userId, cooldownMs);
if (until) return { kind: 'cooldown', until };
applications.push(application);
await writeAll(applications);
return null;
});
}
Reads that only look at data wait for the queue first (await mutationQueue), so they never see a half-applied change.
Never write the file in place
The last problem is a crash, or the server losing power, halfway through writeFile. That leaves a truncated JSON file, and the next start fails to parse it. Writing to a temporary file and renaming it over the original avoids that, because a rename on the same file system is atomic: the file is either entirely old or entirely new.
async function writeAll(applications) {
const temporaryFile = `${file}.tmp`;
await writeFile(temporaryFile, JSON.stringify(applications, null, 2), 'utf8');
await rename(temporaryFile, file);
}
When to stop doing this
This works because there is exactly one process writing the file. If the bot ever ran as two instances, the in-memory queue would not protect anything, and it would be time for SQLite or a real database. For one small bot, a promise queue and a rename were all it needed.