跳到主要内容

应用与设计模式

KeypointsArchitecturePatterns

News Feed

  • Use a fan-out / fan-in strategy for feed updates to enhance availability (1)
  • Read-Your-Writes Consistency (1, 2)
  • Maintain data replication across servers for consistency(1, 2, 3)
  • Architecture: Event-Driven
  • Structure: Microservices
  • Fan-Out / Fan-In Strategy:
    • Distribute a new post to all follower feeds (fan-out); aggregate feeds when users log in (fan-in)
  • Event Sourcing Pattern:
    • Store changes to the news feed as events, ensuring all writes are recorded for user consistency
    • Replay events to rebuild user feed state in case of server failure, ensuring data consistency
  • Sharding Pattern:
    • Shard news feed data by user region to distribute load and reduce latency
    • Shard by user activity level, placing more active users on high-performance shards to balance load

Share

  • Pregenerate URLs for later use
  • Implement caching for frequent redirects to reduce latency (1)
  • Apply rate limiting and validation to enhance security(1)
  • Architecture: Request-Driven
  • Structure: Monolithic
  • Cache-Aside Pattern:
    • Cache frequently accessed short URLs to speed up redirect requests
    • Evict expired URLs from cache to ensure users are redirected correctly
  • Rate Limiting Pattern:
    • Limit URL creation requests to prevent abuse and ensure fair usage
    • Apply rate limiting on redirects to prevent DDoS attacks on popular URLs
  • Publisher-Subscriber Pattern:
    • Notify caching systems to evict or preload URLs when new ones are pregenerated

