Flat database comments: one or many files?

I’m planning on upgrading my website with a flat database comments system (where comments are stored in .txt file(s)). Performance-wise, is it better to have comments be individual .txt files, or to store them all in a single .txt file read with toStructure?

1 Like

Performance-wise, a single file will probably be better, because there are less pages and therefore less files to open and parse.
In the end, it’s basically a question what you like better personally. What option makes more sense also depends on your deployment/backup/edit strategy.

I know how to do it in multiple files already (I coded a similar function for another site earlier this month - not a comments system, but still something based on user-created content). On this other site, using multiple files made sense (the site isn’t going to be large-scale in any fashion), but for my own site I wasn’t so sure hence my question.

Single-file approach it is, then. (It also fits better with “trying to avoid having thousands of minuscule files all over the place” which feels like a good strategy overall.)

Gonna do some research with toStructure then, thanks for your advice!

You could also use PHP serialize or JSON for storage inside an additional file for each blog post.

PS: I have found a potential problem: Let’s say you have a blog post with thousands of comments. If another one gets added, megabytes of data need to be changed in RAM, reconverted to JSON/YAML/whatever and saved. That’s a potential disadvantage of the single file approach. But since it’s probably better to have the bottleneck when writing instead of when reading, that’s kind of alright.

But maybe you could use a totally different approach: Storing your comments in a database. This can be much faster for many comments. For a few it won’t matter.

I’ll have to take a look at serialize, didn’t know about it. Any obvious advantage vs creating a file and updating it with Kirby’s built-in functions?

JSON makes sense, I need to figure out how to write to JSON but it’s probably better than what I was thinking of (a standard Kirby file with a list of comments).

Regarding the potential problem: Maybe I can limit the amount of comments per storage file to 100? So the 101th comment will create a new file for 101th to 200th comment. Not 100% sure how to code this but I should be able to figure it out, if I do decide to use this approach.

Database is what I’m trying to avoid. Basically I dislike working with databases (and that’s what got me into Kirby in the first place) so I’m trying to come up with the best database-less approach to a comment system.

I remembered that PHP’s serialize is/was faster than other methods. But now it seems like it’s the other way around. JSON encoding is faster than serializing.

You can simply use json_encode() and store it as comments.json in your posts’ directories.

That’s a great idea! I’d recommend testing whether the performance actually slows down with a lot of comments before implementing this though, as splitting the comments may (or may not) reduce performance again.

I fully agree. Same here. :wink:

Thanks again for all this data. I think I’ll try my hand at coding something tomorrow and see what happens :blush: .