System Design Basics: Caching
Omar Ismail
Senior Software Engineer @ Digitinary | Java 8 Certified? | Spring & Spring Boot?????? | AWS? | Microservices ?? | RESTFul Apis & Integrations ?? FinTech ?? | Open Banking ?? | Digital Payments and Transformation??
Thanks to original creator :
https://medium.com/geekculture/system-design-basics-caching-46b1614915f8
What is Cache?
Cache is a small memory, fast access local store where we store frequently accessed data. Caching is the technique of storing copies of frequently used application data in a layer of smaller, faster memory in order to improve data retrieval times, throughput, and compute costs.
Why do we use Cache?
Cache is based on the principle of locality. It means that frequently accessed data is kept close to the system. The two kinds of locality are:
How does a Cache work?
When a request comes to a system, there can be two scenarios. If a copy of the data exists in cache it’s called a?cache hit, and when the data has to be fetched from the primary data store it’s called a?cache miss. The performance of a cache is measured by the number of cache hits out of the total number of requests.
Types of Cache
Cache Writing Policies
A?Cache Policy?is a set of rules which define how the data will be loaded (and evicted) from a cache memory. A cache is made of copies of data, and is thus transient storage, so when writing we need to decide when to write to the cache and when to write to the primary data store.
The most common cache writing policies are as follows:
Cache Eviction Policies
Cache eviction policies define a set of rules that decide what data must be removed when the cache is full and a new entry is to be added.
A good replacement policy will ensure that the cached data is as relevant as possible to the application, that is, it utilises the principle of locality to optimise for cache hits.
Therefore a cache eviction policy can only be defined on the basis of what data is stored in the cache. Some of the popular cache eviction algorithms are:
Redis: Distributed Caching
Redis is an in-memory data store that is most often used as a distributed cache. It offers a variety of efficient data structures designed to allow very fast access to your data. It is used as a caching mechanism in most of the distributed systems.
Redis has the option to also persist to a disk so the cache isn’t lost if the server restarts. And Redis can also be built in a cluster which spreads the cache across multiple servers. Technically Redis can even be used as your primary database as well, although more often we use it as a cache to reduce the load on more feature-rich databases that are meant for persisting data.
Redis is written in C, so it does not have multithreading capabilities like it’s java based counterparts like?Hazelcast. Redis is unique in that it supports other data structures like sorted sets, hash sets and a pub/sub mechanism. It’s also extensible via lua scripting. It is probably the most popular and widely used of the two products. Especially outside of the Java ecosystem.
Building @CrysPay on Aptos | ?? @RouterProtocol | Degree-ing ?? | Flutter + Web3?? | Pythoneer | DappDev | Sanskrit ????????| Contributor @PSF |
2 年?????? ??
Senior .Net Developer at FlairsTech
2 年Interesting... Hazelcast is many times faster.?Redis is single-threaded, so it does not efficiently scale for larger loads
Software Engineer (Node.js, Angular, React)
2 年What is amazing about your articles is their simplicity and information, Thanks for your effort.
Your Trusted Online Shopping Mall at Bestow Technolog Solutions
2 年Hi, I am a professional graphic designer. Are You Looking Eye-Catching Creative And Premium Quality Design For Stand Out Your Product/Services from a huge Crowd and reach the target audience?? Contact me:? ?? Email: [email protected] Download now:: 1.envato.market/vn0dE3