Reduce webhook delivery cleanup impact (#2550)

* Add index on webhook_deliveries.created_at

The stale scope (WHERE created_at < ?) and ordered scope
(ORDER BY created_at DESC) both benefit from this index.
Without it, Webhook::Delivery.cleanup does a full table scan.

* Run webhook delivery cleanup every 15 minutes

Each run now deletes ~15 minutes of newly-stale records instead
of ~4 hours worth, a 16x reduction in per-run write volume.
This commit is contained in:
Jeremy Daer
2026-02-16 14:28:05 -08:00
committed by GitHub
parent a1e96670ce
commit bc76633f77
4 changed files with 10 additions and 3 deletions
+1 -1
View File
@@ -20,7 +20,7 @@ production: &production
schedule: every hour at minute 12 schedule: every hour at minute 12
cleanup_webhook_deliveries: cleanup_webhook_deliveries:
command: "Webhook::Delivery.cleanup" command: "Webhook::Delivery.cleanup"
schedule: every 4 hours at minute 51 schedule: every 15 minutes
cleanup_magic_links: cleanup_magic_links:
command: "MagicLink.cleanup" command: "MagicLink.cleanup"
schedule: every 4 hours schedule: every 4 hours
@@ -0,0 +1,5 @@
class AddCreatedAtIndexToWebhookDeliveries < ActiveRecord::Migration[8.2]
def change
add_index :webhook_deliveries, :created_at
end
end
Generated
+2 -1
View File
@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.2].define(version: 2026_02_12_102026) do ActiveRecord::Schema[8.2].define(version: 2026_02_13_170100) do
create_table "accesses", id: :uuid, charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| create_table "accesses", id: :uuid, charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.datetime "accessed_at" t.datetime "accessed_at"
t.uuid "account_id", null: false t.uuid "account_id", null: false
@@ -821,6 +821,7 @@ ActiveRecord::Schema[8.2].define(version: 2026_02_12_102026) do
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.uuid "webhook_id", null: false t.uuid "webhook_id", null: false
t.index ["account_id"], name: "index_webhook_deliveries_on_account_id" t.index ["account_id"], name: "index_webhook_deliveries_on_account_id"
t.index ["created_at"], name: "index_webhook_deliveries_on_created_at"
t.index ["event_id"], name: "index_webhook_deliveries_on_event_id" t.index ["event_id"], name: "index_webhook_deliveries_on_event_id"
t.index ["webhook_id"], name: "index_webhook_deliveries_on_webhook_id" t.index ["webhook_id"], name: "index_webhook_deliveries_on_webhook_id"
end end
+2 -1
View File
@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.2].define(version: 2026_02_12_102026) do ActiveRecord::Schema[8.2].define(version: 2026_02_13_170100) do
create_table "accesses", id: :uuid, force: :cascade do |t| create_table "accesses", id: :uuid, force: :cascade do |t|
t.datetime "accessed_at" t.datetime "accessed_at"
t.uuid "account_id", null: false t.uuid "account_id", null: false
@@ -594,6 +594,7 @@ ActiveRecord::Schema[8.2].define(version: 2026_02_12_102026) do
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.uuid "webhook_id", null: false t.uuid "webhook_id", null: false
t.index ["account_id"], name: "index_webhook_deliveries_on_account_id" t.index ["account_id"], name: "index_webhook_deliveries_on_account_id"
t.index ["created_at"], name: "index_webhook_deliveries_on_created_at"
t.index ["event_id"], name: "index_webhook_deliveries_on_event_id" t.index ["event_id"], name: "index_webhook_deliveries_on_event_id"
t.index ["webhook_id"], name: "index_webhook_deliveries_on_webhook_id" t.index ["webhook_id"], name: "index_webhook_deliveries_on_webhook_id"
end end