Skip to content

refactor(rust): align the cache crates with Python and wire every native backend - #42530

Merged
yujonglee-berri merged 10 commits into
mainfrom
rust-cache-alignment
Sep 22, 2026
Merged

yujonglee-berri merged 10 commits into
mainfrom
rust-cache-alignment

Conversation

@devin-ai-integration

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • Rust cache crates drifted from the Python cache classes
  • Redis-only and lifecycle methods sat on the shared BaseCache trait
  • Counters were pinned to f64, so one backend could not serve both roles
  • Each semantic backend carried its own embedder and prompt handling
  • Only the in-memory backend could be selected natively

How it solves it:

  • Splits disconnect, test_connection and every Redis-only operation into capability traits
  • Decouples counter values from the stored response type
  • Shares one Embedder and prompt contract in litellm_cache::semantic
  • Ports the missing Python operations (refresh TTL, rpush and trim, pipelines, sadd, bulk delete, semantic write-back)
  • Takes the HTTP client from the host pool in the GCS, S3 and Azure backends
  • Registers all nine backends in the Rust catalog, every rule still PYTHON_ONLY
  • Routes the Cache facade's storage calls to the native runtime when the catalog selects it
  • Gives every crate the same layout, moves tests to tests/ on rstest, adds a shared contract suite

User Flow

Before: a proxy admin who enables the Rust cache path gets native storage only for the in-memory backend

  1. The admin sets cache: true with type: redis (or any backend other than local) in the proxy config and starts the proxy
  2. They send POST https://litellm-domain/v1/chat/completions twice with the same body
  3. The second call is served from cache, but by the Python backend, since no native store exists for that type

After: the same admin gets the same behavior, and every backend can be switched to native storage through the catalog

  1. The admin sets cache: true with type: redis (or any of the nine supported types) and starts the proxy
  2. They send POST https://litellm-domain/v1/chat/completions twice with the same body
  3. The second call is served from cache by the Python backend, exactly as before, because every catalog rule stays PYTHON_ONLY
  4. When a rule is flipped to native, the same request round-trips through the Rust store and semantic hits still carry semantic-similarity in the response metadata

Relevant issues

Affected release

Linear ticket

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/test_litellm/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

Pending. The Python-side tests (tests/test_litellm_rust/test_cache.py, tests/test_litellm/caching/test_valkey_semantic_cache_native.py) skip without a built native extension on the box used to open this PR, so the Rust crate tests and the contract suite are left to CI. A live before and after run will be added once CI reports on the tip

Type

🧹 Refactoring

Caveats (if any)

Medium

  • Behavior is unchanged in production only because every catalog rule stays PYTHON_ONLY; flipping a rule activates the native path for that backend

Low

  • Large diff (119 files) since every cache crate was relaid out and tests moved to tests/
  • Proof of fix is not yet captured on this tip

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

Link to Devin session: https://app.devin.ai/sessions/a1813e2dcd104de69956c5d29c584c18
Open in Devin Desktop: https://app.devin.ai/desktop/session/a1813e2dcd104de69956c5d29c584c18?variant=devin
Requested by: @yujonglee-berri

@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@greptile-apps

greptile-apps Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

The PR is not ready to merge because Redis 6 pipeline pops fail, and the new HTTP adapters and comments violate repository requirements.

Findings

  1. P1 Redis 6 pipeline failure ▶
  2. P2 Custom HTTP handlers added ▶
  3. P2 Disallowed source comments ▶

Summary

This PR restructures the Rust cache traits and backends, adds shared semantic and contract-test infrastructure, registers all nine native backends, and routes facade operations through native storage when selected. Shipped catalog rules remain Python-only.

  • Adds backend-specific lifecycle, counter, queue, batch, and semantic capabilities
  • Adds native activation and configuration projection for all supported cache types
  • Reuses host HTTP clients for cloud cache backends
  • Moves and expands backend tests around shared cache contracts
  • Redis pipeline compatibility below version 7 and two repository-rule violations still need correction

