Indexes, limits & performance

Add database indexes, cap the number of documents, cache responses and keep bucket queries fast.

Indexes

Declare indexes in the bucket's indexes array. Spica creates them in the database when you save the bucket and drops the ones you remove.

JSON
{
  "indexes": [
    {"definition": {"slug": 1}, "options": {"unique": true}},
    {"definition": {"status": 1, "published_at": -1}},
    {"definition": {"location": "2dsphere"}}
  ]
}
FieldDescription
definitionProperty paths and their direction: 1 ascending, -1 descending, or "2dsphere" for locations.
optionsIndex options. unique rejects duplicate values.

A duplicate value is rejected with 400:

JSON
{"statusCode": 400, "message": "Value of the property .slug should unique across all documents."}

Engine differences. On MongoDB, options accepts any MongoDB index option, such as sparse, expireAfterSeconds or partialFilterExpression. On PostgreSQL, options accepts only unique, sparse and background, and a 2dsphere index must have a single property and can't be unique.

Which indexes to add

  • Properties you filter on frequently, such as status, owner or a foreign key.
  • Properties you sort on, combined with the filter properties in the same index.
  • Properties that must be unique, such as a slug or an external id.
  • Properties used in rules, because rules run on every user query.

Use Observability in the panel or GET /bucket/:bucketId/data/profile to find slow queries.

Document limits

Cap the number of documents in a bucket with documentSettings:

JSON
{
  "documentSettings": {
    "countLimit": 1000,
    "limitExceedBehaviour": "remove"
  }
}
limitExceedBehaviourWhen the limit is reached
preventNew inserts are rejected.
removeThe insert is accepted and the oldest document is deleted.

This is useful for logs, notifications and caches that should only keep recent entries. On PostgreSQL, remove isn't available when the primary key is a string, because string keys have no insertion order.

To cap the total number of documents across all buckets, start the API with --bucket-data-limit=<count>.

Response cache

The API can cache GET responses of bucket data in memory:

Terminal
--bucket-cache=true --bucket-cache-ttl=60
  • Cached responses are served for --bucket-cache-ttl seconds (default 60).
  • Writes through the API invalidate the cache of the bucket.
  • DELETE /bucket/cache clears all cached responses.

Turn it on for read-heavy public content. Leave it off when documents are changed directly in the database or when every read must reflect the latest write.

Hooks

When --bucket-hooks is on (the default), functions can react to inserts, updates and deletes on buckets with the bucket trigger. Turn it off with --bucket-hooks=false if you don't use bucket triggers.

Performance checklist

  • Always send a limit, and only use paginate=true when you show the total.
  • Resolve only the relations the client renders.
  • Put values that rules check directly on the document instead of behind relations.
  • Index the properties you filter, sort and rule on.
  • Prefer PATCH for small updates.
  • Use realtime only for screens that need live updates.