我有一個Ruby應用程序,每分鐘需要大約10000個請求。 其中一些請求執行寫入數據庫表。到數據庫的最大連接數是200.寫入緩存vs寫入數據庫 - 性能基準?
我想知道什麼是更高效。在緩存中寫入數組並將數據保存在後臺中,或將每個請求直接保存到數據庫中? 如果我將數據寫入緩存中的數組,是否有任何競爭條件或性能問題? 有沒有更好的方法來優化性能並避免數據庫瓶頸?
示例代碼
#...
def self.add_data_message_to_queue(event_id, chat_item)
bucket_name = 'BUCKET_GET_CHAT_' + event_id.to_s
chat_queue = Rails.cache.fetch(bucket_name)
if chat_queue.blank?
chat_queue = []
end
chat_queue.push(chat_item)
Rails.cache.write(bucket_name, chat_queue, expires_in:Integer(30).days)
end
- 服務器:獨角獸(高併發)
在此先感謝