Hi team,
# spicedb
g
Hi team, We are running spicedb with cockroachdb. We just had some issues in production. Cockroachdb was reporting that ix_relation_tuple_by_subject_relation had some hot ranges. This index is defined as
INDEX ix_relation_tuple_by_subject_relation (userset_namespace ASC, userset_relation ASC, namespace ASC, relation ASC)
When checking the db we saw that the cardinality of tuples
(userset_namespace, userset_relation, namespace, relation)
was very small. It seems that cockroachdb then assigns the same page to all data with the same tuple and creates a hot tuple. We solved the incident by doing
create INDEX ix_relation_tuple_by_subject_relation_v2 on relation_tuple (userset_namespace ASC, userset_relation ASC, namespace ASC, relation ASC, object_id);
and
alter index ix_relation_tuple_by_subject_relation set not visible;
This way the index has a high cardinality key (object_id) and cockroachdb can split between different nodes. However, even though the old index is not visible, it is still maintained, so we still have a couple of nodes with high cpu. Do you foresee any problem from dropping the index? Also, are you aware in what queries this is used? We couldn't find it in the database console. Finally, would it make sense to open a pr with the change to spicedb? Cheers
y
what APIs does your workload mostly consist of?
g
I would say mostly in volume: check bulk permissions, lookup subjects, lookup resources
y
hmm... that is interesting. re: the cardinality issue: do you have particularly wide relations? i.e. are there small numbers of subjects with a large number of objectIDs attached to them?
the index is for schema delta checking
i don't think we've gotten this report before
(which isn't to say it isn't a problem)
g
in the table subject_id would be userset_object_id, right?
grouping by object_id I don't see much information, but grouping by userset_object_id, I do see
*
having a lot of object ids (26% of the volume), which is expected since it is the any user wildcard next is some metadata userset_object_id that represents 2.5% of relation_tuple
btw, the original index that gave the problems has neither object_id nor userset_object_id
BTW, talking with cockroachdb, they said that indices that are not unique store the pk. This already seems like a problem, there is an index to compute the schema which is very small in size, but in disk, because it is not unique it is storing a copy of the whole db
y
gotcha, so you have a lot of public resources
and yeah that doesn't sound ideal 🤔
i'm also wondering how that index interacts with the watching schema cache
like if you're using the watching schema cache, do you still need the index? i'm not familiar enough with the use of that particular index
i'll ask around
g
thanks!
j
@Gabriel make sure to update to the latest version of SpiceDB
this is fixed with index forcing
> Do you foresee any problem from dropping the index?
that index cannot be dropped
its necessary for schema validation during schema changes
with your "new" index, its probably fine
unforuntately, we cannot easily change any of the existing indexes, as rebuilding indexes on clusters with billions of relationships is a non-trivial exercise
g
Found the offending query:
Copy code
SELECT
  namespace,
  object_id,
  relation,
  userset_namespace,
  userset_object_id,
  userset_relation,
  caveat_name,
  caveat_context
FROM
  relation_tuple AS OF SYSTEM TIME 1753239450000000000
WHERE
  ((userset_namespace = $1 AND userset_object_id IN ($2,) AND userset_relation = $3))
  AND namespace = $4
  AND relation = $5
ORDER BY
  userset_namespace, userset_object_id, userset_relation, namespace, object_id, relation
LIMIT
  9223372036854775807
With our data distribution and some specific parameters it was using `ix_relation_tuple_by_subject_relation`instead of
ix_relation_tuple_by_subject
thanks @Joey we will check out, index forcing should fix the issue
There is another side of this story, is that our server is constantly failing on this index with
"could not find valid split key"
, this is what prompted us to introduce the new index with a high cardinality subfield. @Joey or @yetitwo Are you aware why this split could be failing? It is still happening once we removed the visibility and it is consuming a lot of CPU and network. According to [CRDB](https://cockroachdb.slack.com/archives/CP4D9LD5F/p1753170168422649), when there are a lot of duplicates in a secondary index, the primary key is stored with it and used for splitting. They claim that the reason behind a key not being capable of splitting would be because the PK is constantly updating. Does this make sense to you? Looking at relation_tuple I cannot see what would be updating, except maybe the timestamp field
j
> With our data distribution and some specific parameters it was using ix_relation_tuple_by_subject_relationinstead of ix_relation_tuple_by_subject with everyone's
this is why we added the forcing
PK shouldn't be updating at all
g
cool, thanks
3 Views