Web Development Technologies

Chapter 6
Server-Side Development and API Engineering

Post Graduate Program in IT Engineering
Web Development Technologies   •   Post Graduate

Server-Side Development and API Engineering

Introduction and Context

Server-side development is the design and implementation of software that executes on servers rather than in a user’s browser or mobile device. It receives requests, applies business rules, accesses data and external services, enforces security controls, and returns responses. API engineering is the disciplined design of interfaces through which applications, services, devices and organizations exchange data and operations.

Modern web systems are rarely single applications. They are ecosystems of browsers, mobile clients, databases, payment providers, identity services, message brokers, analytics platforms and internal microservices. Consequently, server-side engineering involves more than writing endpoint handlers. It requires decisions about architecture, protocols, data contracts, validation, reliability, observability, governance and long-term evolution.

The dominant application protocol is HTTP. REST remains widely used for resource-oriented services; GraphQL provides client-directed querying; and WebSocket-based systems support bidirectional, low-latency communication. Webhooks extend API interaction by allowing a provider to notify a consumer when an event occurs.

Learning Objectives

After studying this chapter, a learner should be able to:

1. Explain the architecture and execution model of server-side applications.

2. Select suitable languages, frameworks and architectural styles.

3. Design secure, usable REST, GraphQL and real-time APIs.

4. Apply validation, middleware, serialization, error handling and versioning.

5. Design API documentation, webhooks and third-party integrations.

6. Evaluate reliability, security, scalability and governance trade-offs.

7. Analyse an API architecture through a realistic engineering case.

1. The Server-Side Execution Model

A typical request passes through several stages:

1. A client resolves a domain name and establishes a connection, commonly using HTTPS.

2. A load balancer or gateway selects an application instance.

3. Middleware performs concerns such as authentication, logging, rate limiting and correlation-ID creation.

4. A route handler validates input and invokes application services.

5. Domain logic applies business rules and may access databases, caches, queues or third-party services.

6. A serializer converts the result into a representation such as JSON.

7. The server returns a status code, headers and response body.

A useful separation is:

  • Transport layer: HTTP methods, routing, headers, status codes and content negotiation.
  • Application layer: use cases, orchestration and transaction boundaries.
  • Domain layer: business rules and domain concepts.
  • Infrastructure layer: databases, queues, file systems and external APIs.

This separation reduces coupling. For example, changing a database adapter should not require rewriting validation or domain rules. In practice, strict layering may be combined with modular monoliths, microservices or event-driven components.

Frameworks and Runtime Choices

Common server-side ecosystems include Java with Spring, C# with ASP.NET Core, JavaScript or TypeScript with Node.js frameworks, Python with Django or FastAPI, Go with net/http-based frameworks, and Ruby with Rails. Framework selection should consider team expertise, ecosystem maturity, performance requirements, security support, deployment model, observability and total cost of ownership—not popularity alone.

A runtime may use a thread-per-request model, an event loop, asynchronous tasks or a hybrid approach. Event-driven runtimes are effective for I/O-bound workloads when handlers avoid blocking operations. CPU-intensive work may require worker processes, native services or an asynchronous job queue. “Asynchronous” does not automatically mean faster: it improves concurrency for suitable workloads but does not reduce the intrinsic time of computation.

2. API Design as Contract Engineering

An API is a contract. It specifies available operations, representations, authentication requirements, errors, performance expectations and compatibility rules. Good API design enables independent teams to develop clients and servers while reducing ambiguity.

REST

REST is an architectural style based on resources, representations and a uniform interface. A practical REST API commonly uses:

  • `GET /orders/742` to retrieve an order;
  • `POST /orders` to create an order;
  • `PATCH /orders/742` to partially modify an order;
  • `DELETE /orders/742` to remove or deactivate an order.

HTTP semantics matter. `GET` is safe because it is intended not to change server state. `PUT` is generally idempotent when repeated requests produce the same intended state. `DELETE` is also intended to be idempotent, although the response to the first and subsequent requests may differ. `POST` is not inherently idempotent. If a client may retry a creation request, the server can support an idempotency key and store the result associated with that key.

Useful status codes include 200 (successful response), 201 (resource created), 202 (accepted for asynchronous processing), 204 (successful response with no body), 400 (malformed or invalid request), 401 (missing or invalid authentication), 403 (authenticated but not permitted), 404 (resource not found), 409 (state conflict), 422 (semantically invalid content, where the API adopts this convention), 429 (rate limit exceeded) and 500 (unexpected server failure). Error responses should have a stable structure, such as an error code, human-readable message, request identifier and optional field-level details. They should not reveal stack traces or secrets.

