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

Redis ในฐานะบริการประสานงาน

ออกแบบโซลูชันที่ใช้ Redis สำหรับค้นหาบริการ จัดการการกำหนดค่า และสื่อสารระหว่างบริการ

บทเรียน 3 จาก 411 ขั้นตอน

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

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Coordination in Distributed Systems

In distributed systems, multiple services work together to achieve a common goal. For these services to function smoothly, they often need to find each other, share configuration, and communicate in an organized way.

This 'orchestration' is called service coordination. Without it, services might struggle to locate their dependencies, use outdated settings, or fail to process tasks efficiently.

Redis's Role in Coordination

Redis, with its speed, atomic operations, and versatile data structures, is an excellent choice for a coordination service.

  • Atomic Operations: Ensures operations are completed entirely or not at all, crucial for consistency.
  • Data Structures: Hashes, Lists, and Sets provide flexible ways to store and manage coordination data.
  • Pub/Sub: Enables real-time notification for events like configuration changes.

These features allow Redis to act as a central hub for various coordination patterns.

Understanding Service Discovery

Service discovery is how applications and microservices locate and communicate with each other on a network. In dynamic environments (like cloud deployments), service instances constantly scale up and down, and their network locations (IPs, ports) can change.

A service discovery mechanism allows services to register their presence and clients to look them up by name, rather than hardcoding addresses.

Registering Services with Redis

We can use a Redis Hash to store information about active service instances. The hash key could be 'services:<serviceName>', and fields would be '<instanceId>' mapping to '<IP:Port>'.

Try running this example to register a service instance:

import redis.clients.jedis.Jedis;

public class ServiceRegistry {
  public static void main(String[] args) {
    Jedis jedis = new Jedis("localhost"); // Connect to Redis
    String serviceName = "paymentService";
    String instanceId = "paymentsvc-001";
    String instanceAddress = "192.168.1.10:8080";

    // Register service instance
    jedis.hset("services:" + serviceName, instanceId, instanceAddress);
    System.out.println("Registered " + serviceName + " instance: " + instanceAddress);

    jedis.close();
  }
}

Discovering Active Services

Once services are registered, clients or other services can query Redis to find available instances. The HGETALL command retrieves all fields and values from a hash, giving us a list of all active instances for a given service.

Run this code to discover the registered service:

import redis.clients.jedis.Jedis;
import java.util.Map;

public class ServiceDiscovery {
  public static void main(String[] args) {
    Jedis jedis = new Jedis("localhost");
    String serviceName = "paymentService";

    // Discover all instances for a service
    Map<String, String> instances = jedis.hgetAll("services:" + serviceName);

    if (instances.isEmpty()) {
      System.out.println("No instances found for " + serviceName);
    } else {
      System.out.println("Active " + serviceName + " instances:");
      for (Map.Entry<String, String> entry : instances.entrySet()) {
        System.out.println("  ID: " + entry.getKey() + ", Address: " + entry.getValue());
      }
    }
    jedis.close();
  }
}

Centralized Configuration Management

Another crucial coordination task is managing application configurations. Instead of hardcoding settings or using local files, centralized configuration management stores configurations in a single, accessible location.

This allows for dynamic updates, consistent settings across all service instances, and avoids redeployments for simple configuration changes.

Storing Configs in Redis

Redis Hashes are well-suited for storing structured application configurations. Each hash can represent the configuration for a specific application or module, with fields being individual settings.

Here's an example of setting and retrieving configuration for an application:

import redis.clients.jedis.Jedis;
import java.util.Map;

public class ConfigManager {
  public static void main(String[] args) {
    Jedis jedis = new Jedis("localhost");
    String appConfigKey = "app:myApp:config";

    // Set configuration properties
    jedis.hset(appConfigKey, "dbHost", "my-db.example.com");
    jedis.hset(appConfigKey, "dbPort", "5432");
    jedis.hset(appConfigKey, "logLevel", "INFO");
    System.out.println("Configuration updated for myApp.");

    // Retrieve all configuration
    Map<String, String> config = jedis.hgetAll(appConfigKey);
    System.out.println("Current myApp configuration:");
    for (Map.Entry<String, String> entry : config.entrySet()) {
      System.out.println("  " + entry.getKey() + ": " + entry.getValue());
    }
    jedis.close();
  }
}

Distributing Config Updates

For dynamic configuration, services need a way to be notified when settings change. While polling Redis periodically is an option, using Redis's Pub/Sub mechanism is more efficient.

When a configuration is updated, the configuration service can publish a message to a specific channel (e.g., 'config:updates'). All subscribed services would then receive this notification and could fetch the latest configuration.

Task Queues for Inter-Service Work

Redis Lists can serve as simple yet powerful task queues, allowing services to coordinate by distributing work. One service pushes tasks onto a list (LPUSH or RPUSH), and another service pulls tasks from it (RPOP or LPOP).

Using blocking pop operations like BRPOP or BLPOP, workers can wait for tasks without busy-looping, making it highly efficient.

import redis.clients.jedis.Jedis;
import java.util.List;

public class TaskConsumer {
  public static void main(String[] args) {
    Jedis jedis = new Jedis("localhost");
    String taskQueueKey = "tasks:processing";

    System.out.println("Worker started, waiting for tasks...");

    // Simulate pushing a task for the demo to ensure something is there
    jedis.lpush(taskQueueKey, "process_order_123");

    // Blockingly pop a task from the right of the list
    // 0 means wait indefinitely until a task is available
    List<String> result = jedis.brpop(0, taskQueueKey);
    if (result != null && result.size() > 1) {
      String queueName = result.get(0); // The key from which the element was popped
      String task = result.get(1);     // The popped element
      System.out.println("Received task '" + task + "' from queue '" + queueName + "'");
      // Simulate processing
      try { Thread.sleep(1000); } catch (InterruptedException e) {}
      System.out.println("Task '" + task + "' processed.");
    }
    jedis.close();
  }
}

Quick Check: Coordination Patterns

Which of the following Redis features or commands are suitable for implementing service coordination patterns in a distributed system?

Lesson Summary

In this lesson, we explored how Redis can act as a powerful coordination service for distributed systems. We covered:

  • Using Redis Hashes for dynamic service discovery, allowing services to register and be found.
  • Leveraging Redis Hashes and Strings for centralized configuration management.
  • Employing Redis Pub/Sub to facilitate dynamic configuration updates.
  • Building task queues with Redis Lists (LPUSH/BRPOP) for inter-service work distribution.

By using Redis for these patterns, you can build more resilient, scalable, and manageable distributed applications.

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

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

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

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

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

บทเรียน “Redis ในฐานะบริการประสานงาน” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “Redis ในฐานะบริการประสานงาน”

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

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

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

บทเรียน “Redis ในฐานะบริการประสานงาน” ใช้เวลานานแค่ไหน

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

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

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

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

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