Where and how to store data
Serverless systems don't have a hard drive, where does your data go? You need a database.
But how do you choose which database and where do you put it? It depends.
Every database has advantages and disadvantages. Always fit your tech to your problem, not your problem to your tech.
First, what is a database? It's a system for storing and organizing data.
A database is an organized collection of data, generally stored and accessed electronically from a computer system.
Every database technology gives you these features:
- keeps your data
- lets you query data
- lets you update data
Keeping data is the difference between a cache and a database. You can have an in-memory database for speed, but that doesn't make it a cache.
How to choose a database
Databases seek to find a balance between different optimization criteria. Your choice depends on how that balance fits the problem you're solving.
The common criteria are:
- Speed of reading data
- Speed of writing data
- Speed of updating data
- Speed of changing the shape of data
- Correctness of data
Notice how the list is about speed? That's because speed of data access is the biggest predictor of app performance.
I've seen API endpoints hit the database 30+ times. Queries that take 10ms instead of 1ms can mean the difference between a great user experience and a broken app.
ACID – a database correctness model
We'll focus on speed in this chapter. But to gain speed and scalability, databases sacrifice correctness. It's important that you know what correctness means in a database context.
Traditional databases follow the ACID model of transactional correctness. A transaction being a logical operation on your data.
- Atomicity ensures that operations inside a transaction succeed or fail together and aren't visible until they all succeed
- Consistency ensures your data is in a valid state and doesn't become corrupted by a half-failed transaction
- Isolation ensures transactions executed in parallel behave the same as if they happened one after another
- Durability ensures that once a transaction succeeds, it stays succeeded and the data doesn't vanish
Certain databases add additional levels of logical correctness on top of the ACID model. We'll talk about those later.