Skip to content

Commit be15b7e

Browse files
authored
fix: lower Redis batch size (#669)
* fix: lower Redis batch size * remove deprecated tag
1 parent 161a75f commit be15b7e

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@
1010
"typescript.enablePromptUseWorkspaceTsdk": true,
1111
"[prisma]": {
1212
"editor.defaultFormatter": "Prisma.prisma"
13+
},
14+
"[typescript]": {
15+
"editor.defaultFormatter": "biomejs.biome"
1316
}
1417
}

src/db/transactions/db.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import superjson from "superjson";
2+
import { env } from "../../utils/env";
23
import { redis } from "../../utils/redis/redis";
34
import { AnyTransaction } from "../../utils/transaction/types";
45

@@ -38,9 +39,6 @@ export class TransactionDB {
3839
private static cancelledTransactionsKey = `transaction:cancelled`;
3940
private static erroredTransactionsKey = `transaction:errored`;
4041

41-
// ioredis has limits over 100k+ (source: https://github.com/redis/ioredis/issues/801).
42-
private static REDIS_BATCH_SIZE = 100_000;
43-
4442
/**
4543
* Inserts or replaces a transaction details.
4644
* Also adds to the appropriate "status" sorted set.
@@ -113,9 +111,13 @@ export class TransactionDB {
113111
}
114112

115113
const result: AnyTransaction[] = [];
116-
for (let i = 0; i < queueIds.length; i += this.REDIS_BATCH_SIZE) {
114+
for (
115+
let i = 0;
116+
i < queueIds.length;
117+
i += env.__EXPERIMENTAL_REDIS_BATCH_SIZE
118+
) {
117119
const keys = queueIds
118-
.slice(i, i + this.REDIS_BATCH_SIZE)
120+
.slice(i, i + env.__EXPERIMENTAL_REDIS_BATCH_SIZE)
119121
.map(this.transactionDetailsKey);
120122
const vals = await redis.mget(...keys);
121123

@@ -139,9 +141,13 @@ export class TransactionDB {
139141
}
140142

141143
let numDeleted = 0;
142-
for (let i = 0; i < queueIds.length; i += this.REDIS_BATCH_SIZE) {
144+
for (
145+
let i = 0;
146+
i < queueIds.length;
147+
i += env.__EXPERIMENTAL_REDIS_BATCH_SIZE
148+
) {
143149
const keys = queueIds
144-
.slice(i, i + this.REDIS_BATCH_SIZE)
150+
.slice(i, i + env.__EXPERIMENTAL_REDIS_BATCH_SIZE)
145151
.map(this.transactionDetailsKey);
146152
numDeleted += await redis.unlink(...keys);
147153
}
@@ -177,10 +183,10 @@ export class TransactionDB {
177183
status === "mined"
178184
? this.minedTransactionsKey
179185
: status === "cancelled"
180-
? this.cancelledTransactionsKey
181-
: status === "errored"
182-
? this.erroredTransactionsKey
183-
: this.queuedTransactionsKey;
186+
? this.cancelledTransactionsKey
187+
: status === "errored"
188+
? this.erroredTransactionsKey
189+
: this.queuedTransactionsKey;
184190

185191
const queueIds = await redis.zrevrange(key, start, end);
186192
const transactions = await this.bulkGet(queueIds);

src/utils/env.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ export const env = createEnv({
9090
// Sets the max amount of memory Redis can use.
9191
// "0" means use all available memory.
9292
REDIS_MAXMEMORY: z.string().default("0"),
93+
// Sets the max batch Redis will handle in batch operations like MGET and UNLINK.
94+
// This will be removed if a consistent batch size works for all use cases.
95+
// ioredis has issues with batches over 100k+ (source: https://github.com/redis/ioredis/issues/801).
96+
__EXPERIMENTAL_REDIS_BATCH_SIZE: z.coerce.number().default(50_000),
9397
// Sets the number of recent transactions to store. Older transactions are pruned periodically.
9498
// In testing, 100k transactions consumes ~300mb memory.
9599
TRANSACTION_HISTORY_COUNT: z.coerce.number().default(100_000),
@@ -130,6 +134,8 @@ export const env = createEnv({
130134
process.env.CONFIRM_TRANSACTION_QUEUE_CONCURRENCY,
131135
ENGINE_MODE: process.env.ENGINE_MODE,
132136
REDIS_MAXMEMORY: process.env.REDIS_MAXMEMORY,
137+
__EXPERIMENTAL_REDIS_BATCH_SIZE:
138+
process.env.__EXPERIMENTAL_REDIS_BATCH_SIZE,
133139
TRANSACTION_HISTORY_COUNT: process.env.TRANSACTION_HISTORY_COUNT,
134140
GLOBAL_RATE_LIMIT_PER_MIN: process.env.GLOBAL_RATE_LIMIT_PER_MIN,
135141
DD_TRACER_ACTIVATED: process.env.DD_TRACER_ACTIVATED,

src/worker/tasks/pruneTransactionsWorker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Job, Processor, Worker } from "bullmq";
1+
import { Worker, type Job, type Processor } from "bullmq";
22
import { TransactionDB } from "../../db/transactions/db";
33
import { pruneNonceMaps } from "../../db/wallets/nonceMap";
44
import { env } from "../../utils/env";

0 commit comments

Comments
 (0)