Users expect their web apps to be fast and responsive. A slow app will see reduced engagement, fewer conversions and worse search rankings. In a full stack application performance is a function of every layer in the stack: the browser, the network, the server and the database. Fixing just one layer is rarely going to be enough. This article looks at how you measure performance and then goes on to look at some practical techniques you can use on the frontend, the backend and the database to build apps that feel fast and are robust as they scale.
Measure Before You Optimize
Optimisation is best done on the basis of data rather than guesswork. Before you change any code at all, you need to establish where the time is actually going.
Key Metrics to Track
Google’s Core Web Vitals are a useful starting point for frontend performance:
- Largest Contentful Paint (LCP): the time it takes for the main content to load.
- Interaction to Next Paint (INP): how quickly the page responds to user interactions such as clicks and taps.
- Cumulative Layout Shift (CLS): how much the layout shifts unexpectedly as the page is loading.
On the backend we track response time, throughput (number of requests per second) and error rate.
Useful Tools
Lighthouse and Chrome DevTools will tell you how a page loads and renders. APM tools will trace the slow request across your services. Load testing tools (k6, Apache JMeter etc) will tell you how the system behaves under load. Record a baseline before you make any changes so you can verify that each of your improvements actually does make a difference.
Frontend Optimization Techniques
Reduce JavaScript Size
Large JS bundles take a long time to download, parse and run. Code splitting breaks the bundle up into smaller pieces which only get loaded when required. Tree shaking gets rid of any code that isn’t actually used during the build. You can also look at your third party libraries and replace the heavy ones with lighter alternatives to reduce the bundle size.
Optimize Images and Assets
Images are typically the biggest files on a page. Use modern formats (WebP, AVIF etc) and serve images at sizes appropriate to the user’s screen. Lazy load images so they only load when the user actually scrolls to them. Set the width and height attributes to prevent layout shifts.
Use Compression and a CDN
Turn Gzip or Brotli compression on the server to compress your text files (HTML, CSS, JavaScript etc). A CDN will store copies of your static assets on servers closer to the end user, so the data doesn’t have to travel as far.
Backend Optimization Techniques
Apply Caching Wisely
Caching stores the results of expensive operations so they do not have to be repeated. In-memory stores such as Redis can hold frequently requested data, like product lists or user sessions. HTTP caching headers tell browsers and CDNs how long they can reuse a response. Each cache needs a clear expiry or invalidation rule so it does not serve outdated data.
Move Heavy Work to the Background
Tasks like sending an email, generating a report or processing a file that’s been uploaded can all be quite slow in the context of a request. The way to deal with this is to put them into a job queue. The server can then respond immediately to the request and the actual work can be done by a separate worker.
Keep APIs Lean
Return only the fields that the client needs and paginate large lists. Smaller responses get there faster and use less memory on both sides. A lot of full stack developer classes will teach these API design practices from the very start as they prevent performance issues before they even arise.
Database Optimization Techniques
Add the Right Indexes
An index is used to help the database locate the rows without having to do a full table scan. Columns that are frequently used in WHERE clauses, JOINS and in sorting are the ones you want to index. Don’t go overboard with the number of indexes you create though as each one will have a negative impact on write performance.
Fix Inefficient Queries
The N+1 query problem is a very common issue in apps that use an ORM. You end up with one query to get the list and then a separate query for each item in that list. Joins and eager loading are the ways of getting around that and reducing the number of queries. Most databases have an EXPLAIN command which will tell you what’s happening when a query is run and where it can be improved.
Use Connection Pooling
Opening a new database connection for every request is expensive. A connection pool maintains a set of connections which can then be reused, resulting in lower response times under load.
Conclusion
Fast full stack apps come from steady, incremental improvements across the board. Start by tracking the right metrics. Then work on reducing the load on the frontend, the load on the cache and the load on the backend, and then go on to optimise your database queries. Test after each of those changes to make sure you’re actually getting any benefit. For those of you who want a bit of structured, hands-on practice with these sorts of techniques, full stack developer classes can provide you with guided projects that will help you build up good performance habits from the outset.