Reviews (1) · Last reviewed commit: "refactor(rust): align the cache crates w..."

@codspeed

codspeed Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing rust-cache-alignment (a45f3dc) with main (1373322)

Open in CodSpeed

yujonglee-berri and others added 2 commits September 22, 2026 19:17
… backend

The cache port had drifted: lifecycle and Redis-only operations sat on
`BaseCache`, counters were pinned to `f64`, each semantic backend defined its
own embedder and prompt handling, and only the in-memory backend could be
selected natively.

- Split `disconnect` and `test_connection` out of `BaseCache` into optional
  capabilities, implemented only where the Python class defines them, and give
  every Redis-only operation its own capability trait.
- Decouple counters from the stored value type, so one backend can serve both
  responses and counters as Python's `RedisCache` does.
- Share one `Embedder` and prompt contract in `litellm_cache::semantic`, and
  make the Redis and Valkey semantic backends generic over their codec.
- Port the Python operations that were missing: `async_refresh_ttl`,
  `async_rpush_and_trim`, `async_set_cache_pipeline_with_ttls`, the DualCache
  pipeline, sadd, bulk delete and TTL reads, and the semantic-similarity
  write-back.
- Take the HTTP client from the host pool in the GCS, S3 and Azure backends.
- Activate all nine backends through the Rust catalog, whose rules all stay
  `PYTHON_ONLY`, and route the `Cache` facade's storage calls to the native
  runtime when one is selected.
- Give every crate the same layout, move all tests to `tests/` on rstest, and
  add the shared `litellm-cache-testing` contract suite.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…scipline

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@codecov

codecov Bot commented Sep 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

yujonglee-berri and others added 5 commits September 22, 2026 19:30
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
… backend

The cache port had drifted: lifecycle and Redis-only operations sat on
`BaseCache`, counters were pinned to `f64`, each semantic backend defined its
own embedder and prompt handling, and only the in-memory backend could be
selected natively.

- Split `disconnect` and `test_connection` out of `BaseCache` into optional
  capabilities, implemented only where the Python class defines them, and give
  every Redis-only operation its own capability trait.
- Decouple counters from the stored value type, so one backend can serve both
  responses and counters as Python's `RedisCache` does.
- Share one `Embedder` and prompt contract in `litellm_cache::semantic`, and
  make the Redis and Valkey semantic backends generic over their codec.
- Port the Python operations that were missing: `async_refresh_ttl`,
  `async_rpush_and_trim`, `async_set_cache_pipeline_with_ttls`, the DualCache
  pipeline, sadd, bulk delete and TTL reads, and the semantic-similarity
  write-back.
- Take the HTTP client from the host pool in the GCS, S3 and Azure backends.
- Activate all nine backends through the Rust catalog, whose rules all stay
  `PYTHON_ONLY`, and route the `Cache` facade's storage calls to the native
  runtime when one is selected.
- Give every crate the same layout, move all tests to `tests/` on rstest, and
  add the shared `litellm-cache-testing` contract suite.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…scipline

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
yujonglee-berri and others added 2 commits September 22, 2026 12:46
…plicitly

#42517 made the Messages, token counter and tokenizer routes Python-only, so
tests/test_litellm_rust silently exercised the Python path or failed outright.
Each suite now prepends a RUST_OPT_IN rule for its route, keeping native
coverage without changing the shipped default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@yujonglee-berri yujonglee-berri changed the title refactor(rust): align the cache crates with Python and activate every backend refactor(rust): align the cache crates with Python and wire every native backend Sep 22, 2026
@yujonglee-berri
yujonglee-berri enabled auto-merge (squash) September 22, 2026 19:48
…planatory comments

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@yujonglee-berri
yujonglee-berri merged commit 4677f10 into main Sep 22, 2026
97 checks passed
@yujonglee-berri
yujonglee-berri deleted the rust-cache-alignment branch September 22, 2026 20:13
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.

1 participant