[DO_NOT_MERGE] Find problems with memory management that kills api and web containers #1961
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We see spikes in both containers that kill the containers. We need to find out what's causing these problems.
What are in these changes?
Interesting (maybe)
Warning
Requires migration but provides the best scalability for production systems
with 1000+ concurrent runs.
📊 Performance Comparison
What kind of migration requires using zrevrange?
(and the Sorted Set approach) requires a data structure migration in Redis. Here's what needs to change:
Current Structure (Hash-based)
Single Redis Hash
Key: runs:active:{workspaceId}:{projectId} Type: HASH Data: { "run-uuid-1": '{"uuid":"run-uuid-1","queuedAt":"2024-...","source":"API"}', "run-uuid-2": '{"uuid":"run-uuid-2","queuedAt":"2024-...","source":"Playground"}', ... }Operations:
•
HSETto add a run•
HDELto remove a run•
HGETALLorHSCANto list all runs (then sort in-memory)New Structure (Sorted Set + Hash)
Two Redis data structures:
Sorted Set (Index)
Key: runs:active:{workspaceId}:{projectId}:index Type: SORTED SET Data: { "run-uuid-1": 1732123456789, // timestamp as score "run-uuid-2": 1732123456790, ... }Hash (Full Data)
Key: runs:active:{workspaceId}:{projectId}:data Type: HASH Data: { "run-uuid-1": '{"uuid":"run-uuid-1",...}', "run-uuid-2": '{"uuid":"run-uuid-2",...}', ... }Operations
•
ZADD + HSETto add a run (2 commands)•
ZREM+HDELto remove a run (2 commands)•
ZREVRANGEto get paginated UUIDs (sorted by timestamp)•
HMGETto fetch only the runs for current pageYou need to update
•
create.ts- Where runs are added to cache• FROM: redis.hset(key, runUuid, JSON.stringify(run))
• TO: redis.zadd(indexKey, timestamp, runUuid) + redis.hset(dataKey,
runUuid, JSON.stringify(run))
•
delete.ts- Where runs are removed from cache• FROM: redis.hdel(key, runUuid)
• TO: redis.zrem(indexKey, runUuid) + redis.hdel(dataKey, runUuid)
•
update.ts- Where run data is updated• FROM: redis.hset(key, runUuid, JSON.stringify(run))
• TO: redis.hset(dataKey, runUuid, JSON.stringify(run)) (only update hash,
score stays same)
•
listActive.ts- Where runs are fetched• FROM: Load all with HGETALL/HSCAN, sort in-memory, slice for pagination
• TO: ZREVRANGE for page UUIDs + HMGET for data (no in-memory sorting)
Data Migration (Redis Layer)
You need a migration script to convert existing data:
Deployment Strategy (Zero-Downtime)
Because this changes the data structure, you need a careful rollout:
Option A: Blue-Green Deployment
Option B: Feature Flag