Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Redis

Real-Time Code Collaboration ขับเคลื่อนด้วย Upstash Redis

บทนำ

เคยสงสัยไหมว่า platforms อย่าง Codeshare สร้างอย่างไร? เคยลองแก้ปัญหาการ scale service แบบนั้นพร้อมรักษา developer experience ที่ดีไหม? ตอนผมตัดสินใจสร้าง Bytecrowds ผมต้องเรียนรู้ทั้งหมดนี้ หลัง research อย่างลึกซึ้ง ตัดสินใจใช้ Yjs สำหรับ text-sharing capabilities มาถึงขั้นเลือก database ด้วย options เยอะมากบนตลาด ผมกรองรายการด้วย keyword สำคัญหนึ่ง: simplicity (ความเรียบง่าย)

ทำไมถึง Upstash?

แม้ก่อนค้นพบ Upstash ผมสนใจ Redis® มาก่อนเพราะ simple API แต่ใช้เป็น primary database มาพร้อม challenges เช่น scaling (อาจต้องการสำหรับ caching ด้วย) และ maintaining persistence พร้อมกัน ก่อน secure sponsorship ใดๆ คุณต้องระมัดระวัง budgeting open-source software มาก จะจ่าย computing power แม้ไม่ได้ใช้บ้างไหม? จัดการ traffic spike ได้ไหม และพร้อมใช้เวลาหลายชั่วโมงกับ infrastructure แทนที่จะ publish features ใหม่ ปรับปรุงเดิม หรือ fix bugs? ผมทำไม่ได้ จึงค้นพบ Upstash

Upstash Redis ตรง 3 เกณฑ์สำคัญของผม: ใช้ง่าย, scale ง่าย, และ ยืดหยุ่น รวมกับ low latency, out-of-the-box REST sdk สำหรับ serverless clients (ข้อกำหนด ของ Bytecrowds ด้วย), global replication และ pay-as-you-go plan สำหรับ computing power อีกมากมาย คุณจะเข้าใจว่าทำไมผมเลือกมันสำหรับโปรเจกต์

ใช้ Redis® ข้ามแอป

มาดูว่า Redis® ใช้เป็น main database ของโปรเจกต์ได้อย่างไร ตอบสนองความต้องการหลาย services เพื่อเข้าใจ context นี่คือ diagram ของ Bytecrowds flow:

Real-Time Code Collaboration ขับเคลื่อนด้วย Upstash Redis

ใช้ Ably sync text ข้าม clients และ custom analytics engine ที่เก็บข้อมูลจาก request IP address

Main Storage

Bytecrowd ใช้รูปแบบ Redis® hash ด้วย properties:

{
  text: string,
  language: string,
  authorizedEmails: undefined || string array
}

เราอัปเดต hash เมื่อ property ไหนเปลี่ยน ด้วย delay "x" วินาทีระหว่าง updates ใน production ตั้งอยู่ที่ 100 milliseconds

Authentication และ Authorization

Authentication

Bytecrowds ใช้ Next.js solid choice สำหรับ GitHub OAuth คือ Auth.js โชคดีที่เราใช้ Upstash adapter เก็บ sessions ได้ ทำให้ใช้ Redis® ข้าม app ทั้งหมด

Authorization

Bytecrowds จัดการ authorization โดยสร้าง field authorizedEmails ตามความต้องการ immutable จาก API หลังตั้งค่าแล้ว หนึ่งในสิ่งที่ผมชอบ Redis® คือ multitude of functions พร้อมใช้สำหรับ routine operations ที่ออกแบบมา algorithmically efficient ตัวอย่างเช่น เพื่อเช็คว่า authorizedEmails ถูกตั้งค่าแล้วหรือยัง ใช้คำสั่ง HEXISTS ซึ่งมี O(1) complexity

Analytics

Bytecrowds ใช้ analytics system จัดการ 2 categories ของข้อมูล:

  • day's stats เป็น Redis® hash ชื่อแบบ year month day properties:
{
  countries: string array,
  hits: integer,
  pages: string array,
  uniqueVisitors: integer,
  addresses: string array,
  continents: string array
}

ที่ addresses เป็น array ของ SHA256-encrypted IP addresses ใช้ determine unique website visitors

  • 3 keys ชื่อ continents, countries, และ pages ใช้ Redis® sorted sets เก็บข้อมูล โดยเก็บ stat ทุกตัวพร้อม count ("score" สำหรับ sorted set) เราสามารถ query หรือ sort ได้ง่ายด้วยคำสั่ง SORT หรือ SORT_RO ความเรียบง่ายของ Redis แสดงออกอีกครั้ง

สนใจโค้ดด้านหลังนี้? เช็ก https://github.com/Bytecrowds/analytics/blob/main/src/index.js

หมายเหตุ: ใน scenario ซับซ้อนกว่า คุณอาจ apply changes เช่น store raw data ก่อน process และใช้ stream processing tool อย่าง Apache Kafka (Upstash ดูแลได้เช่นกัน เช็ก docs)

ดู Live

อยากดู Bytecrowds ใน action? ไป https://www.bytecrowds.com คลิกปุ่ม "new bytecrowd" เปิดลิงก์ใน multiple tabs เริ่มเขียน จะเห็น code replicated ข้าม connected clients ทั้งหมด

จบ

เป็นความลับที่ไม่ได้เป็นความลับว่าเป็น sole active maintainer ของ open-source project ยาก แต่มี right tools และ auto-scaling infrastructure เป็น support ยอดเยี่ยม backing คุณให้ทำงานต่อบน awesome projects โดยไม่ต้องกังวล traffic spikes แล้ว convinced ให้ลอง Upstash Redis®? สมัครฟรี ไม่ต้อง credit card

ดูโค้ดเต็มได้ที่ GitHub repository หากมีคำถามหรือ feedback เกี่ยวกับบทความ ติดต่อผมที่ tudor.zgimbau@gmail.com