Part 7 — Performance
On this page 8 headings
Once the code is deployed, we must make sure the application performs to an acceptable standard. The lecture covers three ways to improve performance:
- Caching
- Task queues
- Making pages load and render quickly
7.1 Caching
Section point: Caching stores the results of common operations in memory, which reduces the load on servers. Instead of fetching data from the database every time, it is fetched from memory, which is faster.
If the same queries run again and again, and the database has not changed, their results can be stored in memory and served from there.
Caching tools: Memcached and Redis. Both keep data in memory as key–value pairs.
Levels of caching (lecture)
Caching can happen at three levels:
- Web browser cache. The browser keeps copies of previously visited pages and checks whether the page has changed. If the page has not changed, it can use its cached copy.
- Web server — page cache. The web server stores generated pages and can send a stored page to the client without going to the database.
- Database — query cache. The database keeps the results of recent queries that have not changed, and returns them without re-running the query.
7.2 Task queues (asynchronous work queues)
As a website grows, some tasks become resource- and time-consuming:
- sending emails or notifications to thousands of users;
- bulk creating or updating database records. For example, deleting a user means deleting all data associated with that user.
These tasks need not be done immediately. The user can be told that the task will be carried out, and notified later when it is complete.
The problem with the normal request–response cycle
Normally: the user sends a request (“send an email to my friend”) → the server processes it and actually sends the email → the server responds “email sent”. The user waits the whole time. For one email that is fine. For thousands, it is not scalable, because nobody can wait that long for a response.
The solution: an asynchronous work queue
Section point: An asynchronous work queue executes tasks outside the normal HTTP request–response cycle. It consists of:
- a queue of jobs to be performed, along with their parameters, and
- a pool of workers that take jobs from the queue and perform them.
The server just adds a job to the queue and responds to the user immediately. The workers do the slow work in the background.
Why a pool of workers?
- Many workers share the work, so the jobs are finished in much less time than with a single worker.
- If you expect higher traffic than usual, you can dynamically increase the number of workers so jobs still finish quickly.
7.3 Loading and rendering pages quickly
Web applications contain a lot of static content: HTML, CSS, JavaScript and images. These must load quickly.
Why does a few seconds matter?
- User stress: the lecture shows research that the stress caused by mobile page delays is comparable to watching a horror movie. Even a few extra seconds of loading make us uncomfortable and agitated.
- Business impact (lecture examples, worth remembering):
- 2016 — AliExpress made its site 30% faster → customer orders rose 10.5%.
- 2006 — Google slowed its search results by half a second → user requests dropped 25%.
Method 1 — Minify the code
Minify = remove unnecessary whitespace and comments, and sometimes shorten names, while preserving program behaviour. Required separators and whitespace inside string values must be preserved. A minifier can also shorten names (such as parameter names). The code still does exactly the same thing; it is just smaller.
Section example: a small JavaScript function that converts Fahrenheit to Celsius.
// Before minification
function fahrenheitToCelsius(fahrenheit) {
// convert the temperature
var celsius = (fahrenheit - 32) * 5 / 9;
return celsius;
}
// After minification
function fahrenheitToCelsius(e){return 5*(e-32)/9}(The code above illustrates the idea; the lecture’s exact snippet came from DigitalOcean’s minify tool.) For the lecture’s small function, minifying saved about 41% of the size. Across a whole website this adds up to a large saving, so pages load faster.
Method 2 — Compress static pages
Tools such as Gzip and Brotli compress web pages and other resources before sending them. The browser decompresses them on arrival, and the original content is restored exactly, because the compression is lossless.
Minification vs compression: the two are different and are used together. Minification rewrites the code itself to remove unneeded characters. Compression encodes the bytes for transfer and is undone by the browser.
