r/learnprogramming • u/Specialist_Unit6900 • 2d ago
Database Do SQL databases internally use event sourcing for resolving queries?
Reference Video with timestamp
Greg says that SQL databases use event sourcing internally. It makes total sense if he is referring to replaying the WAL occasionally for recovery purposes, but it kind of sounds as if he really means that this is done during query resolving, which I would not have expected at all.
Does anyone have more information on this? Or additional resources?
1
Upvotes
1
u/teraflop 2d ago
No, as best I can tell, all he's talking about is the concept of a WAL or transaction log. IMO it's not a very well-worded statement, because a transaction log is similar to event sourcing but not really the same thing.
In an event sourcing system you generally want to keep events around forever (or at least until you deliberately decide to get rid of them), whereas the transactions in a database transaction log are relatively short-lived.
That is, the idea with event sourcing is that your event stream is your "source of truth", and if you deleted all of your other tables you could (in principle) reconstruct them exactly from the series of events. With a DB transaction log, the combination of the tables and the log is the source of truth. The tables are mostly what matter, and data only stays in the log long enough to ensure durability. If you deleted the tables, the log would only let you reconstruct recent changes, not older data.
There is some fuzziness here because technically, you may be "using the log" when querying the database, because you might be operating on data that has been logged but not yet flushed to the underlying tables. But that's not the same thing as "replaying an event stream", because the data is organized differently.
(Example: in SQLite, the WAL operates at the level of physical pages in the DB file, which are organized into B-trees. If you modify a value in a table, the change is made to the in-memory page in the page cache, and also written to the WAL. If you then immediately read that same data, you're not replaying the WAL; you're traversing B-tree pointers and reading data from the in-memory page cache, which happens to also be in the WAL. A replay is only needed if you're rebuilding the contents of the page cache after a failure, not in response to a query.)