Resource names should be consistent, and filtering, sorting and pagination should be explicit. Offset pagination is easy to understand but can become inconsistent when records are inserted between requests. Cursor pagination uses a server-generated position, often based on a stable ordering key, and is generally more reliable for changing large datasets. Any pagination design must define ordering, maximum page size and behavior when a cursor expires.

GraphQL

GraphQL exposes a typed schema through which a client selects fields. A query might request a user and only the fields required by a particular interface. This can reduce over-fetching, but it transfers more responsibility to the server for query validation, complexity control, authorization and resolver performance.

A GraphQL schema typically contains queries for reads, mutations for state changes and subscriptions for event-oriented updates. Resolvers should enforce authorization at the field or operation level where necessary. A common performance problem is the N+1 query pattern: resolving a list of entities and independently querying related data for each entity. Batching and caching tools such as a data loader pattern can combine these requests.

GraphQL is not automatically superior to REST. It is attractive when clients have diverse data requirements or when a unified graph spans multiple sources. REST may be simpler for caching, operational visibility, file transfer and straightforward resource operations. GraphQL queries also require limits on depth, breadth, execution cost and timeout to prevent abusive or accidental expensive requests.

Real-Time APIs

Real-time systems deliver updates without requiring the client to poll repeatedly. WebSockets provide a persistent, bidirectional channel. Server-Sent Events provide a server-to-client stream over HTTP and are suitable when communication is primarily one-way. Long polling is a compatibility technique in which a request remains open until an event or timeout occurs.

A real-time protocol should define connection establishment, authentication, message types, identifiers, acknowledgements, ordering, reconnection, heartbeats and error behavior. Delivery guarantees must be explicit. “At most once” may lose messages; “at least once” may deliver duplicates and therefore requires idempotent consumers; “exactly once” is difficult to guarantee across distributed systems and is often implemented as effectively-once processing through identifiers and deduplication.

3. Validation, Middleware and Serialization

Validation is a security and correctness boundary. It should occur at the external boundary and again where trust changes. Validation commonly covers required fields, types, ranges, formats, length limits, relationships and permitted enumerations. A request should be rejected before expensive business processing if it cannot satisfy the API contract.

Validation is not authorization. A valid `accountId` does not imply that the caller may access that account. Authentication identifies a principal; authorization decides whether that principal may perform an operation on a resource. Object-level authorization must be checked for every relevant resource, not merely at the route level.

Middleware is a pipeline of reusable processing components. Typical middleware handles:

  • request IDs and structured logs;
  • authentication and authorization context;
  • CORS policy;
  • request size limits;
  • rate limiting;
  • compression;
  • schema validation;
  • exception translation;
  • metrics and tracing.

Ordering is significant. A body parser must run before body validation; authentication must usually precede authorization; error middleware must be able to catch failures from downstream components. Middleware should be small, predictable and documented because hidden behavior creates operational risk.

Serialization converts in-memory objects to external representations, while deserialization performs the reverse. A public serializer should not expose internal database records directly. It should control field names, omit confidential properties, normalize timestamps and represent relationships deliberately. JSON dates should use a documented standard such as ISO 8601 with an explicit time-zone convention. Decimal monetary values require care because binary floating-point can introduce representation errors; systems should use decimal types or integer minor units according to domain requirements.

4. Security and Trust Boundaries

API security is an architectural concern, not merely a library feature. Transport encryption should be mandatory for sensitive traffic. Authentication approaches include server sessions, signed tokens and delegated authorization using OAuth 2.0 and OpenID Connect. A token should have an appropriate audience, issuer, lifetime and scope. Long-lived credentials increase exposure and require secure storage, rotation and revocation strategies.

Important threats include broken object-level authorization, injection, excessive data exposure, unrestricted resource consumption, insecure configuration, cross-site request forgery in cookie-based systems, and compromised third-party credentials. Parameterized queries or safe query builders reduce injection risk. Output encoding, content security policy and careful cookie attributes reduce browser-related risks.

Secrets belong in a managed secret store or protected deployment configuration, never in source control or logs. Logs should be useful without containing passwords, access tokens, payment data or unnecessary personal information. Security testing should combine dependency scanning, static analysis, dynamic testing, threat modelling and focused manual review.

