Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Survive a Crash During a Save

In this lesson, you caught the repair café’s app crashing in the middle of a save, saw why that can break tickets.json, and changed the store’s save() so the file survives the app crashing during a save.

Key Points

  • A server doesn’t always finish what it starts. The app can crash, or its process can be killed, right in the middle of a save.
  • A crash during a save can lose the whole list. A test script started the app from the five starting tickets, kept posting tickets to it, killed the app’s process at a random moment, and checked whether tickets.json still parsed. In a hundred runs with the store from the last lesson, ninety-three left a file that parses, one left it empty, at zero bytes, and six left it cut short. On a broken file, load() stops the app, because the file isn’t valid JSON anymore.
  • writeFile() replaces a file in steps, not in one go. It first opens the file for writing, which empties it to zero bytes. Then it writes the new text in, a piece at a time, and then it closes the file. A crash before the open leaves the old list, a crash after the last write leaves the new one, and a crash in between leaves an empty file or one cut short.
  • Write the new text to a temporary file, then rename it over the real one. A rename moves no bytes. It points the name tickets.json at the new file in one step, so at every moment tickets.json holds the whole old list or the whole new one. A ticket caught mid-save gets no 201, so the desk can’t tell whether it was saved. A crash during the write can only break the temporary file, and the next save writes over it.
  • Keep the temporary file beside tickets.json, because the system’s temp folder can be on another disk. A rename only works within one file system, and the same folder keeps both files on the same one.
  • The queue still runs one save at a time, so two saves never share the temporary file.
  • The same hundred crashes against the new store left a tickets.json that parses every time. In forty-five runs, a temporary file was left behind, which the app never reads.
  • Each safeguard prevents one problem. Loading once and changing tickets in memory stops one request from erasing another’s update. The queue stops two writes from overlapping. The rename stops an app crash from breaking the file.
  • A JSON file is enough for one app at the café. Run several copies of the app, and each keeps its own tickets and its own queue, so one copy’s save wipes out another’s tickets. That’s what databases are for, and they’re for later.

The Final Code

Here’s the top of store.ts with the new import, and the start of createTicketStore() with the two-step save(). The rest of the store is unchanged from the last lesson:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your store now loads tickets.json once, changes tickets in memory, and saves every change through one queue. Two tickets at once no longer cost you one of them.

00:11 But a server doesn’t always get to finish what it starts. The app can crash, or its process can be killed, right in the middle of a save. What does the file hold after that?

00:24 In this lesson, you’ll catch a crash during a save and change one function so the file survives it.

00:31 That moment is hard to hit by hand, so a test script hits it for you. It starts the app from the five starting tickets and keeps posting tickets to it. At a random moment, it kills the app’s process, with no chance to finish anything.

00:47 Then it checks whether the file still parses.

00:51 It does that a hundred times with your store from the last lesson. Ninety-three runs left a file that parses. One left it empty, at zero bytes. Six left it cut short, partway through the list.

01:06 Start the app on a broken file, and your load function stops it, because the file isn’t valid JSON anymore. This crash did worse than the race. It lost the whole list.

01:19 Here’s what happens behind the scenes. When writeFile saves tickets.json, it first opens the file for writing, and opening it that way empties it to zero bytes.

01:30 Then it writes the new text in, a piece at a time. Then it closes the file.

01:37 A crash before the open leaves the old list, whole. A crash after the last write leaves the new one. A crash in between leaves an empty file, or one cut short, and the next start can’t parse it.

01:51 So watch out: writeFile replaces a file in steps, not in one go.

01:58 The fix is to stop writing over tickets.json directly. Write the new text first to a temporary file in the same folder. Then rename that file to tickets.json, which replaces the old one.

02:13 A rename moves no bytes. It points the name tickets.json at the new file in one step, and the old file goes. So at every moment, tickets.json holds the whole old list or the whole new one.

02:28 A ticket caught mid-save gets no 201, so the desk can’t tell if it was saved. A crash during the write can only break the temporary file, and the next save writes over it.

02:40 Keep it beside tickets.json, because the system’s temp folder can be on another disk, and a rename only works within one file system.

02:50 In store.ts, the store works out the temporary name once. Inside the queue, each save now awaits two steps: the write to the temporary file, then the rename over the real one.

03:03 Import rename at the top too. The queue still runs one save at a time, so two saves never share the temporary file.

03:12 Run the same hundred crashes against the new store. Every one of them left a tickets.json that parses. In forty-five runs, a temporary file was left behind, which the app never reads.

03:26 Write the new file beside the old one, then rename it over.

03:32 Your store now has three safeguards. Loading once and changing tickets in memory stops one request from erasing another’s update. The queue stops two writes from overlapping.

03:44 The rename stops an app crash from breaking the file.

03:49 That’s plenty for one app at the café. Run several copies, and each keeps its own tickets and queue, so one copy’s save wipes out another’s tickets. That’s what databases are for, and they’re for later.

04:03 Your tickets now survive the app crashing during a save. Next, in Exercise: Save the Parts Shelf, you’ll give the café’s parts shelf its own store, so the spare fuses and zips are still there after a restart.

Become a Member to join the conversation.