SECTION 08 / 9

Part 7 — Performance

4 min read
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
Slow website vs fast website: a slow store makes users wait, frustrates them and may make them leave; a fast store gives a quick response, happy users and smooth shopping.
Slow website vs fast website: a slow store makes users wait, frustrates them and may make them leave; a fast store gives a quick response, happy users and smooth shopping.
Open image ↗
Illustration: Part 7 — Performance

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.

Without a cache, every request makes the application server query the database again (repeated work). With a cache: on a cache miss, the data is fetched from the database and stored; on a cache hit, the stored result is reused.
Without a cache, every request makes the application server query the database again (repeated work). With a cache: on a cache miss, the data is fetched from the database and stored; on a cache hit, the stored result is reused.
Open image ↗
Illustration: Part 7 — Performance

Caching tools: Memcached and Redis. Both keep data in memory as key–value pairs.

Redis and Memcached store results of common operations in memory, reducing repeated database work.
Redis and Memcached store results of common operations in memory, reducing repeated database work.
Open image ↗
Illustration: Part 7 — Performance

Levels of caching (lecture)

The three levels of caching: (1) browser cache — the browser reuses stored website files (CSS, JS, images); (2) server cache — the application server reuses stored data or pages; (3) database query cache — the database reuses results of recent unchanged queries.
The three levels of caching: (1) browser cache — the browser reuses stored website files (CSS, JS, images); (2) server cache — the application server reuses stored data or pages; (3) database query cache — the database reuses results of recent unchanged queries.
Open image ↗
Illustration: Part 7 — Performance

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.

Synchronous vs asynchronous: synchronously, the Flask server saves the order, sends the email, and only then confirms, so the customer waits; asynchronously, it saves the order, puts an email task in the task queue, responds immediately, and a background worker sends the email.
Synchronous vs asynchronous: synchronously, the Flask server saves the order, sends the email, and only then confirms, so the customer waits; asynchronously, it saves the order, puts an email task in the task queue, responds immediately, and a background worker sends the email.
Open image ↗
Illustration: Part 7 — Performance

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.
Task queue and worker pool: the application records the order and creates background jobs; jobs (Email A, B, C) wait in the task queue; several workers take jobs in parallel and send the confirmation emails.
Task queue and worker pool: the application records the order and creates background jobs; jobs (Email A, B, C) wait in the task queue; several workers take jobs in parallel and send the confirmation emails.
Open image ↗
Illustration: Part 7 — Performance

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.

Minification: readable HTML, CSS and JS with comments, whitespace and formatting go through a minifier, which removes unnecessary characters while keeping the intended behaviour, producing smaller files and less data to transfer.
Minification: readable HTML, CSS and JS with comments, whitespace and formatting go through a minifier, which removes unnecessary characters while keeping the intended behaviour, producing smaller files and less data to transfer.
Open image ↗
Illustration: Part 7 — Performance

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.

Gzip and Brotli: the web server compresses HTML, CSS and JS losslessly, the compressed response travels over the network (fewer bytes), and the browser decompresses it so the original content is restored and the site displays normally.
Gzip and Brotli: the web server compresses HTML, CSS and JS losslessly, the compressed response travels over the network (fewer bytes), and the browser decompresses it so the original content is restored and the site displays normally.
Open image ↗
Illustration: Part 7 — Performance

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.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