❌

Normal view

There are new articles available, click to refresh the page.
Today β€” 18 January 2025Main stream

Learning Notes #57 – Partial Indexing in Postgres

16 January 2025 at 14:36

Today, i learnt about partial indexing in postgres, how its optimizes the indexing process to filter subset of table more efficiently. In this blog, i jot down notes on partial indexing.

Partial indexing in PostgreSQL is a powerful feature that provides a way to optimize database performance by creating indexes that apply only to a subset of a table’s rows. This selective indexing can result in reduced storage space, faster index maintenance, and improved query performance, especially when queries frequently involve filters or conditions that only target a portion of the data.

An index in PostgreSQL, like in other relational database management systems, is a data structure that improves the speed of data retrieval operations. However, creating an index on an entire table can sometimes be inefficient, especially when dealing with very large datasets where queries often focus on specific subsets of the data. This is where partial indexing becomes invaluable.

Unlike a standard index that covers every row in a table, a partial index only includes rows that satisfy a specified condition. This condition is defined using a WHERE clause when the index is created.

To understand the mechanics, let us consider a practical example.

Suppose you have a table named orders that stores details about customer orders, including columns like order_id, customer_id, order_date, status, and total_amount. If the majority of your queries focus on pending orders those where the status is pending, creating a partial index specifically for these rows can significantly improve performance.

Example 1:

Here’s how you can create such an index,

CREATE INDEX idx_pending_orders
ON orders (order_date)
WHERE status = 'pending';

In this example, the index idx_pending_orders includes only the rows where status equals pending. This means that any query that involves filtering by status = 'pending' and utilizes the order_date column will leverage this index. For instance, the following query would benefit from the partial index,

SELECT *
FROM orders
WHERE status = 'pending'
AND order_date > '2025-01-01';

The benefits of this approach are significant. By indexing only the rows with status = 'pending', the size of the index is much smaller compared to a full table index.

This reduction in size not only saves disk space but also speeds up the process of scanning the index, as there are fewer entries to traverse. Furthermore, updates or modifications to rows that do not meet the WHERE condition are excluded from index maintenance, thereby reducing the overhead of maintaining the index and improving performance for write operations.

Example 2:

Let us explore another example. Suppose your application frequently queries orders that exceed a certain total amount. You can create a partial index tailored to this use case,

CREATE INDEX idx_high_value_orders
ON orders (customer_id)
WHERE total_amount > 1000;

This index would optimize queries like the following,

SELECT *
FROM orders
WHERE total_amount > 1000
AND customer_id = 123;

The key advantage here is that the index only includes rows where total_amount > 1000. For datasets with a wide range of order amounts, this can dramatically reduce the number of indexed entries. Queries that filter by high-value orders become faster because the database does not need to sift through irrelevant rows.

Additionally, as with the previous example, index maintenance is limited to the subset of rows matching the condition, improving overall performance for insertions and updates.

Partial indexes are also useful for enforcing constraints in a selective manner. Consider a scenario where you want to ensure that no two active promotions exist for the same product. You can achieve this using a unique partial index

CREATE UNIQUE INDEX idx_unique_active_promotion
ON promotions (product_id)
WHERE is_active = true;

This index guarantees that only one row with is_active = true can exist for each product_id.

In conclusion, partial indexing in PostgreSQL offers a flexible and efficient way to optimize database performance by targeting specific subsets of data.

Before yesterdayMain stream

Learning Notes #54 – Architecture Decision Records

14 January 2025 at 02:35

Last few days, i was learning on how to make a accountable decision on deciding technical stuffs. Then i came across ADR. So far i haven’t used or seen used by our team. I think this is a necessary step to be incorporated to make accountable decisions. In this blog i share details on ADR for my future reference.

What is an ADR?

An Architectural Decision Record (ADR) is a concise document that captures a single architectural decision, its context, the reasoning behind it, and its consequences. ADRs help teams document, share, and revisit architectural choices, ensuring transparency and better collaboration.

Why Use ADRs?

  1. Documentation: ADRs serve as a historical record of why certain decisions were made.
  2. Collaboration: They promote better understanding across teams.
  3. Traceability: ADRs link architectural decisions to specific project requirements and constraints.
  4. Accountability: They clarify who made a decision and when.
  5. Change Management: ADRs help evaluate the impact of changes and facilitate discussions around reversals or updates.

ADR Structure

A typical ADR document follows a standard format. Here’s an example:

  1. Title: A clear and concise title describing the decision.
  2. Context: Background information explaining the problem or opportunity.
  3. Decision: A summary of the chosen solution.
  4. Consequences: The positive and negative outcomes of the decision.
  5. Status: Indicates whether the decision is proposed, accepted, superseded, or deprecated.

Example:

Optimistic locking on MongoDB https://docs.google.com/document/d/1olCbicQeQzYpCxB0ejPDtnri9rWb2Qhs9_JZuvANAxM/edit?usp=sharing

References

  1. https://cognitect.com/blog/2011/11/15/documenting-architecture-decisions
  2. https://www.infoq.com/podcasts/architecture-advice-process/
  3. Recommended: https://github.com/joelparkerhenderson/architecture-decision-record/tree/main

Learning Notes #53 – The Expiration Time Can Be Unexpectedly Lost While Using Redis SET EX

12 January 2025 at 09:14

Redis, a high-performance in-memory key-value store, is widely used for caching, session management, and various other scenarios where fast data retrieval is essential. One of its key features is the ability to set expiration times for keys. However, when using the SET command with the EX option, developers might encounter unexpected behaviors where the expiration time is seemingly lost. Let’s explore this issue in detail.

Understanding SET with EX

The Redis SET command with the EX option allows you to set a key’s value and specify its expiration time in seconds. For instance


SET key value EX 60

This command sets the key key to the value value and sets an expiration time of 60 seconds.

The Problem

In certain cases, the expiration time might be unexpectedly lost. This typically happens when subsequent operations overwrite the key without specifying a new expiration. For example,


SET key value1 EX 60
SET key value2

In the above sequence,

  1. The first SET command assigns a value to key and sets an expiration of 60 seconds.
  2. The second SET command overwrites the value of key but does not include an expiration time, resulting in the key persisting indefinitely.

This behavior can lead to subtle bugs, especially in applications that rely on key expiration for correctness or resource management.

Why Does This Happen?

The Redis SET command is designed to replace the entire state of a key, including its expiration. When you use SET without the EX, PX, or EXAT options, the expiration is removed, and the key becomes persistent. This behavior aligns with the principle that SET is a complete update operation.

When using Redis SET with EX, be mindful of operations that might overwrite keys without reapplying expiration. Understanding Redis’s behavior and implementing robust patterns can save you from unexpected issues, ensuring your application remains efficient and reliable.

Learning Notes #52 – Hybrid Origin Failover Pattern

12 January 2025 at 06:29

Today, i learnt about failover patterns from AWS https://aws.amazon.com/blogs/networking-and-content-delivery/three-advanced-design-patterns-for-high-available-applications-using-amazon-cloudfront/ . In this blog i jot down my understanding on this pattern for future reference,

Hybrid origin failover is a strategy that combines two distinct approaches to handle origin failures effectively, balancing speed and resilience.

The Need for Origin Failover

When an application’s primary origin server becomes unavailable, the ability to reroute traffic to a secondary origin ensures continuity. The failover process determines how quickly and effectively this switch happens. Broadly, there are two approaches to implement origin failover:

  1. Stateful Failover with DNS-based Routing
  2. Stateless Failover with Application Logic

Each has its strengths and limitations, which the hybrid approach aims to mitigate.

Stateful Failover

Stateful failover is a system that allows a standby server to take over for a failed server and continue active sessions. It’s used to create a resilient network infrastructure and avoid service interruptions.

This method relies on a DNS service with health checks to detect when the primary origin is unavailable. Here’s how it works,

  1. Health Checks: The DNS service continuously monitors the health of the primary origin using health checks (e.g., HTTP, HTTPS).
  2. DNS Failover: When the primary origin is marked unhealthy, the DNS service resolves the origin’s domain name to the secondary origin’s IP address.
  3. TTL Impact: The failover process honors the DNS Time-to-Live (TTL) settings. A low TTL ensures faster propagation, but even in the most optimal configurations, this process introduces a delayβ€”often around 60 to 70 seconds.
  4. Stateful Behavior: Once failover occurs, all traffic is routed to the secondary origin until the primary origin is marked healthy again.

Implementation from AWS (as-is from aws blog)

The first approach is usingΒ Amazon Route 53 Failover routing policy with health checks on the origin domain name that’s configured as the origin in CloudFront. When the primary origin becomes unhealthy, Route 53 detects it, and then starts resolving the origin domain name with the IP address of the secondary origin. CloudFront honors the origin DNS TTL, which means that traffic will start flowing to the secondary origin within the DNS TTLs.Β The most optimal configuration (Fast Check activated, a failover threshold of 1, and 60 second DNS TTL) means that the failover will take 70 seconds at minimum to occur. When it does, all of the traffic is switched to the secondary origin, since it’s a stateful failover. Note that this design can be further extended with Route 53 Application Recovery Control for more sophisticated application failover across multiple AWS Regions, Availability Zones, and on-premises.

The second approach is using origin failover, a native feature of CloudFront. This capability of CloudFront tries for the primary origin of every request, and if a configured 4xx or 5xx error is received, then CloudFront attempts a retry with the secondary origin. This approach is simple to configure and provides immediate failover. However, it’s stateless, which means every request must fail independently, thus introducing latency to failed requests. For transient origin issues, this additional latency is an acceptable tradeoff with the speed of failover, but it’s not ideal when the origin is completely out of service. Finally, this approach only works for the GET/HEAD/OPTIONS HTTP methods, because other HTTP methods are not allowed on a CloudFront cache behavior with Origin Failover enabled.

