Strings
Strings are Redis's simplest and most-used type — binary-safe blobs up to 512MB.
Introduction
Strings are Redis's simplest and most-used type — binary-safe blobs up to 512MB. They power API caches, atomic counters, distributed locks (SET NX EX), and feature flags.
Under the hood Redis optimizes small strings with embstr (embedded string) and integers with a dedicated int encoding — no heap allocation for SET counter 42.
Master SET options (EX, NX, XX) and you cover 80% of cache and coordination patterns in production microservices.
Understanding the topic
Key concepts
- SET key value — create or overwrite; GET retrieves.
- INCR/DECR — atomic integer math without read-modify-write races.
- SET NX EX — compare-and-set with TTL; foundation of locks.
- MSET/MGET — batch multiple keys in one round trip.
- APPEND and GETRANGE — treat string as mutable buffer.
- Embstr for ≤44 bytes; raw SDS for larger values.
Step-by-step explanation
- SET stores SDS (Simple Dynamic String) or int encoding.
- INCR parses value as integer; errors if not numeric.
- TTL attached via EX/PX/EXAT on SET or separate EXPIRE.
- GET returns bulk string or nil if missing.
- Memory reclaimed on DEL/UNLINK or expiry.
Syntax reference
Common commands
- EX on SET — always TTL cache keys.
- SET NX EX — lock acquire pattern.
- MGET — batch reads without pipeline setup.
SET product:42 "{"sku":"X"}" EX 3600GET product:42INCR pageviews:homeSET lock:order:99 token NX EX 30MGET product:1 product:2 product:3
Informative example
Cache-aside product lookup with StringRedisTemplate:
@Servicepublic class ProductCache {private final StringRedisTemplate redis;public ProductCache(StringRedisTemplate redis) {this.redis = redis;}public Optional<Product> get(String sku) {String key = "product:" + sku;String json = redis.opsForValue().get(key);if (json != null) return Optional.of(parse(json));return db.findBySku(sku).map(p -> {redis.opsForValue().set(key, serialize(p), Duration.ofHours(1));return p;});}}
Serialize once; set with Duration TTL. On update, delete key or write-through.
Real-world use
Real-world use cases
- API response cache — JSON string keyed by request hash.
- Page view counter — INCR with daily key suffix.
- Distributed lock token — SET NX EX.
- OAuth state parameter — short TTL string.
- Config snapshot — versioned string for feature rollout.
Best practices
- Always EX on cache strings.
- Keep JSON payloads small; compress if >10KB.
- Use MGET for batch product lookups.
- Prefix keys: catalog:product:sku.
- Invalidate on write — don't rely on TTL alone for strong consistency needs.
- Use StringRedisTemplate for plain text/JSON.
Common mistakes
- INCR on non-integer string — command error.
- Giant JSON blobs blocking event loop.
- Cache without TTL — memory leak.
- SET NX lock without unique token — unsafe unlock.
Advanced interview questions
Q1BeginnerMax string size in Redis?
Q2BeginnerWhat does INCR do?
Q3IntermediateSET NX vs XX?
Q4IntermediateString vs hash for user object?
Q5AdvancedImplement idempotent lock with strings?
Summary
Strings = cache blobs, counters, locks.