Posts

Showing posts with the label write-behind

Caching strategies

Image
Introduction In this article, we will learn about popular Caching strategies used today. It should be noted that applying cache is inherently a trade-off, it helps improve system performance but increases complexity, while also requiring acceptance that data retrieved by users might not be the latest. When using cache, you need to answer the question of which entity will be responsible for writing to the cache and when to write it. Below are popular Cache Patterns . CACHE-ASIDE (LAZY LOADING) The app manages the cache itself, reading from cache first and upon a cache miss, it queries the Database and then writes back to the cache. This is the most popular type and even if the cache fails, the app remains functional. For each cache miss, it always takes 3 processing steps including Reading cache -> Querying Database -> Writing cache. The first read is always slow (cold start) because there is no cache initially. Used when reading frequently, writing rarely and ac...

WRITE-BEHIND

Image
Introduction This article will guide you through implementing WRITE-BEHIND (WRITE-BACK) in NestJS with Redis and Postgres . Applying this mechanism will perform asynchronous writes to the Database via a Queue, so I will also use @nestjs/bullmq (BullMQ + Redis) , which is a standardized, powerful library for managing background jobs. As for theoretical content, you can review the previous article I mentioned. Detail Please install the following packages: bun add bullmq @nestjs/bullmq Create Drizzle Schema in drizzle-orm/schema/schema.ts as follows: import { bigserial, decimal, integer, pgTable, serial, text, timestamp, varchar, } from 'drizzle-orm/pg-core' export const products = pgTable ( 'products' , { id : bigserial ( 'id' , { mode : 'number' }). primaryKey (), name : varchar ( 'name' , { length : 255 }). notNull (), description : text ( 'description' ), price : decimal ( 'price' , { precision :...