Privacy governance requires data minimization, purpose limitation, retention controls, access auditing and appropriate deletion or anonymization. Depending on jurisdiction and sector, regulations may impose requirements concerning personal data, payment information, health records, breach reporting and cross-border processing. Engineering teams should involve legal, security and privacy stakeholders early rather than treating compliance as a final checklist.

5. Versioning, Compatibility and Documentation

An API evolves through additive and breaking changes. Adding an optional response field is often compatible, but clients that incorrectly reject unknown fields may still fail. Renaming a field, changing its type, removing an endpoint or altering authentication is generally breaking.

Versioning strategies include a URL segment such as `/v2/orders`, a media-type parameter, a query parameter or a negotiated header. No strategy solves poor change management. A version policy should define deprecation notices, support duration, migration guidance and sunset dates. Contract tests can verify that providers continue to satisfy consumer expectations.

OpenAPI is widely used to describe HTTP APIs, including paths, parameters, schemas, responses, security schemes and examples. A high-quality specification can generate documentation, client libraries, mock servers and validation, but generated artifacts remain only as accurate as the specification. GraphQL schemas provide a strongly typed contract and support introspection, although production systems may restrict introspection in sensitive environments.

Documentation should explain authentication, common workflows, examples, errors, pagination, rate limits, webhooks, idempotency and operational expectations. A reference list of endpoints is insufficient; developers need task-oriented guidance and explicit edge cases.

6. Reliability, Performance and Operations

Reliability is designed through controlled failure. Timeouts prevent a dependency from occupying resources indefinitely. Retries are appropriate only for transient failures and should use bounded exponential backoff with jitter. Retrying non-idempotent operations without an idempotency mechanism can duplicate effects.

Circuit breakers can stop calls to an failing dependency after a threshold, allowing recovery and preventing cascading failure. Bulkheads isolate resource pools, while queues absorb bursts and support asynchronous work. These mechanisms introduce trade-offs: queues increase eventual-consistency delay, retries increase load, and circuit breakers can expose temporary failures to users.

Performance analysis should use measurements rather than intuition. Useful metrics include throughput, error rate, latency percentiles, saturation, queue depth and dependency latency. The mean alone can conceal tail latency; the 95th or 99th percentile may better represent user experience. Caching can reduce database and network load, but invalidation, authorization variation and stale data must be addressed. HTTP cache directives, entity tags and conditional requests can reduce transfer and processing.

Observability combines logs, metrics and distributed traces. A correlation ID allows a request to be followed across services. Production systems should define service-level indicators and objectives, establish alert thresholds, and maintain runbooks. Graceful shutdown, health checks, resource limits and controlled deployments are part of server engineering rather than optional operations work.

7. Webhooks and Third-Party Integration

A webhook is an HTTP callback sent when an event occurs. The provider should document event types, payload schemas, signature verification, retry behavior and delivery ordering. Consumers should acknowledge quickly and process expensive work asynchronously. They must authenticate the sender, verify a signature over the exact received payload, reject stale or replayed messages where applicable, and deduplicate event identifiers.

A robust consumer stores the event identifier before or together with processing, subject to transaction design. If the provider retries after a timeout, the consumer can recognize the duplicate. Since delivery may be delayed, duplicated or out of order, business logic should tolerate these conditions. Reconciliation jobs are valuable because no webhook system is immune to network or provider failures.

Third-party integrations require an explicit failure model. Teams should document quotas, timeout behavior, data ownership, credential rotation, sandbox differences, support escalation and exit options. An adapter layer prevents vendor-specific data models from spreading throughout the domain. Payment, identity, messaging and AI providers deserve additional review because failures or incorrect outputs may create financial, legal or safety consequences.

8. Applied Case: Illustrative University Marketplace API

Background

Consider an illustrative university marketplace where students list used books, search listings, message sellers and receive notifications. The initial prototype uses a single application, a relational database and a browser client.

Problem

As usage increases, the system experiences duplicate listings caused by client retries, unauthorized access to private messages, slow search results and unreliable notifications. Developers also lack a stable contract for a planned mobile application.

Analysis

The problems have different causes. Duplicate listings reflect non-idempotent creation. Message exposure indicates an object-level authorization defect. Slow search may result from unindexed filters or unbounded responses. Notification loss reflects an informal real-time design with no event identity or replay strategy. Treating all problems as “scale” would obscure the actual controls required.

