1 - What is Caching?
At high level, caching is the technique used to store frequently access data in a temporary storage location to reduce latency or request your database.
When a user makes a request, it'll first go straight to the cache. If the data is available in the cache, the cache will simply return that data to the user. If it is not , the cache will forward that request to your database. If the database has that information, it'll return that information to the cache and the cache will then store that information and return the data to the user.
Some of the key benefits of caching are it can greatly reduce the latency of incoming requests, and it can also save a lot of money in database access costs.
But on the other hand, you can have stale data or eventual consistency, and your cache can also end up costing you more money if you have a right heavy pattern.
So you want to use caching when you have heavy workloads or people are making a lot of expensive requests for database where you could easily just store the result in your cache and return it instantly.
Cache, en basit haliyle, sık kullanılan verileri geçici bir yerde tutarak uygulamanın daha hızlı çalışmasını sağlayan bir tekniktir.
Bir kullanıcı uygulamana istek gönderdiğinde, sistem önce veritabanına gitmez, önce cache'e bakar. Eğer aranan veri cache’te varsa, sonuç anında kullanıcıya döner. Eğer yoksa, cache bu isteği veritabanına iletir. Veritabanı veriyi bulduğunda ise hem cache’e kaydeder hem de kullanıcıya gönderir. Böylece aynı veri tekrar istendiğinde veritabanına gitmeye gerek kalmaz.
Cache kullanmanın en büyük avantajlarından biri gecikmeyi (latency) ciddi şekilde azaltmasıdır. Ayrıca veritabanına yapılan istek sayısını düşürdüğü için altyapı maliyetlerini de azaltabilir.
Ama her şeyde olduğu gibi cache’in de bazı dezavantajları var:
- Cache’teki veri bazen güncel olmayabilir (stale data).
- Sistemler genellikle eventual consistency modeliyle çalışır; yani veri her noktada aynı anda güncellenmeyebilir.
- Eğer uygulaman çok fazla yazma işlemi yapıyorsa (write-heavy system), cache yönetimi ek maliyet oluşturabilir.
Bu yüzden cache genellikle şu durumlarda tercih edilir:
- Trafiğin yüksek olduğu sistemlerde
- Veritabanı sorgularının pahalı olduğu durumlarda
- Aynı verinin tekrar tekrar istendiği senaryolarda
Doğru yerde kullanıldığında cache, sonucu adeta anında döndürerek hem kullanıcı deneyimini iyileştirir hem de sistemini ciddi şekilde rahatlatır.
2 - Caching Policies
So, a cache replacement policy is basically a rule that defines how data is stored between the cache and the database. First up, we have Least Recently Used (LRU): This policy is exactly how it sounds. The cache removes the least recently accessed element when the cache becomes full. Next, we have Least Frequently Used (LFU): This policy assumes that data that is not accessed frequently will also not be accessed frequently in the future, so it evicts data that has not been frequently accessed. The next cache replacement policy is First In, First Out (FIFO): This basically says that you evict the data that was added first. This policy is super straightforward, but it does not account for how frequently or recently the data was accessed.
⚠️ Note: LFU sounds great in theory but can be expensive to maintain because frequency counters must be updated.
Cache replacement policy, cache ile veritabanı arasında hangi verinin tutulacağına ve hangisinin silineceğine karar veren kurallar bütünü. İlk olarak Least Recently Used (LRU): Bu politika adından da anlaşılacağı gibi çalışır. Cache dolduğunda, en uzun süredir erişilmeyen veri cache’ten çıkarılır. Least Frequently Used (LFU): Bu yaklaşım şunu varsayar: Eğer bir veri sık erişilmiyorsa, gelecekte de büyük ihtimalle sık erişilmeyecek. Bu yüzden en az kullanılan veriler cache’ten silinir. Bir diğer cache politikası ise First In, First Out (FIFO): Bu yöntemde cache’e ilk giren veri, ilk çıkarılır. FIFO oldukça basit ve anlaşılır bir yaklaşımdır, ancak verinin ne kadar sık ya da ne kadar yakın zamanda kullanıldığını dikkate almaz.