Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Redis hashes let a Java application keep related field-value pairs under one Redis key. The three most useful patterns are storing object-like records and reading only needed fields, maintaining atomic integer counters, and grouping session state with a single expiration policy. The examples below use Lettuce-style synchronous commands; adapt connection setup and error handling to your client version.

How a Redis hash maps to Java data

A hash is a Redis key containing named fields. HSET creates or updates fields, HGET reads one field, and HMGET reads selected fields. HGETALL returns every field, but Redis classifies it as a slow command, so use it only when the caller genuinely needs the complete record. See the Redis hashes documentation.

Need Redis command Typical Java use
Write or update fields HSET key field value Save changed properties
Read one property HGET key field Load a single value
Read a subset HMGET key field... Build a small response
Read the complete hash HGETALL key Load all state when necessary
Increment an integer field HINCRBY key field amount Update counters atomically

Redis stores hash values as strings at the protocol level. Convert them to Java types at your application boundary, and define what a missing field means before mapping the result.

1. Store an object-like record and fetch selected fields

Use one hash for a compact record whose properties are naturally grouped, such as a user profile, feature row, or session metadata. A key such as user:123 keeps the record together without serializing an entire Java object.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Write fields with Lettuce

Map<String, String> fields = Map.of(
    "name", "John",
    "surname", "Smith",
    "plan", "pro"
);

commands.hset("user:123", fields);

In this illustrative synchronous shape, commands is a Lettuce synchronous command interface. Redis’s Lettuce connection guide shows the same map-to-hash approach; deployed connections should use TLS and the security practices in that guide.

Read only what the endpoint needs

String name = commands.hget("user:123", "name");
List<KeyValue<String, String>> selected =
    commands.hmget("user:123", "name", "plan");

A profile endpoint that needs only a display name and plan should use HGET or HMGET, rather than transferring unrelated fields. Reserve HGETALL for code that needs every property:

Map<String, String> complete = commands.hgetall("user:123");

This pattern works best when fields are independently useful and modest in size. Keep naming conventions stable, validate missing values, and remember that updating one field does not replace the other fields in the hash.

2. Keep related integer counters in one hash

Hashes are a convenient namespace for counters belonging to one entity. For example, a bike can have rides, crashes, and owners fields under bike:1:stats.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Increment atomically with HINCRBY

commands.hincrby("bike:1:stats", "rides", 1);
commands.hincrby("bike:1:stats", "crashes", 1);

HINCRBY performs the increment in Redis, avoiding a client-side read/modify/write race. Redis documents the command as O(1); an absent field starts at zero, and the value must remain within a signed 64-bit integer range. See the HINCRBY command reference.

Read one or several totals

String rides = commands.hget("bike:1:stats", "rides");
List<KeyValue<String, String>> totals = commands.hmget(
    "bike:1:stats", "rides", "crashes", "owners");

The equivalent command flow is:

HINCRBY bike:1:stats rides 1
HMGET bike:1:stats rides crashes owners

Do not pass fractional amounts to HINCRBY. For decimal measurements, use a representation designed for decimals (for example, a scaled integer with an explicitly documented scale) or another Redis data type; do not assume this command will parse floating-point input.

3. Group session state and expire the whole key

A session is a natural hash: fields such as user ID, login time, cart identifier, and request count share one lifecycle. Redis’s Java session-store example uses this approach with Lettuce, including HSET, HGETALL, HINCRBY, EXPIRE, and DEL.

Create, refresh, and load a session

String key = "session:" + sessionId;

commands.hset(key, Map.of(
    "userId", userId,
    "createdAt", Long.toString(nowEpochSeconds),
    "lastSeenAt", Long.toString(nowEpochSeconds)
));
commands.expire(key, 1800);       // 30-minute whole-key lifetime

Map<String, String> session = commands.hgetall(key);
commands.hincrby(key, "requestCount", 1);

For a sliding session timeout, refresh the whole-key expiration after accepted activity:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
commands.hset(key, "lastSeenAt", Long.toString(nowEpochSeconds));
commands.expire(key, 1800);

On logout, remove the complete session with DEL. Treat internal fields such as timestamps and expiration metadata as reserved names so caller-supplied data cannot overwrite them.

Whole-key expiry versus field expiry

EXPIRE applies to the hash key, so all session fields disappear together; TTL reports the remaining key lifetime. If different fields need independent lifetimes, Redis’s feature-store example documents HEXPIRE and HTTL for Redis 7.4 and later. Those commands are a separate design from whole-key expiration, and your server and client must both support them. See Redis’s Java feature-store example.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choosing a Java Redis client

Choose the client that matches your application’s programming model and the Redis features you require, rather than assuming one client is universally faster.

Client Documented API style When it fits Trade-off
Lettuce Synchronous, asynchronous, and reactive APIs Applications that need non-blocking or reactive access, as well as synchronous commands More complex API; feature support can vary by version
Jedis Synchronous interface Applications that need a straightforward blocking API Does not provide the same async/reactive programming model

Redis’s Lettuce guide shows an example dependency version of 6.7.1.RELEASE, but it explicitly advises checking Maven Central for the current release. Redis’s client overview at Connect with Redis client API libraries describes the changing support matrix, so verify feature and server-version compatibility before pinning dependencies.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Practical safeguards

  • Use a key namespace such as user:, bike:, or session: consistently.
  • Read only required fields on latency-sensitive paths; avoid HGETALL for large or frequently changing hashes.
  • Use HINCRBY for integer counters instead of client-side arithmetic.
  • Document reserved fields in session and shared hashes.
  • Apply TLS and Redis security guidance for deployed connections.
  • Check the Redis server feature floor, especially before using per-field expiry introduced in the documented Redis 7.4+ examples.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.