{
  "id": "nodejs",
  "title": "Redis feature store with node-redis",
  "url": "https://redis.io/docs/latest/develop/use-cases/feature-store/nodejs/",
  "summary": "Build a Redis-backed online feature store in Node.js with node-redis",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc"
  ],
  "last_updated": "2026-06-04T14:49:57+01:00",
  "children": [],
  "page_type": "content",
  "content_hash": "d85c037e088bb6bb7d78679b378558c625a6bfbaaa4bfe0c787afa996b0b4e7f",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "This guide shows you how to build a small Redis-backed online feature store in\nNode.js with [`node-redis`](https://redis.io/docs/latest/develop/clients/nodejs). It\nincludes a local web server built with the Node.js standard `http` module so\nyou can bulk-load a batch of users with a key-level TTL, run a streaming\nworker that overwrites real-time features with per-field TTL, retrieve any\nsubset of features for one user under 1 ms, and pipeline `HMGET` across a\nhundred users for batch scoring."
    },
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "Each entity (here, a user) is one Redis [Hash](https://redis.io/docs/latest/develop/data-types/hashes)\nat a deterministic key — `fs:user:{id}`. The hash holds every feature for that\nentity as one field per feature: batch-materialized aggregates (refreshed once\na day) alongside streaming-updated signals (refreshed every few seconds). One\n[`HMGET`](https://redis.io/docs/latest/commands/hmget) returns whichever subset the model\nneeds in one network round trip.\n\nTwo TTL layers solve the *mixed staleness* problem without an application-side\ncleaner:\n\n* A **key-level** [`EXPIRE`](https://redis.io/docs/latest/commands/expire) aligned with the\n  batch materialization cycle (24 hours in the demo). If the batch refresher\n  fails, the whole entity disappears at the next cycle and inference sees a\n  missing entity — which the model handler can detect and fall back on —\n  rather than silently outdated values.\n* A **per-field** [`HEXPIRE`](https://redis.io/docs/latest/commands/hexpire) (Redis 7.4+) on\n  each streaming feature gives that field its own shorter expiry, independent\n  of the rest of the hash. If the streaming pipeline stops updating a feature,\n  the field self-cleans while the batch fields stay populated.\n\nIn this example, the batch features describe a user's longer-term shape\n(`country_iso`, `risk_segment`, `account_age_days`, `tx_count_7d`,\n`avg_amount_30d`, `chargeback_count_180d`) and are bulk-loaded by\n`buildFeatures.js` — the demo's stand-in for a nightly Spark / Feast\nmaterialization job. The streaming features describe what the user is doing\nright now (`last_login_ts`, `last_device_id`, `tx_count_5m`,\n`failed_logins_15m`, `session_country`) and are written by\n`streamingWorker.js` — the demo's stand-in for a Flink / Kafka Streams job.\nThe inference panel of the demo server reads any subset of those features\nthrough `featureStore.js`'s helper class.\n\nThat gives you:\n\n* A single round trip for retrieval — any subset of features for one entity\n  in one [`HMGET`](https://redis.io/docs/latest/commands/hmget).\n* Sub-millisecond hot path. The Redis-side work is microseconds; in practice\n  the bottleneck is the network round trip plus the model's own feature-prep.\n* Pipelined batch scoring — one round trip for `N` users at once.\n* Independent freshness per feature, expressed as a server-side TTL rather\n  than as application logic.\n* Self-cleanup on pipeline failure: a stalled batch refresher lets entities\n  expire on schedule, and a stalled streaming worker lets each affected field\n  expire on its own timer."
    },
    {
      "id": "how-it-works",
      "title": "How it works",
      "role": "content",
      "text": "There are three paths: a **batch path** that bulk-loads features once per\nmaterialization cycle, a **streaming path** that updates real-time features as\nevents arrive, and an **inference path** that reads features on the request\nside."
    },
    {
      "id": "batch-path-per-materialization-cycle",
      "title": "Batch path (per materialization cycle)",
      "role": "content",
      "text": "1. The batch job calls `synthesizeUsers(N)` (in production, the equivalent\n   computation lives in an offline pipeline against the warehouse). The result\n   is `{userId: {field: value, ...}}` for every user in this cycle.\n2. `store.bulkLoad(rows, ttlSeconds)` batches one\n   [`HSET`](https://redis.io/docs/latest/commands/hset) plus one\n   [`EXPIRE`](https://redis.io/docs/latest/commands/expire) per user through\n   [`multi().exec()`](https://redis.io/docs/latest/develop/clients/nodejs/transpipe), so\n   the whole batch ships in a single round trip. The `HSET` writes every batch\n   field; the `EXPIRE` is what makes the entity disappear if the next batch\n   run fails, so inference reads a missing entity rather than silently\n   outdated values."
    },
    {
      "id": "streaming-path-per-event",
      "title": "Streaming path (per event)",
      "role": "content",
      "text": "When a user does something (login, transaction, page view) the streaming layer\ncomputes whatever real-time signals fall out of that event and calls\n`store.updateStreaming(userId, fields, ttlSeconds)`. That batches:\n\n1. An [`HSET`](https://redis.io/docs/latest/commands/hset) writing the new field values.\n   Redis is single-threaded per shard, so this is atomic against any\n   concurrent batch write on the same hash — no version columns, no locks.\n2. An [`HEXPIRE`](https://redis.io/docs/latest/commands/hexpire) over exactly the fields\n   that were written, with the streaming TTL. Each streaming field carries\n   its own per-field expiry independent of the rest of the hash. Stop the\n   worker and these fields drop out one by one as their TTLs elapse, while\n   the batch fields remain populated under the longer key-level TTL."
    },
    {
      "id": "inference-path-per-request",
      "title": "Inference path (per request)",
      "role": "content",
      "text": "1. The model server picks the feature subset it needs (the schema is owned by\n   the model, not the store).\n2. It calls `store.getFeatures(userId, names)`, which is one\n   [`HMGET`](https://redis.io/docs/latest/commands/hmget). Redis returns the values in\n   the same order as the requested fields, with `null` for any field that\n   doesn't exist (or has expired).\n3. For batch inference, the model server calls\n   `store.batchGetFeatures(userIds, names)`, which pipelines one\n   [`HMGET`](https://redis.io/docs/latest/commands/hmget) per user across all `N` users\n   in a single network round trip via\n   [`multi().exec()`](https://redis.io/docs/latest/develop/clients/nodejs/transpipe)."
    },
    {
      "id": "the-feature-store-helper",
      "title": "The feature-store helper",
      "role": "content",
      "text": "The `FeatureStore` class wraps the read/write paths\n([source](https://github.com/redis/docs/blob/main/content/develop/use-cases/feature-store/nodejs/featureStore.js)):\n\n[code example]"
    },
    {
      "id": "data-model",
      "title": "Data model",
      "role": "content",
      "text": "Each user is one Redis Hash. Every value is stored as a string — Redis hash\nfields are bytes-on-the-wire, so the helper encodes booleans as `\"true\"` /\n`\"false\"` and everything else with `String(value)`. The model server is\nresponsible for parsing back to the right type, the same way it would when\nreading any serialized feature store.\n\n[code example]\n\nThe batch fields sit under the key-level `EXPIRE`. The streaming fields each\ncarry their own [`HEXPIRE`](https://redis.io/docs/latest/commands/hexpire). If the\nstreaming pipeline stops, the streaming fields drop one by one as their\nper-field TTLs elapse; the batch fields stay until the daily key-level\n`EXPIRE` fires (or the next batch cycle re-pins them)."
    },
    {
      "id": "bulk-loading-batch-features",
      "title": "Bulk-loading batch features",
      "role": "content",
      "text": "`bulkLoad` batches one `HSET` and one `EXPIRE` per user into a single round\ntrip through node-redis's `multi()`. With 500 users that's 1000 commands in\none network call — Redis processes them sequentially on the server side but\nthe client only pays one RTT.\n\n[code example]\n\n`multi().exec()` in node-redis 5 wraps the batched commands in `MULTI/EXEC`,\nso Redis runs the queued commands contiguously and returns the replies in\norder. Note that Redis transactions do *not* roll back commands that already\nsucceeded if a later command returns an error — node-redis surfaces those\nerrors by rejecting `exec()` with a `MultiErrorReply` whose `replies` array\nstill contains the successful results. For independent bulk-ingestion\ncommands that don't need the `MULTI/EXEC` wrapper at all,\n`multi().execAsPipeline()` ships the same batch in one round trip with\nslightly lower server-side overhead. (See\n[transactions and pipelining](https://redis.io/docs/latest/develop/clients/nodejs/transpipe)\nfor the full mental model.)\n\nIn production, the equivalent of this script runs as an offline pipeline (a\nSpark or Feast `materialize` job) that reads from the warehouse and writes\ninto Redis. The\n[Feast `RedisOnlineStore`](https://docs.feast.dev/reference/online-stores/redis)\nprovider does exactly this under the hood; the in-house\n[Redis Feature Form](https://redis.io/docs/latest/develop/ai/featureform) integration\ncovers the materialize + serve path end-to-end."
    },
    {
      "id": "streaming-writes-with-per-field-ttl",
      "title": "Streaming writes with per-field TTL",
      "role": "content",
      "text": "`updateStreaming` is the linchpin of the mixed-staleness story:\n\n[code example]\n\n[`HEXPIRE`](https://redis.io/docs/latest/commands/hexpire) sets the TTL on *individual*\nhash fields, not on the whole key. Note node-redis's argument order:\n`hExpire(key, fields, seconds)` — the fields come *before* the TTL, the\nopposite of `EXPIRE(key, seconds)`. The two commands are sent in one round\ntrip and Redis executes them in pipeline order: the `HSET` runs first and\ncreates or overwrites the fields, then `HEXPIRE` attaches a TTL to each of\nthose same fields. `HEXPIRE` returns one status code per field — `1` if the\nTTL was set, `-2` if the field doesn't exist — so the helper throws if any\ncode is anything other than `1`. That makes the \"every streaming write\nrenews its TTL\" invariant fail loudly rather than silently leaving a\nstreaming field with no expiry attached.\n\nIf a streaming pipeline stops, the streaming fields drop out one by one as\ntheir per-field TTLs elapse — there is no application-side cleaner involved.\n[`HTTL`](https://redis.io/docs/latest/commands/httl) lets the model side inspect the\nremaining TTL on any field, which is useful both for debugging (\"why is this\nfeature missing?\" → \"it expired three seconds ago\") and as a freshness signal\nin the model itself.\n\n> **HEXPIRE requires Redis 7.4 or later.** `HEXPIRE` and the field-level TTL\n> commands (`HTTL`, `HPERSIST`, `HEXPIREAT`, `HPEXPIRE`, `HPEXPIREAT`,\n> `HPTTL`, `HEXPIRETIME`, `HPEXPIRETIME`) were added in Redis 7.4. On older\n> Redis builds you would have to put streaming features on their own keys\n> (one key per feature, or one key per feature group) and set a key-level\n> `EXPIRE` instead — at the cost of giving up the single-`HMGET` retrieval."
    },
    {
      "id": "inference-reads-with-hmget",
      "title": "Inference reads with HMGET",
      "role": "content",
      "text": "`getFeatures` is one `HMGET`:\n\n[code example]\n\nThe model knows exactly which features it consumes, so the request path\nalways takes the `HMGET` branch with an explicit field list — that's the\nsub-millisecond path. `HGETALL` is the right call for debugging (which is\nwhat the demo's \"Inspect\" panel does) but not for serving: it forces Redis\nto serialize every field, including ones the model doesn't need.\n\nFields that don't exist (because they were never written, or because they\nexpired) come back as `null`. The helper drops them from the result object\nso the caller sees only the features that are actually available. A real\nmodel server would either treat missing values as a feature (\"this user has\nno streaming signal yet\") or fall back to a default from the model's\ntraining data."
    },
    {
      "id": "batch-scoring-with-pipelined-hmget",
      "title": "Batch scoring with pipelined HMGET",
      "role": "content",
      "text": "For batch inference, the same `HMGET` shape pipelines across users:\n\n[code example]\n\nOne round trip for the whole batch — the demo regularly returns 100 users in\n2-3 ms against a local Redis. On a real network the round trip dominates;\npipelining is what keeps batch scoring practical.\n\nFor very large batches on a clustered deployment, the shape changes: a single\n`multi().exec()` is bound to one shard, because `MULTI/EXEC` cannot span hash\nslots, so the same `batchGetFeatures` call can only serve keys that hash to\nthe same shard. node-redis's\n[cluster client](https://github.com/redis/node-redis/blob/master/docs/clustering.md)\nroutes non-pipelined `hmGet` calls to the right shard transparently — so on a\ncluster, fan out `await Promise.all(ids.map((id) => client.hmGet(...)))` and\nthe client pipelines per-shard for you. For very latency-sensitive batch\ninference where the request-side cost of that fan-out matters, group the IDs\nby hash slot ahead of time and issue one `multi().exec()` per shard in\nparallel: each shard's batch then runs as one round trip. A hash tag like\n`fs:user:{vip}:u0001` forces a known set of keys onto the same shard so one\n`multi()` can cover all of them in a single round trip."
    },
    {
      "id": "the-streaming-worker",
      "title": "The streaming worker",
      "role": "content",
      "text": "`streamingWorker.js` is the demo's stand-in for whatever Flink, Kafka Streams,\nor bespoke service computes the real-time features\n([source](https://github.com/redis/docs/blob/main/content/develop/use-cases/feature-store/nodejs/streamingWorker.js)).\nIt runs as a `setTimeout` loop next to the demo server so the UI can start,\npause, and resume it; in production this code would live in the streaming\nlayer.\n\nEvery tick the worker picks a few random users, generates a new value for each\nstreaming feature, and calls `store.updateStreaming(userId, fields)`. The\ndemo defaults to 5 users per tick at 1-second intervals — so a 200-user store\nsees roughly half its users refreshed in the first minute, and most after a\nfew minutes. Drop `--seed-users` or raise `--users-per-tick` if you'd rather\nhave every user touched quickly.\n\n[code example]\n\nPausing the worker is what shows off the mixed-staleness behavior: leave it\npaused for longer than `streamingTtlSeconds` and the streaming fields\ndisappear from every user's hash one by one, while the batch fields remain\nunder the longer key-level `EXPIRE`. The demo's `Pause / resume` button lets\nyou see this happen in real time.\n\nThe worker uses a `setTimeout` chain rather than `setInterval`, so a slow\ntick (or a paused state) never queues up overlapping work — each tick fully\nfinishes before the next one schedules. That mirrors how a real Flink\noperator backpressures: don't accept the next input until the current one\nhas been committed."
    },
    {
      "id": "the-batch-builder",
      "title": "The batch builder",
      "role": "content",
      "text": "`buildFeatures.js` is the demo's nightly materializer\n([source](https://github.com/redis/docs/blob/main/content/develop/use-cases/feature-store/nodejs/buildFeatures.js)).\nIt generates synthetic feature rows and calls `store.bulkLoad` once. The\nsynthesis itself is not the point — in a real deployment the equivalent code\nreads from the offline store (Snowflake, BigQuery, Iceberg) and writes the\nresulting hashes into Redis.\n\n[code example]\n\nYou can run the builder on its own (independently of the demo server) to\npopulate Redis from the command line:\n\n[code example]\n\nThat writes 500 users at `fs:user:*` with a one-hour key-level TTL, which is\nhow a typical operator would pre-seed a feature store from the command line\nwhen debugging."
    },
    {
      "id": "the-interactive-demo",
      "title": "The interactive demo",
      "role": "content",
      "text": "`demoServer.js` runs a Node.js `http` server on port 8086. The HTML page lets\nyou:\n\n* **Bulk-load** any number of users (default 200) with a configurable\n  key-level TTL. Drop the TTL to 30 s and watch the entire store expire on\n  schedule — the same thing that happens if a daily refresher fails.\n* See the **store state** at a glance: user count, batch / streaming TTLs,\n  cumulative read/write counters.\n* See the **streaming worker** status (running / paused, ticks completed,\n  writes performed) and **pause or resume** it. Leave it paused for longer\n  than the streaming TTL to watch streaming fields drop out.\n* Run an **inference read** for any user with a chosen feature subset, and\n  see the value, the per-field TTL, and the read latency.\n* Run **batch scoring** with a pipelined `HMGET` across `N` users and see\n  the total elapsed time plus the per-user breakdown.\n* **Inspect** any user's full hash with field-level TTLs and the key-level\n  TTL — the right view for debugging \"why is this feature missing?\" at\n  read time.\n\nThe server holds one `FeatureStore` instance and one `StreamingWorker` for\nthe lifetime of the process. Endpoints:\n\n| Endpoint                  | What it does                                                                        |\n|---------------------------|-------------------------------------------------------------------------------------|\n| `GET  /state`             | User count, TTL config, stats counters, worker status.                              |\n| `POST /bulk-load`         | Batched `HSET` + `EXPIRE` over N synthetic users with a chosen TTL.                 |\n| `POST /worker/toggle`     | Pause / resume the streaming worker.                                                |\n| `POST /read`              | `HMGET` a chosen feature subset for one user; report latency and per-field TTLs.    |\n| `POST /batch-read`        | Pipeline `HMGET` across N users; report total latency and per-entity field counts.  |\n| `GET  /inspect`           | `HGETALL` + `HTTL` for one user; full hash view with per-field TTLs.                |\n| `POST /reset`             | Drop every user under the key prefix (used by the demo's reset button).             |"
    },
    {
      "id": "prerequisites",
      "title": "Prerequisites",
      "role": "content",
      "text": "* **Redis 7.4 or later.** [`HEXPIRE`](https://redis.io/docs/latest/commands/hexpire) and\n  [`HTTL`](https://redis.io/docs/latest/commands/httl) were added in Redis 7.4; the demo\n  relies on per-field TTL for the mixed-staleness story.\n* **Node.js 18 or later.**\n* The `node-redis` client. Install it with:\n\n  [code example]\n\n  Field-level TTL bindings (`hExpire`, `hTTL`) ship in node-redis 5.\n\nIf your Redis server is running elsewhere, start the demo with `--redis-host`\nand `--redis-port`."
    },
    {
      "id": "running-the-demo",
      "title": "Running the demo",
      "role": "content",
      "text": ""
    },
    {
      "id": "get-the-source-files",
      "title": "Get the source files",
      "role": "content",
      "text": "The demo consists of five files. Download them from the\n[`nodejs` source folder](https://github.com/redis/docs/tree/main/content/develop/use-cases/feature-store/nodejs)\non GitHub, or grab them with `curl`:\n\n[code example]"
    },
    {
      "id": "start-the-demo-server",
      "title": "Start the demo server",
      "role": "content",
      "text": "From that directory:\n\n[code example]\n\nYou should see:\n\n[code example]\n\nBy default the demo wipes the configured key prefix on startup so each run\nstarts from a clean state. Pass `--no-reset` to keep any existing data, or\n`--key-prefix <prefix>` to point the demo at a different prefix entirely.\n\nOpen [http://127.0.0.1:8086](http://127.0.0.1:8086) in a browser. Useful\nthings to try:\n\n* Pick a user and click **Read features** with a mixed batch/streaming subset\n  — you'll see batch fields with no per-field TTL (covered by the key-level\n  TTL) and streaming fields with a positive per-field TTL.\n* Click **Pipeline HMGET** with `count=100` to see the latency of a 100-user\n  batch read.\n* Click **Pause / resume** on the streaming worker and leave it paused for\n  ~5 minutes (or restart the server with `--streaming-ttl-seconds 30` to\n  make it visible in seconds). Re-run **Read features** on any user and\n  watch the streaming fields disappear while the batch fields stay.\n* Click **Inspect** on a user to see the full hash with field-level TTLs.\n* Click **Bulk-load** with a short TTL (say 30 seconds) and watch the user\n  count fall to zero on the next minute — the same thing that happens if a\n  daily batch run fails to land.\n* Click **Reset** to drop every user and start over.\n\nThe server is read/write against your local Redis. The default key prefix is\n`fs:user:`. Pass `--no-reset` to keep existing data across restarts, or\n`--redis-host` / `--redis-port` to point at a different Redis."
    },
    {
      "id": "production-usage",
      "title": "Production usage",
      "role": "content",
      "text": "The guidance below focuses on the production concerns that are specific to\nrunning a feature store on Redis. For the generic node-redis production\nchecklist — connection pooling, TLS and AUTH, error handling, retry policy —\nsee the [node-redis client guide](https://redis.io/docs/latest/develop/clients/nodejs)\nand the\n[connect-with-TLS recipe](https://redis.io/docs/latest/develop/clients/nodejs/connect#connect-to-your-production-redis-with-tls).\nThe feature-store demo runs against `localhost` with the defaults; a real\ndeployment should harden the client first."
    },
    {
      "id": "pick-the-batch-ttl-to-outlast-a-failed-refresher",
      "title": "Pick the batch TTL to outlast a failed refresher",
      "role": "content",
      "text": "The whole-entity `EXPIRE` is your safety net against silent staleness from a\nbroken batch pipeline. Set it longer than your worst-case batch outage so a\nsingle missed run doesn't take the feature store offline, but short enough\nthat a sustained outage causes loud failures (missing entities) rather than\nquiet ones (yesterday's features being scored as today's). The standard\nchoice is one cycle of \"expected refresh interval × 2\" — for a daily batch,\n48 hours; for a 6-hour batch, 12 hours.\n\nThe same logic applies to the per-field streaming TTL: a few times the\nexpected update interval so a slow-but-alive streaming worker doesn't churn\nfeatures needlessly, but short enough that a stalled worker causes visible\nfreshness failures."
    },
    {
      "id": "co-locate-the-online-store-with-serving-not-with-training",
      "title": "Co-locate the online store with serving, not with training",
      "role": "content",
      "text": "The online store's hash representation does *not* have to match the schema in\nyour offline store. The batch materialization step is your chance to flatten\njoins, encode categoricals, and project to whatever shape the model server\nwants — so the request path is exactly one `HMGET` and zero transforms.\n\nThe training pipeline reads from the offline store with its own schema; the\nserving pipeline reads from Redis with the flattened serving schema. Keeping\nthose two pipelines as the same code path is what prevents training-serving\nskew."
    },
    {
      "id": "pipeline-batch-reads-across-shards",
      "title": "Pipeline batch reads across shards",
      "role": "content",
      "text": "On a single Redis instance, pipelining `HMGET` across `N` users through\n`multi().exec()` is one round trip. A Redis Cluster is different in two ways:\n`MULTI/EXEC` is bound to one shard, so a single `multi()` cannot span keys\nthat hash to different shards; and the keys for a typical user batch will\nland on multiple shards. For batch reads on a cluster, fan out parallel\n`hmGet` calls with `Promise.all` — node-redis's cluster client pipelines\nthe calls per-shard automatically — or, for tighter control, group the IDs\nby hash slot ahead of time and issue one `multi().exec()` per shard in\nparallel.\n\nFor a small number of frequently-queried users (a top-N customer list, for\nexample), a hash tag like `fs:user:{vip}:u0001` forces the keys onto the\nsame shard and lets one `multi().exec()` serve them all in one round trip."
    },
    {
      "id": "make-hexpire-part-of-every-streaming-write",
      "title": "Make HEXPIRE part of every streaming write",
      "role": "content",
      "text": "The single biggest correctness lever in this design is that the streaming\nwrite applies `HEXPIRE` *every time*. If a streaming worker writes a field\nwithout renewing its TTL, the field carries whatever expiry was there before\n— possibly none, possibly stale — and the mixed-staleness invariant breaks.\nKeep the `HSET` and `HEXPIRE` in the same `multi()` (or, even safer, in the\nsame [Lua script](https://redis.io/docs/latest/develop/programmability/eval-intro) if\nyou don't trust the call site)."
    },
    {
      "id": "avoid-hgetall-on-the-request-path",
      "title": "Avoid HGETALL on the request path",
      "role": "content",
      "text": "`HGETALL` reads every field on the hash, including ones the model doesn't\nneed. With dozens of features per entity, that is wasted serialization work\non the server and wasted bandwidth on the wire. Always specify the field list\nexplicitly with `hmGet` in the model server.\n\nThe exception is debugging and feature-set discovery, where you genuinely\nwant the full hash. The demo's \"Inspect\" button uses `hGetAll` for exactly\nthis reason."
    },
    {
      "id": "inspect-the-store-directly-with-redis-cli",
      "title": "Inspect the store directly with redis-cli",
      "role": "content",
      "text": "When testing or troubleshooting, the cli tells you everything:\n\n[code example]\n\nA streaming field that returns `-2` from `HTTL` doesn't exist on the hash\n(either it was never written, or it expired); `-1` means the field has no\nTTL set (and is therefore covered only by the key-level `EXPIRE`); any\npositive value is the remaining TTL in seconds."
    },
    {
      "id": "learn-more",
      "title": "Learn more",
      "role": "related",
      "text": "This example uses the following Redis commands:\n\n* [`HSET`](https://redis.io/docs/latest/commands/hset) to write a feature or a whole\n  feature row in one call.\n* [`HMGET`](https://redis.io/docs/latest/commands/hmget) to retrieve any subset of\n  features for one entity in one round trip.\n* [`HGETALL`](https://redis.io/docs/latest/commands/hgetall) for debugging and\n  feature-set discovery.\n* [`HEXPIRE`](https://redis.io/docs/latest/commands/hexpire) and\n  [`HTTL`](https://redis.io/docs/latest/commands/httl) for per-field TTL on streaming\n  features (Redis 7.4+).\n* [`EXPIRE`](https://redis.io/docs/latest/commands/expire) and\n  [`TTL`](https://redis.io/docs/latest/commands/ttl) for the whole-entity TTL aligned\n  with the batch materialization cycle.\n* Pipelined `HMGET` across many entities for batch scoring with one network\n  round trip — see\n  [transactions and pipelining](https://redis.io/docs/latest/develop/clients/nodejs/transpipe).\n\nSee the [node-redis documentation](https://redis.io/docs/latest/develop/clients/nodejs)\nfor the full client reference, and the\n[Hashes overview](https://redis.io/docs/latest/develop/data-types/hashes) for the deeper\nconceptual model — including the listpack encoding that makes small hashes\nparticularly compact in memory, which matters at feature-store scale."
    }
  ],
  "examples": [
    {
      "id": "the-feature-store-helper-ex0",
      "language": "javascript",
      "code": "const { createClient } = require(\"redis\");\nconst { FeatureStore } = require(\"./featureStore\");\n\nconst client = createClient({ socket: { host: \"localhost\", port: 6379 } });\nclient.on(\"error\", (err) => console.error(\"Redis client error:\", err));\nawait client.connect();\n\nconst store = new FeatureStore({\n  redisClient: client,\n  keyPrefix: \"fs:user:\",\n  batchTtlSeconds: 24 * 60 * 60,    // whole-entity TTL aligned with the daily batch cycle\n  streamingTtlSeconds: 5 * 60,      // per-field TTL on each streaming feature\n});\n\n// Batch materialization: one HSET + EXPIRE per user, all batched.\nawait store.bulkLoad({\n  u0001: { country_iso: \"US\", risk_segment: \"low\",\n           tx_count_7d: 14, avg_amount_30d: 92.40,\n           account_age_days: 612, chargeback_count_180d: 0 },\n  u0002: { country_iso: \"GB\", risk_segment: \"medium\",\n           tx_count_7d: 47, avg_amount_30d: 220.10,\n           account_age_days: 1840, chargeback_count_180d: 1 },\n});\n\n// Streaming write: HSET + HEXPIRE on just the fields that changed.\nawait store.updateStreaming(\"u0001\", {\n  last_login_ts: 1716998413541,\n  last_device_id: \"ios-9f02\",\n  tx_count_5m: 3,\n  failed_logins_15m: 0,\n  session_country: \"US\",\n});\n\n// Inference read: HMGET of whatever the model needs.\nconst features = await store.getFeatures(\"u0001\", [\n  \"risk_segment\", \"tx_count_7d\", \"avg_amount_30d\",\n  \"tx_count_5m\", \"failed_logins_15m\",\n]);\n\n// Batch scoring: pipelined HMGET across many users.\nconst batch = await store.batchGetFeatures(\n  [\"u0001\", \"u0002\", \"u0003\"],\n  [\"risk_segment\", \"tx_count_5m\", \"failed_logins_15m\"],\n);",
      "section_id": "the-feature-store-helper"
    },
    {
      "id": "data-model-ex0",
      "language": "text",
      "code": "fs:user:u0001                                   TTL = 86400 s (key-level)\n  country_iso=US                                <no field TTL>\n  risk_segment=low                              <no field TTL>\n  account_age_days=612                          <no field TTL>\n  tx_count_7d=14                                <no field TTL>\n  avg_amount_30d=92.40                          <no field TTL>\n  chargeback_count_180d=0                       <no field TTL>\n  last_login_ts=1716998413541                   TTL = 300 s (per field, HEXPIRE)\n  last_device_id=ios-9f02                       TTL = 300 s (per field, HEXPIRE)\n  tx_count_5m=3                                 TTL = 300 s (per field, HEXPIRE)\n  failed_logins_15m=0                           TTL = 300 s (per field, HEXPIRE)\n  session_country=US                            TTL = 300 s (per field, HEXPIRE)",
      "section_id": "data-model"
    },
    {
      "id": "bulk-loading-batch-features-ex0",
      "language": "javascript",
      "code": "async bulkLoad(rows, ttlSeconds) {\n  const ttl = ttlSeconds ?? this.batchTtlSeconds;\n  const ids = Object.keys(rows);\n  if (ids.length === 0) return 0;\n\n  const pipe = this.redis.multi();\n  for (const entityId of ids) {\n    const key = this.keyFor(entityId);\n    const encoded = {};\n    for (const [name, value] of Object.entries(rows[entityId])) {\n      encoded[name] = encode(value);\n    }\n    pipe.hSet(key, encoded);\n    pipe.expire(key, ttl);\n  }\n  await pipe.exec();\n  ...\n}",
      "section_id": "bulk-loading-batch-features"
    },
    {
      "id": "streaming-writes-with-per-field-ttl-ex0",
      "language": "javascript",
      "code": "async updateStreaming(entityId, fields, ttlSeconds) {\n  const names = Object.keys(fields);\n  if (names.length === 0) return;\n  const ttl = ttlSeconds ?? this.streamingTtlSeconds;\n  const key = this.keyFor(entityId);\n  const encoded = {};\n  for (const [name, value] of Object.entries(fields)) {\n    encoded[name] = encode(value);\n  }\n\n  const [, expireResult] = await this.redis\n    .multi()\n    .hSet(key, encoded)\n    .hExpire(key, names, ttl)\n    .exec();\n  if (!Array.isArray(expireResult) ||\n      expireResult.some((code) => Number(code) !== 1)) {\n    throw new Error(\n      `HEXPIRE did not set every field TTL for ${key}: ` +\n        JSON.stringify(expireResult),\n    );\n  }\n  ...\n}",
      "section_id": "streaming-writes-with-per-field-ttl"
    },
    {
      "id": "inference-reads-with-hmget-ex0",
      "language": "javascript",
      "code": "async getFeatures(entityId, fieldNames = null) {\n  const key = this.keyFor(entityId);\n  if (fieldNames === null || fieldNames === undefined) {\n    return await this.redis.hGetAll(key);\n  }\n  const names = [...fieldNames];\n  if (names.length === 0) return {};\n  const values = await this.redis.hmGet(key, names);\n  const out = {};\n  for (let i = 0; i < names.length; i += 1) {\n    const v = values[i];\n    if (v !== null && v !== undefined) out[names[i]] = v;\n  }\n  return out;\n}",
      "section_id": "inference-reads-with-hmget"
    },
    {
      "id": "batch-scoring-with-pipelined-hmget-ex0",
      "language": "javascript",
      "code": "async batchGetFeatures(entityIds, fieldNames) {\n  const ids = [...entityIds];\n  const names = [...fieldNames];\n  if (ids.length === 0 || names.length === 0) return {};\n\n  const pipe = this.redis.multi();\n  for (const entityId of ids) {\n    pipe.hmGet(this.keyFor(entityId), names);\n  }\n  const rows = await pipe.exec();\n\n  const out = {};\n  for (let i = 0; i < ids.length; i += 1) {\n    const values = rows[i] || [];\n    const row = {};\n    for (let j = 0; j < names.length; j += 1) {\n      const v = values[j];\n      if (v !== null && v !== undefined) row[names[j]] = v;\n    }\n    out[ids[i]] = row;\n  }\n  return out;\n}",
      "section_id": "batch-scoring-with-pipelined-hmget"
    },
    {
      "id": "the-streaming-worker-ex0",
      "language": "javascript",
      "code": "async _tick() {\n  const ids = await this.store.listEntityIds(500);\n  if (ids.length === 0) return;\n  const chosen = this.rng.sample(ids, this.usersPerTick);\n  const nowMs = Date.now();\n  for (const entityId of chosen) {\n    const fields = {\n      last_login_ts: nowMs,\n      last_device_id: this.rng.choice(DEVICE_IDS),\n      tx_count_5m: this.rng.int(0, 12),\n      failed_logins_15m: this.rng.weightedChoice(\n        FAILED_LOGIN_BUCKETS, FAILED_LOGIN_WEIGHTS,\n      ),\n      session_country: this.rng.choice(SESSION_COUNTRIES),\n    };\n    await this.store.updateStreaming(entityId, fields);\n  }\n}",
      "section_id": "the-streaming-worker"
    },
    {
      "id": "the-batch-builder-ex0",
      "language": "javascript",
      "code": "function synthesizeUsers(count, seed = 42) {\n  const rng = makeRng(seed);\n  const users = {};\n  for (let i = 1; i <= count; i += 1) {\n    const uid = `u${String(i).padStart(4, \"0\")}`;\n    users[uid] = {\n      country_iso: rng.choice(COUNTRY_CHOICES),\n      risk_segment: rng.weightedChoice(RISK_SEGMENTS, RISK_WEIGHTS),\n      account_age_days: rng.int(7, 2400),\n      tx_count_7d: rng.int(0, 80),\n      avg_amount_30d: Number(rng.float(5, 350).toFixed(2)),\n      chargeback_count_180d: rng.weightedChoice(\n        CHARGEBACK_BUCKETS, CHARGEBACK_WEIGHTS,\n      ),\n    };\n  }\n  return users;\n}",
      "section_id": "the-batch-builder"
    },
    {
      "id": "the-batch-builder-ex1",
      "language": "bash",
      "code": "node buildFeatures.js --count 500 --ttl-seconds 3600",
      "section_id": "the-batch-builder"
    },
    {
      "id": "prerequisites-ex0",
      "language": "bash",
      "code": "npm install \"redis@^5\"",
      "section_id": "prerequisites"
    },
    {
      "id": "get-the-source-files-ex0",
      "language": "bash",
      "code": "mkdir feature-store-nodejs-demo && cd feature-store-nodejs-demo\nBASE=https://raw.githubusercontent.com/redis/docs/main/content/develop/use-cases/feature-store/nodejs\ncurl -O $BASE/package.json\ncurl -O $BASE/featureStore.js\ncurl -O $BASE/buildFeatures.js\ncurl -O $BASE/streamingWorker.js\ncurl -O $BASE/demoServer.js\nnpm install",
      "section_id": "get-the-source-files"
    },
    {
      "id": "start-the-demo-server-ex0",
      "language": "bash",
      "code": "node demoServer.js",
      "section_id": "start-the-demo-server"
    },
    {
      "id": "start-the-demo-server-ex1",
      "language": "text",
      "code": "Dropping any existing users under 'fs:user:*' for a clean demo run (pass --no-reset to keep them).\nRedis feature-store demo server listening on http://127.0.0.1:8086\nUsing Redis at localhost:6379 with key prefix 'fs:user:' (batch TTL 86400s, streaming TTL 300s)\nMaterialized 200 user(s); streaming worker running.",
      "section_id": "start-the-demo-server"
    },
    {
      "id": "inspect-the-store-directly-with-redis-cli-ex0",
      "language": "bash",
      "code": "# How many users currently in the store\nredis-cli --scan --pattern 'fs:user:*' | wc -l\n\n# One user's full hash and key-level TTL\nredis-cli HGETALL fs:user:u0001\nredis-cli TTL    fs:user:u0001\n\n# Per-field TTL on the streaming fields\nredis-cli HTTL fs:user:u0001 FIELDS 5 \\\n  last_login_ts last_device_id tx_count_5m failed_logins_15m session_country\n\n# Sample HMGET as the model would issue it\nredis-cli HMGET fs:user:u0001 risk_segment tx_count_7d avg_amount_30d tx_count_5m",
      "section_id": "inspect-the-store-directly-with-redis-cli"
    }
  ]
}
