9 Essential Software Architecture Patterns for Scalable Distributed Systems in 2026

Disclosure: This post includes affiliate links; I may receive compensation if you purchase products or services from the different links provided in this article.

Software Architecture Patterns for Distributed Systems

image_credit - ByteByteGo

Hello friends, in modern software development, distributed systems are very popular but architects and developers face the challenge of designing systems that efficiently manage data and facilitate seamless communication between various components.

Architectural patterns provide proven solutions to common problems encountered in distributed systems, ensuring reliability, scalability, and maintainability.

Among these patterns, some patterns stand out as fundamental for managing data and communication flow effectively, which we will see in this article.

These are also important topic for System design interviews and knowledge of these pattern goes a long way in solving System design problem and impressing your interviewer.

Apart from preparing common System design questions like API Gateway vs load balancer, Forward Proxy vs Reverse Proxy as well common System Design problem , it make sense to know about these patterns as well.

Let's find out more about these patterns to understand their principles and applications.

By the way, if you are preparing for System design interviews and want to learn System Design in depth then you can also checkout sites like ByteByteGo, Design Guru, Exponent, Educative, Codemia.io , bugfree.ai, and Udemy which have many great System design courses

Also, a solid knowledge of various Architecture patterns like Peer to Peer Pattern, API Gateway goes a long way in designing systems that can withstand the test of time on production. On that note, here is a nice diagram from DesignGuru.io on Microservices architecture:

Microservices architecture

9 Best Architectural Patterns for Distributed Systems

In the past, you have learned about essential Microservice design patterns like Event Sourcing, SAGA, Database Per Microservices, API Gateway, Circuit-Breaker and also shared best practices to design Microservices , now its time to see the brief overview of common architecture patterns for Data communication.

1. Peer-to-Peer (P2P) Pattern

The Peer-to-Peer pattern fosters direct communication between two or more components without the need for a central coordinator.

In this decentralized model, each node in the network can act as both a client and a server, enabling efficient resource sharing and collaboration.

P2P architectures are commonly used in file sharing systems, decentralized applications (DApps), and blockchain networks, where resilience and scalability are paramount.

Here is how P2P architecture looks like:

Peer-to-Peer (P2P) Pattern


2. API Gateway Pattern

An API Gateway serves as a unified entry point for client requests to access backend services within an application.

By consolidating multiple APIs into a single interface, it simplifies client-server interactions and enforces security, authentication, and rate limiting policies.

API Gateways are essential components in microservices architectures, enabling service discovery, load balancing, and protocol translation while abstracting the complexities of backend systems.

Here is how it looks:

API Gateway Pattern

If you like to watch, here is another great video from ByteByteGo which explains API Gateway


3. Pub-Sub (Publish-Subscribe)

The Pub-Sub pattern decouples message producers (publishers) from consumers (subscribers) through a message broker or event bus like Kafka, Solace, RabbitMQ, or ActiveMQ.

Publishers broadcast messages to predefined topics or channels, while subscribers express interest in specific topics and receive relevant messages asynchronously.

Pub-Sub architectures facilitate loose coupling, scalability, and fault tolerance, making them ideal for real-time messaging systems, event-driven microservices, and IoT platforms.

Here is how Pub-sub pattern looks like:

Pub sub pattern


4. Request-Response Pattern

The Request-Response pattern represents the fundamental interaction model in distributed systems, where a client sends a request to a server and awaits a corresponding response.

This synchronous communication paradigm is prevalent in web applications, RESTful APIs, and RPC (Remote Procedure Call) frameworks.

Request-Response interactions ensure predictable behavior and enable error handling, making them suitable for transactional workflows and user-facing interfaces.

Here is how Request-Response model looks like in action:

Request-Response Pattern


5. Event Sourcing Pattern

Event Sourcing is a distributed system pattern for persisting the state of an application as a sequence of immutable events.

Instead of storing current state directly, events representing state transitions are stored and replayed to reconstruct the application state when needed.

Event Sourcing enables auditability, temporal querying, and replayability, making it well-suited for financial systems, collaborative editing tools, and domain-driven designs where historical data is crucial.

Here is how a Event Sourcing pattern looks like:

software architecture diagram for interviews

And, if you like to watch, here is a nice video on Event Sourcing which is worth watching:


6. ETL (Extract, Transform, Load) Pattern

ETL is a data integration pattern used to extract data from multiple sources, transform it into a standardized format, and load it into a destination database or data warehouse.

This pattern is essential for data migration, synchronization, and consolidation tasks in business intelligence, data analytics, and data warehousing projects.

ETL pipelines automate data workflows, handle data quality issues, and support batch processing of large datasets.

Here is how ETL looks lin action:

ETL (Extract, Transform, Load) Pattern


7. Batching Pattern

Batching involves accumulating data over a period or until a certain threshold is reached before processing it as a single unit.

