by bborud · 3y ago · 0 comments
1) it will tie you to a specific database. I know a handful of companies that have almost all of their IPR in the shape of stored procedures. Even if they wanted to escape from the database vendor they can't. Not without rewriting everything they have spent a decade or two building.
2) languages such as PL/SQL are much poorer than any modern programming language. If you have ever tried to write a business application in PL/SQL: it is no fun. At least not for me.
(What I mean by "almost never" is that I do use constraints quite a lot which you could argue isn't logic, but it is still asking the database to validate my data and model).
As a counterpoint, tying yourself to a specific database is the only way to actually use any database to its full potential. I'm a big fan of Postgres and use it basically all my projects. But if I was forced to write my code in a way that I could easily swap Postgres for MySQL or Oracle at a moments notice, I would lose much of the value that I feel Postgres offers.
Under CQRS/ES you are DBMS independent, and you can still use RDBMS for its full potential on the read/querying side.
In this setup, your application houses domain-specific code, also known as the "core domain" in DDD terms, while generic code exists in generic tools like RDBMS, called the "generic sub-domain."
For example, report generation is in generic subdomain, and RDBMSes are great at that.
PL/SQL is just an Ada dialect with a bit of SQL on top.
no kidding, you hear about this way more than it actually comes up. especially for the folks who treat the RDBMS as a dumb store and don't use the features.
I've never actually seen a database system get swapped out, in real life. The only two ways I can imagine it happening are: 1) someone made a colossal fuck-up picking the DB software—and, it'd have to be really bad, I've seen a lot of applications stick with a mistake there and just deal with the pain; or 2) extreme hockey-stick growth (which, file under "good problem to have, and we can afford a proper migration")
In fact, #2 is usually addressable with supplementary databases, a little re-structuring of the existing database, and smarter caching, except in super-rare scenarios of truly wild growth or unusual access patterns. More often than not, even then, the original database sticks around in some form or another.
Using the database as the integration point went out of fashion a bit over 20 years ago. And for good reason. It is not a good integration pattern. It makes the implementation the integration interface rather than using a tightly controlled abstraction that can be kept narrow and preserve degrees of freedom for future evolution. And as you allude to: it gets worse because you have to move more application knowledge and logic into the database to make sure applications don't mess it up.
In fact, this approach is so bad that Amazon's CEO explicitly forbade it at Amazon (Bezos API Mandate, 2002). How often do you see CEOs, even of tech companies, find an integration pattern so counterproductive and dangerous they announce to the company that they will fire anyone using it?
I would say that about 60-70% of projects I've worked on in the last decade have switched databases at least once. One project had support for 5-6 databases and used three different databases in production over a span of 5 years. Adding support for them had fairly low cost (because the internal design made it easy) and it gave us the opportunity to try several things in production, at scale, and then choose what produced the best results. This is a lot better than just choosing something up front and sticking with it because you'll have the data to make an informed choice.
https://aws.amazon.com/blogs/aws/migration-complete-amazons-...
I've seen it work well however it requires a proficiency with SQL that most developers don't require so don't invest in.
It is much harder to debug but there is an elegance to the solution. It's often very compact and performs well.
That context switch is routing queries and results between the client and server using IO requests.
I do that in Go for chemical stuff, I am pleased with this approach.
(I often create Go types which are then serialized to some useful representation in the database, and de-serialized to Go types when I query the database. I usually use SQLX for most interaction with SQL databases since it does a lot of the tedious work for me (without introducing ORM'y madness). On the rare occasion where this eats too much into performance I can always use the DB library directly)
First off it depends on what processing you are doing on what kinds of data sizes. If you are doing relatively simple things, sure. But remember that PL/SQL isn't particularly fast if you need to do anything more complicated. Don't do anything complicated (we'll get to that in point three).
Second, someone has to write that code. Writing PL/SQL is laborious because the tooling is bad. Pick any half-decent language today and the tooling is going to be fantastic. You have language servers, you have optimizing compilers, you have profilers, you have debuggers, you have lots of ways to instrument, inspect, and verify code. Where do you plan to recruit PL/SQL developers and how much are you going to pay them? Because in my experience: there really aren't that many usefully competent PL/SQL developers available on the market. There are lots of people willing to "give it a go", but competence is rare.
Third, it complicates your database. Instead of just doing relational math and executing queries it now also runs code that might not be as easy to model on the same cores, eating into your database IO and memory budget and causing lots of variability. Sure you can hound your PL/SQL developers to adopt various tricks in order to lower the variability of database performance, but that gets expensive. And the code gets ugly.
Fourth: It makes no sense to apply extra load to your most expensive component, which also happens to be the component class that most often also ends up being the bottleneck of your entire system. (Cloud provider pricing is a somewhat useful proxy for the cost of different systems. Have a look at the cost of RDS capacity compared to EC2)
Fifth: did you measure or did you assume? In the cases where you measured a clear gain, was it still worth the cost of all of the above? Did you measure the right thing? Could you argued your case with numbers?
Want to do nice things to reason about your data changes using your code, or maybe show someone a preview of their changes? Sorry! All the interesting logic is in the database. You need to write your changeset to the database to figure out what’s really in it.
What if the type isn't supported, what limitations on reading and writing exist in THIS engine? Which types of queries are weirdly slow and amazingly fast?
The list goes on for things that you don't need to add additional logic to.
Agreed. It’s strange to hear the lock-in argument when in practice even a set of pretty regular queries and DDL is generally non-portable, neither syntax nor performance-wise. Personally I can’t think of saying “let’s move <projectname> to <servername>” seriously at work, unless it’s a prototype which doesn’t require support yet or is a really insignificant auxiliary db.
I have separate DDLs for different databases. And while I've heard a lot of people theorize that this is a maintenance nightmare, it has never been that in my experience.
You are going to spend a lot more time being frustrated if you try to make one DDL work for all databases. Especially if a new database is added to the mix (which might not even be an SQL database).
I have written perhaps half a dozen applications in the past 3-4 years that supports more than one database. Typically for schemas somewhere in the region of 5 to 15 tables. It rarely takes me more than 5-10 minutes to take the SQLite DDL and adapt it to, say PostgreSQL, including rewriting the SQL statements in the storage API layer. And you can design with this in mind.
(I often start with SQLite as it is the least feature rich database. Anything I can express for SQLite is easy to express for other SQL databases, and possibly NoSQL stores. And I can make do with "lowest common denominator" because any fancy stuff tends to just make life harder, so I tend to avoid it unless I have very, very specific needs)
Granted, it is a bit annoying to do schema updates across several DDLs, but so far, I've spent far less effort than a colleague of mine who always tries to maintain a single DDL and a single storage API layer implementation that is supposed to work across 2-3 SQL databases.
Also granted that it isn't elegant to maintain different DDL and store API implementations, but it turns out to not be as much work as people tend to claim.
You write a change set that describes the schema you want to build and it generates DDL for whatever database you are using. It can also reverse engineer a change set from one database that you can use to generate DDL for a different database.
I supported an application that ran on half a dozen different DBs and it wasn't much of a problem. There were a few places where I had to specify change sets specific to one database implementation.
Many ORM's can solve this problem.
>I can’t think of saying “let’s move <projectname> to <servername>” seriously at work, unless it’s a prototype which doesn’t require support yet or is a really insignificant auxiliary db.
I've had to do it three times in my career. It usually comes from management. It ain't pretty. Two were from M&A and one was to get rid of an obsolete database.
Many ORMs can hide the problem.
One reason for this is that I had a very bad experience with this. I was involved in two projects in the 1990s where we went all in and used a lot of features unique to two different databases. In one of the cases, the database vendor (a small niche player) got acquired by an automation company and their "real-time capable" database was no longer available for licensing as a separate product. This left us stranded. The other project involved loads of PL/SQL in an Oracle and what killed us was the combination of Oracle licensing fees, the scaling limits of the database and that it was really hard to find good PL/SQL developers.
Today we use several strategies to keep the data store choice flexible.
1) we use databases as pure data stores. Sure, we use constraints, but only as an (optional) extra safeguard that lets us know when we have screwed up. We aren't dependent on the constraints, but they are immensely useful for rooting out inconsistencies in the code and the model.
2) we try to model the data in ways that can be realized on all databases (or stores) of a certain class. Yes, this means that sometimes you have to use slightly different data types than you'd like, but that isn't the end of the world. In some cases what you store is a reasonably useful representation of a type, and then you create richer types to represent it in the application.
3) we tend to start development using SQL databases, but not all parts of the schema are expressed as a purely relational model. This way we can split the schema and handle parts of it using an entirely different class of database or store. This gives us the convenience of being able to start a project with a single database, exploit the availability of constraint enforcement to show us where we make mistakes, but as we need to scale up, parts of the schema can easily be migrated to, for instance, a timeseries database.
This both helps us off the ground quickly and it allows us to postpone actually building a scaled up implementation. (This has proven particularly useful on projects where we have been able to postpone handling a large scale case for years and then having more technology to choose from later).
4) to accomplish 3 we often spend a lot of time understanding what the key data access patterns for the application are going to be and keep this in mind when designing a schema and a scaling plan. It also helps if you can identify where you can decide that data is going to be immutable so that consistency becomes easier later.
5) the application logic never talks directly to the database. There is always a domain specific store service API between the application. We try to make this API as semantically narrow as possible, and try not to make any promises that would be hard to keep if we have to change storage technology. Again, it helps to partition this API into areas that can be realized with different classes of databases, so you can make fewer promises on the parts of the API that will handle the part of the schema that drives most of the operational cost.
6) all tests are written against the store service API, never directly against any database or storage technology. This way you already have a comprehensive test suite if you need to migrate to a new database entirely, or for when you start implementing different parts of the store using different database technologies.
In the beginning this approach will require a bit more work, but when you have established practices and it becomes a habit, it actually doesn't represent more work to implement than more direct ways of using databases. Yes, we sometimes forego using functionality offered by the database that would have saved us some work in the short term, but it pays off in the long run because you end up with simpler and more flexible designs.
A few years ago I did an experiment with two new hires. Their first task was to add support for a relatively new database to an existing application. They had zero prior experience with the application. I pointed them to the storage service API and the tests and I said "write an implementation of it that uses database X" (which was a NoSQL database). After about two days of work they had an implementation that passed all of the tests and could run all of the benchmarks without any problems. This included adding configuration options that allowed the application to initialize and run with the new store implementation. I think we discovered only 2-3 minor issues in their code over the next 12 months, which isn't bad for a first contribution to an unknown code base.
(Since we only had one set of tests which was leveraged across all implementations it also meant that every time we expanded and improved the tests, we'd be able to improve all implementations as the tests got better at finding subtle mistakes).
Because it was so cheap and easy to add support for new databases, I think we had support for something like 5 different databases. Including an embedded SQL database so that you could run the application locally for experimentation or integration testing (it is really nice to be able to fire up the application with an in-memory database for integration tests. It wasn't unusual that we ran integration tests that would create and destroy the storage a few hundred times in just a few seconds). After I left the project they added support for another database and migrated terabytes of production data with minimal downtime. The migration tools they wrote for this used the store service API as well, which means they developed one tool that would let them migrate between arbitrary databases. (So if, in the future, they chose to switch databases again, the migration tool already exists)
We also had adapters that allowed us to run different permutations of databases. So we could handle parts of the schema suited for relational databases using an SQL database, and handle the bulky bits with somewhat narrower requirements using a NoSQL database. This requires a lot of care when you design the API to have built in "fault lines" where you can split the schema, but with a bit of practice it becomes a habit. (In some cases we've made "conditional" schemas - so for instance if you run the entire thing in one database you may have a bunch of constraints to help you ensure consistency, but you run with mixed stores, you may not apply the constraints that would span across databases. Again, we try not to depend on the constraints, but they are a great help in designing the schema, the store service API and the application).
I think people want to avoid this because its annoying or you have to think a decent bit up front or its just cheaper and easier to do this directly this one time, until its everywhere.
true but it is for a reason. For example you can replace/upgrade any piece of code on the running system transactionally. But in a modern programming language, let's say Java, it is not possible.
Should PL/SQL be similar to a "modern programming language" it would lose a lot of its perks.
Generally speaking PL/SQL is not much different from DDL like "create table ...". The latter is similar code describing "business logic"
There are a lot of reasons not to use stored procedures heavily and I personally wouldn’t even work for a company that did, this is a relatively poor one.
This is like all of the people who think using Terraform gives them “cloud agnosticism” even how you do limits and bulk sql is different between the major databases.