Caching and performance

https://github.com/heig-vd-dai-course

Web · PDF

L. Delafontaine and H. Louis, with the help of GitHub Copilot.

Based on the original course by O. Liechti and J. Ehrensberger.

This work is licensed under the CC BY-SA 4.0 license.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Objectives

  • Understand the concepts of caching
  • Understand how caching can improve performance
  • Understand how HTTP features can help to cache data
  • Implement caching in a web application
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Caching

More details for this section in the course material. You can find other resources and alternatives as well.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Caching

Process of storing a copy of a resource to serve it faster.

Caching can improve the performance of a system and reduce the load on the backend.

Caching can be done on the client-side or server-side.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Types of caching

  • Client-side caching (private caches): once a client has received a response from a server, it can store the response in a cache. The next time the client needs the same resource, it can use the cached response instead of sending a new request to the server.
  • Server-side caching (shared caches): the server stores data in a cache with the help of a reverse proxy or by the web application. The next time the server needs the same resource, it can use the cached response instead of processing the request again.
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

CDN

Content Delivery Network (CDN) is a network of servers that are geographically distributed around the world.

Improve performance by serving static content (images, videos, etc.) from the closest server.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Where to cache?

The best would be to cache at each level of the system to ensure the best performance but it is not always possible or faisable:

  • Client-side: the cache is stored on the client
  • Server-side: the cache is stored on the server
  • CDN: the cache is stored on a CDN

Private caches are caches that are only used by one client. Public caches are caches that are used by multiple clients.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Managing cache with HTTP

More details for this section in the course material. You can find other resources and alternatives as well.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Managing cache with HTTP

Managing cache is challenging because it is difficult to know when to invalidate the cache (the data can be stale (= outdated)).

Two main caching models:

  • Expiration model: the cache is considered valid for a certain amount of time
  • Validation model: the cache is considered valid until the data is modified
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Expiration model

  • The cache is valid for a certain amount of time
  • If the cache is not expired, the cache is used
  • Uses the Cache-Control: max-age=<secondes> header
  • The cache is invalidated after the expiration time
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Validation model

  • The cache is valid until the data is modified
  • If the cache is not expired, the cache is used
  • Two ways to validate the cache:
    • Based on the Last-Modified header
    • Based on the ETag header
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Based on the Last-Modified header

  • Last-Modified: indicates the date and time at which the resource was last updated.
  • If-Modified-Since: returns a 304 Not Modified if content is unchanged since the time specified in this field (= the value of the Last-Modified header).
  • If-Unmodified-Since: returns a 412 Precondition Failed if content has changed since the time specified in this field (= the value of the Last-Modified header) when you try to update/delete the resource.
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Based on the ETag header

  • ETag: provides the current entity tag for the selected representation. Think of it like a version number or a hash for the given resource.
  • If-None-Match: returns a 304 Not Modified if content is unchanged for the entity specified (ETag) by this field (= the value of the ETag header).
  • If-Match: returns a 412 Precondition Failed if content is changed for the entity specified (ETag) by this field (= the value of the ETag header) when you try to update/delete the resource.
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Is it possible to use both models?

Yes, it is possible to use the expiration model and the validation model at the same time:

  • No request attempt at all if the cache is not expired
  • Validation model when the cache is expired
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Managing cache with proxies

More details for this section in the course material. You can find other resources and alternatives as well.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Managing cache with proxies

Proxies can cache responses to reduce the load on the backend and improve performance.

Traefik offers a caching middleware in its Enterprise version. Out of reach for this course.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Managing cache with key-value stores

More details for this section in the course material. You can find other resources and alternatives as well.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Managing cache with key-value stores

Redis is a popular key-value store that can be used to store cache data.

We will implement this manually in Javalin.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Questions

Do you have any questions?

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Practical content

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

What will you do?

  • Implement and validate the validation model based on the Last-Modified header in your previous web application using curl and a web browser
  • This is your last practical content for this course..!
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Find the practical content

You can find the practical content for this chapter on GitHub.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Finished? Was it easy? Was it hard?

Can you let us know what was easy and what was difficult for you during this chapter?

This will help us to improve the course and adapt the content to your needs. If we notice some difficulties, we will come back to you to help you.

➡️ GitHub Discussions

You can use reactions to express your opinion on a comment!

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

What will you do next?

We are arriving at the end of the third part of the course.

An evaluation will be done to check your understanding of all the content seen in this third part.

More details will be given in the next chapter.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Sources

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0