Client - Server Patterns

Client-Server is a common architecture pattern in software systems. It is a distributed system architecture that partitions tasks or workloads between the providers of a resource or service, called servers, and service requesters, called clients. There are different patterns in which client and server can communicate with each other. Request-Response The client sends a request to the server and waits for a response. The server processes the request and sends a response back to the client. It is a synchronous communication pattern. It is used in HTTP, RPC, and other synchronous communication protocols. sequenceDiagram participant Client participant Server Client->>Server: Request Server->>Client: Response Short Polling The client sends a request to the server at regular intervals. The server responds with new data if available. It is a synchronous communication pattern with a short interval. sequenceDiagram participant Client participant Server Client->>Server: Request Server->>Client: Response ( no data ) Note left of Client: Wait for some time Client->>Server: Request Server->>Client: Response ( data ready ) Long Polling The client sends a request to the server and waits for a response. The server holds the request until new data is available or a timeout occurs. It is a synchronous communication pattern with a long timeout. sequenceDiagram participant Client participant Server Client->>+Server: Request Note right of Server: Wait Until data is ready Server->>-Client: Response ( delayed ) WebSockets The client and server establish a persistent, two way communication channel. It is used for real-time communication and updates. It is particularly useful for chat applications, online gaming, and financial trading platforms. sequenceDiagram participant Client participant Server Client->>Server: Establish Connection Server->>Client: Establish Connection Client->>Server: Send Message Server->>Client: Send Message Server-Sent Events The server sends updates to the client over a single HTTP connection. Client sends a request and the server uses the response to send updates. Server will not end the response, and will keep sending updates over the established connection. sequenceDiagram participant Client participant Server Client->>Server: Establish Connection Server->>Client: Send Update Server->>Client: Send Update Server->>Client: Send Update Publish-Subscribe The client subscribes to a topic or event on the server. The server publishes messages to the subscribed clients when an event occurs. It’s similar to WebSockets, but the server can send messages to multiple clients and the messages are partitioned by topics. It is an asynchronous communication pattern. sequenceDiagram participant Client participant Server Client->>Server: Subscribe ( establish a connection ) Server->>Client: Publish ( through the connection ) Request-Callback The client sends a request to the server and provides a callback function or URL. The server processes the request and calls the callback function or URL on the client. It is an asynchronous communication pattern. sequenceDiagram participant Client participant Server Client->>Server: Request ( with callback info ) Server->>Client: Invoke the Callback

Testing - Finding Errors Early

Errors in software systems are inevitable. They can be caused by a variety of factors, including human error, hardware failure, or software bugs. Testing a software helps in finding errors early, It is important to find errors as early as possible to avoid any sort of issues in production. Let’s explore some checkpoints in which errors can be found early. Build Time Compiler performs a syntax check on the code and reports any errors. It includes type checking to ensure that the types of variables are used correctly. The limitations depends on the language and compiler, on how much it can check. Unit Testing Unit tests are written to test individual units of code. It helps in finding errors in the code logic. Unit tests works like a double entry book keeping system. Integration Testing Integration tests helps to check if different systems are interacting correctly, and if the data is being passed correctly. It is very helpful to find errors in systems which are dynamically typed and the cases of interactions needs to be valid. Load Testing Load tests help us to find errors in the system when it is under heavy load. We verify if the system is able to handle the incoming requests and if it is able to respond within the expected time. It also helps identify the limits of the system. End-to-End Testing End-to-End tests are mostly used to test the entire system from start to finish in a user-like environment. It helps find errors in a user point of view, while using the system. typically for e2e systems, we deploy the whole system and have another software interact with it like a user. It’s a good practice to have a combination of these tests to find errors early in the development process. It becomes crucial to run more tests as the system grows and becomes more complex. ...

API Patterns

API is a common concept among software systems. Different software systems require to communicate with eachother to invoke some functionality or to exchange data. Since it’s a frequent requirement, it’s important to have a set of standards. There are different patterns and best practices that can be used to design and implement APIs. RESTful API REST APIs are designed around resources, which are any kind of object, data, or service that can be accessed by the client. They are mainly used for CRUD operations (Create, Read, Update, Delete) on resources. They use standard HTTP methods (GET: Read, POST: Create, PUT: Update, DELETE: Delete) and status codes to perform these operations on the resources. REST APIs are stateless, meaning that calls can be made independently of one another, and each call contains all of the data necessary to complete itself successfully. GraphQL GQL is a query language for APIs and a runtime for executing those queries by using a type system you define for your data. Graphql APIs are designed with a schema that defines the types and fields that can be queried. The Client can request only the data it needs, and the server will return only the requested data. The shape of response follows the shape of the request. gRPC It is designed for low latency and high throughput communication between microservices. gRPC is particularly suitable for microservices and between systems written in multiple programming languages due to its support for multiple programming languages. It based on HTTP/2, which is a binary protocol that is more efficient than HTTP/1.1. It uses Protocol Buffers as the Interface Definition Language (IDL) for describing both the service interface and the structure of the payload messages. Webhooks Webhooks are user-defined HTTP callbacks, which are triggered by specific events. When an event occurs, the source site makes an HTTP request to the URL configured for the webhook. They are useful for integrating different systems or for events in asynchronous communication. WebSocket WebSocket provides two-way (full duplex) communication channels over a single TCP connection. It enables interaction between a client and server in a real-time manner. It is particularly useful for applications that require real-time updates, such as chat applications, online gaming, and financial trading platforms. Each of these standards are useful and are best practices for their specific use cases. It’s important to choose the right pattern based on the requirements of the system. ...

Caching Patterns

Caching is one of the critical solution to improve performance and reduce latency in data access. Cache is a fast and temporary storage ( generally RAM ) When using cache there are different patterns in which we can implement reading the cache data and writing the cache data. Cache Aside Ideal where caching everything is not critical for usecase nor necessary. Application and Datastore control the data, and cache is an optimization sidecar. ...

Interfaces

Interfaces are one of my favorite concepts in programming. It’s a clean way to define contract and decouple the implementation parts from code. Interfaces indicate what a class or struct should do, or define the type of methods which a class or struct should implement. We can refer an interface and understand how the different parts used by a code block should behave. I mostly use it to define segments of code, like http handler, service, storage to indicate that a segment is going to recieve some implementation, which will have certain methods which can be used in them and worry about the implementation later. ...