Part 1 of 9
Nonfunctional requirements
Feature lists say what an app should do. This chapter is about how well it should do it. It gives clear words for four qualities: performance, reliability, scalability, and maintainability. With these words you can say exactly what "fast" or "reliable" means for your system, and weigh trade-offs with facts instead of feelings.
Functional and nonfunctional requirements
- Functional requirements say what the app does: which screens exist, what a button does, what an API call returns.
- Nonfunctional requirements say how well it does it: fast, reliable, secure, within the law, easy to keep running and change.
- Nonfunctional needs are often never written down.
- They still matter just as much. A banking app that takes a minute to show your balance, or loses a transfer now and then, is as good as no app.
Case study: social home timelines
The chapter uses one running example: a simple social network like X, where people post short messages and follow each other.
| Fact | Value |
|---|---|
| New posts per day | 500 million |
| Average posts per second | about 5,800 |
| Peak posts per second | up to 150,000 |
| Average follows and followers per user | about 200 each |
The average hides a huge spread. Most people have a handful of followers. A few famous accounts, such as a former president, have over 100 million.
Building the timeline with a query
- Keep the data in a relational database with three tables: users, posts, and follows.
- The main read is the home timeline: the latest posts from everyone you follow (leaving out ads and suggestions).
- As a query, the database finds who you follow, pulls each one's recent posts, merges them, sorts newest first, and keeps about 1,000.
- Goal: a new post should reach followers within about five seconds.
- The simple way is polling: each app reruns the query every five seconds.
- With 10 million people online, that is 2 million queries a second.
- Each query is costly: if you follow 200 accounts, the database fetches and merges 200 lists. So 2 million queries times 200 is about 400 million post lookups a second.
- That is only the average. People who follow tens of thousands of accounts make their own queries far more expensive.
Precomputing timelines (fan-out on write)
- Two changes help: push new posts to followers who are online instead of making them poll, and compute timelines ahead of time so reads come from a cache.
- Keep a ready-made timeline for every user.
- When someone posts, look up their followers and add the post to each follower's timeline. Think of a newsletter printed once and dropped into every subscriber's mailbox.
- Opening the app now just reads your stored timeline. The app can also listen to the stream of new items to show alerts.
- The cost: each post now does more work, because the timelines are derived data that must be kept up to date.
- Fan-out is when one request causes several more requests downstream. The fan-out factor is how much the number of requests multiplies.
- 5,800 posts a second times a fan-out of 200 is just over 1 million timeline writes a second. That is far less than 400 million lookups.
- During a burst of posts, the deliveries can wait in a queue. Posts show up a little later, but timelines still open fast from the cache.
Materialized views
- Computing a query's result ahead of time and keeping it up to date is called materialization.
- The stored timeline cache is a materialized view.
- It makes reads faster and writes more expensive, because every post now touches many timelines.
- Two extreme cases need special care:
- Someone who follows thousands of very active accounts gets a flood of writes to their timeline. They could never read it all, so it is fine to drop some writes and show a sample. - A celebrity's post would need millions of timeline inserts, and dropping them is not OK. So celebrity posts are stored apart and mixed into a timeline when it is read. Even then, serving celebrities takes a lot of machinery.
Building timelines
Step 1 of 5
Building the timeline when it is read: Asha opens the app, and the database pulls recent posts from everyone she follows and merges them.