스트림 데이터 구조 및 명령
항목을 추가하고(`XADD`), 스트림에서 읽고(`XREAD`), 스트림 길이를 관리하는 방법을 배웁니다.
스트림 데이터 구조 및 명령은(는) CoddyKit의 무료 Redis Caching & Messaging (Pub/Sub, Streams) 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Redis Caching & Messaging (Pub/Sub, Streams) 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Redis Caching & Messaging (Pub/Sub, Streams) 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Welcome to Stream Data
In the previous lesson, we learned what Redis Streams are. Now, let's dive into their core structure and how we interact with them.
Redis Streams are powerful, append-only data structures that act like a log, storing a sequence of entries. Each entry has a unique ID and a set of field-value pairs.
Stream Entry Anatomy
Each entry in a Redis Stream is like a small record. It consists of:
- Entry ID: A unique identifier, typically a timestamp and a sequence number (e.g.,
1678886400000-0). - Field-Value Pairs: One or more key-value pairs representing the actual data of the entry (e.g.,
sensor-id: 123,temperature: 25.5).
Think of it like a mini-hash map stored chronologically within the stream.
Adding Entries with XADD
To add a new entry to a Redis Stream, we use the XADD command. This command appends a new entry to the end of the stream, generating a unique ID.
The basic syntax is:
XADD key ID field value [field value ...]For the ID argument, you'll typically use * to let Redis automatically assign a unique ID based on the current time.
XADD Command Example
Let's add some sensor data to a stream named sensor_readings. We'll use * to let Redis automatically assign a unique ID.
XADD sensor_readings * sensor-id 101 temperature 23.5 humidity 60
XADD sensor_readings * sensor-id 102 temperature 24.1 humidity 62Reading from Streams: XREAD
Once data is in a stream, you'll want to read it. The XREAD command is used for this purpose. It allows you to read entries from one or more streams, starting from a specific ID.
A common way to read from the very beginning of a stream is to use 0-0 as the starting ID for that stream.
Basic XREAD Operations
Let's read the entries we just added. We can specify COUNT to limit the number of entries returned. The $ ID means 'read only new entries added since the last time I read' or 'from now' if it's the first read. It's great for tailing a stream!
XREAD COUNT 2 STREAMS sensor_readings 0-0
XREAD STREAMS sensor_readings $Managing Stream Length: MAXLEN
Streams can grow infinitely, consuming a lot of memory. To prevent this, Redis allows you to limit the maximum length of a stream using the MAXLEN option with XADD.
When the stream exceeds the specified length, older entries are automatically evicted from the head of the stream.
XADD with MAXLEN Example
We can add entries and ensure our sensor_readings stream never exceeds a certain number of entries. Using ~ before the count makes the trimming approximate, which is faster and often sufficient.
XADD sensor_readings MAXLEN ~ 3 * sensor-id 103 temperature 24.8 humidity 61
XADD sensor_readings MAXLEN ~ 3 * sensor-id 104 temperature 25.2 humidity 63
XADD sensor_readings MAXLEN ~ 3 * sensor-id 105 temperature 25.0 humidity 60
XLEN sensor_readingsQuick Check: Stream Commands
You've learned about adding and reading stream entries, as well as managing stream length. Let's test your understanding.
Recap: Stream Basics
Great job! In this lesson, you mastered the fundamental commands for interacting with Redis Streams:
XADD: To append new entries with unique IDs and field-value pairs.XREAD: To retrieve entries from streams, starting from a specific ID or reading new entries with$.MAXLEN: To control stream size duringXADDoperations, preventing unbounded memory growth.
Next, we'll explore how Streams provide message persistence and ordered delivery, differentiating them from Pub/Sub.
자주 묻는 질문
“스트림 데이터 구조 및 명령” 강의는 무료인가요?
네 — “스트림 데이터 구조 및 명령” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Redis Caching & Messaging (Pub/Sub, Streams) 강의 전체를 잠금 해제할 수 있습니다. Redis Caching & Messaging (Pub/Sub, Streams) 강의에는 총 4개의 강의가 포함되어 있습니다.
“스트림 데이터 구조 및 명령”에서 뭘 배우나요?
항목을 추가하고(`XADD`), 스트림에서 읽고(`XREAD`), 스트림 길이를 관리하는 방법을 배웁니다. 브라우저에서 직접 실행하는 실습 코드로 Redis Caching & Messaging (Pub/Sub, Streams)을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Redis Caching & Messaging (Pub/Sub, Streams)을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Redis Caching & Messaging (Pub/Sub, Streams)은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.
“스트림 데이터 구조 및 명령” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Redis Caching & Messaging (Pub/Sub, Streams) 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Redis Caching & Messaging (Pub/Sub, Streams) 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- Redis Streams란 무엇인가요?
- 스트림 데이터 구조 및 명령
- 스트림 메시지 영속성
- 스트림 길이 제한 및 정리