Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

doc: Redis caching strategy (#2771)

Max Deichmann939b2331202a
diff --git a/web/src/features/README.md b/web/src/features/README.md
new file mode 100644
index 0000000000..9fcd002735
--- /dev/null
+++ b/web/src/features/README.md
@@ -0,0 +1,36 @@
+# Public API
+
+## How to add new api routes
+
+Implementation
+
+- Wrap with `withMiddleware`
+- Type-safe and authed API Route with `createAuthedAPIRoute`
+- Add zod types to `/features/public-api/types` folder.
+
+ - Use [`coerce`](https://zod.dev/?id=coercion-for-primitives) to handle primitives, such as dates, for use in your application and tests.
+ - Use `strict()` on all objects that should not return additional properties. Recommended as default. In these cases, the test utility `makeZodVerifiedAPICall` will throw an error if the response contains additional properties. Also, we will log an error in production if the response contains additional properties.
+
+- Throw errors defined in `shared/src/errors` which translate to HTTP status codes
+
+Testing
+
+- Add tests for all standard cases
+- use `makeZodVerifiedAPICall` to test the API response against the zod response schema
+
+API Reference
+
+- Add to `fern` including `docs` attributes
+- Build with `fern generate --api server` and `fern generate --api client`, then commit the changes to the API reference
+
+SDKs
+
+- Copy/paste fern-generated types or api reference to Python and JS SDKs respectively
+- Implement wrapping functions if needed
+
+How to refactor existing apis to this pattern
+
+1. Move request types to `features/public-api/types`
+2. Create response types in `features/public-api/types`
+3. Validate response type with `validateZodSchema` in the API route to get type warnings in case of mismatch
+4. Refactor api route by passing current api route to LLM together with example of e.g. public/v2/datasets.ts
diff --git a/web/src/features/prompts/readme.md b/web/src/features/prompts/readme.md
new file mode 100644
index 0000000000..2ce8d793f1
--- /dev/null
+++ b/web/src/features/prompts/readme.md
@@ -0,0 +1,17 @@
+## Caching Strategy of Prompts
+
+The caching strategy for prompts is implemented in the `PromptService` class and is utilized in the `createPrompt` function. Here is an overview of how the caching mechanism works:
+
+### Cache Structure
+
+The cache for prompts is managed using Redis. The cache key looks like the following: `prompt:<project-id>:<prompt-name>:<prompt-<version ?? label>` This means that for each prompt name we have multiple keys in Redis. Also, if a prompt has multiple labels, it will appear in the cache multiple times.
+
+### Creation and updates of prompts
+
+We never update prompts in the cache. Instead, we remove all cache entries for a prompt name of a project when a prompt is updated. This ensures that the cache is always up-to-date with the database.
+For this, we first acquire a lock in Redis, invalidate the cache, execute the operation in Postgres, and then release the lock.
+
+### Reading prompts
+
+When reading prompts, we check whether a lock exists. If it does not, we proceed to read the prompt from the cache. Thereby, we reset the ttl of the cache entry to ensure it remains in the cache.
+If the lock exists, or the entry is not in Redis, we read the prompt from Postgres and store it in the cache.
diff --git a/web/src/features/public-api/README.md b/web/src/features/public-api/README.md
index 9fcd002735..a92a3636f5 100644
--- a/web/src/features/public-api/README.md
+++ b/web/src/features/public-api/README.md
@@ -1,36 +1,13 @@
-# Public API
+## Caching Strategy of API Keys
-## How to add new api routes
+### Cache Structure
-Implementation
+The cache for API keys is managed using Redis. The cache key looks like the following: `api-key:<secret-key>:`. The hash is the `fastHashedSecretKey` from postgres. Hence, we can easily find the key in Redis.
-- Wrap with `withMiddleware`
-- Type-safe and authed API Route with `createAuthedAPIRoute`
-- Add zod types to `/features/public-api/types` folder.
+### Creation and updates of API keys
- - Use [`coerce`](https://zod.dev/?id=coercion-for-primitives) to handle primitives, such as dates, for use in your application and tests.
- - Use `strict()` on all objects that should not return additional properties. Recommended as default. In these cases, the test utility `makeZodVerifiedAPICall` will throw an error if the response contains additional properties. Also, we will log an error in production if the response contains additional properties.
+When creating a new API key, nothing happens in the cache. The API key is only created in the database. There are no functionalities in Langfuse to update API keys.
-- Throw errors defined in `shared/src/errors` which translate to HTTP status codes
+### Reading API keys
-Testing
-
-- Add tests for all standard cases
-- use `makeZodVerifiedAPICall` to test the API response against the zod response schema
-
-API Reference
-
-- Add to `fern` including `docs` attributes
-- Build with `fern generate --api server` and `fern generate --api client`, then commit the changes to the API reference
-
-SDKs
-
-- Copy/paste fern-generated types or api reference to Python and JS SDKs respectively
-- Implement wrapping functions if needed
-
-How to refactor existing apis to this pattern
-
-1. Move request types to `features/public-api/types`
-2. Create response types in `features/public-api/types`
-3. Validate response type with `validateZodSchema` in the API route to get type warnings in case of mismatch
-4. Refactor api route by passing current api route to LLM together with example of e.g. public/v2/datasets.ts
+When reading API keys, we prefer to get the key from Redis and reset the TTL (time-to-live) on each read to ensure it remains in the cache. If the key is not found in Redis, we read from Postgres and store it in the cache.