Tuesday, 14 January 2025

RESTful vs GraphQL: Making the Right Design Decision


Why Enterprises Are Embracing GraphQL Over REST ?

RESTful API: A web service architecture that uses standardized HTTP methods and endpoints to manage resources.

GraphQL: A query language for APIs that allows clients to request exactly the data they need from a single endpoint.

In recent years, many enterprises have started recommending GraphQL as the preferred API architecture over traditional RESTful APIs

While REST has long been the industry standard for building APIs due to its simplicity, standardization, and widespread adoption, GraphQL introduces a new paradigm that addresses some of REST's inherent limitations.

This shift is driven by the increasing need for flexibility, efficiency, and performance in enterprise systems. As applications grow in complexity—especially with the rise of microservices, multiple client types (web, mobile, IoT), and data-driven experiences—GraphQL’s ability to adapt to these demands makes it a strong contender.

  1. Microservices: Combines data from multiple microservices into a single API, reducing client-side complexity.
  2. Multiple Clients: Allows each client (web, mobile, IoT) to fetch exactly the data it needs, avoiding over-fetching or under-fetching.
  3. Efficient Data Fetching: Fetches all required data in one query, reducing latency and API calls.
  4. Evolving APIs: Supports schema-driven, backward-compatible evolution of APIs.
  5. Developer Experience: Self-documenting schema simplifies development and debugging.

GraphQL excels at managing diverse data needs efficiently, making it ideal for modern, complex apps.

Comparison of RESTful APIs vs GraphQL : Features, Examples, and Winners ??

FeatureRESTful APIGraphQLWinnerReal-Time Example
Data FetchingRequires multiple endpoints for different resources, which may lead to over-fetching (retrieving unnecessary data) or under-fetching (not getting enough data).Allows the client to specify exactly which fields and subfields of data are needed, thus avoiding over-fetching and under-fetching.✔ GraphQLREST: A weather API with separate endpoints for current weather (/weather/current), forecast (/weather/forecast), and alerts (/weather/alerts), requiring separate calls.
GraphQL: A single query fetching current weather, forecast, and alerts at once with only the fields needed (e.g., temperature, humidity).
EfficiencyOften requires multiple requests to different endpoints to fetch related data (increasing the number of requests).Can gather all related data in one request, reducing the overall number of API calls and improving efficiency.✔ GraphQLREST: An e-commerce site with separate endpoints for products, categories, and reviews, requiring multiple API calls.
GraphQL: A single query fetching products, categories, and reviews in one call, optimizing the data retrieval process.
VersioningTypically requires versioning through URL paths (e.g., /v1/endpoint, /v2/endpoint).No versioning required; the API evolves without breaking existing queries.✔ GraphQLREST: A social media API that uses versioning like /v1/users, /v2/users.
GraphQL: An API where clients continue using the same endpoint, and the schema evolves to include new fields without breaking existing clients.
FlexibilityThe server defines the structure of responses, limiting client control over the data returned.The client defines the structure of the response, providing more flexibility in data retrieval.✔ GraphQLREST: A music streaming API always returns a fixed set of data, even if the client doesn't need all of it.
GraphQL: A music API where the client can request only specific data, like song names and album artwork, without any unnecessary fields.
ComplexitySimple to implement for basic applications, but may become cumbersome as complexity grows.More complex to set up but highly beneficial for large, complex applications with various data requirements.✔ RESTful API (for simplicity)REST: A simple blog API that retrieves posts with fixed data.
GraphQL: A complex blogging platform API that allows querying multiple related entities like posts, authors, and comments in one request.
Error HandlingUses HTTP status codes (e.g., 404, 500) for clear indication of success or failure, making it easy to detect errors immediately.Returns a 200 OK status code even if there's an error. Errors are embedded in the response body, requiring extra steps to detect and interpret.✔ RESTful APIREST: A file upload API returning 404 if the file is not found, or 500 for a server issue.
GraphQL: A file upload request may return 200 OK, but you need to inspect the body to find the error (e.g., {"errors": [{"message": "File not found"}]}).

If GraphQL is the clear winner from above table, why do enterprises still use RESTful APIs?

While GraphQL is a great option for specific scenarios that require flexibility and efficiency, RESTful APIs continue to dominate in enterprise environments for their:

  • Simplicity and ease of implementation.
  • Widespread support and ecosystem.
  • Standardization and interoperability.
  • Mature tooling for monitoring, security, and versioning.
  • Compatibility with legacy systems.

Many enterprises continue to use REST because it meets their needs, is easier to implement, and has an established track record. As GraphQL evolves, however, it’s becoming a strong contender for new applications or for areas where dynamic, flexible data fetching is required.

