Redis Caching & Messaging (Pub/Sub, Streams) · บทเรียน

โครงสร้างข้อมูลและคำสั่งของสตรีม

เรียนรู้การเพิ่มรายการ (`XADD`) การอ่านจากสตรีม (`XREAD`) และการจัดการความยาวของสตรีม

บทเรียน 2 จาก 410 ขั้นตอน

โครงสร้างข้อมูลและคำสั่งของสตรีม เป็นบทเรียน Redis Caching & Messaging (Pub/Sub, Streams) ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 62

Reading 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_readings

Quick 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 during XADD operations, preventing unbounded memory growth.

Next, we'll explore how Streams provide message persistence and ordered delivery, differentiating them from Pub/Sub.

เริ่มต้นได้ฟรี

เรียนรู้ Redis Caching & Messaging (Pub/Sub, Streams) ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “โครงสร้างข้อมูลและคำสั่งของสตรีม” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “โครงสร้างข้อมูลและคำสั่งของสตรีม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Redis Caching & Messaging (Pub/Sub, Streams) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Redis Caching & Messaging (Pub/Sub, Streams) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “โครงสร้างข้อมูลและคำสั่งของสตรีม”

เรียนรู้การเพิ่มรายการ (`XADD`) การอ่านจากสตรีม (`XREAD`) และการจัดการความยาวของสตรีม คุณปฏิบัติ Redis Caching & Messaging (Pub/Sub, Streams) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Redis Caching & Messaging (Pub/Sub, Streams) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Redis Caching & Messaging (Pub/Sub, Streams) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “โครงสร้างข้อมูลและคำสั่งของสตรีม” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Redis Caching & Messaging (Pub/Sub, Streams) นี้ได้ไหม

ได้ บทเรียน Redis Caching & Messaging (Pub/Sub, Streams) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. Redis Streams คืออะไร
  2. โครงสร้างข้อมูลและคำสั่งของสตรีม
  3. การคงอยู่ของข้อความในสตรีม
  4. การจำกัดและตัดทอนสตรีม
← กลับไปที่ Redis Caching & Messaging (Pub/Sub, Streams)