News for hackers. Stuff that matters.
Re:Ask HN: What novel tools are you using to write web sites... by jasfi on Monday April 05, 2021 @12:34AM (#26695388)
No SQL database is the one thing I disagree with. What if you want to write a SQL query/report? Do you have transactions? As the app gets bigger you might want that.
Re:Ask HN: What novel tools are you using to write web sites... by kouteiheika on Monday April 05, 2021 @02:06AM (#26695804)
> No SQL database is the one thing I disagree with.

I'm not saying this approach is a good idea in every case. There are many situations where I would use a proper database. It really depends on what kind of app you're building, what kind of queries you want to run, how big is your dataset, your failover and availability requirements, your programming language, etc.

In my case it was a great decision. The code is simple, and the data is convenient to access since it is already packed in native data structures. And it's really, really fast since all of the "queries" are basically LLVM-optimized machine code.

> What if you want to write a SQL query/report?

I just write a few lines of normal Rust code to do that. Sure, it's not as convenient. But I don't need to do that as often, so I don't care.

> Do you have transactions?

The concept of 'transaction' doesn't really make sense in this approach, since I'm directly modifying the data in memory, every change I make is atomic and can't fail, and I flush the data to the disk atomically as well.

Re:Ask HN: What novel tools are you using to write web sites... by jolux on Monday April 05, 2021 @03:22AM (#26696211)
>The concept of 'transaction' doesn't really make sense in this approach, since I'm directly modifying the data in memory, every change I make is atomic and can't fail, and I flush the data to the disk atomically as well.

So you don't have mutable data shared between threads then?

Re:Ask HN: What novel tools are you using to write web sites... by kouteiheika on Monday April 05, 2021 @03:57AM (#26696389)
> So you don't have mutable data shared between threads then?

I might have worded that one sentence a little better, sorry.

I do technically need to share data between threads, since my event loop runs on multiple threads, so if I want to mutably modify something I do need to wrap it either in a `Mutex` or an `RwLock` and lock it.

What I meant was that it is not the same as a "transaction" in the SQL sense. It can't fail, there is no rollback, I decide on my own what level of granularity I need, and I bake the necessary atomicity into the data structures itself. So there are no SQL-style transactions anywhere - I just use a lock where it's appropriate to directly modify the memory, just as you'd do in any other situation. (Either by wrapping the whole structure in a lock, or wrapping only a single field, or by simply making the field atomic by itself.)

Re:Ask HN: What novel tools are you using to write web sites... by tinco on Monday April 05, 2021 @04:34AM (#26696600)
So it's a simple mmap database like early versions of MongoDB? I like using atomic commands, but I imagine it would become tedious fast to do the extra work of doing the accounting of the transaction manually every time you want to do a non atomic transaction.
Re:Ask HN: What novel tools are you using to write web sites... by kouteiheika on Monday April 05, 2021 @05:08AM (#26696807)
> So it's a simple mmap database like early versions of MongoDB?

Even simpler. I use mmap only for things that I build offline and that never change during the runtime. Everything else is just kept as your garden variety objects in memory.

(Well, although I guess you could say it is an mmap database, it's just that the mmaped file is the swap, and the OS manages it for me.)

> I like using atomic commands, but I imagine it would become tedious fast to do the extra work of doing the accounting of the transaction manually every time you want to do a non atomic transaction.

In my case it's not really tedious at all. Usually when I want to mutate something it looks something like this:

    let user_ref = state.require_login()?;
    let user = user_ref.write();
    user.set_field(value);
    drop(user);
Of course this depends on your requirements. If you'd have 20 different objects of 10 different types that you'd have to lock separately and you'd want to update all of them atomically it might become hairy (SQL would definitely be better in such a case), but I don't have such cases.
Re:Ask HN: What novel tools are you using to write web sites... by codetrotter on Monday April 05, 2021 @06:25AM (#26697231)
I do similar stuff to what you do, writing my backend code in Rust and not using a database but opting instead to keep things in memory and flushing to disk on my own. And running servers that I manage with SSH.

I would love to talk with you and exchange knowledge. Your HN profile has no publicly visible contact info though.

Do you have an email address I can reach you on? Can you put it in your HN profile description?

Re:Ask HN: What novel tools are you using to write web sites... by kouteiheika on Monday April 05, 2021 @08:30AM (#26698112)
Sure. You can email me at [email protected].

(Just please note that if I apparently don't reply and if you're using gmail then my reply probably went to spam or gmail's /dev/null, so email me again; lately gmail doesn't like emails that I send and either puts them into spam or just sends them to /dev/null, even when I'm replying to someone that emailed me first! So much for Google having no monopoly, sigh...)

Re:Ask HN: What novel tools are you using to write web sites... by codetrotter on Monday April 05, 2021 @07:48PM (#26706183)
I run my own mail server so there’s also a chance that I end up in your spam folder instead when I send you a mail, if you do spam filtering :p
Re:Ask HN: What novel tools are you using to write web sites... by pastage on Monday April 05, 2021 @03:55AM (#26696377)
The basic stuff Rust will solve for you, but it is easy to forget logical dependencies and get an inconsistent state anyways... Just the ordinary close a session, send error message if not alive has bitten us in a 20 person project.
Re:Ask HN: What novel tools are you using to write web sites... by eloff on Monday April 05, 2021 @10:37AM (#26699570)
Yeah, this seems problematic. What if you hard power off your server, how do you know your data didn't get corrupted? As someone who has written databases, and knows how difficult this is to get right, it makes my skin crawl.

Backups and restore will also have to be DIY. Point in time restore probably won't be possible - and that's a life saver when you need it.

Re:Ask HN: What novel tools are you using to write web sites... by kouteiheika on Monday April 05, 2021 @10:56AM (#26699864)
> What if you hard power off your server, how do you know your data didn't get corrupted?

Atomically replacing the whole file instead of updating the data in-place, and using a checksumming filesystem like ZFS should give you such guarantee, no?

> Backups and restore will also have to be DIY.

Well, yeah. A daily cron job that `scp`s the data to an offsite location. Personally I don't really see much problem with that? It's easy to backup, and easy to restore.

Re:Ask HN: What novel tools are you using to write web sites... by eloff on Monday April 05, 2021 @11:38AM (#26700438)
> Atomically replacing the whole file instead of updating the data in-place, and using a checksumming filesystem like ZFS should give you such guarantee, no?

No. You need to use fsync to ensure the file is flushed to disk. Also careful not to rename the file across mount points (/tmp is often a different mount and the most common location for people trying this.)

> Well, yeah. A daily cron job that `scp`s the data to an offsite location. Personally I don't really see much problem with that? It's easy to backup, and easy to restore.

That's fine, but your backup and restore granularity is daily. If you introduce a bug that writes the wrong / partial data, you can't recover anything that isn't in the previous day's backup.

With a typical database you have a transaction log and you could restore back to any point during the day.

This has been necessary about a dozen times during my career (once every two years or so on average.)

Re:Ask HN: What novel tools are you using to write web sites... by kouteiheika on Monday April 05, 2021 @11:58AM (#26700631)
> No. You need to use fsync to ensure the file is flushed to disk.

Are you sure? At least in case of ZFS AFAIK the file wouldn't get corrupted, since it uses copy-on-write internally. Sure, you might get the old data back, but you wouldn't get corruption. And it automatically checksums your files anyway so it will detect any data that did get corrupted, and if you're using RAID-1 (as you should for any important data) it will automatically repair it.

> Also careful not to rename the file across mount points (/tmp is often a different mount and the most common location for people trying this.)

You can't rename across mount points. (Linux's `rename` syscall and the corresponding C API will return an EXDEV error if you try.)

> If you introduce a bug that writes the wrong / partial data, you can't recover anything that isn't in the previous day's backup.

Sure. If I was working in a bank not being able to do that would be totally unacceptable. But in my case that's the tradeoff I'm willing to live with.

Re:Ask HN: What novel tools are you using to write web sites... by onion2k on Monday April 05, 2021 @01:41AM (#26695681)
Adding in a service like a SQL database because "you might want that" in the future is not a good idea. Apps should do what they need to do and no more. "Future proofing" is almost always a waste of time - even if you definitely want the feature in the future it's usually better to add it in when it's needed than adding it early and have to support something that adds no value yet.

As for reports and transactions specifically, noSQL databases support both.

Re:Ask HN: What novel tools are you using to write web sites... by d1sxeyes on Monday April 05, 2021 @05:22AM (#26696887)
OP isn't talking about noSQL databases, but no database at all (if I understand correctly - OP says '- No SQL database; I just keep everything in memory in native data structures'.

In this case, I don't think OP is even thinking about noSQL databases.

Re:Ask HN: What novel tools are you using to write web sites... by kouteiheika on Monday April 05, 2021 @05:31AM (#26696959)
Correct. Sorry, I should have worded that better; unfortunately I cannot edit the post anymore.
Re:Ask HN: What novel tools are you using to write web sites... by arkitaip on Monday April 05, 2021 @03:09AM (#26696136)
Unless, of course, switching to SQL turns out to be a major headache because it requires major rewrites, organizational changes, etc.
Re:Ask HN: What novel tools are you using to write web sites... by zepolen on Monday April 05, 2021 @06:43AM (#26697338)
> "Future proofing" is almost always a waste of time

Not really, unless you lack the experience to know what to future proof and what not to future proof.

I've saved days/weeks of development by future proofing certain things, I've also saved days/weeks of development by taking pragmatic YAGNI in the right places.

Obviously this will only surface on projects that last longer than a couple months ;)

Re:Ask HN: What novel tools are you using to write web sites... by weego on Monday April 05, 2021 @09:51AM (#26698953)
Isn't this the problem? "What if I need to do something I don't need to do right now? I know I best add it in just incase!"

You have now recreated Drupal.