Regular Use Cases :

GraphQL for structured data (e.g., text, metadata).

RESTful APIs for multimedia (e.g., images, videos, audio).

Why GraphQL for Structured Data?

  1. Precise Data Fetching:

    • GraphQL allows clients to request exactly the structured data (e.g., text, metadata, attributes) they need, avoiding over-fetching or under-fetching.
    • This is particularly useful for dynamic data models where the client requirements may vary.
  2. Relationships and Hierarchical Data:

    • Structured data often includes nested or relational information. GraphQL excels at handling these with a single query.
  3. Dynamic Query Capabilities:

    • Clients can modify their queries based on changing needs without requiring API endpoint modifications, making GraphQL ideal for structured and evolving datasets.

Why RESTful APIs for Multimedia?

  1. Simple and Efficient for Binary Data:

    • REST is well-suited for delivering multimedia files like images, videos, or audio using HTTP’s native support for file transfers (e.g., GET requests).
  2. Caching and Content Delivery:

    • Multimedia files are often cached using CDNs or HTTP caching mechanisms. RESTful APIs integrate seamlessly with these systems, providing better performance.
  3. No Need for Query Flexibility:

    • Multimedia typically doesn’t require the complex querying capabilities of GraphQL; clients usually need the entire file, not partial or customized content.
  4. Streamlined Download Process:

    • REST endpoints allow for straightforward URL-based access to files, making integration with file-handling tools or download managers easy.


Monday, 30 December 2024

Key Design Considerations for Effective Salesforce Data Reconciliation

 Business Use Case: 

Salesforce serves as the entry point for creating cases & Updates. When a case is created or updated in Salesforce, the data is sent to an external system via a middle layer for processing. The external system is the source of truth, and once the case is successfully created there, the data flows back into Salesforce through the same middle layer.

However, only a subset of the case data fields is saved in the Salesforce database. Data reconciliation is needed to handle scenarios where a case is successfully created in the external system but fails to get updated or recorded in Salesforce. This ensures that both systems remain synchronized and data integrity is maintained.

Objective of Data Reconciliation :

  1. Ensure Data Consistency: Maintain alignment between Salesforce and the external system for accurate reporting and operations.

  2. Identify and Resolve Discrepancies: Detect missing, incomplete, or incorrect data and address the root causes.

  3. Enhance Data Reliability: Build trust in the data by ensuring it reflects the source of truth.

  4. Minimize Business Impact: Prevent errors or delays caused by data mismatches in critical workflows.

  5. Support Compliance: Ensure data integrity to meet audit and regulatory requirements.

Possible Reasons for Data Discrepancies :

Sno

Category

Reason for Discrepancy

1

Connectivity Issues

- Network interruptions between systems.

- Downtime or unavailability of systems.

2

API Failures

- Timeouts during API calls.

- Rate limits exceeded.

- Invalid authentication.

3

Data Mapping Errors

- Incorrect field mappings.

- Schema changes not updated in integration logic.

4

Validation Failures

- Salesforce validation rules rejecting data.

- Missing mandatory fields.

5

Middleware Errors

- Logic issues in the middleware causing dropped or stuck cases.

- Failed retries.

6

Duplicate Data

- Duplicate case creation due to retries or misconfigurations.

7

Incomplete Data

- Field truncation or data loss.

- Partial updates with missing fields.

8

Integration Gaps

- Lack of synchronization for updates.

- Missed notifications or events.

Proposed Approach/Process for Data Reconciliation :

1. Who should be responsible for creating data reconciliation jobs when Salesforce acts as the entry point for data creation, but the data is first recorded in the external system, with the middle layer serving only as a pass-through?

System

Responsibility

External System

- Primary owner of data reconciliation.

- Ensures data consistency and integrity between systems.

- Records all cases and manages the authoritative data.

- Handles discrepancies and logs errors.

Middle Layer

- Acts as a supporting role in data reconciliation.

- Provides detailed logs of data transfers and errors.

- Monitors data flow and triggers alerts for issues.

- Can initiate retries or handle temporary failures.

Salesforce

- Consumer role in data reconciliation.

- Monitors and displays reconciled data.

- Requests reconciliation jobs from the external system if discrepancies are found.

- Ensures data accuracy for reporting and operations in Salesforce.

2. What is the best approach for ensuring data reconciliation when the external system cannot directly verify if a record was created / updated in Salesforce?

