4 ms·
SQL databases are global variables
by paulsutter 2y ago
SQL databases are global variables
- tombert 2y agoOr caches! I have gotten shit before for using global variables, and sometimes that is justified, but I almost never see anyone given shit over treating Redis as a big ol’ global map.
- moring 2y agoWhen it comes to "patterns" and "anti-patterns", double standards are common.
- tombert 2y agoYeah. Honestly, I think Redis is very often overused and likely makes stuff slower. If your application is only running on one computer, and especially if it's only running in one process, you will likely get better performance using a big ol' thread-safe hashmap that's global/singleton. You pay basically no latency cost, no (de)serialization costs, and the code is likely going to be simpler. I've seen people who seem to think that just inserting stuff into Redis will somehow automatically make their code better, only for the code to actually become slower because of network latency. I've also seen people use Redis as a way to have global variables because it's too hard to figure out how to scope stuff correctly.
- macintux 2y agoAnd man, the bugs they cause.
- nejsjsjsbsb 2y agoMan, the bugs they prevent. Vs everyone rolling their own multithreaded file reader/writer code in C. How many programmers would think to journal transactions and ship them for backup for example. SQL or 15000 lines of C++ or Go or Rust doing whatever with files.
- lmm 2y agoYes, and code that mutates them in an unmanaged way should be banned.
- ykonstant 2y agoWhat is the managed way in this context?
- lmm 2y agoWell personally I favour event sourcing and so in my systems the SQL database (if there is one) is only written to by the event rollup component. But even if you're not that extreme you probably want to have some clear structure to where your database writes happen rather than randomly writing it from any arbitrary line of code. The important thing is to have a structure that everyone working on the code understands so that you all know where any write to the database must have come from, not the specifics of what that structure is.
- nejsjsjsbsb 2y agoWhat does the ideal look like then? Data storage encapsulation in the app? Perhaps different DB users with granular access, accessed from different non-global parts of the program. Chuck in some views later when you need performance pragmatism!