Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Ruby

Theo Dõi Cache Hits Sử Dụng Custom Metrics Cho Performance Tăng Cao

Mỗi server chạy app dùng AppSignal gửi collection của samples và metrics đến Push API every 30 seconds.

Mỗi request có key dùng để determine app nào data came from. Để làm vậy, cần query database để find app cho each incoming request. Với 30 tỷ requests per month, chúng tôi constantly trying find ways reduce queries để làm AppSignal nhanh hơn.

Chúng tôi implemented caching để reduce queries trên database clusters. Mỗi khi fetch app từ database, store nó trong Memcached cho one minute. Sau deploy change này production, chúng tôi thấy đang làm more queries than before. Cache dường như bị invalidated too often. Để find out where happened, chúng tôi added custom metrics để find out where cache improperly invalidated.

Có vài places invalidate cache, như khi push processed time updated hoặc khi detect new namespace.

def find_app(key)
  Rails.cache.fetch("app:#{key}", expires_in: 1.minute) do
    App.find_by!(push_api_key: key)
  end
end

Chúng tôi added multiple counters để determine cache invalidation nào là culprit. Trong ví dụ này increment app.cache.invalidate counter count total validations, và dùng specific keys như app.cache.invalidate_push_time và app.cache.invalidate_namespaces cho specific invalidations.

def invalidate_cache(key)
  AppSignal.increment_counter("app.cache.invalidate")
  
  if push_time_changed?
    AppSignal.increment_counter("app.cache.invalidate_push_time")
  end
  
  if new_namespace_detected?
    AppSignal.increment_counter("app.cache.invalidate_namespaces")
  end
  
  Rails.cache.delete("app:#{key}")
end

Adding custom metrics trên, chúng ta able to graph cache hits over time. Ngay lập tức apparent cache key nào caused rise in queries. Key app.cache.invalidate_namespaces bị invalidated cho each request.

Theo Dõi Cache Hits Sử Dụng Custom Metrics Cho Performance Tăng Cao

Total number of cacheable requests counted as app.cache.maybe.

Sau deploy fix cho issue này, số invalidations dropped to zero miễn là namespaces cho app không updated.

Theo Dõi Cache Hits Sử Dụng Custom Metrics Cho Performance Tăng Cao

Adding custom metrics làm easier understand what happening where, when, và how often. Trong case này, biết số cache invalidations và show readable graph allowed quickly find issue. Chỉ cần vài lines code increment certain value và create dashboard.

Let us know nếu bạn có questions về custom metrics và nếu chúng tôi can help setup trong application. Chúng tôi happy to help!

Theo Dõi Cache Hits Sử Dụng Custom Metrics Cho Performance Tăng Cao

Robert Beekman

Là co-founder, Robert viết very first commit. Anh cũng là support role-model và biết all about tiny details trong code. Travels and photographs (at the same time).

Tất cả bài viết của Robert Beekman