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

Giám Sát Cache Hits Với Custom Metrics Cho Hiệu Suất Tối Ưu

Mỗi server chạy app dùng AppSignal gửi collection samples và metrics đến Push API mỗi 30 giây.

Mỗi request có key dùng xác định app data đến từ đâu. Để làm đó, ta query database tìm app cho mỗi incoming request. Với 30 tỷ request/tháng, ta luôn tìm cách giảm queries làm AppSignal nhanh hơn.

Ta implement caching giảm queries trên database clusters. Khi fetch app từ database, ta store trong Memcached 1 phút. Sau deploy production, ta thấy làm nhiều queries hơn trước. Cache có vẻ invalidated quá thường. Để tìm invalidated ở đâu, ta thêm custom metrics tìm cache invalidated không đúng chỗ.

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

# Update last push processed at time if app.last_push_processed_at < 5.minutes.ago app.set(:last_push_processed_at => Time.now) Rails.cache.delete(cache_key) end if namespaces_diff.any? app.add_to_set(:namespaces => namespaces_diff) Rails.cache.delete(cache_key) end

Ta thêm multiple counters xác định cache invalidation nào là thủ phạm. Ví dụ increment counter app.cache.invalidate đếm total invalidations, và specific keys như app.cache.invalidate_push_time và app.cache.invalidate_namespaces cho specific invalidations.

# Update last push processed at at time if app.last_push_processed_at < 5.minutes.ago app.set(:last_push_processed_at => Time.now) Rails.cache.delete(cache_key) Appsignal.increment_counter('app.cache.invalidate', 1) Appsignal.increment_counter('app.cache.invalidate_push_time', 1) end if namespaces_diff.any? app.add_to_set(:namespaces => namespaces_diff) Rails.cache.delete(cache_key) Appsignal.increment_counter('app.cache.invalidate', 1) Appsignal.increment_counter('app.cache.invalidate_namespaces', 1) end

Thêm custom metrics trên, ta graph cache hits theo thời gian. Ngay lập tức rõ ràng cache key nào gây tăng queries. Key app.cache.invalidate_namespaces bị invalidate cho mỗi request.

Tổng số cacheable requests đếm là app.cache.maybe.

Sau deploy fix issue, số invalidations giảm về 0 miễn là namespaces cho app không update.

Thêm custom metrics dễ hiểu gì xảy ra ở đâu, khi nào, và bao nhiêu lần. Trường hợp này, biết số cache invalidations và show readable graph cho phép nhanh tìm issue. Chỉ cần vài dòng code increment value và tạo dashboard.

Cho biết nếu có câu hỏi về custom metrics và chúng tôi có thể giúp setup trong app. Chúng tôi sẵn sàng hỗ trợ!