Approach and Solution

The team defines REST resources such as `/listings`, `/messages` and `/notifications`. Listing creation accepts an idempotency key scoped to the authenticated seller. The server stores the key and resulting response, returning the same result for a safe retry.

A policy service checks that a requester belongs to the conversation before returning messages. Input schemas constrain title length, price representation, category values and image metadata. Response serializers exclude internal moderation fields. Cursor pagination provides stable browsing, and database indexes support the documented search predicates.

The application emits a `ListingCreated` event after the transaction commits. A notification worker consumes the event and sends a WebSocket update when the user is connected. Each event has a unique identifier and sequence information. Clients acknowledge the last processed position and can refresh missed notifications through a REST endpoint. This combination is more reliable than assuming a persistent connection never fails.

OpenAPI documentation and consumer contract tests are added to the delivery pipeline. Rate limits protect search and messaging endpoints. Logs contain request and event identifiers but not message contents. A dashboard tracks latency percentiles, authorization failures, duplicate idempotency requests, queue depth and notification delivery failures.

Outcome

The design does not eliminate every failure. Instead, it makes failures bounded, visible and recoverable. Retries no longer create duplicate listings, authorization is evaluated against the requested conversation, large result sets are controlled, and disconnected clients can recover notifications.

Learning Points

1. Reliability mechanisms must match the failure mode.

2. Authorization is a resource-level decision, not merely a login check.

3. REST and real-time APIs can complement each other.

4. Contracts, observability and operational procedures are part of API quality.

5. A modular monolith can provide strong boundaries without premature microservice complexity.

9. Contemporary Developments and Critical Evaluation

API platforms increasingly use event-driven architectures, schema registries, service meshes and automated policy enforcement. Internal developer portals can publish ownership, documentation and operational metadata. AI-assisted coding and API generation may accelerate scaffolding and test creation, but generated code can reproduce insecure authorization, weak validation or inaccurate assumptions. Human review, threat modelling and contract tests remain necessary.

Edge runtimes and serverless functions can reduce infrastructure management and place computation near users, but they introduce execution limits, cold-start considerations, distributed debugging challenges and provider coupling. Microservices can enable independent deployment and scaling, yet they add network failure, data-consistency, observability and governance costs. The appropriate architecture is determined by domain boundaries and organizational capability, not by fashion.

A mature API programme treats interfaces as products. It assigns ownership, measures consumer satisfaction and reliability, manages lifecycle policy, and funds maintenance. Technical excellence includes the ability to explain trade-offs: strong consistency versus availability, flexibility versus governability, convenience versus privacy, and speed of delivery versus future compatibility.

Best-Practice Checklist

  • Define API contracts before implementation and keep them version-controlled.
  • Use HTTPS, least-privilege authorization and safe secret management.
  • Validate inputs and limit request size, query complexity and resource consumption.
  • Separate transport, application, domain and infrastructure concerns.
  • Use stable error formats and meaningful HTTP semantics.
  • Make retryable operations idempotent or avoid retrying them.
  • Document pagination, rate limits, authentication, examples and deprecation policy.
  • Instrument logs, metrics and traces with correlation identifiers.
  • Design webhooks for signatures, retries, duplicates, ordering and reconciliation.
  • Test security, contracts, failure modes and performance under realistic load.
  • Review privacy, accessibility, regulatory and third-party risks.

Chapter Summary

Server-side development combines request handling, business logic, data access, security and operations. API engineering treats interfaces as durable contracts rather than collections of routes. REST provides resource-oriented interaction and HTTP semantics; GraphQL provides typed, client-selected data access; and real-time protocols support event delivery with explicit guarantees. Validation, authorization, middleware and serialization protect correctness and control exposure. Versioning, documentation and contract testing support evolution. Timeouts, idempotency, queues, circuit breakers and observability make failures manageable. Webhooks and third-party integrations require authentication, deduplication, reconciliation and governance. Finally, architecture choices—including monoliths, microservices, serverless and AI-assisted development—must be evaluated against domain, risk and organizational context.

Key Terms

API: A defined interface through which software systems exchange operations or data.

REST: An architectural style using resources, representations and a uniform interface.

GraphQL: A typed API query language and execution model in which clients select required fields.

WebSocket: A persistent, bidirectional communication protocol over a connection initiated through HTTP.

Middleware: Reusable processing executed during request or response handling.

