Cache backends
The gateway stores cached responses in a backend you choose. Pick one with type in cache_params, or with the Redis Type selector on the Response Cache page. Exact match backends return a cached response only for identical requests. Semantic backends embed the prompt and return a cached response for prompts that are similar enough, which raises the hit rate for natural language traffic at the cost of an embedding call per request. Every backend is part of the free gateway.
A real-world scenario
A help centre bot gets the same questions phrased a hundred ways, "how do I reset my password", "password reset?", "forgot my password". An exact match cache almost never hits. The admin switches the backend to redis-semantic with a similarity threshold of 0.9 and an embedding model from the gateway's own model list. The three phrasings now share one cached answer, the embedding calls are themselves routed, logged and costed through the gateway, and the cache is scoped per key so one customer never receives another customer's cached answer.
Step by step
In the Admin UI
- Open
/ui/cachingas a proxy admin and pick the Cache Settings tab. - Choose the Redis Type. Node (Single Instance), Cluster and Sentinel are exact match Redis layouts, and Semantic turns on similarity matching. Each type shows its own section, Connection Settings, Cluster Configuration, Sentinel Configuration or Semantic Configuration.
- For Semantic, set Similarity Threshold, pick an Embedding Model from your model list and choose the Semantic Cache Scope, Key or End user.
- Click Test Connection, then Save Changes. Object storage, disk and local backends have no form and are set in the config file.
With curl
An object storage cache for expensive calls that should live for a day.
spanway_settings:
cache: true
cache_params:
type: s3
s3_bucket_name: my-spanway-cache
s3_region_name: us-west-2
ttl: 86400
A semantic Redis cache.
spanway_settings:
cache: true
cache_params:
type: redis-semantic
host: os.environ/REDIS_HOST
port: os.environ/REDIS_PORT
password: os.environ/REDIS_PASSWORD
similarity_threshold: 0.9
redis_semantic_cache_embedding_model: text-embedding-3-large
Save a semantic cache without a restart, then ask two similar questions.
curl -X POST http://localhost:4000/cache/settings \
-H "Authorization: Bearer $SPANWAY_MASTER_KEY" -H "Content-Type: application/json" \
-d '{"cache_settings": {"type": "redis-semantic", "host": "redis.internal", "port": "6379", "password": "s3cret", "similarity_threshold": 0.9, "redis_semantic_cache_embedding_model": "text-embedding-3-large"}}'
curl -i -X POST http://localhost:4000/v1/chat/completions \
-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
-d '{"model": "claude-haiku-5-5", "messages": [{"role": "user", "content": "How do I reset my password?"}]}'
curl -i -X POST http://localhost:4000/v1/chat/completions \
-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
-d '{"model": "claude-haiku-5-5", "messages": [{"role": "user", "content": "I forgot my password, how can I reset it?"}]}'
Expected result
The save answers {"message": "Cache settings updated successfully", "status": "success", ...} and the gateway caches on the new backend from the next request. The first question goes to the provider. The second, differently worded question is answered from the cache with the first answer, carries x-spanway-cache-key, and its spend log row is a cache hit at zero cost. The embedding call that compared the two prompts appears in the logs under the embedding model, costed like any other request.
Exact match backends
type | Storage | Main settings |
|---|---|---|
local | In-process memory | ttl, default_in_memory_ttl |
redis | Redis standalone, cluster or sentinel | host, port, password, namespace, redis_startup_nodes, redis_flush_size |
disk | Local disk | disk_cache_dir |
s3 | Amazon S3 or a compatible store | s3_bucket_name, s3_region_name, s3_endpoint_url, s3_path and AWS credentials |
gcs | Google Cloud Storage | gcs_bucket_name, gcs_path_service_account, gcs_path |
azure-blob | Azure Blob Storage | azure_account_url, azure_blob_container |
Redis is the usual choice for a gateway with several instances, because all instances share entries and Redis expires them automatically. Object storage backends suit large responses that should live for days. local is per process, so each worker has its own cache.
Semantic backends
type | Storage | Main settings |
|---|---|---|
redis-semantic | Redis with vector search | similarity_threshold, redis_semantic_cache_embedding_model, redis_semantic_cache_index_name |
valkey-semantic | Valkey with vector search | similarity_threshold, valkey_semantic_cache_embedding_model, valkey_semantic_cache_index_name |
qdrant-semantic | Qdrant | qdrant_api_base, qdrant_api_key, qdrant_collection_name, similarity_threshold |
similarity_threshold is between 0 and 1, where higher values require closer matches. The embedding model should be a model_name from your model_list, so the embedding calls are themselves routed, logged and costed by the gateway. Semantic caches can be scoped per key or per end user so that one caller never receives another caller's cached answer.
| Setting | Default | What it does |
|---|---|---|
semantic_cache_scope | key | key keeps hits apart per key, team and organization, so all end users of one key share them. end_user also keeps every end user apart, and a request without an end user id falls back to the key scope. |
redis_semantic_cache_embedding_model, valkey_semantic_cache_embedding_model, qdrant_semantic_cache_embedding_model | text-embedding-ada-002 | The model that turns each prompt into a vector. Every lookup calls it once, so pick a cheap, fast one that your model_list serves. |
semantic_cache_embedding_timeout | 5.0 seconds (SEMANTIC_CACHE_EMBEDDING_TIMEOUT_SECONDS) | How long a lookup may spend embedding the prompt before it gives up and lets the request go to the model. |
semantic_cache_embedding_max_input_tokens | the embedding deployment's max_input_tokens | Prompts longer than this are cut before they are embedded, so a long prompt does not fail the embedding call. |
qdrant_semantic_cache_vector_size | 1536 (QDRANT_VECTOR_SIZE) | Vector size of a new Qdrant collection. It must match the embedding model's output size. |
qdrant_quantization_config | binary | How Qdrant compresses stored vectors: binary, scalar or product. Stronger compression saves memory at some loss of match quality. |
Choosing a backend
Use redis for shared exact caching on a multi-instance gateway, local for a single process, object storage for long lived caches of expensive calls, and a semantic backend when users ask the same question in different words. Whatever you choose, the per-request controls described in caching on the gateway work the same way.
Errors
| What you see | Why | Fix |
|---|---|---|
| Only Redis cache type is currently supported for testing | Test Connection and /cache/settings/test only test Redis backends. | Save the settings and run the health check on the Cache Health tab instead. |
| The semantic cache never hits | similarity_threshold is too high for your traffic, or the embedding model is not on the gateway. | Lower the threshold a little at a time, and check the model name on Models + Endpoints. |
| The semantic cache returns a wrong answer | The threshold is too low, so unrelated prompts match. | Raise similarity_threshold. |
AccessDenied or NoSuchBucket in the gateway log | The object storage credentials or bucket name are wrong. | Check the bucket, region and the credentials the gateway runs with. |
| Hit rates differ per replica | The backend is local or disk, which are per process. | Use redis or an object store. |
| Vector search command unknown | The Redis or Valkey server lacks the search module semantic caching needs. | Use a server build with vector search, or qdrant-semantic. |
Related
- Caching on the gateway turns caching on and lists the per request controls.
- Testing a Redis connection walks through the test and save routes.
- Response Cache page documents the Redis Type selector and every field.