Location Related

  • Implement efficient matching algorithms for low latency(1, 2
  • Use GPS data for location tracking
  • Use log database to record events to facilitate data traceability
  • Architecture: Event-Driven
  • Structure: Microservices
  • Geode Pattern:
    • Use geodes to efficiently match riders with nearby drivers for low latency
    • Store and index GPS data geospatially for quick location-based queries
  • Event Sourcing Pattern:
    • Record all ride-related events for full traceability of the system's state
    • Use events to reconstruct a trip's state in case of disputes or audits
  • Saga Distributed Transactions Pattern:
    • Manage complex transactions like booking, payment, and confirmation across services
    • Ensure consistency in distributed systems when riders cancel or change routes

IM

  • efficient messaging protocols like XMPP (1)
  • Ensure data consistency with eventual consistency model
  • Encrypt messages for security (1)
  • Architecture: Peer-to-Peer (P2P)
  • Structure: Monolithic
  • Valet Key Pattern:
    • Give clients temporary keys to upload/download attachments securely
    • Use keys to authenticate WebSocket/XMPP connections for sending/receiving messages

Live

  • Optimize for real-time video delivery
  • Use scalable cloud services, CDN
  • Maintain stream quality with adaptive bitrate streaming using RTMPS and WebRTC (1)
  • Thundering herd problem (1)
  • Architecture: Event-Driven
  • Structure: Monolithic
  • Sharding Pattern:
    • Shard video streams across multiple servers for load balancing
    • Reduce latency by directing viewers to the nearest shard or CDN endpoint
  • Cache-Aside Pattern:
    • Cache popular streams and fetch from the origin server if not in cache
    • Update cache with the most viewed streams to handle sudden traffic spikes
  • Circuit Breaker Pattern:
    • Prevent system overloads during peak times by cutting off non-critical operations
    • Degrade service if a component fails to maintain overall system function

Video

  • Use scalable cloud services like CDN
  • Implement adaptive bitrate streaming for dynamic network conditions
  • Use redundant systems, failover strategies
  • Architect for horizontal scalability
  • Architecture: Request-Driven
  • Structure: Monolithic
  • Sharding Pattern:
    • Distribute video content across multiple servers to balance load
    • Serve users from the nearest data shard to reduce latency
  • Cache-Aside Pattern:
    • Store frequently accessed videos in cache to minimize read latency
    • Load data into the cache on demand, ensuring popular content is quickly accessible
  • Circuit Breaker Pattern:
    • Prevent cascading failures during peak loads by stopping operations to a failing service
    • Enable failover mechanisms to redirect traffic if a service or server fails

File Sync

  • Implement efficient file transfer protocols like Rsync algorithm (1)
  • Use asynchronous and chunked file uploading for efficiency (1)
  • Shift to microservices improved latency, resiliency, and resource efficiency(1)
  • Architecture: Event-Driven
  • Structure: Monolithic
  • Asynchronous Request-Reply Pattern:
    • Handle file uploads where the client initiates a request and is free to perform other tasks while waiting for the response
    • Process large file downloads in the background
  • CQRS Pattern:
    • Separate read requests (retrieving file lists) from write requests (uploading files) for optimized performance
    • Allow quick access to file metadata while ensuring consistent writes during uploads
  • Retry Pattern & Circuit Breaker Pattern:
    • Retry file transfers when transient network issues occur
    • Open circuit breaker to prevent a failing file transfer service from being overwhelmed

Search

  • Implement Inverted Index for efficient keyword matching (1)
  • Personalize search results using user history and PageRank/Panda
  • Optimize search algorithms for speed and accuracy
  • Architecture: Request-Driven
  • Structure: Microservices
  • Sharding Pattern:
    • Shard the search index by keyword ranges to distribute the load across servers
    • Parallelize web pages across shards to enhance performance and scalability
  • Cache-Aside Pattern:
    • Cache popular search results and serve them quickly for repeated queries
    • Update the cache proactively when frequent keywords are queried to reduce latency
  • Publisher-Subscriber Pattern:
    • Publish search queries to a topic, with subscribers updating personalized ranking asynchronously
    • Use subscribers to trigger index updates when new content is available or changed

Ranking

  • Design for scalability to handle complex, multi-dimensional queries
  • Develop a caching strategy for frequently accessed filtered results
  • Pre-compute rankings to meet specific business timeframes for updates
  • Batch multiple update transactions into a single operation to optimize performance (1)
  • Employ Lossy Counting and Global Query Buckets for efficient, approximate data aggregation
  • Architecture: Request-Driven
  • Structure: Monolithic
  • Cache-Aside Pattern:
    • Cache top tags or tracks to quickly serve frequent queries without hitting the database
  • Sharding Pattern:
    • Shard data by tags or music genres to distribute query load and improve scalability
    • Allow parallel processing of multi-dimensional queries across different shards
  • Fan-Out/Fan-In Strategy:
    • Fan-out updates to pre-computed rankings across multiple systems, then fan-in for a global view
    • Aggregate data from multiple shards (fan-in) for complex queries to generate final results

Trading

  • Implement efficient data streaming services for low-latency market updates
  • Ensure reliable trade execution with minimal processing delay
  • Integrate timely financial news delivery and custom alerting features
  • Facilitate secure and compliant fund transfer mechanisms
  • Architecture: Event-Driven
  • Structure: Monolithic
  • CQRS Pattern:
    • Separate read queries for financial news delivery from write operations for trade execution
    • Streamline command processing to reduce trade execution latency
  • Valet Key Pattern:
    • Secure fund transfers by providing temporary credentials for transactional operations
    • Minimize data access scope using valet keys for third-party financial news integration

Cooperative editing

  • Implement operational transformation or CRDTs for low-latency, consistent collaborative editing (1)
  • Maintain document consistency with version control and conflict resolution mechanisms (1)
  • Design for scalability to handle simultaneous edits by thousands of users
  • Optimize for multi-device compatibility, ensuring consistent user experience
  • Architecture: Event-Driven
  • Structure: Monolithic
  • Event Sourcing Pattern:
    • Record each edit as an event, enabling operational transformation for collaborative editing
    • Replay events to resolve conflicts and maintain document version history
  • Compensating Transaction Pattern:
    • Reverse an edit operation when a conflict is detected to maintain consistency
    • Apply compensating transactions to handle operational transformation conflicts

Booking

  • Employ distributed transactions, possibly with XA protocol, for cross-service operations
  • Prevent write skew and phantoms using serializable isolation levels in databases
  • Architecture: Request-Driven
  • Structure: Monolithic
  • Saga Distributed Transactions Pattern:
    • Coordinate event bookings across multiple services without XA protocol overhead
    • Handle failures in one service by executing compensating transactions in others
  • Event Sourcing Pattern:
    • Store state changes as events to prevent write skew in distributed transactions
    • Replay events to rebuild system state for serializable isolation level enforcement

Shopping

  • Implement elastic search for efficient, scalable product discovery
  • Employ event sourcing for reliable order tracking and management
  • Secure user accounts with OAuth and multi-factor authentication
  • Utilize a microservices architecture to ensure scalability of user and order systems
  • Architecture: Event-Driven
  • Structure: Microservices
  • Event Sourcing Pattern:
    • Record order events to track status changes
    • Enable modifying orders by replaying and adjusting events
  • Cache-Aside Pattern:
    • Cache product listings and details
    • Invalidate cache on product updates to ensure users see current information

CPU-bound

  • Serverless architecture to reduce system complexity
  • Utilize divide and conquer, map-reduce strategies to break down tasks
  • Manage variable workloads with auto-scaling cloud services
  • Architecture: Request-Driven
  • Structure: Monolithic
  • Fan-Out/Fan-In Strategy:
    • Distribute PDF conversion tasks across multiple functions
    • Apply video effects in parallel, then merge clips into a final video
  • Asynchronous Request-Reply Pattern:
    • Queue PDF conversion jobs and notify users asynchronously when done
    • Submit video rendering jobs and provide results via callback or webhook
  • Health Endpoint Monitoring Pattern:
    • Monitor health of serverless functions to ensure availability for video editing
    • Track PDF converter service health to scale or redeploy functions

IoT

  • Utilize event sourcing to capture and store each user interaction and device event
  • Implement efficient data compression / protocol for low-latency synchronization like MQTT
  • Employ eventual consistency model for user data synchronization
  • Architecture: Event-Driven
  • Structure: Monolithic
  • Event Sourcing Pattern:
    • Record data as events for historical analysis
    • Capture device temperature changes to recreate historical states for troubleshooting
  • Publisher-Subscriber Pattern:
    • Devices publish sensor data, which apps subscribe to for real-time updates
    • Subscribe to user preferences changes to update device behavior dynamically
  • Sharding Pattern:
    • Shard data by user ID for scalable, distributed data storage
    • Use geographical sharding to reduce latency for location-based IoT devices

Infrastructure

  • Use partitioning for scalable throughput
  • Replicate data for high availability
  • Implement consistent hashing for consistency
  • Encrypt data at rest for security
  • Architecture: Event-Driven / Request-Driven
  • Structure: Monolithic
  • Health Endpoint Monitoring Pattern:
    • Monitor the health of nodes, ensuring consistent message processing
    • Check object storage service health to detect and resolve access issues
  • Event Sourcing Pattern:
    • Store every change in system configuration as an event
    • Record log generation and access events to reconstruct system states during incidents