By aggregating multiple operations into larger batches, it reduces overhead and improves efficiency in data processing pipelines.

Batching is commonly employed in data ingestion, ETL processes, and distributed computing frameworks to optimize resource utilization and minimize latency.

Here is how a Batching pattern looks like:

Batching Pattern


9. Streaming Processing Pattern

Streaming Processing enables the continuous ingestion, processing, and analysis of data streams in real-time. Unlike batch processing, which operates on static datasets, streaming systems handle infinite data streams with low latency and high throughput.

Streaming architectures support event-driven processing, complex event processing (CEP), and real-time analytics applications in domains such as finance, IoT, and cybersecurity.

Here is a nice diagram from Hazlecast which shows Stream Processing in action:

software architecture pattern


10. Orchestration Pattern

Orchestration involves a central coordinator (an orchestrator) managing the interactions between distributed components or services to execute a workflow or business process.

By coordinating task execution, handling exceptions, and enforcing dependencies, orchestration ensures the orderly execution of complex workflows spanning multiple systems.

Orchestration engines are used in workflow automation, business process management (BPM), and microservices orchestration to streamline operations and improve agility.

Here is how it looks by using Saga Orchestrator Pattern

software architecture pattern

And, here is a nice diagram from ByteByteGowhich explains all these architecture styles in a more visual way

10 Software Architecture Patterns for System Design Interviews


Best System Design Interviews Resources

And, here are curated list of best system design books, online courses, and practice websites which you can check to better prepare for System design interviews. Most of these courses also answer questions I have shared here.

  1. DesignGuru's Grokking System Design Course: An interactive learning platform with hands-on exercises and real-world scenarios to strengthen your system design skills.

  2. Codemia.io : This is another great platform to practice System design problems for interviews. It got more than 120+ System design problems, many of them are free and also a proper structure to solve them.

  3. "System Design Interview" by Alex Xu: This book provides an in-depth exploration of system design concepts, strategies, and interview preparation tips.

  4. "Designing Data-Intensive Applications" by Martin Kleppmann: A comprehensive guide that covers the principles and practices for designing scalable and reliable systems.

  5. Bugfree.ai: Bugfree AI is a popular platform for technical interview preparation. It's a LeetCode-style platform to practice System Design and Coding interview questions. It includes a variety of questions to practice.

  6. "System Design Primer" on GitHub: A curated list of resources, including articles, books, and videos, to help you prepare for system design interviews.

  7. Educative's System Design Course: An interactive learning platform with hands-on exercises and real-world scenarios to strengthen your system design skills.

  8. High Scalability Blog: A blog that features articles and case studies on the architecture of high-traffic websites and scalable systems.

  9. YouTube Channels: Check out channels like "Gaurav Sen" and "Tech Dummies" for insightful videos on system design concepts and interview preparation.

  10. ByteByteGo: A live book and course by Alex Xu for System design interview preparation. It contains all the content of the System Design Interview book volumes 1 and 2, and will be updated with volume 3 which is coming soon.

  11. Exponent: A specialized site for interview prep, especially for MAANG companies like Amazon and Google, They also have a great system design course and many other material which can help you crack FAANG interviews.

how to prepare for system design

image_credit - ByteByteGo

You should also combine theoretical knowledge with practical application by working on real-world projects and participating in mock interviews. Continuous practice and learning will undoubtedly enhance your proficiency in system design interviews.


That's all about 9 essential Software architecture patterns. Most of these patterns are also applicable to distributed systems, and they are also quite important for system design interviews.

In short, the effective management of data and communication flow is critical for building robust and scalable distributed systems.

Architectural patterns such as Peer-to-Peer, API Gateway, Pub-Sub, Request-Response, Event Sourcing, ETL, Batching, Streaming Processing, and Orchestration offer valuable solutions to address diverse challenges in system design and implementation.

