Posts

Showing posts with the label backend development

NestJS Controller and Swagger Guide

Image
Introduction NestJS is a progressive Node.js framework built with TypeScript, facilitating the development of efficient and scalable server-side applications. By default, NestJS uses Express as its core HTTP processing library. Key advantages include a tight modular architecture inspired by Angular and strong TypeScript support to reduce code errors. Controllers serve as the layer for processing incoming requests and returning responses to the client side. Their primary responsibility is to receive HTTP requests, route data to business logic services, and coordinate the returned result in the correct format. Swagger is a powerful suite of tools used for designing, building, and documenting RESTful APIs developed based on the OpenAPI data format specification. In NestJS, it helps automatically generate an intuitive UI interface for testing endpoints, enabling developers and stakeholders to understand the API structure without directly reading the source code. Detail After creating the N...

Transaction with Isolation Level

Image
Introduction This is a concept created by Postgres as a set of behavior rules to decide whether other people can see or edit data when someone is modifying it. The isolation level only makes sense and only works when accompanied by a Transaction. This is the factor that ensures Isolation, helping to decide how parallel transactions handle operations independently of each other. When you execute a single statement like UPDATE products SET price = 100, Postgres activates Autocommit mode to automatically wrap the statement with BEGIN...COMMIT. It automatically creates a Transaction for extremely fast execution. Levels There are currently 4 levels ranging from the most relaxed to the strictest. The stricter the level, the more accurate the data, but the system runs slightly slower. Read Uncommitted This is the most relaxed level, allowing you to see what others are drafting even if they have not yet committed. Because of that characteristic, using it is very dangerous as it causes Dirty Re...

Advisory Lock

Image
Introduction Unlike Level Locks created by the database at the physical layer, developers can freely create Advisory Locks based on custom logic at the application level The database does not know the meaning of this lock and it only acts as an intermediary to hold the lock (usually a bigint number) The process that arrives first is granted the lock for processing Meanwhile, subsequent processes must line up and wait to acquire the lock When a process finishes processing, it returns the lock to the next process Advantages When using other types of locks (such as Table level lock, Row level lock or Page level lock), you must rely on an actual existing data row in the table to lock, whereas Advisory Lock does not require any available data row, you can use any random number to lock It is extremely lightweight because it only exists on the Database RAM, does not write to the hard drive and does not generate redundant data (dead tuples) Functions To create an advisory lock, Postgres provid...