dummy text

    How to increase performance for any Web App

    Wondering how to increase performance of your web-app to meet the increasing load? All this without causing any downtime to the already implementd system? Read on to find out more.

    Posted by Simar Mann Singh on 14 Jul, 2023

    Introduction

    In today's fast-paced digital world, the web applications must deliver lightning-fast performance to meet the demands of millions of concurrent users. Whether it is an e-commerce platform, a social media network, or a content streaming service, optimizing the performance of a web application is crucial for providing a seamless user experience. The increase in load (number of requests) may not necessarily be constant. Perhaps some special event or some season might also require proper planning and preparation to cope up with the increase in load. Fortunately, there are various strategies that developers and system architects can employ to ensure their web apps can handle the immense load and scale effortlessly. In this blog post, we will explore some effective techniques to increase the performance of a web app serving millions of requests per second, ensuring its reliability, responsiveness, and overall user satisfaction.

    In the highly competitive online market, even the slightest delay or performance hiccup can result in frustrated users and lost business opportunities. To avoid such pitfalls, it is essential to implement strategies that not only enhance performance but also maintain stability under heavy traffic loads. From caching and load balancing to database optimization and content delivery networks, each technique plays a crucial role in optimizing the web app's performance. By adopting these strategies and continually refining the application, developers can provide users with a seamless browsing experience even during peak usage periods.

    Strategies

    To start with, Lets take a look what can be implmented to boost the performance.

    Implement a Caching Layer

    What exactly is a caching layer and how will it help boost performance? Let me explain. So, you already might have some sort of Database layer in your application. But most of the queries you receive at the DB end are purely redundant, and unnecessary. This actually puts load on your DB engine. A Caching layer can be considered as a first point of contact before a DB query is passed on to the DB engine itself. So the caching layer will act as a smaller but faster DB instance which would reduce load on the actual DB. And it shall only store queries that have been requested before. So, the next time a redundant query is requested by the webapp, it's result would already be cached in the caching layer, and thus we would not need to query the DB engine.

    There are various ways we can implement a Caching layer. The most popular being Reddis, MemCache, Cassendra etc.

    What we also need to consider for Caching layer is that we must try to host the caching as close to the web-app itself as possible, even on the same machine / node if that is possible. This would reduce the network latency by a great factor and not only our queries will be fast, but the to and fro communication between webapp and caching layer would also be snappy.

    We can also try to to put heavy or expensive DB queries or complex SQL joins on the caching layer. So, the DB is requested less often and that reduces load on the DB.

    Maximum Number of Connections

    One another area where performance can be boosted is the Web-app or the Load-balancer (if it exists). So, if the Web app gets some traction and it starts receiving high number of requests, there might come a situation where the web-app starts to take longer to respond to request because the maximum number of concurrent connections of the web-app are reached quickly. For exmaple, for the Apache server, the maximum number of concurrent connections is 8k-10k.

    The most common cause for this that the DB queries are taking more than expected amount of time to execute. Basically, some queries are holding up. So naturally, the first thing we can do is check if there exists any such DB queries. If yes, we can try to optimise the DB queries as much as we can. If the desired performance is not acheived, we can also consider switching to solutions like Elastic Search. Anyhow, the goal is the release the connection quickly and not keep them holding.

    In case the DB is not the culprit, it could be that the server itself runs out of the available connection because the demand is too high than what the server can cater to. In such a scenario, what we can do is try to switch to a language / framework that supports more number of concurrent solutions. One example could be, switching to Phoenix Framwork for example, might give us ability to have 2,000,000 concurrent connections.

    Content Delivery Network (CDN)

    There could be image data or some static data that our application serves to the user requests. Normally, for each request, this data would be fetched from the origin server and this will always put load on the server itself and will result in higher latency for users who are farther geo-located to the server. So, request from around the region of server would be served quickly but the latency for users across the globe will be catastrophic.

    To solve this problem, we can utilize a CDN to cache and serve static content from geographically distributed edge servers. A CDN is basically a network of globally distributed but connected servers, which acts as the source for the static files we might have in our application like images. This reduces latency and offloads the serving of static files from your application servers.

    Database optimization

    Its very likely that the DB will fail first before anything else. So we would need to take steps to avoid this from happening. One possible solution to counter this issue could be to optimize database queries as much as possible, and use indexes. DB Optimization itself is a vast topic which cannot be covered in a section on some blog post. Many books have been published, just to serve this topic. One quick optimization we can do is, using the right combination of the primary key. Most of the time, if we happen to have primary key made out of multiple information, it would perform well on those set of information, which is included in the primary keys.

    Another thing we can do is, consider using database sharding or replication to distribute the database load across multiple servers. So that in case of some mis-happening, we can always rollback to the most recent version of database. Many cloud DB solutions offer this out of the box. But, in case a Database layer is implement explicitly, without using any cloud DB, then data replication and sharind must be considered for better reliability.

    One other solution might be to implement a Master-Slave Database achitechture. Basically, we can have two or more database servers - one would be the master server which would be source of truth, and write operations would always be done on the master database, others would be slave servers which are always copying data from the master. Data can be read from a slave but never written into it.

    Asynchronous processing

    Some times our application could have some expensive sections which might take a while. Holding a request till the entire process is completed is not recommended actually. In such a scenario, we can implement a Queue mechanism to offload time-consuming tasks to background processes or queues, allowing the main application to respond quickly. This can be achieved using message or task queues, or distributed job systems. This allows for efficient handling of peak loads and ensures better resource utilization.

    Optimizing code and algorithms

    The application could have been created some time back and a lot more efficient algorithms or approaches might have been available. A good strategy is always to keep doing some research and development in this section and regular reviewing and optimizing our codebase.

    Using efficient algorithms and data structures, minimizing unnecessary computations and useless complication in queries and identify and fix any performance bottlenecks will result in an increase in the performance of the web application for sure.

    Prioritizing the delivery of critical content, implementing lazy loading techniques for non-critical or below-the-fold content to defer its loading until necessary, will give extra boost to the web app, making it snappy even if its just a little (due to different network speeds, this could be significant as well).

    Monitoring and profiling

    There could be scenarios where a web application might be struggling on some area, and it could cause serious business damage if such issues are not discovered soon enough. But what could be done to identify such bottlenecks quickly? The answer to that question is to Implement a robust monitoring mechanism using cutting eedge profiling tools to identify performance issues and optimize the application the moment any issues appear.

    Measuring response times, tracking database queries, and analyzing server resource usage could be the primary responsibilities under this umbrella of tasks. Most of the profiling tools these days offer all these metrics and a lot more out of box. Using such tools regularly would help in pinpointing pain areas to be considered for improvement.

    Conclusion

    There's surely a lot more stretagies and approaches that could be considered for a large application like Twitter or Facebook. But the approaches listed above would give us a good picture of what all pain areas exists and how these can be mitigated.

    Obviously, the specific strategies we choose will depend on our web application's architecture, technology stack, and requirements.

    Do let me know your reviews, if you like this post, or if you've any suggestion, in the comments below.