Database scaling patterns: meet Vinnie

Space Llama hands over to Vinnie, the space dog with a Very Important Nose. Why the database is often one of the first things to slow down when an app gets busy, and eight scaling patterns, roughly in the order teams reach for them: indexing, query optimisation, connection pooling, caching, read replicas, materialized views, sharding and CQRS.

GazeekDATABASE PATTERNS WITH VINNIE·EPISODE 1
EPISODE 1

Database scaling patterns: meet Vinnie

Space Llama hands over to Vinnie, the space dog with a Very Important Nose. Why the database is often one of the first things to slow down when an app gets busy, and eight scaling patterns, roughly in the order teams reach for them: indexing, query optimisation, connection pooling, caching, read replicas, materialized views, sharding and CQRS.

0:00 / 2:55

Diagrams in this video · 1Free in archhy

Copy and edit in archhy: The scaling ladder (new tab)

The diagrams, full size

Copy any of them into archhy, free, and change it for your own system.

After this lesson you can

  • Say why the database is often one of the first things to slow down
  • Name the eight database scaling patterns
  • Put them roughly in the order teams reach for them
Transcript

Database scaling patterns: meet Vinnie

Space Llama here. And today, I'm handing over. Meet Vinnie. Very Important Nose. He sniffs out slow queries. Hello. I'm Vinnie. Space dog. Databases are my thing. He's all yours. I'll be in the hammock. Right. Let's get sniffing.

Why the database often hurts first

Here's the thing. Almost every request your app handles ends up at the database. Web servers are easy to add, as long as they don't keep anything between requests. Then you just run more of them. The database is different. It holds the state, and state is much harder to spread across machines. So when an app gets busy, the database is often one of the first things to slow down. This series is what to do then.

The scaling ladder

Here's the whole series, as a ladder. Roughly the order teams reach for them, the simpler fixes at the bottom. One. Indexing. Help the database find rows without reading the whole table. Two. Query optimisation. Read the query plan, fix the slow query, and kill the N plus one problem. Three. Connection pooling. Reuse a limited set of open connections, instead of opening a new one for every request. Four. Caching. Keep popular answers in memory for a while, so the database isn't asked every time. Five. Read replicas. Copies of the database take reads, while the primary takes writes. They can lag a little behind. Six. Materialized views. Work out an expensive answer ahead of time, store it, and refresh it. Seven. Sharding. Split the data across several databases, by a key. Eight. CQRS, command query responsibility segregation: separate models for reads and writes. And a database per service, split by who owns the data. Climb only as high as you need. Each rung costs more to run, and more to get wrong.

Remember

Let's recap. The database holds the state, so it's often one of the first things to slow down. There are eight patterns to try, from indexing up to CQRS. And you climb only as high as you need to.

The full article, and what's next

Next time, the first rung. Indexing. See you there. Not bad. For a dog.

Read the article

PostgreSQL: Indexes: the whole idea in writing, free.

Read it ↗