By understanding these software architecture and distributed system patterns and their respective strengths and trade-offs, architects and developers can make informed decisions to design systems that meet the evolving needs of their applications and users.

    19 Microservices Patterns for System Design Interviews

    Disclosure: This post includes affiliate links; I may receive compensation if you purchase products or services from the different links provided in this article.

    19 Microservices Patterns for System Design Interviews

    image_credit - ByteByteGo

    Hello friends, if you are preparing for system design interviews, then it makes sense to prepare for Microservices design patterns as well, not just to do well on interviews or make your architecture more robust, but also to understand existing projects.

    Microservices patterns like Circuit Breaker, API Gateway, Saga, and Event Sourcing are tried and tested solutions to common Microservices Problems.

    These patterns address common challenges in microservices architectures like scalability, fault tolerance, and data consistency.

    In the past, I have talked about common system design questions like API Gateway vs Load Balancer and Horizontal vs Vertical Scaling, Forward proxy vs reverse proxy as well common System Design problems and in this article I am going to share 24 key Microservices design patterns that are essential knowledge for technical interviews.

    They are also one of the essential System design topics for interview, and you must prepare them well.

    Many companies use microservices, so understanding these patterns shows you're up-to-date with current trends. Knowing when and how to apply these patterns also demonstrates your ability to solve complex distributed system problems.

    These patterns often involve trade-offs, allowing you to showcase your analytical thinking. Interviewers often present scenarios where these patterns are relevant solutions.

    By the way, if you are preparing for System design interviews and want to learn System Design in depth then you can also checkout sites like ByteByteGo, Design Guru, Exponent, Educative, Codemia.io, and Udemy which have many great System design courses and a System design interview template like this which you can use to answer any System Design question.

    how to answer system design question

    If you need more choices, you can also see this list of best System Design courses, books, and websites

    P.S. Keep reading until the end. I have a free bonus for you.

    So, what are we waiting for? Let's jump right into it

    19 Microservices Design Patterns for System Design Interviews

    Microservices architecture is a design approach that structures an application as a collection of loosely coupled services.

    To build scalable, maintainable, and resilient microservices-based systems, various patterns have emerged.

    Here are essential microservices patterns you can use in your project, and also remember for system design interviews.

    1. Service Registry

    Since there are many microservices in a microservice architecture, they need to discover and communicate with each other.

    A Service Registry, such as Netflix Eureka or Consul, acts as a centralized directory where services can register themselves and discover others.

    Here is what it looks like:

    Service Registry Pattern


    2. API Gateway

    An API Gateway serves as a single entry point for client applications, aggregating multiple microservices into a unified API.

    It handles requests, routing them to the appropriate services, and may perform tasks like authentication, authorization, and load balancing.

    Here is what API Gateway looks like:

    API Gateway


    3. Circuit Breaker

    Inspired by electrical circuit breakers, this pattern prevents a microservice failure from cascading to other services. The Circuit breaker pattern monitors for failures and, if a threshold is crossed, opens the circuit, preventing further requests.

    This helps with graceful degradation and fault tolerance, and it's essential in a Microservice architecture to prevent a total shutdown of your services.

    Here is an example of Netflix Hysrix as a circuit breaker:

    Circuit Breaker


    4. Bulkhead

    In a microservices system, isolating failures is crucial. The Bulkhead pattern involves separating components or services to contain failures.

    For example, thread pools or separate databases for different services can be used to prevent a failure in one part of the system from affecting others.

    Here is a diagram showing the Bulkhead pattern in Microservices architecture:

    Bulkhead Pattern


    5. Saga Pattern

    This pattern is used for managing distributed transactions. The Saga pattern breaks down a long-running business transaction into a series of smaller, independent transactions.

    Each microservice involved in the saga handles its own transaction and publishes events to trigger subsequent actions.

    Here is how Saga Pattern looks in action:

    Saga Pattern


    6. Event Sourcing

    This is another popular pattern which is used heavily in high frequently low latency applications.

    In this pattern, instead of storing only the current state, Event Sourcing involves storing a sequence of events that led to the current state.

    This pattern provides a reliable audit trail and allows for rebuilding the system state at any point in time.

    Here is how Event Sourcing looks in action:

    Event Sourcing


    7. Command Query Responsibility Segregation (CQRS)

    CQRS Pattern separates the read and write sides of an application. It uses different models for updating information (commands) and reading information (queries).

    This pattern can improve scalability, as read and write operations have different optimization requirements.

    Here is a nice diagram that shows the CQRS pattern:

    Command Query Responsibility Segregation (CQRS)


    8. Data Sharding

    Database sharing pattern is used to distribute the database load and avoid bottlenecks. Data Sharding involves partitioning data across multiple databases or database instances.

    In this pattern, each microservice may handle a subset of data or specific types of requests.

    Here is how database sharding looks, credit - Design Guru

    Types of Database sharding


    9. Polyglot Persistence

    Different microservices may have different data storage needs. Polyglot Persistence allows using multiple database technologies based on the requirements of each microservice, optimizing for data storage, retrieval, and query capabilities.

    Here is a nice diagram that shows Polyglot persistence in Azure :

    Polyglot Persistence


    10. Retry

    In Microservice architecture, when a transient failure occurs, the Retry pattern involves retrying the operation instead of immediately failing.

    It can be applied at various levels, such as service-to-service communication or database interactions.

    Here is a nice diagram from ByteByteGo, a great place for system design learning, which shows the Retry pattern in Microservices:

    Retry Pattern in Microservices


    12. Sidecar

    The Sidecar pattern involves attaching a helper service (the sidecar) to the main microservice to provide additional functionalities such as logging, security, or communication with external services.

    This allows the main service to focus on its core functionality.

    Here is what a Sidecar pattern looks like:

    Sidecar pattern in Microservices


    13. Backends for Frontends (BFF)

    Also known as BFF, this pattern is useful when dealing with multiple client types (e.g., web, mobile). The BFF pattern involves creating separate backend services tailored for each type of client.

    This allows for optimized and specialized APIs for each client.

    Here is what a Backends for Frontends (BFF) pattern looks like:

    Backends for Frontends (BFF)


    14. Shadow Deployment

    The Shadow Deployment pattern involves routing a copy (shadow) of production traffic to a new microservice version without affecting the actual user experience.

    This is one of the popular deployment strategies, and it helps validate the new version's performance and correctness.

    Here is what shadow deployment looks like

    Shadow Deployment


    15. Consumer-Driven Contracts

    In a microservices ecosystem, multiple services often interact with one another. The Consumer-Driven Contracts pattern involves consumers specifying their expectations from producers, allowing for more robust and coordinated changes.

    Here is a nice diagram that explains Consumer Driven contracts

    Consumer-Driven Contracts


    16. Smart Endpoints, Dumb Pipes

    This pattern advocates for placing business logic in microservices (smart endpoints) rather than relying on complex middleware. The communication infrastructure (pipes) should be simple and handle only message routing.


    17. Database per Service

    This is another popular Microservices pattern where each microservice has its own database, and services communicate through well-defined APIs.

    Database per Service pattern provides isolation but also requires careful consideration of data consistency and integrity.

    Here is how this pattern looks:

    Database per Service pattern


    18. Async Messaging

    Instead of synchronous communication between microservices, the Async Messaging pattern involves using message queues to facilitate asynchronous communication. This can improve system responsiveness and scalability.

    Here is a nice diagram that shows the difference between sync and async messaging

    Async Messaging pattern


    19. Stateless Services

    Designing microservices to be stateless simplifies scalability and resilience. Each service processes a request independently, without relying on stored state, making it easier to scale horizontally.

    Here is a nice diagram which shows the difference between Stateless Services and Stateful Services

    Stateless Services


    System Design Interviews Resources

    And, here is my curated list of best system design books, online courses, and practice websites which you can check to better prepare for System design interviews. Most of these courses also answer questions I have shared here.

    1. DesignGuru's Grokking System Design Course: An interactive learning platform with hands-on exercises and real-world scenarios to strengthen your system design skills.

    2. Codemia.io: This is one of the best places to practice System design problems for interviews online. It has more than 120+ System design problems, many of which are free and also a proper structure to solve them.

    3. Exponent: A specialized site for interview prep, especially for FAANG companies like Amazon and Google. They also have a great system design course and many other materials which can help you crack FAANG interviews.

    4. "Designing Data-Intensive Applications" by Martin Kleppmann: A comprehensive guide that covers the principles and practices for designing scalable and reliable systems.

    5. LeetCode System Design Tag: LeetCode is a popular platform for technical interview preparation. The System Design tag on LeetCode includes a variety of questions to practice.

    6. "System Design Primer" on GitHub: A curated list of resources, including articles, books, and videos, to help you prepare for system design interviews.

    7. Educative's System Design Course: An interactive learning platform with hands-on exercises and real-world scenarios to strengthen your system design skills.

    8. High Scalability Blog: A blog that features articles and case studies on the architecture of high-traffic websites and scalable systems.

    9. YouTube Channels: Check out channels like "Gaurav Sen" and "Tech Dummies" for insightful videos on system design concepts and interview preparation.

    10. ByteByteGo: A live book and course by Alex Xu for System design interview preparation. It contains all the content of the System Design Interview book volumes 1 and 2, and will be updated with volume 3 which is coming soon.

    11. "System Design Interview" by Alex Xu: This book provides an in-depth exploration of system design concepts, strategies, and interview preparation tips.

    how to prepare for system design

    image_credit - ByteByteGo


    That's all about the common Microservice patterns and concepts a developer should know. These microservices patterns help address various challenges associated with building and maintaining distributed systems, providing solutions for communication, fault tolerance, data management, and scalability.

    When designing microservices architectures, combining these patterns judiciously can lead to a robust and resilient system.

    These additional microservices patterns, when applied thoughtfully, contribute to building resilient, scalable, and maintainable distributed systems.

    The choice of patterns depends on the specific requirements and challenges faced during the design and implementation of microservices architectures.

    Bonus

    As promised, here is the bonus for you, a free book. I just found a new free book to learn Distributed System Design, you can also read it here on Microsoft --- https://info.microsoft.com/rs/157-GQE-382/images/EN-CNTNT-eBook-DesigningDistributedSystems.pdf

      Caching Strategies in System Design: Types, Patterns, Trade-offs & Best Practices

      Disclosure: This post includes affiliate links; I may receive compensation if you purchase products or services from the different links provided in this article.

      System Design Basics - Caching
      image_credit - DesignGuru.io

      Hello friends, Caching is not just an important topic for System design interviews, its also technique in software development, enabling faster data retrieval, reducing load times, and enhancing user experience.

      For developers, mastering caching concepts is crucial as it can significantly optimize application performance and scalability.

      In the past, I have talked about common system design questions like API Gateway vs Load Balancer and Horizontal vs Vertical Scaling, Forward proxy vs reverse proxy as well common System Design problems and in this article we will explore the fundamentals of caching in system design and learn different caching strategies that are essential knowledge for technical interviews.

      It's also one of the essential System design topics for interview and you must prepare it well.

      In this article, you will learn ten essential caching concepts, ranging from client-side and server-side strategies to more advanced techniques like distributed caching and cache replacement policies

      So what are we waiting for? let's start

      By the way, if you are preparing for System design interviews and want to learn System Design in depth then you can also checkout sites like ByteByteGo, Design Guru, Exponent, Educative, Codemia.io and Udemy which have many great System design courses and a System design interview template like this which you can use to answer any System Design question.

      how to answer system design question

      If you need more choices, you can also see this list of best System Deisgn courses, books, and websites

      P.S. Keep reading until the end. I have a free bonus for you.

      What is Caching? Which data to Cache? Where to Cache?

      While designing distributed system, caching should be strategically placed to optimize performance, reduce latency, and minimize load on backend services.

      Caching can be implemented at multiple layers like

      1. Client-Side Cache
        This involves storing frequently accessed data on the client device, reducing the need for repeated requests to the server. It is effective for data that doesn't change frequently and can significantly improve user experience by reducing latency.

      2. Edge Cache (Content Delivery Network - CDN)
        CDNs cache content at the edge nodes closest to the end-users, which helps in delivering static content like images, videos, and stylesheets faster by serving them from geographically distributed servers.

      3. Application-Level Cache
        This includes in-memory caches such as Redis or Memcached within the application layer. These caches store results of expensive database queries, session data, and other frequently accessed data to reduce the load on the database and improve application response times.

      4. Database Cache
        Techniques such as query caching in the database layer store the results of frequent queries. This reduces the number of read operations on the database and speeds up data retrieval.

      5. Distributed Cache
        In a distributed system, a distributed cache spans multiple nodes to provide high availability and scalability. It ensures that the cached data is consistent across the distributed environment and can handle the high throughput required by large-scale systems.

      When designing a caching strategy, it's crucial to determine what data to cache by analyzing usage patterns, data volatility, and access frequency.

      Implementing an appropriate cache eviction policy (such as LRU - Least Recently Used, or TTL - Time to Live) ensures that stale data is purged, maintaining the cache's relevance.

      Moreover, considering consistency models and cache invalidation strategies is vital to ensure that cached data remains accurate and up-to-date across the system.

      And, here is a nice diagram on caching from DesignGuru.io to illustrate what I just said.

      System Design Caching cheat shet


      10 Caching Basics for System Design Interview

      Here are 10 essential caching related basics and concepts every programmer must know before going for any System design interview.

      1) client-side caching

      Client-side caching is a fundamental technique where data is stored on the user's device to minimize server requests and improve load times. Two primary methods include:

      • Browser Cache: Stores resources like CSS, JavaScript, and images locally to reduce page load times on subsequent visits.
      • Service Workers: Enable offline access by caching responses, allowing applications to function without an internet connection.

      In short:

      • browser cache: stores CSS, js, images to reduce load time\
        • service workers: enable offline access by caching response

      Here is how client side caching looks like:

      client side caching


      2) server-side caching

      This is another type of caching which involves storing data on the server to expedite response times for user requests.

      Key strategies include:

      • Page Caching: Saves entire web pages, allowing faster delivery on subsequent requests .
      • Fragment Caching:
        Caches specific parts of a page, such as sidebars or navigation bars, to enhance loading efficiency.

      • Object Caching:
        Stores expensive query results to prevent repeated calculations

      In short:

      • page caching: cache the entire web page
        • fragment caching: cache page components like sidebars, navigation bar\
        • object caching: cache expensive query results

      Here is how server side caching looks like:

      server side caching

      image_credit --- ByteByteGo


      3) Database caching

      Database caching is crucial for reducing database load and improving query performance. Important techniques include:

      • Query Caching:
        Stores the results of database queries to quickly serve repeat requests.

      • Row Level Caching:
        Caches frequently accessed rows to avoid repeated database fetches.

      In short:

      • query caching: cache db query results to reduce load
        • row level caching: cache popular rows to avoid repeated fetches

      Here is an example of database caching on AWS:

      database caching


      4) application-level caching

      Application-level caching focuses on caching within the application to reduce computation and data retrieval times. Strategies include:

      • Data Caching: Stores specific data points or entire datasets for quick access.
      • Computational Caching: Caches the results of expensive computations to avoid repeated processing.

      In short:

      • data caching: cache specific data points or entire datasets\
        • computational caching: cache expensive computation results to avoid recalculation

      application-level caching


      5) Distributed caching

      Distributed caching enhances scalability by spreading cache data across multiple servers, allowing high availability and fault tolerance.

      In short, this type of caching just spreads cache across many servers for scalability

      Here is how a distributed cache with Redis looks like:

      distributed cache with Redis


      6) CDN

      Content Delivery Networks (CDNs) are used to cache static files close to users via edge servers, significantly reducing latency and speeding up content delivery.

      In short, CDN store static files near users using edge servers for low latency

      Also, here is a nice diagram on how CDN Works by DeisgnGuru.io

      how CDN Works


      7) cache replacement policies

      Cache replacement policies determine how caches handle data eviction. Common policies include:

      • Least Recently Used (LRU): Evicts the least recently accessed items first.
      • Most Recently Used (MRU): Evicts the most recently accessed items first.
      • Least Frequently Used (LFU): Evicts items that are accessed least often.

      In short:

      - LRU: removes the least recently accessed items first\
      - MRU: removes the most recently accessed items first\
      - LFU: removes items that are accessed least often
      

      cache replacement policies


      8) hierarchical caching

      Hierarchical caching involves multiple cache levels (e.g., L1, L2) to balance speed and storage capacity. This model is quit popular on CPU.

      In short:

      • caching at many levels (L1, L2 caches) for speed and capacity

      L1 and L2 Cache


      9) cache invalidation

      Cache invalidation ensures that stale data is removed from the cache. Methods include:

      • Time-to-Live (TTL): Sets an expiry time for cached data.
      • Event-based Invalidation: Triggers invalidation based on specific events or conditions.
      • Manual Invalidation: Allows developers to manually update the cache using tools.

      In short:

      - TTL: set expiry time\
      - event based: invalidate based on events or conditions\
      - manual: update cache using tools
      

      Here is a nice System design cheat sheet about cache invalidation methods by DesignGuru.io to understand this concept better:

      cache invalidation strategies


      10) caching patterns

      Finally, caching patterns are strategies for synchronizing cache with the database. Common patterns include:

      • Write-through: Writes data to both the cache and the database simultaneously.
      • Write-behind: Writes data to the cache immediately and to the database asynchronously.
      • Write-around: Directly writes data to the database, bypassing the cache to avoid cache misses on subsequent reads.

      In short:

      - write-through: data is written to the cache and the database at once\
      - write-behind: data is written to the cache and asynchronously to database\
      - write-around: data is written directly to the database, bypassing the cache
      

      Here is another great diagram to understand various caching strategies, courtesy DesignGuru.io, one of the best place to learn System Design.

      caching patterns

      Best System Design Interviews Resources

      And, here are curated list of best system design books, online courses, and practice websites which you can check to better prepare for System design interviews. Most of these courses also answer questions I have shared here.

      1. DesignGuru's Grokking System Design Course: An interactive learning platform with hands-on exercises and real-world scenarios to strengthen your system design skills.

      2. Codemia.io : This is another great platform to practice System design problems for interviews. It got more than 120+ System design problems, many of them are free and also a proper structure to solve them.

      3. "System Design Interview" by Alex Xu: This book provides an in-depth exploration of system design concepts, strategies, and interview preparation tips.

      4. "Designing Data-Intensive Applications" by Martin Kleppmann: A comprehensive guide that covers the principles and practices for designing scalable and reliable systems.

      5. LeetCode System Design Tag: LeetCode is a popular platform for technical interview preparation. The System Design tag on LeetCode includes a variety of questions to practice.

      6. "System Design Primer" on GitHub: A curated list of resources, including articles, books, and videos, to help you prepare for system design interviews.

      7. Educative's System Design Course: An interactive learning platform with hands-on exercises and real-world scenarios to strengthen your system design skills.

      8. High Scalability Blog: A blog that features articles and case studies on the architecture of high-traffic websites and scalable systems.

      9. YouTube Channels: Check out channels like "Gaurav Sen" and "Tech Dummies" for insightful videos on system design concepts and interview preparation.

      10. ByteByteGo: A live book and course by Alex Xu for System design interview preparation. It contains all the content of System Design Interview book volume 1 and 2 and will be updated with volume 3 which is coming soon.

      11. Exponent: A specialized site for interview prep especially for FAANG companies like Amazon and Google, They also have a great system design course and many other material which can help you crack FAAN interviews.

      how to prepare for system design

      image_credit - ByteByteGo

      Conclusion

      That's all about 10 essential Cache related concepts for System design interview. Caching can improve the performance and scalability of your application. So use it carefully. Understanding and implementing these caching concepts can significantly enhance application performance, scalability, and user satisfaction.

      Other System Design Articles and Resources you may like

      Thanks for reading this article so far. If you like this Twitter system design interview solution then please share it with your friends and colleagues. If you have any questions feel free to ask in the comments.

      Bonus\
      As promised, here is the bonus for you, a free book. I just found a new free book to learn Distributed System Design, you can also read it here on Microsoft --- https://info.microsoft.com/rs/157-GQE-382/images/EN-CNTNT-eBook-DesigningDistributedSystems.pdf

      System design tutorials

      SQL Query Execution Order: How SQL Queries Actually Work (Must Know for Interviews)

      Disclosure: This post includes affiliate links; I may receive compensation if you purchase products or services from the different links provided in this article.

      SQL query execution order

      Hello guys, one of the common question on technical interviews about SQL is how exactly SQL query work? While this may seems simple, many programmers including experienced one fail to answer this with confidence.

      Many developer don't even know how the SQL commands are executed and in which order?

      For them the SQL query is executed as they are written but that's not true, you can see from the above diagram that FROM and JOIN is executed before you can SELECT anything, which is again very rational if you think through.

      Earlier, I have shared 20 SQL queries from interviews and 50 System design questions and in this article, I am going to answer how exactly SQL query works under the hood, so stay tuned and continue reading.

      And, if are preparing for tech interviews and you need more questions not just queries but also database and SQL related questions from other topics like indexes, joins, group by, aggregation, and window functions then you can also checkout these 200+ SQL Interview Questions .

      This course is one of the specially designed course to prepare you for SQL interviews by answering popular questions. You can also get this for big discount now.

      How exactly SQL Query is executed?

      Structured Query Language or SQL is the standard language for managing and manipulating relational databases.

      It provides a powerful and efficient way to interact with data, enabling developers, analysts, and data scientists to retrieve, insert, update, and delete information from databases.

      While SQL queries are written in a declarative, human-readable format, there is a complex process that occurs behind the scenes to execute these queries and retrieve the desired results.

      In this article, we'll delve into the inner workings of SQL queries, breaking down the process step by step.

      1. Query Parsing and Tokenization

      The journey of an SQL query begins with parsing and tokenization. When a user submits an SQL query, the database management system (DBMS) must first break down the query into individual tokens.

      Tokens are the smallest units of the query and can include keywords (SELECT, FROM, WHERE, etc.), table and column names, operators (=, >, <, etc.), and values.

      This process involves identifying the syntax and structure of the query to ensure it follows the rules of the SQL language.

      how SQL query are executed


      2. Query Optimization

      Once the query is parsed and tokenized, the DBMS performs query optimization. This is a crucial step that aims to improve the efficiency of query execution.

      The DBMS analyzes the query and explores various execution plans to determine the most efficient way to retrieve the requested data.

      It considers factors such as indexes, table relationships, and available resources to create an execution plan that minimizes the time and resources needed to complete the query.

      how to do query optimization


      3. Execution Plan Generation

      The chosen execution plan outlines the sequence of steps required to fulfill the query.

      It determines the order in which tables are accessed, the types of joins performed, and the filtering conditions applied.

      The DBMS generates this plan based on statistical information about the data distribution and the database schema.

      The goal is to reduce the amount of data that needs to be processed and to optimize disk and memory usage.

      On Microsoft SQL Server, a Query Execution plan looks like below:

      how Execution plan looks like


      4. Data Retrieval and Joins

      With the execution plan in place, the DBMS begins the process of data retrieval. If the query involves multiple tables, the DBMS performs join operations to combine the relevant data.

      Joining tables efficiently requires comparing and matching rows based on specified conditions. Depending on the type of join (inner join, outer join, etc.), the DBMS determines which rows from each table should be included in the result set.

      How SQL join works


      5. Filtering and Sorting

      After joining the necessary tables, the DBMS applies filtering conditions specified in the WHERE clause. This involves evaluating each row to determine whether it meets the criteria set by the user.

      Rows that do not satisfy the conditions are discarded, while those that pass the filter are retained for further processing.

      Additionally, if the query includes an ORDER BY clause, the DBMS will sort the resulting rows based on the specified column(s).

      Sorting involves arranging the data in a specific order, such as ascending or descending, to produce the final ordered result set.

      When does filtering and sorting happens in SQL Query Execution


      6. Aggregation and Grouping

      Aggregation functions such as SUM, COUNT, AVG, MIN, and MAX are commonly used in SQL queries to perform calculations on groups of data.

      If the query includes a GROUP BY clause, the DBMS groups the rows based on the specified columns. It then applies the aggregation functions to each group separately, producing summary statistics or calculations for the grouped data.

      Aggregation and Grouping in sQL query


      7. Result Set Generation

      With all the necessary operations performed, the DBMS generates the final result set. This set of rows and columns represents the data that satisfies the user's query. T

      he result set is then returned to the user or the application that initiated the query.

      when is result generated from SQL query


      8. Index Utilization

      Indexes play a vital role in optimizing the performance of SQL queries. An index is a data structure that provides a quick way to look up data based on specific columns.

      When executing a query, the DBMS may utilize indexes to efficiently locate the relevant rows, reducing the need for full-table scans and improving query response times.

      Index Utilization in SQL query


      9. Transaction Management

      Transactional operations in SQL, such as INSERT, UPDATE, and DELETE, involve modifying data in the database. These operations are grouped into transactions, which ensure data consistency and integrity.

      When a transaction is initiated, the DBMS may lock the affected rows or tables to prevent other transactions from accessing or modifying them concurrently.

      Once the transaction is completed, the changes are either committed to the database or rolled back, depending on the success or failure of the transaction.

      Transaction Management in SQL


      10. Caching and Memory Management

      Modern database systems employ various caching and memory management techniques to optimize query performance.

      Caching involves storing frequently accessed data in memory to reduce the need for disk reads, which are slower in comparison.

      The DBMS may also use buffer pools to manage memory allocation for query execution and result set generation, further enhancing efficiency.

      Caching and Memory Management in SQL


      SQL Query Order? How SQL Query are executed under the hood?

      It's also important to know and remember in which order various SQL commands like SELECT, FROM, COUNT, WHERE, HAVING, ORDER BY, JOIN etc are applied

      SQL queries are processed in a specific order, and understanding this order is crucial for writing and optimizing queries effectively. The typical order of SQL query processing involves the following steps:

      1. FROM: The query begins by specifying the source tables or views from which the data will be retrieved. This clause defines the primary data source for the query.

      2. JOIN: If the query involves multiple tables, the JOIN clause is used to combine data from different tables based on specified conditions. Different types of joins (INNER JOIN, LEFT JOIN, RIGHT JOIN, etc.) determine how rows from each table are matched and included in the result set.

      3. WHERE: The WHERE clause is used to filter rows based on specific conditions. It restricts the data to only those rows that meet the specified criteria. Rows that do not satisfy the conditions are excluded from further processing.

      4. GROUP BY: If aggregation is required, the GROUP BY clause is used to group rows with similar values in specified columns. This step is often used in conjunction with aggregation functions like COUNT, SUM, AVG, etc. to perform calculations on grouped data.

      5. HAVING: The HAVING clause is used to filter the result set after the GROUP BY operation has been performed. It specifies conditions for filtering aggregated data. Similar to the WHERE clause, rows that do not meet the criteria are excluded from the final result.

      6. SELECT: The SELECT clause is used to specify the columns that should appear in the final result set. It determines which data will be retrieved and displayed in the query output.

      7. DISTINCT: The DISTINCT keyword, if used, removes duplicate rows from the result set, ensuring that only unique values are displayed.

      8. ORDER BY: The ORDER BY clause is used to sort the result set based on specified columns. It arranges the rows in ascending or descending order, as specified.

      9. LIMIT/OFFSET or FETCH/FIRST: Depending on the database system, you might use LIMIT (or FETCH or FIRST) and OFFSET clauses to control the number of rows returned and to implement pagination.

      10. UNION/INTERSECT/EXCEPT: If needed, these set operations can be used to combine the results of multiple queries.

      Here is a nice diagram from Medium which clearly explains how the SQL query looks like and how its executed by Query engine:

      SQL Query execution order

      It's important to note that the actual order of execution may vary based on the specific database management system being used.** However, the logical processing order remains consistent across most SQL databases.

      Additionally, modern query optimizer may rearrange some of these steps for performance reasons while ensuring that the final result remains accurate and consistent.

      Understanding the order of SQL query processing not only help in technical interviews but also allows you to write efficient and effective queries, and it provides insights into query optimization and performance tuning.

      By structuring your queries with this order in mind, you can better control the flow of data and achieve the desired results.

      Conclusion

      That's all about how SQL query are executed under the hood. SQL queries might seem like simple statements, but there is a complex process that unfolds behind the scenes to retrieve, manipulate, and manage data.

      From parsing and optimization to execution plan generation and result set generation, every step is meticulously orchestrated to ensure efficient and accurate query processing.

      Understanding how SQL queries work under the hood provides developers and database administrators with valuable insights into performance optimization and query tuning, ultimately leading to better utilization of database resources and improved application responsiveness.

      And, if are preparing for tech interviews and you need more questions not just queries but also database and SQL related questions from other topics like indexes, joins, group by, aggregation, and window functions then you can also read Grokking the SQL Interview book or join 200+ SQL Interview Questions .

      Both are great resources to prepare you for SQL interviews by answering popular questions.

      All the best !!