Cache: Optimizing Performance

Fatma Erkan
3 min readJun 19, 2024

--

Cache is a technique that is used to improve the performance of applications. It stores commonly used data in temporary storage so that the app can quickly show it to users without having to get it from the primary storage every time. This makes things speed up and work more smoothly for app clients.

The main goal of caching is to make apps work better. It does this by making them respond faster to user requests. This improves the user experience and overall application performance. In addition to this caching also reduces the load on resources which reduces costs and provides scalability.

Caching involves storing data temporarily, allowing for quicker access the next time it’s requested again. It is one of the most effective methods of speeding up the application. Various levels of cache exist, each serving a distinct purpose within an application. Common types include client-side cache, in-memory cache, and distributed cache. Despite their independence, they are all working to achieve the same goal: a faster application.

Common use cases contain API responses, database queries, and static data. However, it is important to refresh cached data to avoid serving outdated content. Caching provides clients with a positive application experience. Caching the infrequently accessed data consumes resources unnecessarily. When caching frequently changing data, caution is necessary.

What Can Be Cached?

Almost anything you see on the app can cached. However, for good caching in the app, the cached data should have two main properties:

  1. It should not change a lot.
  2. It is being accessed frequently.

Why Use Cache?

  • Application Performance: As the frequently read data is requested from the cache, data retrieval will be extremely fast to improve application performance. Usage when on special events (holidays) has an impact on the app performance with increasing primary database load which outcome is higher latency.
  • Reduce Backend Load: If the app loads data from the cache rather than invoking the same from the primary database, this helps to reduce the load on the backend and also improves the act.
  • Increase Productivity: Cache systems provide lower latency and high request processing rates compared to the primary database. (request/second)
  • Reduce Database Cost: Database cost is reduced with the cache mechanism, by performing numerous requests/responses per second.

Things to Pay Attention

  • In a cache, the stored data is a copy of the original data. The data on the original source can be modified, and in this case, the copy of the data in the cache should also be updated. Otherwise, the user utilizing the data in the cache might perform incorrect operations. The data stored in the cache should be invalidated after a certain time and refreshed from the original source.
  • In a cache, an expiration time should be determined for the cached data. If not determined, the data will remain in the cache, and if proper principles are not established, retrieving the data from the original source might be restricted. Short periods should be preferred for frequently changing data, while long periods should be preferred for data that doesn’t change often. To ensure cache health and allow users to access up-to-date data, expiration times should be defined for the data stored in the cache, and this duration can be managed by the application.
  • In order to access the data copied into the cache, the data needs to be addressed, meaning a key must be defined, and this key information must be unique. In the cache mechanism, a unique key should be defined to access the copied data, thus preventing the data from overwriting or mixing with each other.

--

--