Serialization: Conversion of internal data into an external representation.

Validation: Checking whether input conforms to structural and semantic constraints.

Authentication: Establishing the identity of a caller.

Authorization: Determining whether an identified caller may perform an action.

Idempotency: A property in which repeating an operation has the same intended effect as performing it once.

Pagination: Dividing a large result set into manageable responses.

Webhook: An HTTP callback notifying a consumer about an event.

Circuit breaker: A reliability mechanism that temporarily stops calls to a failing dependency.

OpenAPI: A standard machine-readable description format for HTTP APIs.

Contract test: A test verifying that an API provider and consumer agree on an interface.

Observability: The ability to understand system behavior through logs, metrics and traces.

Rate limiting: Controlling the frequency or volume of requests.

OAuth 2.0: A framework for delegated authorization.

Eventual consistency: A model in which replicas may temporarily differ but converge later.

Correlation ID: An identifier used to trace related operations across components.

Review Questions

Short Answer Questions

1. Distinguish authentication from authorization.

2. Why is idempotency important when clients retry requests?

3. State two differences between REST and GraphQL.

4. What responsibilities should webhook consumers implement?

5. Why should public serializers not expose database entities directly?

Long Answer Questions

1. Explain the layers of a server-side application and discuss how separation of concerns improves maintainability.

2. Compare offset and cursor pagination, including their operational trade-offs.

3. Explain how validation, middleware, serialization and error handling work together in an API pipeline.

4. Discuss the reliability implications of timeouts, retries, circuit breakers and queues.

5. Compare REST, GraphQL and WebSocket-based APIs for a multi-client application.

Analytical / Application Questions

1. Design an API strategy for a food-delivery platform that must support mobile clients, restaurant dashboards, live courier locations and payment-provider webhooks. Justify protocol, consistency and security choices.

2. An API receives duplicate payment requests after network timeouts. Analyse the failure and propose a safe end-to-end design.

3. A GraphQL endpoint has rising latency and excessive database load. Diagnose likely causes and propose controls involving query complexity, resolver design, caching and observability.

Multiple Choice Questions with Answers

Question 1

Which HTTP method is generally intended to be idempotent when replacing a resource?

A. POST

B. PUT

C. CONNECT

D. TRACE

Correct Answer: B. PUT

Question 2

What is the primary purpose of authorization?

A. To compress a response

B. To identify a server process

C. To determine whether a caller may perform an action

D. To serialize database records

Correct Answer: C. To determine whether a caller may perform an action

Question 3

Which mechanism is most appropriate for preventing duplicate effects when a client retries a creation request?

A. An idempotency key

B. A larger response body

C. A random HTTP status code

D. Disabling validation

Correct Answer: A. An idempotency key

Question 4

Which protocol provides persistent bidirectional communication?

A. WebSocket

B. DNS

C. SMTP

D. FTP

Correct Answer: A. WebSocket

Question 5

What problem does the GraphQL N+1 pattern commonly create?

A. Too few authentication tokens

B. Repeated database queries for related items

C. Invalid TLS certificates

D. Duplicate URL versions

Correct Answer: B. Repeated database queries for related items

Question 6

Which status code commonly indicates that a request is unauthenticated?

A. 201

B. 301

C. 401

D. 503

Correct Answer: C. 401

Question 7

Why should a webhook consumer deduplicate event identifiers?

A. Webhook delivery may be retried and produce duplicates

B. It increases image resolution

C. It changes HTTP into UDP

D. It removes the need for authentication

Correct Answer: A. Webhook delivery may be retried and produce duplicates

Question 8

Which combination is commonly regarded as the three principal pillars of observability?

A. HTML, CSS and JavaScript

B. Logs, metrics and traces

C. DNS, DHCP and ARP

D. Users, roles and passwords

Correct Answer: B. Logs, metrics and traces

Question 9

What is a major limitation of offset pagination on frequently changing data?

A. It cannot return JSON

B. Records may shift between requests, causing skips or duplicates

C. It requires WebSockets

D. It prevents all filtering

Correct Answer: B. Records may shift between requests, causing skips or duplicates

Question 10

What is a principal risk of retrying a non-idempotent operation without safeguards?

A. The operation may produce duplicate side effects

B. The API becomes automatically encrypted

C. The response becomes statically typed

D. The database is always read-only

Correct Answer: A. The operation may produce duplicate side effects

Web Development Technologies
Chapter 6 of 15