What about searching flat files?



This content originally appeared on Go Make Things and was authored by Go Make Things

On Friday, I wrote about why I love using flat text files instead of a database for my projects.

One of the more common questions I got was how I search for things, and the performance impacts of doing so.

The truth is… I don’t!

The structure of my file system—lots of small, sensibly named files—means that I almost never have to search for anything.

Let’s take the Lean Web Club for example.

Every user has a JSON file with their hashed password, account status (active or cancelled), and an array of their bookmarked lessons. The file name is their username/email address.

ryder@gomakethings.com.json

If I want to check if a user has an active account, I read their file, parse it to JSON, and check the status property.

<?php
$user_file = file_get_contents('ryder@gomakethings.com.json');
$user = json_decode($user_file, true);
$is_active = $user['status'] === 'active';

On any particular lesson, I need to know if the lesson is currently bookmarked or not so I can show the correct button state.

I can use PHP to check if the page ID is in the bookmarks array or not.

<?php
$is_bookmarked = in_array($page_id, $user['bookmarks']);

When a user logs in, I create a cryptographically random token.

I set that token as a cookie, and create a file with the token as its name. The file contains the associated email address for the user.

On every page load, I check if an auth cookie exists. If it does, I get the associated session file and read it to get the user’s email address. I can use that to read their file, check their status, show their bookmarks, and so on.

No file? No active session.

The only “searching” I really need to do is periodically finding expired token files and deleting them.

I include the “created on” date as part of the token (and by extension, file name).

Every day, I loop through all of the token files. If the “create on” part of the file name is older than the duration of an active session, I delete the file. I never even need to read the contents.

I can imagine certain situations where this kind of setup wouldn’t work. But for the types of apps I build, it’s simple, absurdly fast, and makes building and maintaining apps so much more simple than maintaining a database.

🎉 Preorder Getting Sh!t Done today and get 40% off! Be more productive, get more done, and feel more in-control of your work and life. Click here to learn more.


This content originally appeared on Go Make Things and was authored by Go Make Things