Back to Go Redis

HIMPORT example

example/himport/README.md

9.22.02.3 KB
Original Source

HIMPORT example

Fast hash ingestion with the HIMPORT command family (hinted hash templates, Redis 8.10+).

Experimental. The HIMPORT commands and the typed go-redis API around them are experimental and may change in a future release.

HIMPORT PREPARE registers an ordered list of field names (a fieldset) inside a connection's session; HIMPORT SET then creates hashes by sending only the values, paired positionally with the prepared fields. The server stores such hashes in a memory-efficient template encoding where the shared field names are kept only once.

Fieldsets are scoped to a single physical connection on the server. go-redis hides that from pooled callers: fieldsets registered with HImportPrepare are remembered client-side and the PREPARE is replayed lazily — at most once per connection session — on whichever pooled connection executes an HImportSet, including inside pipelines and transactions. A connection that lost its session state (RESET, reconnect) is re-prepared transparently.

These guarantees apply only to the typed methods (HImportPrepare, HImportSet, HImportDiscard, HImportDiscardAll). Any HIMPORT command sent through generic command interfaces — Do, raw Cmd values, or similar low-level access — bypasses the client-side registry entirely: nothing is replayed onto other connections, no such fieldset is not recovered, and discards do not propagate. Such commands act on the session of whichever connection happens to execute them, so they are only meaningful on a dedicated connection (client.Conn()) managed explicitly by the caller.

The example walks through:

  • basic prepare/set and reading the result back with regular hash commands
  • bulk ingestion through pipelines, timed against plain HSET pipelines
  • 100 concurrent goroutines writing through the pool with no explicit connection management
  • the observable server side: OBJECT ENCODING template encodings and the INFO memory template counters
  • discarding a fieldset and the resulting server error for further sets

Run

Requires a Redis 8.10+ server built with the hinted hash templates feature (the example exits with a message otherwise). The address defaults to localhost:6379 and can be overridden with REDIS_ADDR:

sh
go run .
# or
REDIS_ADDR=localhost:6390 go run .