Redis Streams란 무엇인가요?
강력하고 영속적인 메시지 큐인 Redis Streams의 아키텍처와 이점을 이해합니다.
Redis Streams란 무엇인가요?은(는) CoddyKit의 무료 Redis Caching & Messaging (Pub/Sub, Streams) 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Redis Caching & Messaging (Pub/Sub, Streams) 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Redis Caching & Messaging (Pub/Sub, Streams) 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
What are Redis Streams?
Welcome! In this lesson, we'll dive into Redis Streams, a powerful data structure for managing real-time event data.
Think of a stream as an append-only log of messages, similar to a traditional message queue or a Kafka topic, but built right into Redis.
Streams are ideal for building event-driven architectures, processing sensor data, or tracking user activity.
Streams vs. Pub/Sub
Redis already has Pub/Sub, so why Streams?
- Persistence: Stream messages are stored and not lost if consumers are offline. Pub/Sub messages are fire-and-forget.
- Consumer Groups: Streams support consumer groups for cooperative message processing, allowing multiple applications to read the same stream without duplicating work.
- Message History: You can read messages from any point in the stream's history.
Streams offer durability and advanced consumption patterns that Pub/Sub doesn't.
Stream Architecture: Entries & IDs
A Redis Stream is an ordered sequence of entries. Each entry is composed of:
- A unique Stream ID: This is a timestamp-based ID (e.g.,
1678886400000-0) that ensures messages are strictly ordered. - One or more field-value pairs: These are the actual data for the message, similar to a small Redis Hash.
Think of it as a log where each line has a unique timestamp and some key-value data.
Adding Entries with XADD
Producers add new entries to a stream using the XADD command. Redis automatically generates a unique ID for each new entry, or you can provide one.
The * wildcard tells Redis to generate a new, unique ID for the entry.
Try running this example:
public class Main {
public static void main(String[] args) {
System.out.println("Simulating Redis XADD command:");
String streamName = "sensor_data";
// XADD sensor_data * temp 25 hum 60
System.out.println("redisClient.xadd(");
System.out.println(" \"" + streamName + "\", \"*\",");
System.out.println(" \"temp\", \"25\", \"hum\", \"60\"");
System.out.println(");");
System.out.println("\nOutput (example ID): 1678886400000-0");
}
}Understanding Stream IDs
Stream IDs have two parts: <milliseconds-timestamp>-<sequence-number>.
- The timestamp is the time the entry was added (in Unix milliseconds).
- The sequence number is incremented if multiple entries are added within the same millisecond.
This structure guarantees that IDs are always increasing, providing a natural order for messages. You can use 0-0 to refer to the very beginning of a stream.
Reading from a Stream with XREAD
Consumers use the XREAD command to retrieve entries from one or more streams. You specify the stream name and the ID from which to start reading.
XREAD allows you to fetch a batch of messages, making it efficient for consumers.
Try running this example:
public class Main {
public static void main(String[] args) {
System.out.println("Simulating Redis XREAD command:");
String streamName = "sensor_data";
String startId = "0-0"; // Read from the beginning
// XREAD STREAMS sensor_data 0-0
System.out.println("List<Map<String, String>> entries =");
System.out.println(" redisClient.xread("STREAMS",");
System.out.println(" \"" + streamName + "\", \"" + startId + "\"");
System.out.println(");");
System.out.println("\nExample Output:");
System.out.println("ID: 1678886400000-0, {temp=25, hum=60}");
System.out.println("ID: 1678886400001-0, {temp=26, hum=61}");
}
}Blocking Reads for Real-time
To read new messages as they arrive, you can use the BLOCK option with XREAD. This makes the command wait for new data if no new entries are available.
You can also specify a timeout for the blocking operation. This is crucial for building real-time applications that react instantly to events.
Example: XREAD BLOCK 0 STREAMS mystream $ ($ means 'last ID in stream').
Stream Persistence & History
Unlike Pub/Sub, Redis Streams are persistent. Messages added to a stream remain there until explicitly removed or trimmed.
This allows new consumers to read the entire history of events from the beginning, or catch up from a specific point if they were temporarily offline.
This persistence is a key feature for reliable event logging and replay capabilities.
Ordered & Immutable Entries
Redis Streams guarantee that entries are always added in the order they are received and assigned strictly incrementing IDs.
Once an entry is added to a stream, it cannot be modified. This immutability is fundamental for maintaining an accurate, auditable log of events.
These properties make Streams excellent for event sourcing and ensuring data consistency.
Check Your Understanding
Which of the following are key benefits or characteristics of Redis Streams?
Recap: What are Redis Streams?
In this lesson, we learned that Redis Streams are a powerful, persistent, append-only log data structure.
- They offer persistence, ordered delivery, and support for consumer groups, differentiating them from Pub/Sub.
- We explored how to add entries with
XADDand read them withXREAD. - Streams are ideal for event sourcing, real-time data processing, and building reliable message queues.
Next, we'll dive deeper into Stream commands and features!
자주 묻는 질문
“Redis Streams란 무엇인가요?” 강의는 무료인가요?
네 — “Redis Streams란 무엇인가요?” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Redis Caching & Messaging (Pub/Sub, Streams) 강의 전체를 잠금 해제할 수 있습니다. Redis Caching & Messaging (Pub/Sub, Streams) 강의에는 총 4개의 강의가 포함되어 있습니다.
“Redis Streams란 무엇인가요?”에서 뭘 배우나요?
강력하고 영속적인 메시지 큐인 Redis Streams의 아키텍처와 이점을 이해합니다. 브라우저에서 직접 실행하는 실습 코드로 Redis Caching & Messaging (Pub/Sub, Streams)을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Redis Caching & Messaging (Pub/Sub, Streams)을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Redis Caching & Messaging (Pub/Sub, Streams)은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.
“Redis Streams란 무엇인가요?” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Redis Caching & Messaging (Pub/Sub, Streams) 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Redis Caching & Messaging (Pub/Sub, Streams) 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- Redis Streams란 무엇인가요?
- 스트림 데이터 구조 및 명령
- 스트림 메시지 영속성
- 스트림 길이 제한 및 정리