Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: improve docs on caching #886

Merged
merged 9 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/content/concepts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ For example, the following returns all the users of type `user` that have the `v
}}
/>

For more information, see the the [ListUsers API Reference](/api/service#Relationship%20Queries/ListUsers).
For more information, see the [ListUsers API Reference](/api/service#Relationship%20Queries/ListUsers).

</details>
<details>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ The following list outlines best practices for running OpenFGA in a production e

We recommend:

1. Turn on caching via the flag `--check-query-cache-enabled`. This will reduce latency of requests, but it will increase the staleness of OpenFGA's responses. (The TTL is configurable).
1. Turn on in-memory caching in Check API via flags. This will reduce latency of requests, but it will increase the staleness of OpenFGA's responses. Please see [Cache Expiration](../interacting/consistency.mdx#cache-expiration) for details on the flags.
2. Prefer having a small pool of servers with high capacity (memory and CPU cores) instead of a big pool of servers, to increase cache hit ratios and simplify pool management.
3. Turn on metrics collection via the flags `--metrics-enabled` and `--datastore-metrics-enabled`. This will allow you to debug issues.
4. Turn on tracing via the flag `--trace-enabled`, but set sampling ratio to a low value, for example `--trace-sample-ratio=0.3`. This will allow you to debug issues without overwhelming the tracing server. However, keep in mind that enabling tracing comes with a slight performance cost.
Expand Down
21 changes: 12 additions & 9 deletions docs/content/interacting/consistency.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ When querying <ProductName format={ProductNameFormat.ShortForm}/> using Read or
| Name | Description |
|-----------------------------|---------------------------------------------------------------------------------------------------------------|
| MINIMIZE_LATENCY (default) | <ProductName format={ProductNameFormat.ShortForm}/> will serve queries from the cache when possible |
| HIGHER_CONSISTENCY | <ProductName format={ProductNameFormat.ShortForm}/> will skip the cache and query the the database directly |
| HIGHER_CONSISTENCY | <ProductName format={ProductNameFormat.ShortForm}/> will skip the cache and query the database directly |

If you write a tuple and you immediately make a check on a relation affected by that tuple using `MINIMIZE_LATENCY`, the tuple change might not be taken in consideration if <ProductName format={ProductNameFormat.ShortForm}/> serves the result from the cache.
If you write a tuple and you immediately make a Check on a relation affected by that tuple using `MINIMIZE_LATENCY`, the tuple change might not be taken in consideration if <ProductName format={ProductNameFormat.ShortForm}/> serves the result from the cache.

## When to use higher consistency

Expand All @@ -51,19 +51,22 @@ if (date_modified + cache_time_to_live_period > Date.now()) {

## Cache expiration

<ProductName format={ProductNameFormat.ShortForm}/> cache is disabled by default. When not enabled, all queries will have strong consistency regardless of the consistency mode specified.
<ProductName format={ProductNameFormat.ShortForm}/> caching is disabled by default. When caching is disabled, all queries will have strong consistency regardless of the consistency mode specified. When caching is enabled, the cache will be used for queries with `MINIMIZE_LATENCY` consistency mode.

You can use the following parameters to configure <ProductName format={ProductNameFormat.ShortForm}/>'s cache:

| Name | Description |
|-------------------------|---------------------------------------------------------------------------------|
| check-query-cache | Enables caching (default = false) |
| check-query-cache-limit | Configures the number of items that will be kept in the cache (default = 10000) |
| check-query-cache-ttl | Specifies the time that items will be kept in the cache (default = 10s) |
| Name | Description |
|----------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
aaguiarz marked this conversation as resolved.
Show resolved Hide resolved
| check-cache-limit | Configures the number of items that will be kept in the in-memory cache used to resolve Check queries (default = 1000) |
| check-query-cache-enabled | Enables in-memory caching of Check subproblems (default = false). For example, if you have a relation `define viewer: owner or editor`, and the query is `Check(user:anne, viewer, doc:1)`, we'll evaluate the `owner` relation and the `editor` relation and cache both results: `(user:anne, viewer, doc:1) -> allowed=true` and `(user:anne, owner, doc:1) -> allowed=true`. |
| check-query-cache-ttl | Specifies the time that items will be kept in the cache of Check subproblems (default = 10s) |
| check-iterator-cache-enabled | Enables in-memory caching of database iterators. Each iterator is the result of a database query, for example, usersets related to a specific object, or objects related to a specific user, up to a certain number of tuples per iterator (default = false) |
| check-iterator-cache-max-results | Configures the number of tuples that will be stored for each database iterator (default = 10000) |
| check-iterator-cache-ttl | Specifies the time that items will be kept in the cache of database iterators (default = 10s) |

Learn how to [configure <ProductName format={ProductNameFormat.ShortForm}/>](../getting-started/setup-openfga/configure-openfga.mdx).

Currently, the cache is used by Check and partially in ListObjects. It will be implemented it for other query endpoints in the future.
Currently, the cache is used by Check and partially in ListObjects. It will be implemented for other query endpoints in the future.

## Future work

Expand Down
Loading