Key-value store for ingest JS rules

Ingest JS rules (transform(event)) can keep state across events and batches through a per-project key-value store: drop duplicates, count per user, remember...

Embed
Preview of the card
<a href="https://lucidace.com/help/ingest-rule-kv"><img src="https://lucidace.com/help/_api/public/orgs/af34862a-c837-4478-8335-0505efe40459/spaces/2f4b888b-c4e6-488b-81bd-6e838fb12d2e/pages/51e1b988-9b84-4332-ac4d-ef9bc7dee3b7/social-card.svg?v=3e7ae33eec8b" alt="Key-value store for ingest JS rules" width="600" height="315"></a>

Ingest JS rules (transform(event)) can keep state across events and batches through a per-project key-value store: drop duplicates, count per user, remember the first value seen.

JS rules are their own resource, /v1/projects/{id}/js_rules (test one with POST .../js_rules/test). They are not the code kind of a transformation (POST /v1/projects/{id}/transformations with {"rule":{"kind":"code","source":...}}): that kind takes Lucida's statement language, not JavaScript. Its statements are if / else, set, unset, rename ... to, drop, log(...) and call, for example:

if properties.plan = 'trial' {
  set properties.segment = concat('trial-', lower(properties.country));
} else if properties.internal = true {
  drop;
}
rename properties.old to properties.new;

API in rule code

function transform(event) {
  const seen = "seen:" + event.properties.order_id;
  if (kv.get(seen)) return null;           // a duplicate order: drop it
  kv.set(seen, true, 7 * 24 * 3600);       // remember for 7 days (ttl in seconds)
  event.properties.order_number = kv.incr("orders:" + event.distinct_id);
  return event;
}

Call

Returns

kv.get(key)

the stored JSON value, or null when missing or expired

kv.set(key, value, ttl?)

nothing; ttl in seconds (1 s to 10 years), none = kept until deleted

kv.incr(key, by = 1, ttl?)

the new number (a missing or non-number value counts as 0); without ttl the expiry is kept

kv.del(key)

true when the key existed

kv.ttl(key)

seconds left, -1 when it never expires, -2 when missing

Keys are 1 to 256 bytes, values any JSON up to 64 KiB. A write past the project quota throws (the rule then fails open for that event, as any rule error does). Writes made before a rule throws are kept.

Consistency

  • Every rule and event of one batch sees the same store, in order, starting from the state when the batch began.

  • lc-worker applies the writes to Postgres (rule_kv) after the batch's events are written to ClickHouse, once per batch (marker kv:{token}:{project} in ingest_rule_batches). A redelivered batch neither repeats nor loses them; a dedupe rule then also drops events that were already written, which ClickHouse would dedupe anyway.

  • Two workers on the same project merge counters (incr is applied as an increment, not a copy); set is last write wins. The quota is enforced in the sandbox and re-checked on apply.

Quota and admin

Default 10,000 keys and 4 MiB (key plus JSON value) per project; admins may raise it to 100,000 keys and 64 MiB.

Route

Access

GET /v1/projects/{id}/js_rules/kv?prefix=&limit=&cursor=

read: live keys in key order

GET /v1/projects/{id}/js_rules/kv/usage

read: {keys, bytes, max_keys, max_bytes}

DELETE /v1/projects/{id}/js_rules/kv?key= / ?prefix= / no parameter

admin: delete a key, a prefix or everything (audited)

PUT /v1/projects/{id}/js_rules/kv/quota {max_keys, max_bytes}

admin (audited)

The web app shows the store under Data -> Transformations -> Rule key-value store. POST /v1/projects/{id}/js_rules/test runs against the live store and returns the writes it would make as kv_ops without applying them.

Updated