Event streaming best practices

This guide covers best practices for event streaming in Fauna.

Follow query best practices

When composing FQL queries for event streams, follow best practices for queries.

Manage the stream’s source and shape

A stream’s cost and performance are closely related to its source and shape.

For more details, see Costs and performance in the event streaming reference docs.

Use projection to limit event fields

Projection lets you select the fields to include in a stream’s events.

For the best performance and costs, use an index and only project fields covered as an index term or value. This lets the stream read data from the index rather than the underlying documents.

See Projecting only the data you need into the stream

Use index terms to filter tracked sets

For the best performance, especially on large datasets, use an index with terms to filter collection documents based on an exact field value.

For example, instead of using collection.where() to perform an equality comparison:

let produce = Category.byName("produce").first()
Product.where(.category == produce && .price < 100_00) {
  category, name, description, price }.toStream()

You can create an index definition with category as an index term and price as an index value:

collection Product {
  ...
  index byCategory {
    terms [.category]
    values [.price, .name, .description]
  }
}

Then update the query to replace the equality comparison with an index call:

let produce = Category.byName("produce").first()
Product.byCategory(produce).where( .price < 100_00) {
  category, name, description, price }.toStream()
See Optimize stream performance with indexes

Use a key with a built-in role

If you don’t need identity-based authentication, use a key with a built-in role to authenticate stream requests.

A stream is only sent events for documents the authentication secret can access.

Streams that use a key with the same built-in role can access the same documents and are always sent the same events. Internally, this lets Fauna unify event processing for these streams.

Streams that use a token or a JWT with user-defined roles may discard different events based on attribute-based access control (ABAC). To account for this, Fauna must process events for these streams individually.

See Costs and performance

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!