{
  "id": "ruby",
  "title": "Redis feature store with redis-rb",
  "url": "https://redis.io/docs/latest/develop/use-cases/feature-store/ruby/",
  "summary": "Build a Redis-backed online feature store in Ruby with redis-rb",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc"
  ],
  "last_updated": "2026-06-04T14:49:57+01:00",
  "children": [],
  "page_type": "content",
  "content_hash": "ac71945d6c45580a3ce487e816b3ede32ca0ac0938ab43b4ec5e3777632b68fb",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "This guide shows you how to build a small Redis-backed online feature store\nin Ruby with the [`redis`](https://redis.io/docs/latest/develop/clients/ruby) gem. The\ndemo runs on top of WEBrick (the stdlib HTTP server) so you can bulk-load a\nbatch of users with a key-level TTL, run a streaming worker that overwrites\nreal-time features with per-field TTL, retrieve any subset of features for\none user under 2 ms, and pipeline `HMGET` across a hundred users for batch\nscoring."
    },
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "Each entity (here, a user) is one Redis\n[Hash](https://redis.io/docs/latest/develop/data-types/hashes) at a deterministic key —\n`fs:user:{id}`. The hash holds every feature for that entity as one field per\nfeature: batch-materialized aggregates (refreshed once a day) alongside\nstreaming-updated signals (refreshed every few seconds). One\n[`HMGET`](https://redis.io/docs/latest/commands/hmget) returns whichever subset the\nmodel needs in one network round trip.\n\nTwo TTL layers solve the *mixed staleness* problem without an\napplication-side cleaner:\n\n* A **key-level** [`EXPIRE`](https://redis.io/docs/latest/commands/expire) aligned with\n  the batch materialization cycle (24 hours in the demo). If the batch\n  refresher fails, the whole entity disappears at the next cycle and\n  inference sees a missing entity — which the model handler can detect and\n  fall back on — rather than silently outdated values.\n* A **per-field** [`HEXPIRE`](https://redis.io/docs/latest/commands/hexpire) (Redis 7.4+)\n  on each streaming feature gives that field its own shorter expiry,\n  independent of the rest of the hash. If the streaming pipeline stops\n  updating a feature, the field self-cleans while the batch fields stay\n  populated.\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\n  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\n  field expire on its own timer."
    },
    {
      "id": "how-redis-rb-fits-the-demo",
      "title": "How redis-rb fits the demo",
      "role": "content",
      "text": "Two gem facts shape the helper:\n\n* **One shared `Redis` client serves the whole process.** The `redis` gem\n  uses a single TCP connection per `Redis` instance — and the instance is\n  thread-safe (synchronized with a mutex). Handing the same `Redis` to\n  every WEBrick worker thread and the streaming worker is fine and is the\n  canonical way to run this kind of demo.\n* **`Redis#call` is the escape hatch for commands not yet typed on the\n  gem.** redis-rb 5.4 ships no stable typed helpers for the per-field\n  TTL commands. The helper sends `HEXPIRE` and `HTTL` with\n  `r.call('HEXPIRE', key, ttl, 'FIELDS', count, *fields)` so the wire\n  bytes match the protocol exactly regardless of which patch release\n  is installed.\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`build_features.rb` — 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`streaming_worker.rb` — a daemon Ruby thread that stands in for a\nFlink / Kafka Streams job. The WEBrick servlet in `demo_server.rb` reads\nany subset of those features through `feature_store.rb`'s helper class."
    },
    {
      "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\nas events arrive, and an **inference path** that reads features on the\nrequest side."
    },
    {
      "id": "batch-path-per-materialization-cycle",
      "title": "Batch path (per materialization cycle)",
      "role": "content",
      "text": "1. The batch job calls `synthesize_users(N, seed)` (in production, the\n   equivalent computation lives in an offline pipeline against the\n   warehouse). The result is `{user_id => {field => value, ...}}` for every\n   user in this cycle.\n2. `store.bulk_load(rows, ttl_seconds:)` queues 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   `redis.pipelined`, so the whole batch ships in a single round trip."
    },
    {
      "id": "streaming-path-per-event",
      "title": "Streaming path (per event)",
      "role": "content",
      "text": "When a user does something (login, transaction, page view) the streaming\nlayer computes whatever real-time signals fall out of that event and calls\n`store.update_streaming(user_id, fields)`. That queues:\n\n1. An [`HSET`](https://redis.io/docs/latest/commands/hset) writing the new field\n   values. Redis is single-threaded per shard, so this is atomic against\n   any concurrent batch write on the same hash — no version columns, no\n   locks.\n2. An [`HEXPIRE`](https://redis.io/docs/latest/commands/hexpire) over exactly the\n   fields that were written, with the streaming TTL. Each streaming field\n   carries its own per-field expiry independent of the rest of the hash.\n   Stop the worker and these fields drop out one by one as their TTLs\n   elapse, while the batch fields remain populated under the longer\n   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\n   owned by the model, not the store).\n2. It calls `store.get_features(user_id, names)`, which is one\n   [`HMGET`](https://redis.io/docs/latest/commands/hmget). Redis returns the values\n   in the same order as the requested fields, with `nil` for any field\n   that doesn't exist (or has expired).\n3. For batch inference, the model server calls\n   `store.batch_get_features(user_ids, names)`, which pipelines one\n   [`HMGET`](https://redis.io/docs/latest/commands/hmget) per user across all `N`\n   users in a single network round trip."
    },
    {
      "id": "project-layout",
      "title": "Project layout",
      "role": "content",
      "text": "[code example]\n\nRun with `bundle exec ruby demo_server.rb` or\n`bundle exec ruby build_features.rb --count 500`."
    },
    {
      "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/ruby/feature_store.rb)):\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\nhash fields are bytes on the wire, so the helper renders booleans as\n`'true'` / `'false'` and uses `value.to_s` for everything else. The model\nserver is responsible for parsing back to the right type, the same way it\nwould when reading any serialized feature store.\n\n[code example]"
    },
    {
      "id": "bulk-loading-batch-features",
      "title": "Bulk-loading batch features",
      "role": "content",
      "text": "`bulk_load` pipelines one `HSET` and one `EXPIRE` per user into a single\nround trip via `redis.pipelined`. With 500 users that's 1000 commands in\none network call — Redis processes them sequentially on the server side\nbut the client only pays one RTT.\n\n[code example]\n\n`Redis#pipelined` is a non-transactional batch: commands queue up and ship\nin one round trip but they don't run inside a `MULTI/EXEC` block. That's\nthe right choice here because each user's `HSET` + `EXPIRE` pair is\nindependent of every other user's, and an all-or-nothing transaction\nwould block the server for the duration of the batch. For the rare case\nwhere the pair has to be inseparable, use `redis.multi do |tx| ... end`\nor a Lua script via\n[`EVAL`](https://redis.io/docs/latest/commands/eval) /\n[Eval scripting](https://redis.io/docs/latest/develop/programmability/eval-intro).\n\nIn production, the equivalent of this script runs as an offline pipeline\n(a Spark or Feast `materialize` job) that reads from the warehouse and\nwrites into 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": "`update_streaming` 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\n*individual* hash fields, not on the whole key. The two commands are\nqueued in the same `pipelined` block so Redis runs them in order: the\n`HSET` first creates or overwrites the fields, then `HEXPIRE` attaches a\nTTL to each of those same fields. `HEXPIRE` returns one status code per\nfield:\n\n* `1` — TTL set / updated.\n* `2` — the expiry was 0 or in the past, so Redis deleted the field\n  instead of applying a TTL.\n* `0` — an `NX | XX | GT | LT` conditional flag was specified and not\n  met (we never use one here).\n* `-2` — no such field, or no such key.\n\nThe helper raises if any code is anything other than `1`, so the \"every\nstreaming write renews its TTL\" invariant fails loudly rather than\nsilently leaving a streaming field with no expiry attached.\n\nWhy `redis.call('HEXPIRE', ...)` instead of a typed `redis.hexpire`?\nredis-rb 5.4 ships no stable typed helpers for the per-field TTL\ncommands, so `Redis#call` is the canonical way to issue them. The wire\nbytes match the protocol exactly. The same `r.call('HTTL', ...)` shape\nappears in `field_ttls_seconds`.\n\nIf a streaming pipeline stops, the streaming fields drop out one by one\nas their per-field TTLs elapse. `field_ttls_seconds` lets the model side\ninspect the remaining TTL on any field — useful both for debugging\n(\"why is this feature missing?\" → \"it expired three seconds ago\") and as\na freshness signal in the model itself.\n\n> **HEXPIRE requires Redis 7.4 or later.** `HEXPIRE` and the field-level\n> TTL commands were added in Redis 7.4. The demo's `Gemfile` pins\n> `redis ~> 5.4`, which speaks the protocol natively."
    },
    {
      "id": "inference-reads-with-hmget",
      "title": "Inference reads with HMGET",
      "role": "content",
      "text": "`get_features` 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\nRedis to 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 `nil`. The helper drops them from the result hash\nso the caller sees only the features that are actually available."
    },
    {
      "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 returns a 30-user batch in\n~2 ms against a local Redis.\n\nA Redis Cluster is different: a single `redis.pipelined` block ships\nthrough one connection to one node. For batch reads on a cluster, use\nthe [`redis-clustering`](https://github.com/redis/redis-rb-cluster) gem\nand either fan out parallel `hmget` calls (the cluster client routes\neach one to the right shard) or, for tighter control, group entity IDs\nby hash slot and run one `pipelined` block per shard in parallel."
    },
    {
      "id": "the-streaming-worker",
      "title": "The streaming worker",
      "role": "content",
      "text": "`streaming_worker.rb` is the demo's stand-in for whatever Flink, Kafka\nStreams, or bespoke service computes the real-time features\n([source](https://github.com/redis/docs/blob/main/content/develop/use-cases/feature-store/ruby/streaming_worker.rb)).\nIt runs as a daemon `Thread` next to the WEBrick server so the UI can\nstart, pause, and resume it.\n\nThe lifecycle (start / stop / pause / resume / wait_for_idle) is the same\nas every other client in this use case. The two correctness levers:\n\n[code example]\n\nThe same pre-flight `@tick_in_flight = true` before the pause check and\nthe outer `ensure` block that clears both flags on every exit path\nappears in every other client demo, for the same reason: a reset that's\nabout to `DEL` every key needs to be able to call `worker.pause` to stop\n*future* ticks AND `worker.wait_for_idle` to flush a mid-flight tick\nbefore issuing the DEL sweep.\n\nPausing the worker is what shows off the mixed-staleness behavior: leave\nit paused for longer than `streaming_ttl_seconds` and the streaming\nfields disappear from every user's hash one by one, while the batch\nfields remain under the longer key-level `EXPIRE`. The demo's\n`Pause / resume` button lets you see this happen in real time."
    },
    {
      "id": "the-batch-builder",
      "title": "The batch builder",
      "role": "content",
      "text": "`build_features.rb` is the demo's nightly materializer\n([source](https://github.com/redis/docs/blob/main/content/develop/use-cases/feature-store/ruby/build_features.rb)).\nIt generates synthetic feature rows and calls `store.bulk_load` once.\n\nRun 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,\nwhich is how a typical operator would pre-seed a feature store from the\ncommand line when debugging."
    },
    {
      "id": "the-interactive-demo",
      "title": "The interactive demo",
      "role": "content",
      "text": "`demo_server.rb` runs a WEBrick server on port 8093. The HTML page lets\nyou:\n\n* **Bulk-load** any number of users (default 200) with a configurable\n  key-level TTL.\n* See the **store state**: user count, batch / streaming TTLs,\n  cumulative read/write counters.\n* See the **streaming worker** status and **pause or resume** it.\n* Run an **inference read** for any user with a chosen feature subset,\n  and see the value, the per-field TTL, and the read latency.\n* Run **batch scoring** with a pipelined `HMGET` across `N` users.\n* **Inspect** any user's full hash with field-level TTLs and the\n  key-level TTL.\n\nThe server holds one `Redis` client, one `FeatureStore`, and one\n`StreamingWorker` for the lifetime of the process. Every WEBrick request\nthread shares the same `Redis` (the gem synchronizes its own access).\nEndpoints:\n\n| Endpoint                  | What it does                                                                        |\n|---------------------------|-------------------------------------------------------------------------------------|\n| `GET  /state`             | User count, TTL config, stats counters, worker status.                              |\n| `POST /bulk-load`         | Pipelined `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)\n  and [`HTTL`](https://redis.io/docs/latest/commands/httl) were added in Redis 7.4;\n  the demo relies on per-field TTL for the mixed-staleness story.\n* **Ruby 3.0 or later.**\n* The `redis` and `webrick` gems. The demo's `Gemfile` pins\n  `redis ~> 5.4` and `webrick ~> 1.9`. WEBrick was removed from Ruby's\n  default-gem set in 3.0, so the explicit pin keeps the demo runnable\n  on modern Rubies.\n\nIf your Redis server is running elsewhere, start the demo with\n`--redis-url redis://host: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 lives in a small directory under\n[`feature-store/ruby`](https://github.com/redis/docs/tree/main/content/develop/use-cases/feature-store/ruby).\nClone the repo or copy the directory:\n\n[code example]"
    },
    {
      "id": "start-the-demo-server",
      "title": "Start the demo server",
      "role": "content",
      "text": "From the project directory:\n\n[code example]\n\nYou should see:\n\n[code example]\n\nOpen [http://127.0.0.1:8093](http://127.0.0.1:8093). Useful things to try:\n\n* Pick a user and click **Read features** with a mixed batch/streaming\n  subset — you'll see batch fields with no per-field TTL (covered by the\n  key-level TTL) and streaming fields with a positive per-field TTL.\n* Click **Pipeline HMGET** with `count=100` to see the latency of a\n  100-user batch read.\n* Click **Pause / resume** on the streaming worker and leave it paused\n  for ~5 minutes (or restart the server with\n  `--streaming-ttl-seconds 30` to make it visible in seconds). Re-run\n  **Read features** on any user and watch the streaming fields\n  disappear while the batch fields stay.\n* Click **Inspect** on a user to see the full hash with field-level\n  TTLs.\n* Click **Reset** to drop every user and start over."
    },
    {
      "id": "production-usage",
      "title": "Production usage",
      "role": "content",
      "text": "The guidance below focuses on the production concerns specific to\nrunning a feature store on Redis. For the generic redis-rb production\nchecklist — connection options, TLS, AUTH, retry policy — see the\n[`redis` gem documentation](https://redis.io/docs/latest/develop/clients/ruby).\nThe feature-store demo runs against `localhost` with the defaults; a\nreal deployment 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\nfrom a broken batch pipeline. Set it longer than your worst-case batch\noutage so a single missed run doesn't take the feature store offline,\nbut short enough that a sustained outage causes loud failures (missing\nentities) rather than quiet ones (yesterday's features being scored as\ntoday's). The standard choice is one cycle of \"expected refresh\ninterval × 2\" — for a daily batch, 48 hours; for a 6-hour batch, 12\nhours.\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\nchurn features needlessly, but short enough that a stalled worker\ncauses visible freshness 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\nschema in your offline store. The batch materialization step is your\nchance to flatten joins, encode categoricals, and project to whatever\nshape the model server wants — so the request path is exactly one\n`HMGET` and zero transforms.\n\nThe training pipeline reads from the offline store with its own schema;\nthe serving pipeline reads from Redis with the flattened serving\nschema. Keeping those two pipelines as the same code path is what\nprevents training-serving skew."
    },
    {
      "id": "use-redis-clustering-for-cluster-deployments",
      "title": "Use redis-clustering for cluster deployments",
      "role": "content",
      "text": "A single `redis.pipelined` block ships through one connection to one\nnode. On a Redis Cluster you need the\n[`redis-clustering`](https://github.com/redis/redis-rb-cluster) gem,\nwhich routes each command to the right shard transparently. For batch\nreads on a cluster, either fan out parallel `hmget` calls (each routed\nper-shard) or group entity IDs by hash slot ahead of time and run one\n`pipelined` block per shard in parallel.\n\nA hash tag like `fs:user:{vip}:u0001` forces a known set of keys onto\nthe same shard so one pipeline can cover them all in a single round\ntrip."
    },
    {
      "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\nstreaming write applies `HEXPIRE` *every time*. If a streaming worker\nwrites a field without renewing its TTL, the field carries whatever\nexpiry was there before — possibly none, possibly stale — and the\nmixed-staleness invariant breaks. Keep the `HSET` and `HEXPIRE` in the\nsame pipeline (or, even safer, in the same\n[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\ndoesn't need. With dozens of features per entity, that is wasted\nserialization work on the server and wasted bandwidth on the wire.\nAlways specify the field list explicitly with `hmget` in the model\nserver.\n\nThe exception is debugging and feature-set discovery, where you\ngenuinely want the full hash. The demo's \"Inspect\" button uses\n`hgetall` for exactly this 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\nhash (either it was never written, or it expired); `-1` means the\nfield has no TTL set (and is therefore covered only by the key-level\n`EXPIRE`); any positive 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\n  whole feature row in one call.\n* [`HMGET`](https://redis.io/docs/latest/commands/hmget) to retrieve any subset\n  of 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\n  streaming 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\n  aligned with the batch materialization cycle.\n\nSee the [`redis` gem documentation](https://redis.io/docs/latest/develop/clients/ruby)\nfor the full client reference, and the\n[Hashes overview](https://redis.io/docs/latest/develop/data-types/hashes) for the\ndeeper conceptual model."
    }
  ],
  "examples": [
    {
      "id": "project-layout-ex0",
      "language": "text",
      "code": "feature-store/ruby/\n├── Gemfile                — redis ~> 5.4, webrick ~> 1.9\n├── feature_store.rb       — FeatureStore class\n├── streaming_worker.rb    — daemon-thread worker\n├── build_features.rb     — synthesize_users + CLI main\n└── demo_server.rb         — WEBrick servlet + HTML page (single file)",
      "section_id": "project-layout"
    },
    {
      "id": "the-feature-store-helper-ex0",
      "language": "ruby",
      "code": "require 'redis'\nrequire_relative 'feature_store'\n\nredis = Redis.new(url: 'redis://localhost:6379')\nstore = FeatureStore.new(\n  redis: redis,\n  key_prefix: 'fs:user:',\n  batch_ttl_seconds: 24 * 60 * 60,    # whole-entity TTL aligned with the daily batch cycle\n  streaming_ttl_seconds: 5 * 60,      # per-field TTL on each streaming feature\n)\n\n# Batch materialization: one HSET + EXPIRE per user, all pipelined.\nstore.bulk_load({\n  'u0001' => {\n    '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  },\n}, ttl_seconds: 24 * 60 * 60)\n\n# Streaming write: HSET + HEXPIRE on just the fields that changed.\nstore.update_streaming('u0001', {\n  'last_login_ts' => (Time.now.to_f * 1000).to_i,\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.\nfeatures = store.get_features('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.\nbatch = store.batch_get_features(\n  %w[u0001 u0002 u0003],\n  %w[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": "ruby",
      "code": "def bulk_load(rows, ttl_seconds: nil)\n  return 0 if rows.empty?\n  ttl = ttl_seconds || @batch_ttl_seconds\n  @redis.pipelined do |pipe|\n    rows.each do |entity_id, fields|\n      key = key_for(entity_id)\n      encoded = fields.transform_values { |v| encode_value(v) }\n      pipe.hset(key, encoded)\n      pipe.expire(key, ttl)\n    end\n  end\n  ...\nend",
      "section_id": "bulk-loading-batch-features"
    },
    {
      "id": "streaming-writes-with-per-field-ttl-ex0",
      "language": "ruby",
      "code": "def update_streaming(entity_id, fields, ttl_seconds: nil)\n  return if fields.empty?\n  ttl = ttl_seconds || @streaming_ttl_seconds\n  key = key_for(entity_id)\n  encoded = fields.transform_values { |v| encode_value(v) }\n  names = encoded.keys\n\n  results = @redis.pipelined do |pipe|\n    pipe.hset(key, encoded)\n    pipe.call('HEXPIRE', key, ttl, 'FIELDS', names.size, *names)\n  end\n  codes = results[1] || []\n  codes.each do |code|\n    unless code == 1\n      raise \"HEXPIRE did not set every field TTL for #{key}: #{codes.inspect}\"\n    end\n  end\n  ...\nend",
      "section_id": "streaming-writes-with-per-field-ttl"
    },
    {
      "id": "inference-reads-with-hmget-ex0",
      "language": "ruby",
      "code": "def get_features(entity_id, field_names = nil)\n  key = key_for(entity_id)\n  if field_names.nil?\n    return @redis.hgetall(key)\n  end\n  return {} if field_names.empty?\n  values = @redis.hmget(key, *field_names)\n  out = {}\n  field_names.each_with_index do |n, i|\n    out[n] = values[i] unless values[i].nil?\n  end\n  out\nend",
      "section_id": "inference-reads-with-hmget"
    },
    {
      "id": "batch-scoring-with-pipelined-hmget-ex0",
      "language": "ruby",
      "code": "def batch_get_features(entity_ids, field_names)\n  return {} if entity_ids.empty? || field_names.empty?\n  rows = @redis.pipelined do |pipe|\n    entity_ids.each { |id| pipe.hmget(key_for(id), *field_names) }\n  end\n  out = {}\n  entity_ids.each_with_index do |id, i|\n    values = rows[i] || []\n    row = {}\n    field_names.each_with_index do |n, j|\n      row[n] = values[j] unless values[j].nil?\n    end\n    out[id] = row\n  end\n  out\nend",
      "section_id": "batch-scoring-with-pipelined-hmget"
    },
    {
      "id": "the-streaming-worker-ex0",
      "language": "ruby",
      "code": "def run\n  until @stop\n    sleep(@tick)\n    break if @stop\n    # Set tick_in_flight *before* the pause check so a concurrent\n    # pause + wait_for_idle can never observe tick_in_flight=false\n    # in the window between the pause check and the actual tick call.\n    @tick_in_flight = true\n    begin\n      do_tick unless @paused\n    rescue => e\n      warn \"[streaming-worker] tick failed: #{e.class}: #{e.message}\"\n    ensure\n      @tick_in_flight = false\n    end\n  end\nensure\n  # Clear running and tick_in_flight no matter how the thread exits\n  # so a later start can spin a fresh thread.\n  @running = false\n  @tick_in_flight = false\nend",
      "section_id": "the-streaming-worker"
    },
    {
      "id": "the-batch-builder-ex0",
      "language": "bash",
      "code": "bundle exec ruby build_features.rb --count 500 --ttl-seconds 3600",
      "section_id": "the-batch-builder"
    },
    {
      "id": "get-the-source-files-ex0",
      "language": "bash",
      "code": "git clone https://github.com/redis/docs.git\ncd docs/content/develop/use-cases/feature-store/ruby\nbundle install",
      "section_id": "get-the-source-files"
    },
    {
      "id": "start-the-demo-server-ex0",
      "language": "bash",
      "code": "bundle exec ruby demo_server.rb",
      "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:8093\nUsing Redis at redis://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"
    }
  ]
}
