Performance optimization drives us to implement a caching mechanism in our system. One of the best way is to use Caching to store frequent data.
Caching is the way of storing data in a temporary storage location, so that data retrieval can be made more efficient.
Usually, we put that kind of data in cache that changes infrequently.
For example, to get a person’s Avatar you might need a trip to the database. Instead of performing that trip every time, we will save that Avatar in the cache, pulling it from memory every time you need it.
Benefits of Caching:
1. Reduces the load on server by reducing the round trips to the server
2. Increases performance by making application more responsive.
Types of Cache:
- In-Memory Cache is used for when you want to implement cache in a single process. When the process dies, the cache dies with it. If you’re running the same process on several servers, you will have a separate cache for each server.
- Persistent in-process Cache is when you back up your cache outside of process memory. It might be in a file, or in a database. This is more difficult, but if your process is restarted, the cache is not lost. Best used when getting the cached item is expansive, and your process tends to restart a lot.
- Distributed Cache is when you want to have shared cache for several machines. Usually, it will be several servers. With a distributed cache, it is stored in an external service. This means if one server saved a cache item, other servers can use it as well. Services like Redis are great for this.
The use of Redis is widespread in distributed caching scenarios. Redis is an in-fast-memory datastore, it is opensource and is of key-value type.
It provides response times of less than one millisecond, allowing millions of requests per second for each real-time application in a variety of fields.
If the infrastructure of your application is based on an AWS cloud, the service Elastic Cache -> Redis Cache is available, and it can be configured from your subscription.
To use the distributed cache on Redis, the installation of the package Microsoft.Extensions.Caching.Redis is necessary.