🦉
Programming Notes
  • My Programming Notes
  • CKA Exam Preparation
    • Certified Kubernetes Administrator
    • Setup Minikube
    • Network Design Principles
    • Role-Based Access Control (RBAC)
    • Namespace
    • Resource Quota
    • Pod
    • Deployment
    • Deployment: Rollout
    • ConfigMap
    • Service
    • Service: kubectl expose
    • Pod: Resources Management
    • Pod & Container: Quality of Service Class
    • Pod & Container: Probes
    • Limit Range
    • Scaling: Manual
    • Scaling: Horizontal Pod Autoscaler
    • Persistent Volume & Claim
    • Secret
    • Ingress: Routing
    • Ingress: TLS
    • Ingress: Rate Limit
    • Ingress: Basic Auth
    • Ingress: CRD (Custom Resource Definition)
    • Job
    • CronJob
    • Mutli-Node Cluster
  • Golang
    • Generics
    • Context
    • Goroutines and Channels in Go
    • Goroutine: Concurrency vs Parallelism
    • Goroutine: Performance & Tradeoffs
    • JSON: omitzero
  • Rust
    • Arrays & Slices
    • Closures
    • Generics & Traits
    • Iterators
    • Run Code Simultaneously
    • String vs &str
    • Tests
    • Rustlings Exercises
      • Variables
      • Functions
      • If
      • Primitive Types
      • Vectors
      • Move Semantics
      • Structs
      • Enums and Matching Pattern
      • Strings
      • Modules
      • Hashmaps
      • Options
      • Error Handling
      • Generics
      • Traits
      • Lifetimes
      • Tests
      • Iterators
      • Smart Pointers
      • Threads
      • Macros
      • Quiz 1
      • Quiz 2
      • Quiz 3
  • Software Engineering
    • CAP Theorem
    • Circuit Breaker
    • Decoupling
    • GraphQL: Query Caching
    • HMAC Signature
    • Idempotency
    • Monolith VS Microservice
    • OWASP Top 10 2021
    • PCI DSS
    • PostgreSQL: Partitioning
    • PostgreSQL: Replication
    • Protobuf & gRPC
    • Redis: Streams
    • Resource Scaling
    • Signed URL
    • SOLID
    • Stack VS Heap
    • Stateful VS Stateless
  • Site Reliability Engineering
    • Chaos Engineering
    • Distributed Tracing
    • Kubernetes (k8s)
    • SLA, SLO, and SLI Metrics
    • Site Reliability Engineer
  • Others
    • FFMPEG Cheat sheet
Powered by GitBook
On this page
  • Basic Example
  • Setup
  • XADD
  • XREAD
  • References
  1. Software Engineering

Redis: Streams

A Redis stream is a data structure that acts like an append-only log but also implements several operations to overcome some of the limits of a typical append-only log. We can use streams to record and simultaneously syndicate events in real time.

  • Redis generates a unique ID for each stream entry.

  • Streams keep messages in Redis for as long as you want (configurable).

  • Consumers can read at their own pace, even if they were offline.

  • We can acknowledge messages (XACK) and track delivery.

Similar like pub/sub we need A producer and consumers to implement redis streams in our system.

Basic Example

Lets create a basic example of redis streams using XADD and XREAD command.

Setup

To run redis server you can install it directly in your machine or using virtualization. For me I prefer to use docker and here is how to do it.

Run a detached container with name my-redis using latest redis image and expose the port 6379.

docker run -d -p 6379:6379 --name my-redis redis

Run redis-cli inside the newly created redis container.

➜ redis_streams docker exec -it my-redis redis-cli         
127.0.0.1:6379> 

XADD

This is the simplest form of adding a message to a Redis Stream. Each message will have a unique ID and some data fields.

127.0.0.1:6379> XADD mystream * message Hello
"1740039025432-0"
  • mystream: name of the stream, redis will create the stream if not exist.

  • *: Stream ID, will auto-generate a unique ID if the ID argument specified is the *.

  • message Hello: Key value pair data, message is the key and value is Hello.

XREAD

Read data from one or multiple streams. This command has an option to block if items are not available.

127.0.0.1:6379> XREAD COUNT 1 STREAMS mystream 0
1) 1) "mystream"
   2) 1) 1) "1740039025432-0"
         2) 1) "message"
            2) "Hello"
  • COUNT 1: Return maximum 1 element for each stream.

  • STREAMS mystream 0:

    • mystream: Name of the stream.

    • 0: Read element from the start.

References

PreviousProtobuf & gRPCNextResource Scaling

Last updated 3 months ago

https://redis.io/docs/latest/develop/data-types/streams/
https://www.docker.com/blog/how-to-use-the-redis-docker-official-image/