Advantages

  • Works for all HTTP methods and request types.
  • Ensures complete switchover, minimizing ongoing failures.

Disadvantages

  • Relatively slower failover due to DNS propagation time.
  • Requires a reliable health-check mechanism.

Approach 2: Stateless Failover with Application Logic

This method handles failover at the application level. If a request to the primary origin fails (e.g., due to a 4xx or 5xx HTTP response), the application or CDN immediately retries the request with the secondary origin.

How It Works

  1. Primary Request: The application sends a request to the primary origin.
  2. Failure Handling: If the response indicates a failure (configurable for specific error codes), the request is retried with the secondary origin.
  3. Stateless Behavior: Each request operates independently, so failover happens on a per-request basis without waiting for a stateful switchover.

Implementation from AWS (as-is from aws blog)

The hybrid origin failover pattern combines both approaches to get the best of both worlds. First, you configure both of your origins with a Failover Policy in Route 53 behind a single origin domain name. Then, you configure an origin failover group with the single origin domain name as primary origin, and the secondary origin domain name as secondary origin. This means that when the primary origin becomes unavailable, requests are immediately retried with the secondary origin until the stateful failover of Route 53 kicks in within tens of seconds, after which requests go directly to the secondary origin without any latency penalty. Note that this pattern only works with the GET/HEAD/OPTIONS HTTP methods.

Advantages

  • Near-instantaneous failover for failed requests.
  • Simple to configure and doesn’t depend on DNS TTL.

Disadvantages

  • Adds latency for failed requests due to retries.
  • Limited to specific HTTP methods like GET, HEAD, and OPTIONS.
  • Not suitable for scenarios where the primary origin is entirely down, as every request must fail first.

The Hybrid Origin Failover Pattern

The hybrid origin failover pattern combines the strengths of both approaches, mitigating their individual limitations. Here’s how it works:

  1. DNS-based Stateful Failover: A DNS service with health checks monitors the primary origin and switches to the secondary origin if the primary becomes unhealthy. This ensures a complete and stateful failover within tens of seconds.
  2. Application-level Stateless Failover: Simultaneously, the application or CDN is configured to retry failed requests with a secondary origin. This provides an immediate failover mechanism for transient or initial failures.

Implementation Steps

  1. DNS Configuration
    • Set up health checks on the primary origin.
    • Define a failover policy in the DNS service, which resolves the origin domain name to the secondary origin when the primary is unhealthy.
  2. Application Configuration
    • Configure the application or CDN to use an origin failover group.
    • Specify the primary origin domain as the primary origin and the secondary origin domain as the backup.

Behavior

  • Initially, if the primary origin encounters issues, requests are retried immediately with the secondary origin.
  • Meanwhile, the DNS failover switches all traffic to the secondary origin within tens of seconds, eliminating retry latencies for subsequent requests.

Benefits of Hybrid Origin Failover

  1. Faster Failover: Immediate retries for failed requests minimize initial impact, while DNS failover ensures long-term stability.
  2. Reduced Latency: After DNS failover, subsequent requests don’t experience retry delays.
  3. High Resilience: Combines stateful and stateless failover for robust redundancy.
  4. Simplicity and Scalability: Leverages existing DNS and application/CDN features without complex configurations.

Limitations and Considerations

  1. HTTP Method Constraints: Stateless failover works only for GET, HEAD, and OPTIONS methods, limiting its use for POST or PUT requests.
  2. TTL Impact: Low TTLs reduce propagation delays but increase DNS query rates, which could lead to higher costs.
  3. Configuration Complexity: Combining DNS and application-level failover requires careful setup and testing to avoid misconfigurations.
  4. Secondary Origin Capacity: Ensure the secondary origin can handle full traffic loads during failover.

Learning Notes #51 – Postgres as a Queue using SKIP LOCKED

11 January 2025 at 06:56

Yesterday, i came across a blog from inferable.ai https://www.inferable.ai/blog/posts/postgres-skip-locked, which walkthrough about using postgres as a queue. In this blog, i jot down notes on using postgres as a queue for future references.

PostgreSQL is a robust relational database that can be used for more than just storing structured data. With the SKIP LOCKED feature introduced in PostgreSQL 9.5, you can efficiently turn a PostgreSQL table into a job queue for distributed processing.

Why Use PostgreSQL as a Queue?

Using PostgreSQL as a queue can be advantageous because,

  • Familiarity: If you’re already using PostgreSQL, there’s no need for an additional message broker.
  • Durability: PostgreSQL ensures ACID compliance, offering reliability for your job processing.
  • Simplicity: No need to manage another component like RabbitMQ or Kafka

Implementing a Queue with SKIP LOCKED

1. Create a Queue Table

To start, you need a table to store the jobs,


