Redis streams learnings

Just a guy who loves to write code and watch anime.
Search for a command to run...

Just a guy who loves to write code and watch anime.
No comments yet. Be the first to comment.
Qualities that make outstanding builders.

Bipedal vs quadrupedal: how many legs This is about the number of legs the creature walks on. Bipedal. Two legs. Humans, ostriches, T-rex, kangaroos, most fantasy humanoids. Quadrupedal. Four legs. Do

Intro You hear "lerp" and "smoothstep" everywhere in game dev. They sound like math jargon. They're not. Both are small tools that do the same job: smoothly move from one value to another. The problem

What is Kinematics Kinematics is the math field. It's the study of how things move without worrying about forces (which would be dynamics). FK and IK are the two branches: forward kinematics and inver

Intro Textures are usually the biggest cost in a 3D scene. Memory, bandwidth, and load time all get eaten by them. Resizing them is the obvious lever. There's more. This post is about the less obvious

Redis Streams are append-only logs that maintain ordered message sequences. Each stream uses a unique identifier for isolation between users or sessions.
Core operations: XADD appends messages, XREAD consumes them sequentially, XRANGE reads historical data. Blocking reads wait for new data with timeout support.
Deltas are incremental message fragments that arrive piece by piece. Instead of resending complete messages, deltas contain only new or changed portions, reducing bandwidth and memory usage.
Delta structure includes ID, content fragment, timestamp, and metadata. Multiple producers write concurrently while Redis maintains message ordering.
Processing flow: generate deltas → push to Redis streams via XADD → consume sequentially.
Go routines handle stream processing independently of HTTP request lifecycles. Streaming continues even if the original request terminates.
Consumer pattern uses infinite loop: read from stream, process messages, sleep 100ms if empty. This balances responsiveness with resource efficiency.
for {
msgs := redis.XRead(stream, lastID)
if len(msgs) == 0 {
time.Sleep(100 * time.Millisecond)
continue
}
processDelta(msgs)
updateLastID(msgs)
}
Channels enable safe communication between goroutines for producer-consumer patterns.
TTL automatically expires inactive streams to prevent memory leaks. Monitor activity and extend TTL for active streams while letting inactive ones expire.
For high-volume scenarios, use XTRIM to limit stream length or implement retention policies.
Server-Sent Events work with Redis streams for real-time updates. Server maintains long-lived HTTP connections and pushes deltas as they arrive.
func streamHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
for {
deltas := readFromRedisStream()
for _, delta := range deltas {
fmt.Fprintf(w, "data: %s\n\n", delta)
w.(http.Flusher).Flush()
}
}
}
Browser EventSource API provides automatic reconnection handling.
Use connection pooling and automatic reconnection for Redis reliability. Circuit breaker patterns handle Redis unavailability.
Consumer groups provide at-least-once delivery with acknowledgments. Failed processing triggers retries or dead letter queues.
Monitor: stream length, consumer lag, processing time. Use XINFO STREAM for statistics and slow log analysis for debugging.