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.
{
"indexes": [
{"definition": {"slug": 1}, "options": {"unique": true}},
{"definition": {"status": 1, "published_at": -1}},
{"definition": {"location": "2dsphere"}}
]
}| Field | Description |
|---|---|
definition | Property paths and their direction: 1 ascending, -1 descending, or "2dsphere" for locations. |
options | Index options. unique rejects duplicate values. |
A duplicate value is rejected with 400:
{"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,owneror a foreign key. - Properties you sort on, combined with the filter properties in the same index.
- Properties that must be unique, such as a
slugor 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:
{
"documentSettings": {
"countLimit": 1000,
"limitExceedBehaviour": "remove"
}
}limitExceedBehaviour | When the limit is reached |
|---|---|
prevent | New inserts are rejected. |
remove | The 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:
--bucket-cache=true --bucket-cache-ttl=60- Cached responses are served for
--bucket-cache-ttlseconds (default60). - Writes through the API invalidate the cache of the bucket.
DELETE /bucket/cacheclears 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 usepaginate=truewhen 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
PATCHfor small updates. - Use realtime only for screens that need live updates.