Data Synchronization and Reconciliation for Case Creation :

  1. The external system creates the case, sending a request to the middle layer.

  2. The middle layer sends the case creation request to Salesforce.

  3. Upon success or failure of the case creation in Salesforce, the middle layer immediately acknowledges the response back to the external system (Acknowledgment Mechanism Approach).

    1. If the creation is successful, the external system will be notified by the middle layer.

      1.  Salesforce then updates the external system record with the Salesforce Case ID through the middle layer, ensuring that the external system’s record is synchronized.

      2. If the update of the Salesforce Case ID  fails even after retries, Salesforce can initiate a data reconciliation process to identify the record discrepancies and reattempt the update.For specific to this scenario requires data reconciliation job at Salesforce end.

    2. If the creation is unsuccessful, the external system will be notified by the middle layer, and necessary actions (e.g., retries or manual intervention) can be taken by the middle layer and external system.

  4. For cases created successfully, the Salesforce Case ID will be populated in the external system. The external system can periodically check for records with null Salesforce Case IDs to identify those that didn't sync properly.

    1. If a null  is found, it will trigger the case record creation process again, ensuring that missing records are re-synced.

Data Synchronization and Reconciliation for Case Updates :

  1. Trigger Status Reset on New Updates:Every time the same record update request is made in Salesforce, the Update Sync Status automatically resets to In Progress to accommodate the latest update attempt.Since Salesforce is the entry point on every update request this field value also passed respectively.

  2. The external system updates the case, sending a request to the middle layer.

  3. The middle layer sends the case update request to Salesforce.

  4. Upon success or failure of the case update in Salesforce, the middle layer immediately acknowledges the response back to the external system (Acknowledgment Mechanism Approach).

    1. If the update is successful, the external system will be notified by the middle layer.

      1.  Salesforce then updates the external system record with the Update Sync Status(Field) value with ‘Success’ through the middle layer, ensuring that the external system’s record is synchronized.

      2. If the update of the Update Sync field  fails even after retries, Salesforce can initiate a data reconciliation process to identify the record discrepancies and reattempt the update.For specific to this scenario requires data reconciliation job at Salesforce end.

    2. If the update is unsuccessful, the external system will be notified by the middle layer, and necessary actions (e.g., retries or manual intervention) can be taken by the middle layer and external system.

  5. For cases updated successfully, the Update Sync Status( Field) will be populated with the value ‘Success’  in the external system. The external system can periodically check for records with ‘Failed’ status in the field Update Sync Status to identify those that didn't sync properly.

    1. If a record  is found, it will trigger the case record update process again, ensuring that missing records are re-synced.

Operational Add-Ons  :

Sno

Add-On

Description

Benefits

Responsibility

1

On-Demand Reconciliation Jobs

Salesforce can request the external system to run data reconciliation jobs for a specific date range.

Provides flexibility to address discrepancies for specific periods or scenarios.

External System

2

Long-Term Failed Records Escalation

Automatically identify and escalate records stuck in a failed state for an extended period.

Ensures no critical updates remain unresolved, reducing business impact.

External System

3

Monitoring and Reporting Dashboards

Create dashboards to track reconciliation job status, pending records, and resolution trends.

Improves visibility into system performance and reconciliation outcomes.

Salesforce/External System

4

Audit Trail for Reconciliation Events

Maintain a detailed log of reconciliation attempts, including timestamps, statuses, and error messages.

Enhances troubleshooting and accountability for failed reconciliations.

External System


Summary Action Table : 

Layer

Action Item

Salesforce

- Receives and processes case creation and update requests from the middle layer.


- Acknowledges success/failure of case creation or update to the middle layer.


- Updates external system records with Salesforce Case ID after successful creation (for case creation).


- Resets "Update Sync Status" to "In Progress" upon receiving update requests.


- Initiates data reconciliation process if Salesforce Case ID or "Update Sync Status" update fails.


- Requests external system to run data reconciliation jobs for a specific date range (on-demand).


- Displays reconciliation status and logs for transparency and reporting.

Middle Layer

- Acts as a pass-through, routing case creation/update requests between Salesforce and the external system.


- Acknowledges the success/failure of case creation and updates to the external system.


- Sends case creation/update requests to Salesforce.


- Ensures successful synchronization between Salesforce and the external system by passing Salesforce Case ID and "Update Sync Status" back to the external system.


- Triggers retries for failed updates (for case creation and update).

External System

- Manages the authoritative data and records all cases.


- Ensures data integrity and consistency between systems.


- Monitors case creation and update status for discrepancies (e.g., null Salesforce Case IDs or failed updates).


- Initiates retries in case of failed crete / updates.


- Runs data reconciliation jobs periodically to identify missing or failed records.


- Receives reconciliation job requests from Salesforce for specific date ranges.


- Escalates long-term failed records for manual intervention if needed.


- Maintains detailed audit trail logs of reconciliation attempts for accountability.