Skip to content

Conversation

@aditya1702
Copy link
Contributor

@aditya1702 aditya1702 commented Dec 15, 2025

What

Optimizes Redis storage for the Account Token Cache to significantly reduce memory usage through multiple compression strategies.

  1. PostgreSQL Asset ID Mapping - Store compact integer IDs in Redis instead of full CODE:ISSUER strings. Asset definitions are stored once in a new trustline_assets PostgreSQL table.
  2. Bucket Sharding - Distribute accounts across 12,000 Redis hash buckets using FNV-1a hashing instead of one key per account. This reduces key overhead and improves Redis memory efficiency.
  3. VARINT Binary Encoding - Encode asset IDs using variable-length integer format instead of ASCII
  4. Frequency-Based ID Assignment - Sort assets by frequency during initial population so common assets (USDC, EURC) get lowest IDs (1, 2, 3...), which encode to fewer bytes in VARINT format.

With all these optimizations the memory usage went from 3 GB -> 387 MB.

Why

Optimizing memory usage for our token cache is important since the account keys will have an unbounded growth and this could lead to increased storage.

Known limitations

N/A

Issue that this PR addresses

Closes #422

@aditya1702 aditya1702 requested a review from a team December 16, 2025 19:04
README.md Outdated
**2. Bucket Sharding**
Instead of creating one Redis key per account (millions of keys), accounts are distributed across 12,000 buckets using FNV-1a hashing:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this should say a configurable size for Redis hash buckets or something to that effect?

- redis-data:/data
command: >
redis-server
--hash-max-listpack-entries 1000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would operators want to tune these as well, since they can tune the bucket size?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aristidesstaffieri Yes I have added a section in README explaining how these can be tuned and affect the token cache

@socket-security
Copy link

socket-security bot commented Jan 2, 2026

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedgithub.com/​dgraph-io/​ristretto/​v2@​v2.3.099100100100100

View full report

@aditya1702 aditya1702 merged commit 30ee0b2 into main Jan 5, 2026
9 checks passed
@aditya1702 aditya1702 deleted the redis-optimize-storage branch January 5, 2026 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize account token cache structure design to cut down storage usage

3 participants