Advanced Optimisation Techniques
Overview
Beyond the basics, advanced performance optimisation requires a holistic approach. This tutorial covers image optimisation, caching strategies, CDN integration, database tuning, and profiling techniques that will make your application truly production‑ready.
1. Image Optimisation
Images are often the largest source of page weight. Optimise them with:
- Format selection: WebP, AVIF for modern browsers, JPEG/PNG as fallbacks.
- Responsive images:
srcsetandsizesattributes. - Compression: Use tools like Sharp, ImageMagick, or Squoosh.
- Lazy loading: Defer off‑screen images.
2. Caching Strategies
Implement caching at multiple levels:
Browser Caching
Application Caching
Cache‑invalidation strategies
- TTL (Time‑To‑Live): Expire after a set time.
- Event‑based: Invalidate on data updates.
- Versioned keys: Increment version number on updates.
3. Content Delivery Networks (CDN)
CDNs distribute your content across global edge locations, reducing latency.
- Static assets: Host images, CSS, JS on CDN.
- Dynamic content: Edge caching with Cloudflare Workers or Vercel Edge.
- Popular CDNs: Cloudflare, Akamai, Fastly, AWS CloudFront.
4. Database Tuning
Database performance is critical for backend responsiveness.
- Indexing: Index columns used in WHERE, JOIN, ORDER BY, GROUP BY.
- Query optimisation: Use EXPLAIN to analyse queries.
- Connection pooling: Reuse database connections.
- Read replicas: Offload read queries to replicas.
5. Profiling & Monitoring
Use profiling tools to identify bottlenecks:
- Node.js:
--inspectflag with Chrome DevTools. - Python: Pyflame, cProfile.
- Java: JProfiler, VisualVM.
- Go: pprof (built‑in).
Quiz
Question 1
Which image format offers the best compression for modern browsers?
- JPEG
- PNG
- WebP
- GIF
Show answer
Question 2
Which cache invalidation strategy uses a fixed expiration time?
- Event‑based invalidation
- TTL (Time‑To‑Live)
- Versioned keys
- Manual invalidation
Show answer
Question 3
What does a CDN primarily improve?
- CPU usage
- Latency and load times
- Memory usage
- Database performance
Show answer
Exercises
Exercise 1
Write a function that compresses an image using Sharp, resizes it to 800x600, and converts it to WebP format.
Sample answer
Exercise 2
Write a SQL CREATE INDEX statement for a table with columns `user_id`, `created_at`, and `status` to optimise queries filtering by user and ordering by date.
Sample answer
This index supports queries that filter by user_id, sort by created_at, and filter by status.
Homework
Homework 1
Implement caching for your capstone project's API endpoints using Redis. Add caching for the most frequently accessed endpoint (e.g., GET /products) with a 5‑minute TTL. Document the performance improvement.
Sample outline
- Setup: Install Redis and connect to your application.
- Implementation: Cache the GET /products endpoint.
- TTL: 300 seconds (5 minutes).
- Testing: Measure response time with and without caching.
- Documentation: Show the before/after latency improvement.
Mini‑Project
Full Performance Optimisation
Apply a comprehensive performance optimisation to your capstone project:
- Optimise all images (convert to WebP, resize, lazy load)
- Implement Redis caching for at least 3 API endpoints
- Add cache headers for static assets (1 year for JS/CSS, no-cache for HTML)
- Set up a CDN for static assets
- Add at least 2 database indexes to improve query performance
- Re‑run Lighthouse and document the improvement
Sample outline
- Images: All images converted to WebP with responsive srcset.
- Cache: Redis for products, categories, and user profiles.
- Headers: Static assets served with immutable cache headers.
- CDN: Cloudflare set up for static assets.
- Indexes: Added indexes for query performance.
- Results: Lighthouse score improved from 78 to 94.
Tutorial Summary
You learned advanced performance optimisation techniques: image optimisation, caching strategies, CDN integration, database tuning, and profiling. These techniques are essential for building high‑performance, production‑ready applications that deliver excellent user experiences.
Key takeaway: Performance optimisation is a multi‑layer effort. Address bottlenecks at every level – from images and code to databases and infrastructure.