CREATE TABLE job_queue (
    id SERIAL PRIMARY KEY,
    job_data JSONB NOT NULL,
    status TEXT DEFAULT 'pending',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

This table has the following columns,

  • id: A unique identifier for each job.
  • job_data: The data or payload for the job.
  • status: Tracks the job’s state (β€˜pending’, β€˜in_progress’, or β€˜completed’).
  • created_at: Timestamp of job creation.

2. Insert Jobs into the Queue

Adding jobs is straightforward,


INSERT INTO job_queue (job_data)
VALUES ('{"task": "send_email", "email": "user@example.com"}');

3. Fetch Jobs for Processing with SKIP LOCKED

Workers will fetch jobs from the queue using SELECT ... FOR UPDATE SKIP LOCKED to avoid contention,

WITH next_job AS (
    SELECT id, job_data
    FROM job_queue
    WHERE status = 'pending'
    FOR UPDATE SKIP LOCKED
    LIMIT 1
)
UPDATE job_queue
SET status = 'in_progress'
FROM next_job
WHERE job_queue.id = next_job.id
RETURNING job_queue.id, job_queue.job_data;

Key Points:

  • FOR UPDATE locks the selected row to prevent other workers from picking it up.
  • SKIP LOCKED ensures locked rows are skipped, enabling concurrent workers to operate without waiting.
  • LIMIT 1 processes one job at a time per worker.

4. Mark Jobs as Completed

Once a worker finishes processing a job, it should update the job’s status,


UPDATE job_queue
SET status = 'completed'
WHERE id = $1; -- Replace $1 with the job ID

5. Delete Old or Processed Jobs

To keep the table clean, you can periodically remove completed jobs,


DELETE FROM job_queue
WHERE status = 'completed' AND created_at < NOW() - INTERVAL '30 days';

Example Worker Implementation

Here’s an example of a worker implemented in Python using psycopg2


import psycopg2
from psycopg2.extras import RealDictCursor

connection = psycopg2.connect("dbname=yourdb user=youruser")

while True:
    with connection.cursor(cursor_factory=RealDictCursor) as cursor:
        cursor.execute(
            """
            WITH next_job AS (
                SELECT id, job_data
                FROM job_queue
                WHERE status = 'pending'
                FOR UPDATE SKIP LOCKED
                LIMIT 1
            )
            UPDATE job_queue
            SET status = 'in_progress'
            FROM next_job
            WHERE job_queue.id = next_job.id
            RETURNING job_queue.id, job_queue.job_data;
            """
        )

        job = cursor.fetchone()
        if job:
            print(f"Processing job {job['id']}: {job['job_data']}")

            # Simulate job processing
            cursor.execute("UPDATE job_queue SET status = 'completed' WHERE id = %s", (job['id'],))

        else:
            print("No jobs available. Sleeping...")
            time.sleep(5)

    connection.commit()

Considerations

  1. Transaction Isolation: Use the REPEATABLE READ or SERIALIZABLE isolation level cautiously to avoid unnecessary locks.
  2. Row Locking: SKIP LOCKED only skips rows locked by other transactions, not those locked within the same transaction.
  3. Performance: Regularly archive or delete old jobs to prevent the table from growing indefinitely. Consider indexing the status column to improve query performance.
  4. Fault Tolerance: Ensure that workers handle crashes or timeouts gracefully. Use a timeout mechanism to revert jobs stuck in the β€˜in_progress’ state.
  5. Scaling: Distribute workers across multiple nodes to handle a higher job throughput.
  6. The SKIP LOCKED clause only applies to row-level locks – the required ROW SHARE table-level lock is still taken normally.
  7. Using SKIP LOCKED provides an inconsistent view of the data by design. This is why it’s perfect for queue-like tables where we want to distribute work, but not suitable for general purpose work where consistency is required.

Learning Notes #50 – Fixed Partition Pattern | Distributed Pattern

9 January 2025 at 16:51

Today, i learnt about fixed partition, where it handles about balancing the data among servers without high movement of data. In this blog, i jot down notes on how fixed partition helps in solving the problem.

This entire blog is inspired from https://www.linkedin.com/pulse/distributed-systems-design-pattern-fixed-partitions-retail-kumar-v-c34pc/?trackingId=DMovSwEZSfCzKZEKa7yJrg%3D%3D

Problem Statement

In a distributed key-value store system, data items need to be mapped to a set of cluster nodes to ensure efficient storage and retrieval. The system must satisfy the following requirements,

  1. Uniform Distribution: Data should be evenly distributed across all cluster nodes to avoid overloading any single node.
  2. Deterministic Mapping: Given a data item, the specific node responsible for storing it should be determinable without querying all the nodes in the cluster.

A common approach to achieve these goals is to use hashing with a modulo operation. For example, if there are three nodes in the cluster, the key is hashed, and the hash value modulo the number of nodes determines the node to store the data. However, this method has a critical drawback,

Rebalancing Issue: When the cluster size changes (e.g., nodes are added or removed), the mapping for most keys changes. This requires the system to move almost all the data to new nodes, leading to significant overhead in terms of time and resources, especially when dealing with large data volumes.

Challenge: How can we design a mapping mechanism that minimizes data movement during cluster size changes while maintaining uniform distribution and deterministic mapping?

Solution

There is a concept of Fixed Partitioning,

What Is Fixed Partitioning?

This pattern organizes data into a predefined number of fixed partitions that remain constant over time. Data is assigned to these partitions using a hashing algorithm, ensuring that the mapping of data to partitions is permanent. The system separates the fixed partitioning of data from the physical servers managing these partitions, enabling seamless scaling.

Key Features of Fixed Partitioning

  1. Fixed Number of Partitions
    • The number of partitions is determined during system initialization (e.g., 8 partitions).
    • Data is assigned to these partitions based on a consistent hashing algorithm.
  2. Stable Data Mapping
    • Each piece of data is permanently mapped to a specific partition.
    • This eliminates the need for large-scale data reshuffling when scaling the system.
  3. Adjustable Partition-to-Server Mapping
    • Partitions can be reassigned to different servers as the system scales.
    • Only the physical location of the partitions changes; the fixed mapping remains intact.
  4. Balanced Load Distribution
    • Partitions are distributed evenly across servers to balance the workload.
    • Adding new servers involves reassigning partitions without moving or reorganizing data within the partitions.

Naive Example

We have a banking system with transactions stored in 8 fixed partitions, distributed based on a customer’s account ID.


CREATE TABLE transactions (
    id SERIAL PRIMARY KEY,
    account_id INT NOT NULL,
    transaction_amount NUMERIC(10, 2) NOT NULL,
    transaction_date DATE NOT NULL
) PARTITION BY HASH (account_id);

1. Create Partition


DO $$
BEGIN
    FOR i IN 0..7 LOOP
        EXECUTE format(
            'CREATE TABLE transactions_p%s PARTITION OF transactions FOR VALUES WITH (modulus 8, remainder %s);',
            i, i
        );
    END LOOP;
END $$;

This creates 8 partitions (transactions_p0 to transactions_p7) based on the hash remainder of account_id modulo 8.

2. Inserting Data

When inserting data into the transactions table, PostgreSQL automatically places it into the correct partition based on the account_id.


INSERT INTO transactions (account_id, transaction_amount, transaction_date)
VALUES (12345, 500.00, '2025-01-01');

The hash of 12345 % 8 determines the target partition (e.g., transactions_p5).

3. Querying Data

Querying the base table works transparently across all partitions


SELECT * FROM transactions WHERE account_id = 12345;

PostgreSQL automatically routes the query to the correct partition.

4. Scaling by Adding Servers

Initial Setup:

Suppose we have 4 servers managing the partitions,

  • Server 1: transactions_p0, transactions_p1
  • Server 2: transactions_p2, transactions_p3
  • Server 3: transactions_p4, transactions_p5
  • Server 4: transactions_p6, transactions_p7

Adding a New Server:

When a 5th server is added, we redistribute partitions,

  • Server 1: transactions_p0
  • Server 2: transactions_p1
  • Server 3: transactions_p2, transactions_p3
  • Server 4: transactions_p4
  • Server 5: transactions_p5, transactions_p6, transactions_p7

Partition Migration

  • During the migration, transactions_p5 is copied from Server 3 to Server 5.
  • Once the migration is complete, Server 5 becomes responsible for transactions_p5.

Benefits:

  1. Minimal Data Movement – When scaling, only the partitions being reassigned are copied to new servers. Data within partitions remains stable.
  2. Optimized Performance – Queries are routed directly to the relevant partition, minimizing scan times.
  3. Scalability – Adding servers is straightforward, as it involves reassigning partitions, not reorganizing data.

What happens when a new server is added then. Don’t we need to copy the data ?

When a partition is moved to a new server (e.g., partition_b from server_A to server_B), the data in the partition must be copied to the new server. However,

  1. The copying is limited to the partition being reassigned.
  2. No data within the partition is reorganized.
  3. Once the partition is fully migrated, the original copy is typically deleted.

For example, in PostgreSQL,

  • Export the Partition pg_dump -t partition_b -h server_A -U postgres > partition_b.sql
  • Import on New Server: psql -h server_B -U postgres -d mydb < partition_b.sql

Learning Notes #49 – Pitfall of Implicit Default Values in APIs

9 January 2025 at 14:00

Today, we faced a bug in our workflow due to implicit default value in an 3rd party api. In this blog i will be sharing my experience for future reference.

Understanding the Problem

Consider an API where some fields are optional, and a default value is used when those fields are not provided by the client. This design is common and seemingly harmless. However, problems arise when,

  1. Unexpected Categorization: The default value influences logic, such as category assignment, in ways the client did not intend.
  2. Implicit Assumptions: The API assumes a default value aligns with the client’s intention, leading to misclassification or incorrect behavior.
  3. Debugging Challenges: When issues occur, clients and developers spend significant time tracing the problem because the default behavior is not transparent.

Here’s an example of how this might manifest,


POST /items
{
  "name": "Sample Item",
  "category": "premium"
}

If the category field is optional and a default value of "basic" is applied when it’s omitted, the following request,


POST /items
{
  "name": "Another Item"
}

might incorrectly classify the item as basic, even if the client intended it to be uncategorized.

Why This is a Code Smell

Implicit default handling for optional fields often signals poor design. Let’s break down why,

  1. Violation of the Principle of Least Astonishment: Clients may be unaware of default behavior, leading to unexpected outcomes.
  2. Hidden Logic: The business logic embedded in defaults is not explicit in the API’s contract, reducing transparency.
  3. Coupling Between API and Business Logic: When defaults dictate core behavior, the API becomes tightly coupled to specific business rules, making it harder to adapt or extend.
  4. Inconsistent Behavior: If the default logic changes in future versions, existing clients may experience breaking changes.

Best Practices to Avoid the Trap

  1. Make Default Behavior Explicit
    • Clearly document default values in the API specification (but we still missed it.)
    • For example, use OpenAPI/Swagger to define optional fields and their default values explicitly
  2. Avoid Implicit Defaults
    • Instead of applying defaults server-side, require the client to explicitly provide values, even if they are defaults.
    • This ensures the client is fully aware of the data being sent and its implications.
  3. Use Null or Explicit Indicators
    • Allow optional fields to be explicitly null or undefined, and handle these cases appropriately.
    • In this case, the API can handle null as β€œno category specified” rather than applying a default.
  4. Fail Fast with Validation
    • Use strict validation to reject ambiguous requests, encouraging clients to provide clear inputs.

{
  "error": "Field 'category' must be provided explicitly."
}

5. Version Your API Thoughtfully:

  • Document changes and provide clear migration paths for clients.
  • If you must change default behaviors, ensure backward compatibility through versioning.

Implicit default values for optional fields can lead to unintended consequences, obscure logic, and hard-to-debug issues. Recognizing this pattern as a code smell is the first step to building more robust APIs. By adopting explicitness, transparency, and rigorous validation, you can create APIs that are easier to use, understand, and maintain.

Learning Notes #48 – Common Pitfalls in Event Driven Architecture

8 January 2025 at 15:04

Today, i came across Raul Junco post on mistakes in Event Driven Architecture – https://www.linkedin.com/posts/raul-junco_after-years-building-event-driven-systems-activity-7278770394046631936-zu3-?utm_source=share&utm_medium=member_desktop. In this blog i am highlighting the same for future reference.

Event-driven architectures are awesome, but they come with their own set of challenges. Missteps can lead to unreliable systems, inconsistent data, and frustrated users. Let’s explore some of the most common pitfalls and how to address them effectively.

1. Duplication

Idempotent APIs – https://parottasalna.com/2025/01/08/learning-notes-47-idempotent-post-requests/

Events often get re-delivered due to retries or system failures. Without proper handling, duplicate events can,

  • Charge a customer twice for the same transaction: Imagine a scenario where a payment service retries a payment event after a temporary network glitch, resulting in a duplicate charge.
  • Cause duplicate inventory updates: For example, an e-commerce platform might update stock levels twice for a single order, leading to overestimating available stock.
  • Create inconsistent or broken system states: Duplicates can cascade through downstream systems, introducing mismatched or erroneous data.

Solution:

  • Assign unique IDs: Ensure every event has a globally unique identifier. Consumers can use these IDs to detect and discard duplicates.
  • Design idempotent processing: Structure your operations so they produce the same outcome even when executed multiple times. For instance, an API updating inventory could always set stock levels to a specific value rather than incrementing or decrementing.

2. Not Guaranteeing Order

Events can arrive out of order when distributed across partitions or queues. This can lead to

  • Processing a refund before the payment: If a refund event is processed before the corresponding payment event, the system might show a negative balance or fail to reconcile properly.
  • Breaking logic that relies on correct sequence: Certain workflows, such as assembling logs or transactional data, depend on a strict event order to function correctly.

Solution

  • Use brokers with ordering guarantees: Message brokers like Apache Kafka support partition-level ordering. Design your topics and partitions to align with entities requiring ordered processing (e.g., user or account ID).
  • Add sequence numbers or timestamps: Include metadata in events to indicate their position in a sequence. Consumers can use this data to reorder events if necessary, ensuring logical consistency.

3. The Dual Write Problem

Outbox Pattern: https://parottasalna.com/2025/01/03/learning-notes-31-outbox-pattern-cloud-pattern/

When writing to a database and publishing an event, one might succeed while the other fails. This can

  • Lose events: If the event is not published after the database write, downstream systems might remain unaware of critical changes, such as a new order or a status update.
  • Cause mismatched states: For instance, a transaction might be logged in a database but not propagated to analytical or monitoring systems, creating inconsistencies.

Solution

  • Use the Transactional Outbox Pattern: In this pattern, events are written to an β€œoutbox” table within the same database transaction as the main data write. A separate process then reads from the outbox and publishes events reliably.
  • Adopt Change Data Capture (CDC) tools: CDC tools like Debezium can monitor database changes and publish them as events automatically, ensuring no changes are missed.

4. Non-Backward-Compatible Changes

Changing event schemas without considering existing consumers can break systems. For example:

  • Removing a field: A consumer relying on this field might encounter null values or fail altogether.
  • Renaming or changing field types: This can lead to deserialization errors or misinterpretation of data.

Solution:

  • Maintain versioned schemas: Introduce new schema versions incrementally and ensure consumers can continue using older versions during the transition.
  • Use schema evolution-friendly formats: Formats like Avro or Protobuf natively support schema evolution, allowing you to add fields or make other non-breaking changes easily.
  • Add adapters for compatibility: Build adapters or translators that transform events from new schemas to older formats, ensuring backward compatibility for legacy systems.

Learning Notes #41 – Shared Lock and Exclusive Locks | Postgres

6 January 2025 at 14:07

Today, I learnt about various locking mechanism to prevent double update. In this blog, i make notes on Shared Lock and Exclusive Lock for my future self.

What Are Locks in Databases?

Locks are mechanisms used by a DBMS to control access to data. They ensure that transactions are executed in a way that maintains the ACID (Atomicity, Consistency, Isolation, Durability) properties of the database. Locks can be classified into several types, including

  • Shared Locks (S Locks): Allow multiple transactions to read a resource simultaneously but prevent any transaction from writing to it.
  • Exclusive Locks (X Locks): Allow a single transaction to modify a resource, preventing both reading and writing by other transactions.
  • Intent Locks: Used to signal the type of lock a transaction intends to acquire at a lower level.
  • Deadlock Prevention Locks: Special locks aimed at preventing deadlock scenarios.

Shared Lock

A shared lock is used when a transaction needs to read a resource (e.g., a database row or table) without altering it. Multiple transactions can acquire a shared lock on the same resource simultaneously. However, as long as one or more shared locks exist on a resource, no transaction can acquire an exclusive lock on that resource.


-- Transaction A: Acquire a shared lock on a row
BEGIN;
SELECT * FROM employees WHERE id = 1 FOR SHARE;
-- Transaction B: Acquire a shared lock on the same row
BEGIN;
SELECT * FROM employees WHERE id = 1 FOR SHARE;
-- Both transactions can read the row concurrently
-- Transaction C: Attempt to update the same row
BEGIN;
UPDATE employees SET salary = salary + 1000 WHERE id = 1;
-- Transaction C will be blocked until Transactions A and B release their locks

Key Characteristics of Shared Locks

1. Concurrent Reads

  • Shared locks allow multiple transactions to read the same resource at the same time.
  • This is ideal for operations like SELECT queries that do not modify data.

2. Write Blocking

  • While a shared lock is active, no transaction can modify the locked resource.
  • Prevents dirty writes and ensures read consistency.

3. Compatibility

  • Shared locks are compatible with other shared locks but not with exclusive locks.

When Are Shared Locks Used?

Shared locks are typically employed in read operations under certain isolation levels. For instance,

1. Read Committed Isolation Level:

  • Shared locks are held for the duration of the read operation.
  • Prevents dirty reads by ensuring the data being read is not modified by other transactions during the read.

2. Repeatable Read Isolation Level:

  • Shared locks are held until the transaction completes.
  • Ensures that the data read during a transaction remains consistent and unmodified.

3. Snapshot Isolation:

  • Shared locks may not be explicitly used, as the DBMS creates a consistent snapshot of the data for the transaction.

    Exclusive Locks

    An exclusive lock is used when a transaction needs to modify a resource. Only one transaction can hold an exclusive lock on a resource at a time, ensuring no other transactions can read or write to the locked resource.

    
    -- Transaction X: Acquire an exclusive lock to update a row
    BEGIN;
    UPDATE employees SET salary = salary + 1000 WHERE id = 2;
    -- Transaction Y: Attempt to read the same row
    BEGIN;
    SELECT * FROM employees WHERE id = 2;
    -- Transaction Y will be blocked until Transaction X completes
    -- Transaction Z: Attempt to update the same row
    BEGIN;
    UPDATE employees SET salary = salary + 500 WHERE id = 2;
    -- Transaction Z will also be blocked until Transaction X completes
    

    Key Characteristics of Exclusive Locks

    1. Write Operations: Exclusive locks are essential for operations like INSERT, UPDATE, and DELETE.

    2. Blocking Reads and Writes: While an exclusive lock is active, no other transaction can read or write to the resource.

    3. Isolation: Ensures that changes made by one transaction are not visible to others until the transaction is complete.

      When Are Exclusive Locks Used?

      Exclusive locks are typically employed in write operations or any operation that modifies the database. For instance:

      1. Transactional Updates – A transaction that updates a row acquires an exclusive lock to ensure no other transaction can access or modify the row during the update.

      2. Table Modifications – When altering a table structure, the DBMS may place an exclusive lock on the entire table.

      Benefits of Shared and Exclusive Locks

      Benefits of Shared Locks

      1. Consistency in Multi-User Environments – Ensure that data being read is not altered by other transactions, preserving consistency.
      2. Concurrency Support – Allow multiple transactions to read data simultaneously, improving system performance.
      3. Data Integrity – Prevent dirty reads and writes, ensuring that operations yield reliable results.

      Benefits of Exclusive Locks

      1. Data Integrity During Modifications – Prevents other transactions from accessing data being modified, ensuring changes are applied safely.
      2. Isolation of Transactions – Ensures that modifications by one transaction are not visible to others until committed.

      Limitations and Challenges

      Shared Locks

      1. Potential for Deadlocks – Deadlocks can occur if two transactions simultaneously hold shared locks and attempt to upgrade to exclusive locks.
      2. Blocking Writes – Shared locks can delay write operations, potentially impacting performance in write-heavy systems.
      3. Lock Escalation – In systems with high concurrency, shared locks may escalate to table-level locks, reducing granularity and concurrency.

      Exclusive Locks

      1. Reduced Concurrency – Exclusive locks prevent other transactions from accessing the locked resource, which can lead to bottlenecks in highly concurrent systems.
      2. Risk of Deadlocks – Deadlocks can occur if two transactions attempt to acquire exclusive locks on resources held by each other.

      Lock Compatibility

      Learning Notes #40 – SAGA Pattern | Cloud Patterns

      5 January 2025 at 17:08

      Today, I learnt about SAGA Pattern, followed by Compensation Pattern, Orchestration Pattern, Choreography Pattern and Two Phase Commit. SAGA is a combination of all the above. In this blog, i jot down notes on SAGA, for my future self.

      Modern software applications often require the coordination of multiple distributed services to perform complex business operations. In such systems, ensuring consistency and reliability can be challenging, especially when a failure occurs in one of the services. The SAGA design pattern offers a robust solution to manage distributed transactions while maintaining data consistency.

      What is the SAGA Pattern?

      The SAGA pattern is a distributed transaction management mechanism where a series of independent operations (or steps) are executed sequentially across multiple services. Each operation in the sequence has a corresponding compensating action to roll back changes if a failure occurs. This approach avoids the complexities of distributed transactions, such as two-phase commits, by breaking down the process into smaller, manageable units.

      Key Characteristics

      1. Decentralized Control: Transactions are managed across services without a central coordinator.
      2. Compensating Transactions: Every operation has an undo or rollback mechanism.
      3. Asynchronous Communication: Services communicate asynchronously in most implementations, ensuring loose coupling.

      Types of SAGA Patterns

      There are two primary types of SAGA patterns:

      1. Choreography-Based SAGA

      • In this approach, services communicate with each other directly to coordinate the workflow.
      • Each service knows which operation to trigger next after completing its own task.
      • If a failure occurs, each service initiates its compensating action to roll back changes.

      Advantages:

      • Simple implementation.
      • No central coordinator required.

      Disadvantages:

      • Difficult to manage and debug in complex workflows.
      • Tight coupling between services.
      import pika
      
      class RabbitMQHandler:
          def __init__(self, queue):
              self.connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
              self.channel = self.connection.channel()
              self.channel.queue_declare(queue=queue)
              self.queue = queue
      
          def publish(self, message):
              self.channel.basic_publish(exchange='', routing_key=self.queue, body=message)
      
          def consume(self, callback):
              self.channel.basic_consume(queue=self.queue, on_message_callback=callback, auto_ack=True)
              self.channel.start_consuming()
      
      # Define services
      class FlightService:
          def book_flight(self):
              print("Flight booked.")
              RabbitMQHandler('hotel_queue').publish("flight_booked")
      
      class HotelService:
          def on_flight_booked(self, ch, method, properties, body):
              try:
                  print("Hotel booked.")
                  RabbitMQHandler('invoice_queue').publish("hotel_booked")
              except Exception:
                  print("Failed to book hotel. Rolling back flight.")
                  FlightService().cancel_flight()
      
          def cancel_flight(self):
              print("Flight booking canceled.")
      
      # Setup RabbitMQ
      flight_service = FlightService()
      hotel_service = HotelService()
      
      RabbitMQHandler('hotel_queue').consume(hotel_service.on_flight_booked)
      
      # Trigger the workflow
      flight_service.book_flight()
      

      2. Orchestration-Based SAGA

      • A central orchestrator service manages the workflow and coordinates between the services.
      • The orchestrator determines the sequence of operations and handles compensating actions in case of failures.

      Advantages:

      • Clear control and visibility of the workflow.
      • Easier to debug and manage.

      Disadvantages:

      • The orchestrator can become a single point of failure.
      • More complex implementation.
      import pika
      
      class Orchestrator:
          def __init__(self):
              self.rabbitmq = RabbitMQHandler('orchestrator_queue')
      
          def execute_saga(self):
              try:
                  self.reserve_inventory()
                  self.process_payment()
                  self.generate_invoice()
              except Exception as e:
                  print(f"Error occurred: {e}. Initiating rollback.")
                  self.compensate()
      
          def reserve_inventory(self):
              print("Inventory reserved.")
              self.rabbitmq.publish("inventory_reserved")
      
          def process_payment(self):
              print("Payment processed.")
              self.rabbitmq.publish("payment_processed")
      
          def generate_invoice(self):
              print("Invoice generated.")
              self.rabbitmq.publish("invoice_generated")
      
          def compensate(self):
              print("Rolling back invoice.")
              print("Rolling back payment.")
              print("Rolling back inventory.")
      
      # Trigger the workflow
      Orchestrator().execute_saga()
      

      How SAGA Works

      1. Transaction Initiation: The first operation is executed by one of the services.
      2. Service Communication: Subsequent services execute their operations based on the outcome of the previous step.
      3. Failure Handling: If an operation fails, compensating transactions are triggered in reverse order to undo any changes.
      4. Completion: Once all operations are successfully executed, the transaction is considered complete.

      Benefits of the SAGA Pattern

      1. Improved Resilience: Allows partial rollbacks in case of failure.
      2. Scalability: Suitable for microservices and distributed systems.
      3. Flexibility: Works well with event-driven architectures.
      4. No Global Locks: Unlike traditional transactions, SAGA does not require global locking of resources.

      Challenges and Limitations

      1. Complexity in Rollbacks: Designing compensating transactions for every operation can be challenging.
      2. Data Consistency: Achieving eventual consistency may require additional effort.
      3. Debugging Issues: Debugging failures in a distributed environment can be cumbersome.
      4. Latency: Sequential execution may increase overall latency.

      When to Use the SAGA Pattern

      • Distributed systems where global ACID transactions are infeasible.
      • Microservices architectures with independent services.
      • Applications requiring high resilience and eventual consistency.

      Real-World Applications

      1. E-Commerce Platforms: Managing orders, payments, and inventory updates.
      2. Travel Booking Systems: Coordinating flight, hotel, and car rental reservations.
      3. Banking Systems: Handling distributed account updates and transfers.
      4. Healthcare: Coordinating appointment scheduling and insurance claims.

      Learning Notes #38 – Choreography Pattern | Cloud Pattern

      5 January 2025 at 12:21

      Today i learnt about Choreography pattern, where each and every service is communicating using a messaging queue. In this blog, i jot down notes on choreography pattern for my future self.

      What is the Choreography Pattern?

      In the Choreography Pattern, services communicate directly with each other via asynchronous events, without a central controller. Each service is responsible for a specific part of the workflow and responds to events produced by other services. This pattern allows for a more autonomous and loosely coupled system.

      Key Features

      • High scalability and independence of services.
      • Decentralized control.
      • Services respond to events they subscribe to.

      When to Use the Choreography Pattern

      • Event-Driven Systems: When workflows can be modeled as events triggering responses.
      • High Scalability: When services need to operate independently and scale autonomously.
      • Loose Coupling: When minimizing dependencies between services is critical.

      Benefits of the Choreography Pattern

      1. Decentralized Control: No single point of failure or bottleneck.
      2. Increased Flexibility: Services can be added or modified without affecting others.
      3. Better Scalability: Services operate independently and scale based on their workloads.
      4. Resilience: The system can handle partial failures more gracefully, as services continue independently.

      Example: E-Commerce Order Fulfillment

      Problem

      A fictional e-commerce platform needs to manage the following workflow:

      1. Accepting an order.
      2. Validating payment.
      3. Reserving inventory.
      4. Sending notifications to the customer.

      Each step is handled by an independent service.

      Solution

      Using the Choreography Pattern, each service listens for specific events and publishes new events as needed. The workflow emerges naturally from the interaction of these services.

      Implementation

      Step 1: Define the Workflow as Events

      • OrderPlaced: Triggered when a customer places an order.
      • PaymentProcessed: Triggered after successful payment.
      • InventoryReserved: Triggered after reserving inventory.
      • NotificationSent: Triggered when the customer is notified.

      Step 2: Implement Services

      Each service subscribes to events and performs its task.

      shared_utility.py

      import pika
      import json
      
      def publish_event(exchange, event_type, data):
          connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
          channel = connection.channel()
          channel.exchange_declare(exchange=exchange, exchange_type='fanout')
          message = json.dumps({"event_type": event_type, "data": data})
          channel.basic_publish(exchange=exchange, routing_key='', body=message)
          connection.close()
      
      def subscribe_to_event(exchange, callback):
          connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
          channel = connection.channel()
          channel.exchange_declare(exchange=exchange, exchange_type='fanout')
          queue = channel.queue_declare('', exclusive=True).method.queue
          channel.queue_bind(exchange=exchange, queue=queue)
          channel.basic_consume(queue=queue, on_message_callback=callback, auto_ack=True)
          print(f"Subscribed to events on exchange '{exchange}'")
          channel.start_consuming()
      
      

      Order Service

      
      from shared_utils import publish_event
      
      def place_order(order_id, customer):
          print(f"Placing order {order_id} for {customer}")
          publish_event("order_exchange", "OrderPlaced", {"order_id": order_id, "customer": customer})
      
      if __name__ == "__main__":
          # Simulate placing an order
          place_order(order_id=101, customer="John Doe")
      

      Payment Service

      
      from shared_utils import publish_event, subscribe_to_event
      import time
      
      def handle_order_placed(ch, method, properties, body):
          event = json.loads(body)
          if event["event_type"] == "OrderPlaced":
              order_id = event["data"]["order_id"]
              print(f"Processing payment for order {order_id}")
              time.sleep(1)  # Simulate payment processing
              publish_event("payment_exchange", "PaymentProcessed", {"order_id": order_id})
      
      if __name__ == "__main__":
          subscribe_to_event("order_exchange", handle_order_placed)
      
      

      Inventory Service

      
      from shared_utils import publish_event, subscribe_to_event
      import time
      
      def handle_payment_processed(ch, method, properties, body):
          event = json.loads(body)
          if event["event_type"] == "PaymentProcessed":
              order_id = event["data"]["order_id"]
              print(f"Reserving inventory for order {order_id}")
              time.sleep(1)  # Simulate inventory reservation
              publish_event("inventory_exchange", "InventoryReserved", {"order_id": order_id})
      
      if __name__ == "__main__":
          subscribe_to_event("payment_exchange", handle_payment_processed)
      
      

      Notification Service

      
      from shared_utils import subscribe_to_event
      import time
      
      def handle_inventory_reserved(ch, method, properties, body):
          event = json.loads(body)
          if event["event_type"] == "InventoryReserved":
              order_id = event["data"]["order_id"]
              print(f"Notifying customer for order {order_id}")
              time.sleep(1)  # Simulate notification
              print(f"Customer notified for order {order_id}")
      
      if __name__ == "__main__":
          subscribe_to_event("inventory_exchange", handle_inventory_reserved)
      
      

      Step 3: Run the Workflow

      1. Start RabbitMQ using Docker as described above.
      2. Run the services in the following order:
        • Notification Service: python notification_service.py
        • Inventory Service: python inventory_service.py
        • Payment Service: python payment_service.py
        • Order Service: python order_service.py
      3. Place an order by running the Order Service. The workflow will propagate through the services as events are handled.

      Key Considerations

      1. Event Bus: Use an event broker like RabbitMQ, Kafka, or AWS SNS to manage communication between services.
      2. Event Versioning: Include versioning to handle changes in event formats over time.
      3. Idempotency: Ensure services handle repeated events gracefully to avoid duplication.
      4. Monitoring and Tracing: Use tools like OpenTelemetry to trace and debug distributed workflows.
      5. Error Handling:
        • Dead Letter Queues (DLQs) to capture failed events.
        • Retries with backoff for transient errors.

      Advantages of the Choreography Pattern

      1. Loose Coupling: Services interact via events without direct knowledge of each other.
      2. Resilience: Failures in one service don’t block the entire workflow.
      3. High Autonomy: Services operate independently and can be deployed or scaled separately.
      4. Dynamic Workflows: Adding new services to the workflow requires subscribing them to relevant events.

      Challenges of the Choreography Pattern

      1. Complex Debugging: Tracing errors across distributed services can be difficult.
      2. Event Storms: Poorly designed workflows may generate excessive events, overwhelming the system.
      3. Coordination Overhead: Decentralized logic can lead to inconsistent behavior if not carefully managed.

      Orchestrator vs. Choreography: When to Choose?

      • Use Orchestrator Pattern when workflows are complex, require central control, or involve many dependencies.
      • Use Choreography Pattern when you need high scalability, loose coupling, or event-driven workflows.

      Learning Notes #37 – Orchestrator Pattern | Cloud Pattern

      5 January 2025 at 11:16

      Today, i learnt about orchestrator pattern, while l was learning about SAGA Pattern. It simplifies the coordination of these workflows, making the system more efficient and easier to manage. In this blog i jot down notes on Orchestrator Pattern for better understanding.

      What is the Orchestrator Pattern?

      The Orchestrator Pattern is a design strategy where a central orchestrator coordinates interactions between various services or components to execute a workflow.

      Unlike the Choreography Pattern, where services interact with each other independently and are aware of their peers, the orchestrator acts as the central decision-maker, directing how and when services interact.

      Key Features

      • Centralized control of workflows.
      • Simplified service communication.
      • Enhanced error handling and monitoring.

      When to Use the Orchestrator Pattern

      • Complex Workflows: When multiple services or steps need to be executed in a defined sequence.
      • Error Handling: When failures in one step require recovery strategies or compensating transactions.
      • Centralized Logic: When you want to encapsulate business logic in a single place for easier maintenance.

      Benefits of the Orchestrator Pattern

      1. Simplifies Service Communication: Services remain focused on their core functionality while the orchestrator manages interactions.
      2. Improves Scalability: Workflows can be scaled independently from services.
      3. Centralized Monitoring: Makes it easier to track the progress of workflows and debug issues.
      4. Flexibility: Changing a workflow involves modifying the orchestrator, not the services.

      Example: Order Processing Workflow

      Problem

      A fictional e-commerce platform needs to process orders. The workflow involves:

      1. Validating the order.
      2. Reserving inventory.
      3. Processing payment.
      4. Notifying the user.

      Each step is handled by a separate microservice.

      Solution

      We implement an orchestrator to manage this workflow. Let’s see how this works in practice.

      
      import requests
      
      class OrderOrchestrator:
          def __init__(self):
              self.services = {
                  "validate_order": "http://order-service/validate",
                  "reserve_inventory": "http://inventory-service/reserve",
                  "process_payment": "http://payment-service/process",
                  "notify_user": "http://notification-service/notify",
              }
      
          def execute_workflow(self, order_id):
              try:
                  # Step 1: Validate Order
                  self.call_service("validate_order", {"order_id": order_id})
      
                  # Step 2: Reserve Inventory
                  self.call_service("reserve_inventory", {"order_id": order_id})
      
                  # Step 3: Process Payment
                  self.call_service("process_payment", {"order_id": order_id})
      
                  # Step 4: Notify User
                  self.call_service("notify_user", {"order_id": order_id})
      
                  print(f"Order {order_id} processed successfully!")
              except Exception as e:
                  print(f"Error processing order {order_id}: {e}")
      
          def call_service(self, service_name, payload):
              url = self.services[service_name]
              response = requests.post(url, json=payload)
              if response.status_code != 200:
                  raise Exception(f"{service_name} failed: {response.text}")
      

      Key Tactics for Implementation

      1. Services vs. Serverless: Use serverless functions for steps that are triggered occasionally and don’t need always-on services, reducing costs.
      2. Recovery from Failures:
        • Retry Mechanism: Configure retries with limits and delays to handle transient failures.
        • Circuit Breaker Pattern: Detect and isolate failing services to allow recovery.
        • Graceful Degradation: Use fallbacks like cached results or alternate services to ensure continuity.
      3. Monitoring and Alerting:
        • Implement real-time monitoring with automated recovery strategies.
        • Set up alerts for exceptions and utilize logs for troubleshooting.
      4. Orchestration Service Failures:
        • Service Replication: Deploy multiple instances of the orchestrator for failover.
        • Data Replication: Ensure data consistency for seamless recovery.
        • Request Queues: Use queues to buffer requests during downtime and process them later.

      Important Considerations

      The primary goal of this architectural pattern is to decompose the entire business workflow into multiple services, making it more flexible and scalable. Due to this, it’s crucial to analyze and comprehend the business processes in detail before implementation. A poorly defined and overly complicated business process will lead to a system that would be hard to maintain and scale.

      Secondly, it’s easy to fall into the trap of adding business logic into the orchestration service. Sometimes it’s inevitable because certain functionalities are too small to create their separate service. But the risk here is that if the orchestration service becomes too intelligent and performs too much business logic, it can evolve into a monolithic application that also happens to talk to microservices. So, it’s crucial to keep track of every addition to the orchestration service and ensure that its work remains within the boundaries of orchestration. Maintaining the scope of the orchestration service will prevent it from becoming a burden on the system, leading to decreased scalability and flexibility.

      Why Use the Orchestration Pattern

      The pattern comes with the following advantages

      • Orchestration makes it easier to understand, monitor, and observe the application, resulting in a better understanding of the core part of the system with less effort.
      • The pattern promotes loose coupling. Each downstream service exposes an API interface and is self-contained, without any need to know about the other services.
      • The pattern simplifies the business workflows and improves the separation of concerns. Each service participates in a long-running transaction without any need to know about it.
      • The orchestrator service can decide what to do in case of failure, making the system fault-tolerant and reliable.

      Learning Notes #36 – Active Active / Active Passive Patterns | HA Patterns

      4 January 2025 at 18:04

      Today, i learnt about High Availability patterns. Basically on how to handle the clusters for high availability. In this blog i jot down notes on Active Active and Active Passive Patterns for better understanding.

      Active-Active Configuration

      In an Active-Active setup, all nodes in the cluster are actively processing requests. This configuration maximizes resource utilization and ensures high throughput. If one node fails, the remaining active nodes take over the load.

      Example Scenario

      Consider a web application with two servers:

      1. Server 1: IP 192.168.1.10
      2. Server 2: IP 192.168.1.11
      3. Server 3: IP 192.168.1.12
      4. Server 4: IP 192.168.1.13

      Both servers handle incoming requests simultaneously. A load balancer distributes traffic between these servers to balance the load.

      Pros and Cons

      Pros:

      • Higher resource utilization.
      • Better scalability and performance.

      Cons:

      • Increased complexity in handling data consistency and synchronization.
      • Potential for split-brain issues in certain setups.

      Sample HAProxy config

      
      frontend http_front
          bind *:80
          default_backend http_back
      
      defaults
          mode http
          timeout connect 5000ms
          timeout client 50000ms
          timeout server 50000ms
      
      backend http_back
          balance roundrobin
          server server_a 192.168.1.10:80 check
          server server_b 192.168.1.11:80 check
      

      Active-Passive Configuration

      In an Active-Passive setup, one node (Active) handles all the requests, while the other node (Passive) acts as a standby. If the active node fails, the passive node takes over.

      Example Scenario

      Using the same servers:

      1. Server 1: IP 192.168.1.10 (Active)
      2. Server 2: IP 192.168.1.11 (Active)
      3. Server 3: IP 192.168.1.12 (Passive)
      4. Server 4: IP 192.168.1.13 (Passive)

      Server B remains idle until Server A becomes unavailable, at which point Server B assumes the active role.

      Pros and Cons

      Pros:

      • Simplified consistency management.
      • Reliable failover mechanism.

      Cons:

      • Underutilized resources (passive node is idle most of the time).
      • Slight delay during failover.

      Sample HA Proxy Config

      
      frontend http_front
          bind *:80
          default_backend http_back
      
      defaults
          mode http
          timeout connect 5000ms
          timeout client 50000ms
          timeout server 50000ms
      
      backend http_back
          server server_a 192.168.1.10:80 check
          server server_b 192.168.1.11:80 check backup
      

      Learning Notes #35 – Durability in ACID | Postgres

      4 January 2025 at 12:47

      As part of ACID series, i am refreshing on topic Durability. In this blog i jot down notes on durability for better understanding.

      What is Durability?

      Durability ensures that the effects of a committed transaction are permanently saved to the database. This property prevents data loss by ensuring that committed transactions survive unexpected interruptions such as power outages, crashes, or system reboots.

      PostgreSQL achieves durability through a combination of

      • Write-Ahead Logging (WAL): Changes are written to a log file before they are applied to the database.
      • Checkpointing: Periodic snapshots of the database state.
      • fsync and Synchronous Commit: Ensures data is physically written to disk.

      How PostgreSQL Achieves Durability

      Miro Board for Postgres Architechture – https://miro.com/app/board/uXjVLD2T5os=/

      1. Write-Ahead Logging (WAL)

      PostgreSQL uses WAL to ensure durability. Before modifying the actual data, it writes the changes to a WAL file. This ensures that even if the system crashes, the database can be recovered by replaying the WAL logs.

      
      -- Enable WAL logging (default in PostgreSQL)
      SHOW wal_level;
      

      2. Checkpoints

      A checkpoint is a mechanism where the database writes all changes to disk, ensuring the database’s state is up-to-date. Checkpoints reduce the time required for crash recovery by limiting the number of WAL files that need to be replayed.

      
      -- Force a manual checkpoint
      CHECKPOINT;
      

      3. Synchronous Commit

      By default, PostgreSQL ensures that changes are flushed to disk before a transaction is marked as committed. This is controlled by the synchronous_commit setting.

      
      -- Show current synchronous commit setting
      SHOW synchronous_commit;
      
      -- Change synchronous commit setting
      SET synchronous_commit = 'on';
      

      4. Backup and Replication

      To further ensure durability, PostgreSQL supports backups and replication. Logical and physical backups can be used to restore data in case of catastrophic failures.

      Practical Examples of Durability

      Example 1: Ensuring Transaction Durability

      
      BEGIN;
      
      -- Update an account balance
      UPDATE accounts SET balance = balance - 500 WHERE account_id = 1;
      
      -- Commit the transaction
      COMMIT;
      
      -- Crash the system now; the committed transaction will persist.
      

      Even if the database crashes immediately after the COMMIT, the changes will persist, as the transaction logs have already been written to disk.

      Example 2: WAL Recovery after Crash

      Suppose a crash occurs immediately after a transaction is committed.

      Scenario:

      
      BEGIN;
      INSERT INTO transactions (account_id, amount, transaction_type) VALUES (1, 500, 'credit');
      COMMIT;
      

      During the recovery process, PostgreSQL replays the WAL logs to restore the committed transactions.

      Example 3: Configuring Synchronous Commit

      Control durability settings based on performance and reliability needs.

      
      -- Use asynchronous commit for faster performance (risking durability)
      SET synchronous_commit = 'off';
      
      -- Perform a transaction
      BEGIN;
      UPDATE accounts SET balance = balance + 200 WHERE account_id = 2;
      COMMIT;
      
      -- Changes might be lost if the system crashes before the WAL is flushed.
      

      Trade-offs of Durability

      While durability ensures data persistence, it can affect database performance. For example:

      • Enforcing synchronous commits may slow down transactions.
      • Checkpointing can momentarily impact query performance due to disk I/O.

      For high-performance systems, durability settings can be fine-tuned based on the application’s tolerance for potential data loss.

      Durability and Other ACID Properties

      Durability works closely with the other ACID properties:

      1. Atomicity: Ensures the all-or-nothing nature of transactions.
      2. Consistency: Guarantees the database remains in a valid state after a transaction.
      3. Isolation: Prevents concurrent transactions from interfering with each other.

      Learning Notes #34 – Consistency (Correctness) in ACID | Postgres

      4 January 2025 at 12:37

      As part of the ACID Series, i am refreshing on consistency. In this blog, i jot down notes on consistency (correctness) in postgres database.

      What is Consistency?

      Consistency ensures that a transaction brings the database from one valid state to another, adhering to predefined rules such as constraints, triggers, and relational integrity. If a transaction violates these rules, it is aborted, and the database remains unchanged. This guarantees that only valid data exists in the database.

      Consistency works together with other ACID properties:

      • Atomicity ensures the β€œall-or-nothing” execution of a transaction.
      • Isolation ensures transactions don’t interfere with each other.
      • Durability guarantees committed transactions persist despite system failures

      Key Aspects of Consistency in PostgreSQL

      1. Constraints
        • Primary Key: Ensures uniqueness of rows.
        • Foreign Key: Maintains referential integrity.
        • Check Constraints: Enforces custom business rules.
        • Not Null: Ensures that specific columns cannot have null values.
      2. Triggers
        • Custom logic executed before or after specific database events.
      3. Rules
        • Enforce application-specific invariants on the database.
      4. Transactions
        • Changes are made in a controlled environment, ensuring consistency even in the event of errors or system failures.

      Practical Examples of Consistency in PostgreSQL

      1. Primary Key Constraint

      Ensures that no two rows in a table have the same primary key value.

      
      CREATE TABLE accounts (
          account_id SERIAL PRIMARY KEY,
          account_holder_name VARCHAR(255) NOT NULL,
          balance NUMERIC(15, 2) NOT NULL CHECK (balance >= 0)
      );
      
      -- Attempt to insert duplicate primary keys.
      INSERT INTO accounts (account_id, account_holder_name, balance)
      VALUES (1, 'Alice', 1000.00);
      
      INSERT INTO accounts (account_id, account_holder_name, balance)
      VALUES (1, 'Bob', 2000.00); -- This will fail.
      

      2. Foreign Key Constraint

      Enforces referential integrity between tables.

      
      CREATE TABLE transactions (
          transaction_id SERIAL PRIMARY KEY,
          account_id INT NOT NULL REFERENCES accounts(account_id),
          amount NUMERIC(15, 2) NOT NULL,
          transaction_type VARCHAR(10) NOT NULL CHECK (transaction_type IN ('credit', 'debit')),
          transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
      );
      
      -- Attempt to insert a transaction for a non-existent account.
      INSERT INTO transactions (account_id, amount, transaction_type)
      VALUES (999, 500, 'credit'); -- This will fail.
      

      3. Check Constraint

      Validates custom business rules.

      
      -- Ensure account balance cannot go negative.
      INSERT INTO accounts (account_holder_name, balance)
      VALUES ('Charlie', -500); -- This will fail due to the CHECK constraint.
      

      4. Trigger for Business Logic

      Ensures derived data or additional checks are implemented.

      
      CREATE OR REPLACE FUNCTION enforce_minimum_balance()
      RETURNS TRIGGER AS $$
      BEGIN
          IF NEW.balance < 0 THEN
              RAISE EXCEPTION 'Balance cannot be negative';
          END IF;
          RETURN NEW;
      END;
      $$ LANGUAGE plpgsql;
      
      CREATE TRIGGER check_balance_before_insert
      BEFORE INSERT OR UPDATE ON accounts
      FOR EACH ROW EXECUTE FUNCTION enforce_minimum_balance();
      
      -- Attempt to update an account with a negative balance.
      UPDATE accounts SET balance = -100 WHERE account_id = 1; -- This will fail.
      

      5. Transactions to Maintain Consistency

      A transaction groups multiple operations into a single unit, ensuring all succeed or none.

      
      BEGIN;
      
      -- Deduct from sender's account.
      UPDATE accounts SET balance = balance - 500 WHERE account_id = 1;
      
      -- Credit to receiver's account.
      UPDATE accounts SET balance = balance + 500 WHERE account_id = 2;
      
      -- If any operation fails, rollback the transaction.
      COMMIT;
      

      If the system crashes before the COMMIT, the database remains unchanged, ensuring consistency.

      How Consistency Works with Other ACID Properties

      1. With Atomicity: If any step in a transaction violates a constraint, the entire transaction is rolled back, ensuring that the database remains consistent.
      2. With Isolation: Concurrent transactions operate independently, preventing inconsistent states caused by interference.
      3. With Durability: Once a transaction is committed, its consistency guarantees persist even in the event of a crash.

      Benefits of Consistency

      1. Data Integrity: Prevents invalid data from being stored.
      2. Application Reliability: Reduces the need for additional application-level checks.
      3. Simplified Maintenance: Developers can rely on the database to enforce business rules and relationships.
      4. Error Prevention: Constraints and triggers act as safeguards, catching mistakes early.

      Learning Notes #30 – Queue Based Loading | Cloud Patterns

      3 January 2025 at 14:47

      Today, i learnt about Queue Based Loading pattern, which helps to manage intermittent peak load to a service via queues. Basically decoupling Tasks from Services. In this blog i jot down notes on this pattern for my future self.

      In today’s digital landscape, applications are expected to handle large-scale operations efficiently. Whether it’s processing massive data streams, ensuring real-time responsiveness, or integrating with multiple third-party services, scalability and reliability are paramount. One pattern that elegantly addresses these challenges is the Queue-Based Loading Pattern.

      What Is the Queue-Based Loading Pattern?

      The Queue-Based Loading Pattern leverages message queues to decouple and coordinate tasks between producers (such as applications or services generating data) and consumers (services or workers processing that data). By using queues as intermediaries, this pattern allows systems to manage workloads efficiently, ensuring seamless and scalable operation.

      Key Components of the Pattern

      1. Producers: Producers are responsible for generating tasks or data. They send these tasks to a message queue instead of directly interacting with consumers. Examples include:
        • Web applications logging user activity.
        • IoT devices sending sensor data.
      2. Message Queue: The queue acts as a buffer, storing tasks until consumers are ready to process them. Popular tools for implementing queues include RabbitMQ, Apache Kafka, AWS SQS, and Redis.
      3. Consumers: Consumers retrieve messages from the queue and process them asynchronously. They are typically designed to handle tasks independently and at their own pace.
      4. Processing Logic: This is the core functionality that processes the tasks retrieved by consumers. For example, resizing images, sending notifications, or updating a database.

      How It Works

      1. Task Generation: Producers push tasks to the queue as they are generated.
      2. Message Storage: The queue stores tasks in a structured manner (FIFO, priority-based, etc.) and ensures reliable delivery.
      3. Task Consumption: Consumers pull tasks from the queue, process them, and optionally acknowledge completion.
      4. Scalability: New consumers can be added dynamically to handle increased workloads, ensuring the system remains responsive.

      Benefits of the Queue-Based Loading Pattern

      1. Decoupling: Producers and consumers operate independently, reducing tight coupling and improving system maintainability.
      2. Scalability: By adding more consumers, systems can easily scale to handle higher workloads.
      3. Fault Tolerance: If a consumer fails, messages remain in the queue, ensuring no data is lost.
      4. Load Balancing: Tasks are distributed evenly among consumers, preventing any single consumer from becoming a bottleneck.
      5. Asynchronous Processing: Consumers can process tasks in the background, freeing producers to continue generating data without delay.

      Issues and Considerations

      1. Rate Limiting: Implement logic to control the rate at which services handle messages to prevent overwhelming the target resource. Test the system under load and adjust the number of queues or service instances to manage demand effectively.
      2. One-Way Communication: Message queues are inherently one-way. If tasks require responses, you may need to implement a separate mechanism for replies.
      3. Autoscaling Challenges: Be cautious when autoscaling consumers, as it can lead to increased contention for shared resources, potentially reducing the effectiveness of load leveling.
      4. Traffic Variability: Consider the variability of incoming traffic to avoid situations where tasks pile up faster than they are processed, creating a perpetual backlog.
      5. Queue Persistence: Ensure your queue is durable and capable of persisting messages. Crashes or system limits could lead to dropped messages, risking data loss.

      Use Cases

      1. Email and Notification Systems: Sending bulk emails or push notifications without overloading the main application.
      2. Data Pipelines: Ingesting, transforming, and analyzing large datasets in real-time or batch processing.
      3. Video Processing: Queues facilitate tasks like video encoding and thumbnail generation.
      4. Microservices Communication: Ensures reliable and scalable communication between microservices.

      Best Practices

      1. Message Durability: Configure your queue to persist messages to disk, ensuring they are not lost during system failures.
      2. Monitoring and Metrics: Use monitoring tools to track queue lengths, processing rates, and consumer health.
      3. Idempotency: Design consumers to handle duplicate messages gracefully.
      4. Error Handling and Dead Letter Queues (DLQs): Route failed messages to DLQs for later analysis and reprocessing.

      Learning Notes #29 – Two Phase Commit Protocol | ACID in Distributed Systems

      3 January 2025 at 13:45

      Today, i learnt about compensating transaction pattern which leads to two phase commit protocol which helps in maintaining the Atomicity of a distributed transactions. Distributed transactions are hard.

      In this blog, i jot down notes on Two Phase Commit protocol for better understanding.

      The Two-Phase Commit (2PC) protocol is a distributed algorithm used to ensure atomicity in transactions spanning multiple nodes or databases. Atomicity ensures that either all parts of a transaction are committed or none are, maintaining consistency in distributed systems.

      Why Two-Phase Commit?

      In distributed systems, a transaction might involve several independent nodes, each maintaining its own database. Without a mechanism like 2PC, failures in one node can leave the system in an inconsistent state.

      For example, consider an e-commerce platform where a customer places an order.

      The transaction involves updating the inventory in one database, recording the payment in another, and generating a shipment request in a third system. If the payment database successfully commits but the inventory database fails, the system becomes inconsistent, potentially causing issues like double selling or incomplete orders. 2PC mitigates this by providing a coordinated protocol to commit or abort transactions across all nodes.

      The Phases of 2PC

      The protocol operates in two main phases

      1. Prepare Phase (Voting Phase)

      The coordinator node initiates the transaction and prepares to commit it across all participating nodes.

      1. Request to Prepare: The coordinator sends a PREPARE request to all participant nodes.
      2. Vote: Each participant checks if it can commit the transaction (e.g., no constraints violated, resources available). It logs its decision (YES or NO) locally and sends its vote to the coordinator. If any participant votes NO, the transaction cannot be committed.

      2. Commit Phase (Decision Phase)

      Based on the votes received in the prepare phase, the coordinator decides the final outcome.

      Commit Decision:

      If all participants vote YES, the coordinator logs a COMMIT decision, sends COMMIT messages to all participants, and participants apply the changes and confirm with an acknowledgment.

      Abort Decision:

      If any participant votes NO, the coordinator logs an ABORT decision, sends ABORT messages to all participants, and participants roll back any changes made during the transaction.

      Implementation:

      For a simple implementation of 2PC, we can try out the below flow using RabbitMQ as a medium for Co-Ordinator.

      Basically, we need not to write this from scratch, we have tools,

      1. Relational Databases

      Most relational databases have built-in support for distributed transactions and 2PC.

      • PostgreSQL: Implements distributed transactions using foreign data wrappers (FDWs) with PREPARE TRANSACTION and COMMIT PREPARED.
      • MySQL: Supports XA transactions, which follow the 2PC protocol.
      • Oracle Database: Offers robust distributed transaction support using XA.
      • Microsoft SQL Server: Provides distributed transactions through MS-DTC.

      2. Distributed Transaction Managers

      These tools manage distributed transactions across multiple systems.

      • Atomikos: A popular Java-based transaction manager supporting JTA/XA for distributed systems.
      • Bitronix: Another lightweight transaction manager for Java applications supporting JTA/XA.
      • JBoss Transactions (Narayana): A robust Java transaction manager that supports 2PC, often used in conjunction with JBoss servers.

      3. Message Brokers

      Message brokers provide transaction capabilities with 2PC.

      • RabbitMQ: Supports the 2PC protocol using transactional channels.
      • Apache Kafka: Supports transactions, ensuring β€œexactly-once” semantics across producers and consumers.
      • ActiveMQ: Provides distributed transaction support through JTA integration

      4. Workflow Engines

      Workflow engines can orchestrate 2PC across distributed systems.

      • Apache Camel: Can coordinate 2PC transactions using its transaction policy.
      • Camunda: Provides BPMN-based orchestration that can include transactional boundaries.
      • Zeebe: Supports distributed transaction workflows in modern architectures.

      Key Properties of 2PC

      1. Atomicity: Ensures all-or-nothing transaction behavior.
      2. Consistency: Guarantees system consistency across all nodes.
      3. Durability: Uses logs to ensure decisions survive node failures.

      Challenges of 2PC

      1. Blocking Nature: If the coordinator fails during the commit phase, participants must wait indefinitely unless a timeout or external mechanism is implemented.
      2. Performance Overhead: Multiple message exchanges and logging operations introduce latency.
      3. Single Point of Failure: The coordinator’s failure can stall the entire transaction.

      Learning Notes #22 – Claim Check Pattern | Cloud Pattern

      31 December 2024 at 17:03

      Today, i learnt about claim check pattern, which tells how to handle a big message into the queue. Every message broker has a defined message size limit. If our message size exceeds the size, it wont work.

      The Claim Check Pattern emerges as a pivotal architectural design to address challenges in managing large payloads in a decoupled and efficient manner. In this blog, i jot down notes on my learning for my future self.

      What is the Claim Check Pattern?

      The Claim Check Pattern is a messaging pattern used in distributed systems to manage large messages efficiently. Instead of transmitting bulky data directly between services, this pattern extracts and stores the payload in a dedicated storage system (e.g., object storage or a database).

      A lightweight reference or β€œclaim check” is then sent through the message queue, which the receiving service can use to retrieve the full data from the storage.

      This pattern is inspired by the physical process of checking in luggage at an airport: you hand over your luggage, receive a claim check (a token), and later use it to retrieve your belongings.

      How Does the Claim Check Pattern Work?

      The process typically involves the following steps

      1. Data Submission The sender service splits a message into two parts:
        • Metadata: A small piece of information that provides context about the data.
        • Payload: The main body of data that is too large or sensitive to send through the message queue.
      2. Storing the Payload
        • The sender uploads the payload to a storage service (e.g., AWS S3, Azure Blob Storage, or Google Cloud Storage).
        • The storage service returns a unique identifier (e.g., a URL or object key).
      3. Sending the Claim Check
        • The sender service places the metadata and the unique identifier (claim check) onto the message queue.
      4. Receiving the Claim Check
        • The receiver service consumes the message from the queue, extracts the claim check, and retrieves the payload from the storage system.
      5. Processing
        • The receiver processes the payload alongside the metadata as required.

      Use Cases

      1. Media Processing Pipelines In video transcoding systems, raw video files can be uploaded to storage while metadata (e.g., video format and length) is passed through the message queue.

      2. IoT Systems – IoT devices generate large datasets. Using the Claim Check Pattern ensures efficient transmission and processing of these data chunks.

      3. Data Processing Workflows – In big data systems, datasets can be stored in object storage while processing metadata flows through orchestration tools like Apache Airflow.

      4. Event-Driven Architectures – For systems using event-driven models, large event payloads can be offloaded to storage to avoid overloading the messaging layer.

      Example with RabbitMQ

      1.Sender Service

      
      import boto3
      import pika
      
      s3 = boto3.client('s3')
      bucket_name = 'my-bucket'
      object_key = 'data/large-file.txt'
      
      response = s3.upload_file('large-file.txt', bucket_name, object_key)
      claim_check = f's3://{bucket_name}/{object_key}'
      
      # Connect to RabbitMQ
      connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
      channel = connection.channel()
      
      # Declare a queue
      channel.queue_declare(queue='claim_check_queue')
      
      # Send the claim check
      message = {
          'metadata': 'Some metadata',
          'claim_check': claim_check
      }
      channel.basic_publish(exchange='', routing_key='claim_check_queue', body=str(message))
      
      connection.close()
      

      2. Consumer

      
      import boto3
      import pika
      
      s3 = boto3.client('s3')
      
      # Connect to RabbitMQ
      connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
      channel = connection.channel()
      
      # Declare a queue
      channel.queue_declare(queue='claim_check_queue')
      
      # Callback function to process messages
      def callback(ch, method, properties, body):
          message = eval(body)
          claim_check = message['claim_check']
      
          bucket_name, object_key = claim_check.replace('s3://', '').split('/', 1)
          s3.download_file(bucket_name, object_key, 'retrieved-large-file.txt')
          print("Payload retrieved and processed.")
      
      # Consume messages
      channel.basic_consume(queue='claim_check_queue', on_message_callback=callback, auto_ack=True)
      
      print('Waiting for messages. To exit press CTRL+C')
      channel.start_consuming()
      

      References

      1. https://learn.microsoft.com/en-us/azure/architecture/patterns/claim-check
      2. https://medium.com/@dmosyan/claim-check-design-pattern-603dc1f3796d

      Learning Notes #19 – Blue Green Deployments – An near ZERO downtime deployment

      30 December 2024 at 18:19

      Today, i got refreshed on Blue Green Deployment from a podcast https://open.spotify.com/episode/03p86zgOuSEbNezK71CELH. Deployment designing is a plate i haven’t touched yet. In this blog i jot down the notes on blue green deployment for my future self.

      What is Blue-Green Deployment?

      Blue-Green Deployment is a release management strategy that involves maintaining two identical environments, referred to as β€œBlue” and β€œGreen.” At any point in time, only one environment is live (receiving traffic), while the other remains idle or in standby. Updates are deployed to the idle environment, thoroughly tested, and then switched to live with minimal downtime.

      How It Works

      • This approach involves setting up two environments: the Blue environment, which serves live traffic, and the Green environment, a replica used for staging updates.
      • Updates are first deployed to the Green environment, where comprehensive testing is performed to ensure functionality, performance, and integration meet expectations.
      • Once testing is successful, the routing mechanism, such as a DNS or API Gateway or load balancer, is updated to redirect traffic from the Blue environment to the Green environment.
      • The Green environment then becomes live, while the Blue environment transitions to an idle state.
      • If issues arise, traffic can be reverted to the Blue environment for a quick recovery with minimal impact.

      Benefits of Blue-Green Deployment

      • Blue-Green Deployment provides zero downtime during the deployment process, ensuring uninterrupted user experiences.
      • Rollbacks are simplified because the previous version remains intact in the Blue environment, enabling quick reversion if necessary. Consideration of forward and backwar capability is important. eg, Database.
      • It also allows seamless testing in the Green environment before updates go live, reducing risks by isolating production from deployment issues.

      Challenges and Considerations

      • Maintaining two identical environments can be resource intensive.
      • Ensuring synchronization between environments is critical to prevent discrepancies in configuration and data.
      • Handling live database changes during the environment switch is complex, requiring careful planning for database migrations.

      Implementing Blue-Green Deployment (Not Yet Tried)

      • Several tools and platforms support Blue-Green Deployment. Kubernetes simplifies managing multiple environments through namespaces and services.
      • AWS Elastic Beanstalk offers built-in support for Blue-Green Deployment, while HashiCorp Terraform automates the setup of Blue-Green infrastructure.
      • To implement this strategy, organizations should design infrastructure capable of supporting two identical environments, automate deployments using CI/CD pipelines, monitor and test thoroughly, and define rollback procedures to revert to previous versions when necessary.

      Reference:

      ❌
      ❌