6 ms·
Always start with in-memory data structure with straightforward persistence (i.e. load and save it all at once, using some popular format). If you need ACID, t
by int_19h 8y ago
Always start with in-memory data structure with straightforward persistence (i.e. load and save it all at once, using some popular format).
If you need ACID, then start with SQLite.
If you need scalability as well, then PostgreSQL. Most other features aren't worth the hassle of configuration compared to a single file on disk and a library to link to.
(But I suspect that most NoSQL apps these days would do just fine with SQLite, perf-wise. As an industry, we don't have a good sense of scale.)
- seanwilson 8y ago> Always start with in-memory data structure with straightforward persistence (i.e. load and save it all at once, using some popular format). Do you have any examples of this? It's rare I see suggestions like this. I've done this for HTML mobile apps a few times before where the entire app (fairly simple) state is saved and loaded as JSON when you make any changes. It's super simple and very little can go wrong. This was as opposed to using Web SQL (which wasn't deprecated at the time) where it's much more complex to keep what's in-memory synchronised with what's in the database. Edit: I'm talking about client side code to store local state here.
- ousta 8y agoi struggle to see why in-memory data structure is the way to start can you please explain. do you create json for all your users?
- chrisseaton 8y ago> i struggle to see why in-memory data structure is the way to start can you please explain. It’s much simpler. The idea is to start simple and avoid complexity. Do you need the extra complexity of a PostgreSQL instance or can you get away with doing it in-memory? If so you can save yourself a lot of complexity.
- lowercased 8y ago> If so you can save yourself a lot of complexity. The complexity exists, you're just shifting it around some. The "complexity" of setting up a populate db server is non-zero, but generally a known-entity. The complexity of managing your own stuff 'in-memory', with all the attendant issues about concurrency/corruption/performance/etc - would almost always end up being far more custom to your particular project.
- int_19h 8y agoIf that were true, we wouldn't store any data in memory at all - everything would be in the database to avoid "the attendant issues about concurrency/corruption/performance/etc". But we don't. Perhaps because the attendant issues often aren't issues in practice? There's no concurrency problem if it's just your app working with data, and it's logically single threaded (i.e. possibly async, but serialized) - which is by far the most common model for non-web apps. There's also no concurrency problem if you use immutable data structures. There's no corruption problem if you are using a memory-safe language, and your data structures enforce their invariants, as they should in any good design regardless of anything else. Performance is going to be better, often significantly so, until you have enough data that dumb queries over it are slower than the overhead of communicating with the database. And so on. So, no, the complexity doesn't necessarily exist. The complexity of your data is constant, and of the kind of processing that you need to do with it, true. But we're talking about complexity of the pipeline you use to access that data - and there, you can absolutely make it more complicated that it needs to be, and I'm arguing that it is what usually happens in practice.
- ousta 8y agosurely you are using angular, nodejs or react right? why not just raw html and css? its much simpler
- int_19h 8y agoYou create a JSON (for example) for all your users when you need to store it. But in memory, it's just a collection of User objects - or whatever is idiomatic for your PL. And you query it with the same tools your language offers - e.g. sequence comprehensions. So there's no impedance mismatch, and no need for the vastly more complicated code that is needed to bridge it. Even if it's not your code - i.e. if you're using an ORM or something similar - it's still unneeded complexity, if you don't have too many users. Think of it from the opposite perspective - if you can work directly with in-memory data, why would you prefer to run SQL queries instead? The latter is obviously more complex, but what's the advantage? There are many valid cases that have a good answer to that question, but there are many more that don't. I can't help but think that we're conditioned to use DBMS (SQL or not) largely by inertia. When we learn about them, the examples are necessarily toy ones - tables with a dozen of records, that sort of thing. But one side effect is that it subtly normalizes the notion that this much data warrants a dedicated DBMS - so people don't balk at even patently ridiculous setups, like a separate SQL server used to hold a grand total of a couple thousand records in all its tables (this is a real thing, something that I personally did in a LOB app many years ago; I didn't have a good justification, it was pure cargo cult, and the app could have done everything it did in-memory, faster, and easier to code). And sometimes, when you ask, people say, "yeah, it's only 1000 records now, but we're going to grow later, and then we'll need a DBMS". But you'd need to grow by many orders of magnitude to get there - and if you get that opportunity, you'll also have the resources to adapt. But everybody dreams of being Google. And then you have apps like, say, a todo list manager app. Is it ever going to deal with millions of todo records? I don't think so. Then why does it need an SQLite DB?
- dchest 8y agoUnfortunately, I've rarely seen such a simple thing as file saving implemented correctly by many developers. Missing fsync(), missing checking of errors on close(), in-place non-atomic data overwrites. One of the good things about using SQLite/Postgres/MySQL is that at least they save your data correctly.
- fauigerzigerk 8y agoI don't think the number of records is the right benchmark. The reason why data is better kept in a database system is because it often has a very different life cycle than applications. It often lives longer and gets used by more than one application. That's why modelling and storing data somewhat separately from applications often makes sense regardless of the amount of data you have. Also, procedural code is often far more complicated than a SQL query regardless of the number of records being processed. But that obviously depends very much on the specific problem, on the programming language and on the developer's skill set.
- seanwilson 8y agoTo clarify, I was talking about client-side code where there isn't a server component e.g. so your choices are usually to store in files, SQLite or IndexedDB.
- int_19h 8y agoI mean, most apps that have configs treat them in this manner. It's just that people assume that configs and other metadata are "small enough", and the actual data they process is not. But when you look at what "small enough" really means on modern hardware - heck, even on 10 year old hardware! - that divide doesn't really follow. The same thing goes for queries, by the way. When it's in-memory, you often don't even need indices, because linear scans will be "fast enough" on many datasets. And with list comprehensions etc, this can be written just as declaratively as SQL (for an extreme example, see LINQ, which even has outer joins). And yeah, the "very little can go wrong" part is exactly what's so great about this. It's KISS at work. If you don't really need complexity, why pay the tax?
- kevinmgranger 8y agoFor most languages and frameworks, using Postgres as opposed to sqlite is barely a higher cost if you’ve got the ops skills. I’ve yet to see an app that straddled this local/web-scale line where SQLite was a better choice or Postgres wasn’t the obvious choice. There isn’t some massive cost to using Postgres instead, is there some desktop divide I’m missing here?
- rtpg 8y agosqlite doesn't require a server, right? It's just "read the database file", whereas Postgres requires a running server as well as the client processes. Granted, if your ops are at the stage where "run postgres myself" is a pain, using some managed DB (heroku?) probably makes more sense than sticking to the sqlite file, if only because you'll be paying for "real backups" and availability.
- derefr 8y agoWould you use Postgres for developing something like, say, iTunes? I’d think SQLite would be a much more “natural” fit there.
- darpa_escapee 8y agoFor an app developed with Postgres, you need a Postgres instance running as a prerequisite. Whereas with SQLite, you just need to use a small library, there is no server or daemon involved.
- aljarry 8y agoI can use SQLite for prototyping without db server. It's a file or some in-memory buffer, I don't need docker or anything else to run some very simple unit/integration tests with the DB.
- mr_overalls 8y agoFrom: https://www.sqlite.org/whentouse.html https://www.sqlite.org/whentouse.html Appropriate Uses For SQLite: SQLite is not directly comparable to client/server SQL database engines such as MySQL, Oracle, PostgreSQL, or SQL Server since SQLite is trying to solve a different problem. Client/server SQL database engines strive to implement a shared repository of enterprise data. They emphasize scalability, concurrency, centralization, and control. SQLite strives to provide local data storage for individual applications and devices. SQLite emphasizes economy, efficiency, reliability, independence, and simplicity. SQLite does not compete with client/server databases. SQLite competes with fopen().
- paxys 8y agoIn-memory or local disk databases aren't going to work for any real world use case, regardless of how small or simple your application is. Using Postgres or MySQL is barely more complex than SQLite, and it makes more sense to start with one of them than move everything over once you realize you aren't going to be running on your dev machine forever.
- nouveaux 8y agoAfter developing with Postgres for several years and then moving to sqlite for dev, sqlite is a breath of fresh air. Now our policy is that everything goes into sqlite db unless the data requires multiple writers. The fact that you have to keep a process running and updated is reason enough to not use Postgres if you can help it. For instance, we have processes that fetches and stores data from APIs. It's trivial to share this with other developers for dev purposes. This data is read only and it reduces an annoying step of loading the data into Postgres. This is coming from someone who starts projects with only Postgres for many years and I do love Postgres. Sqlite is an underrated tool in the web dev world.
- Scarbutt 8y agoReally though, I would bet 95% of wordpress websites will make do with sqlite.
- bschwindHN 8y agoI'm using sqlite for a small project but I was kind of disappointed to learn how annoying it is to drop or rename columns. Especially when you write migration scripts with up/down statements, it's kind of a pain compared to postgres. But otherwise it's been fine to use, a good starting point for internal projects.
- h1d 8y agoHow do you browse the data on the remote server with any GUI tools? Last time I checked not even DataGrip could do it.
- int_19h 8y ago
- Scarbutt 8y agoRegarding your first point, even simpler could be to store the json as string in sqlite as to avoid all the file handling stuff, but I would say that sqlite is already a popular solid application format ;)
- int_19h 8y agoBut the only file handling stuff you need to load JSON from a file is to open the file. And if it's in SQLite, then you'll need to open the database and query - more complicated, no?
- Scarbutt 8y agoI think is more complex than that, when you modify some data structure in memory you also want write to disk immediately as to not lose any data in case of a crash, so this means to write the new file under a separate name and then renaming the new file to replace the old file only after it has been successfully written as to provide safety against storage corruption. I wouldn't recommend this to a beginner, the sqlite driver(of your language) would handle all this for you in a safe way.
- int_19h 8y ago> this means to write the new file under a separate name and then renaming the new file to replace the old file only after it has been successfully written as to provide safety against storage corruption I agree, that's about the only bit of complexity in all this. But it's completely generic, so it can be made into a small library, and then reused without dealing with the complexity every time. For example: https://www.npmjs.com/package/write-file-atomic https://www.npmjs.com/package/write-file-atomic
- kevin_thibedeau 8y agoOpen the file, parse its content, deal with any errors. SQLite is simpler.
- 8y ago
- rndgermandude 8y ago"popular format", sqlite, Postgres are quite sensible. My personal mix also contains rockdb (lmdb is interesting, didn't use yet) and redis, as potential alternatives for sqlite and Postgres, depending on the use case.
- tyldum 8y agoYou can actually use postgresql for pubsub and thus instead of redis in some use cases.
- jeffdavis 8y ago"Always start with in-memory data structure with straightforward persistence (i.e. load and save it all at once, using some popular format)." That sounds like a bad idea in many cases. Prone to many kinds of problems, like accidentally corrupting the stored state beyond recognition from a tiny application bug.
- int_19h 8y agoHow exactly would you corrupt it? For all the stuff that's in-memory, it's as safe as your language is. If you're writing it to disk all at once, it's easy to implement this atomically with rename - this guarantees that you have either the old file or the new file, but not some weird in-between state. And if you mean corrupting by the app going amok and asking to corrupt data because of logic errors in the app itself, that's just as doable with a database, and especially doable with local-file DBs (which usually don't try to limit what you can do).
- tim333 8y agoI had a python script that was roughly: read file, unpickle, do stuff in memory, pickle, write. A bug in "do stuff in memory" could easily muck things up and I didn't get round to renaming the file for every edit. Yeah you could do some auto rename thing but it might be simpler to just use sqlite.
- int_19h 8y ago> I didn't get round to renaming the file for every edit. That was your problem then, no? If you don't do that from the get go, you're essentially saying that you don't care about atomicity, consistency and durability. > Yeah you could do some auto rename thing but it might be simpler to just use sqlite. You mean atomic rename? The simplest way is to just use a library that does it for you. One does exist for Python. It takes 2 lines of code to safely dump your JSON data with it.
- jeffdavis 8y agoCreate some ID that's supposed to be unique, and due to a race condition, it's not. Then, before you know it, the data is a mess. Unique keys, foreign keys, check constraints, and DDL changes were all meant to solve real problems. I guess some apps might not benefit as much, but it seems like asking for trouble. Given how easy it is to just use postgres, why invite all the risk?
- skunkworker 8y agoYup this. People don't realize just how performant SQLite can be. If you are running just 1 server and won't probably need more than SQLite is more than enough. 2 application servers and up is where PostgreSQL can come into play. https://www.sqlite.org/whentouse.html https://www.sqlite.org/whentouse.html After reading the SQLite when-to-use document I transitioned some smaller marketing sites over from Postgres because it was overkill for the specific situation. Other multi-app server applications or ones that scale, SQLite might not be the best use, but it still has it's cases. Like copying over a subset of the original data and running queries locally instead of hitting a production database
- fauigerzigerk 8y agoIf only SQLite had a decimal type. I would be using it a lot more. I know all the workarounds but they are tedious and error prone.
- arendtio 8y agoThe biggest problem with SQLite is that it seems to be easy to use it wrong. If you are using SQLite as it is supposed to be used, it has much better performance than its reputation suggests.
- pjc50 8y agoIn what way?
- tim333 8y agoNot quite sure but Quora suggests >[slow]...This is because the entire database was locked every time someone viewed the page because it contained updates/inserts. So avoid loads of inserts I guess https://stackoverflow.com/questions/54998/how-scalable-is-sqlite https://stackoverflow.com/questions/54998/how-scalable-is-sq...
- CydeWeys 8y agoThere's plenty of use cases where you want to record analytics or metrics for each page load though -- so I guess SQLite simply isn't suitable for that?
- inopinatus 8y agoThere was a time when it was a shoo-in to also say "if you only need a key-value store, use Berkeley DB".
- deleted 8y ago[deleted]
- ngrilly 8y agoSQLite is really great, but as a datastore for a web app or a web service, it lacks a few important things to my eyes: - Cannot rename or drop column - Cannot create an index concurrently (without blocking writes) - No remote protocol (you have to use the CLI through SSH to run ad-hoc queries)
- bunderbunder 8y agoThat seems like good advice for smaller-scale web development, and extremely good advice for single-user applications. If you're working on a larger scale, or expect to be growing into a larger scale, though, then I think it's wise to skip straight to using a full-blown RDBMS. It's not about scalability, per se - very, very few projects need more performance than SQLite can deliver - so much as about manageability. A top-tier RDBMS will have a better story for resiliency and availability, it will have a more robust security model, it will handle concurrency better, it will have a more complete SQL dialect that allows you to handle more complex queries while keeping them more maintainable, etc. This is one of the few spots where I think YAGNI doesn't apply. In expectation, it's cheaper to start on the RDBMS than it is to start small and grow as needed. The costs of switching databases are so high, the costs of outgrowing your data layer are even higher, and the costs of trying (and, usually, failing) to keep your data layer database-agnostic are still higher yet, and, in that particular problem domain, the likelihood of eventually being able to benefit from an industrial-grade RDBMS is so easy to anticipate.
- thijsvandien 8y agoI really like Firebird's model where you can either access a database file directly (like SQLite) or through a server process (like Postgres). Switch anytime you want. Best of both worlds. How lovely it would be if Postgres were to support something like that.