search
HomeWeb Front-endJS TutorialBoosting Speed and Performance with Advanced Caching in NestJS: How to Use AVL Trees and Redis

Boosting Speed and Performance with Advanced Caching in NestJS: How to Use AVL Trees and Redis

In today's world, speed and efficiency in responding to requests are of paramount importance for large-scale and high-traffic systems. Online platforms such as e-commerce websites, social networks, and banking services face a massive volume of data and user requests. This high demand not only places a significant load on servers and databases but can also significantly impact the user experience. In this context, implementing a caching system can be an effective solution to improve performance and reduce resource load.

In this article, we explore the implementation of an advanced caching system that combines AVL trees and Redis. This system includes security mechanisms, TTL (Time to Live) management, and integration with Redis to enhance performance and flexibility. The goal is to leverage the advantages of both technologies while mitigating their weaknesses.

Important Note: This article was developed with the assistance of artificial intelligence.


Advantages and Disadvantages of Combining an AVL Tree-Based Caching System with Redis

Advantages:

  1. Improved Memory Efficiency:

    • Intelligent TTL Management: By using an AVL tree to manage data expiration, memory consumption can be optimized, and the retention of stale data can be prevented. This is particularly useful in scenarios where data changes rapidly and precise expiration is required.
  2. Enhanced Security:

    • Token Validation: Adding a token-based validation mechanism enhances Redis security. This additional security layer prevents unauthorized access to the cache, thereby strengthening the overall system security.
  3. Advanced TTL Management:

    • Custom Expiration Policies: AVL trees allow the implementation of more complex and tailored expiration policies that Redis might not support out of the box.
  4. Diverse Data Structures:

    • Balanced Tree Structure: As a balanced data structure, AVL trees can offer better performance for certain use cases that require fast searches and sorting compared to Redis's default data structures.
  5. Increased Flexibility and Customization:

    • Greater Customization: Combining the two systems allows for more extensive customization, enabling the development of more precise and application-specific solutions.

Disadvantages:

  1. Increased Architectural Complexity:

    • Managing Two Caching Systems: Simultaneously using Redis and an AVL tree-based caching system increases architectural complexity and requires coordinated management between the two systems.
  2. Increased Time Overhead:

    • Additional Latency: Adding an extra caching layer may introduce delays. It is essential to ensure that performance benefits outweigh these potential delays.
  3. Data Maintenance and Synchronization:

    • Data Consistency: Maintaining consistency and synchronization between Redis and the AVL tree is crucial to prevent data discrepancies, necessitating complex synchronization mechanisms.
  4. Higher Development and Maintenance Costs:

    • Increased Expenses: Developing and maintaining two caching systems require more resources and diverse expertise, potentially increasing overall project costs.
  5. Security Complexity:

    • Coordinating Security Policies: Ensuring that security policies are correctly and consistently implemented across both systems can be challenging.

Implementation of the Caching System Using AVL Trees and Redis

Below, we introduce the professional implementation of this caching system. This implementation includes an AVL tree for managing data with TTL capabilities and Redis for fast data storage.

1. AVL Tree with TTL

First, we implement the AVL tree with TTL management capabilities.

// src/utils/avltree.ts

export class AVLNode {
  key: string;
  value: any;
  ttl: number; // Expiration time in milliseconds
  height: number;
  left: AVLNode | null;
  right: AVLNode | null;

  constructor(key: string, value: any, ttl: number) {
    this.key = key;
    this.value = value;
    this.ttl = Date.now() + ttl;
    this.height = 1;
    this.left = null;
    this.right = null;
  }

  isExpired(): boolean {
    return Date.now() > this.ttl;
  }
}

export class AVLTree {
  private root: AVLNode | null;

  constructor() {
    this.root = null;
  }

  private getHeight(node: AVLNode | null): number {
    return node ? node.height : 0;
  }

  private updateHeight(node: AVLNode): void {
    node.height = 1 + Math.max(this.getHeight(node.left), this.getHeight(node.right));
  }

  private rotateRight(y: AVLNode): AVLNode {
    const x = y.left!;
    y.left = x.right;
    x.right = y;
    this.updateHeight(y);
    this.updateHeight(x);
    return x;
  }

  private rotateLeft(x: AVLNode): AVLNode {
    const y = x.right!;
    x.right = y.left;
    y.left = x;
    this.updateHeight(x);
    this.updateHeight(y);
    return y;
  }

  private getBalance(node: AVLNode): number {
    return node ? this.getHeight(node.left) - this.getHeight(node.right) : 0;
  }

  insert(key: string, value: any, ttl: number): void {
    this.root = this.insertNode(this.root, key, value, ttl);
  }

  private insertNode(node: AVLNode | null, key: string, value: any, ttl: number): AVLNode {
    if (!node) return new AVLNode(key, value, ttl);

    if (key  node.key) {
      node.right = this.insertNode(node.right, key, value, ttl);
    } else {
      node.value = value;
      node.ttl = Date.now() + ttl;
      return node;
    }

    this.updateHeight(node);
    const balance = this.getBalance(node);

    // Balancing the tree
    if (balance > 1 && key  node.right!.key) return this.rotateLeft(node);
    if (balance > 1 && key > node.left!.key) {
      node.left = this.rotateLeft(node.left!);
      return this.rotateRight(node);
    }
    if (balance  node.key) {
      node.right = this.deleteNode(node.right, key);
    } else {
      if (!node.left || !node.right) return node.left || node.right;
      let minLargerNode = node.right;
      while (minLargerNode.left) minLargerNode = minLargerNode.left;
      node.key = minLargerNode.key;
      node.value = minLargerNode.value;
      node.ttl = minLargerNode.ttl;
      node.right = this.deleteNode(node.right, minLargerNode.key);
    }

    this.updateHeight(node);
    const balance = this.getBalance(node);

    if (balance > 1 && this.getBalance(node.left!) >= 0) return this.rotateRight(node);
    if (balance  1 && this.getBalance(node.left!)  0) {
      node.right = this.rotateRight(node.right!);
      return this.rotateLeft(node);
    }

    return node;
  }
}

2. Cache Service (CacheService) with Redis Integration

In this section, we implement the cache service that utilizes both the AVL tree and Redis for cache management. Additionally, we incorporate a token validation mechanism.

// src/cache/cache.service.ts

import { Injectable, UnauthorizedException, InternalServerErrorException } from '@nestjs/common';
import { AVLTree } from '../utils/avltree';
import { InjectRedis, Redis } from '@nestjs-modules/ioredis';

@Injectable()
export class CacheService {
  private avlTree: AVLTree;
  private authorizedTokens: Set<string> = new Set(['your_authorized_token']); // Authorized tokens

  constructor(@InjectRedis() private readonly redis: Redis) {
    this.avlTree = new AVLTree();
  }

  validateToken(token: string): void {
    if (!this.authorizedTokens.has(token)) {
      throw new UnauthorizedException('Invalid access token');
    }
  }

  async set(key: string, value: any, ttl: number, token: string): Promise<void> {
    this.validateToken(token);
    try {
      // Store in Redis
      await this.redis.set(key, JSON.stringify(value), 'PX', ttl);
      // Store in AVL Tree
      this.avlTree.insert(key, value, ttl);
    } catch (error) {
      throw new InternalServerErrorException('Failed to set cache');
    }
  }

  async get(key: string, token: string): Promise<any> {
    this.validateToken(token);
    try {
      // First, attempt to retrieve from Redis
      const redisValue = await this.redis.get(key);
      if (redisValue) {
        return JSON.parse(redisValue);
      }

      // If not found in Redis, retrieve from AVL Tree
      const avlValue = this.avlTree.search(key);
      if (avlValue) {
        // Re-store in Redis for faster access next time
        // Assuming the remaining TTL is maintained in AVL Tree
        // For simplicity, we set a new TTL
        const newTtl = 60000; // 60 seconds as an example
        await this.redis.set(key, JSON.stringify(avlValue), 'PX', newTtl);
        return avlValue;
      }

      return null;
    } catch (error) {
      throw new InternalServerErrorException('Failed to get cache');
    }
  }

  async delete(key: string, token: string): Promise<void> {
    this.validateToken(token);
    try {
      // Remove from Redis
      await this.redis.del(key);
      // Remove from AVL Tree
      this.avlTree.delete(key);
    } catch (error) {
      throw new InternalServerErrorException('Failed to delete cache');
    }
  }
}
</void></any></void></string>

3. API Controller (CacheController)

The controller manages API requests to the cache service.

// src/cache/cache.controller.ts

import { Controller, Get, Post, Delete, Body, Param, Query, HttpCode, HttpStatus } from '@nestjs/common';
import { CacheService } from './cache.service';

class SetCacheDto {
  key: string;
  value: any;
  ttl: number; // milliseconds
  token: string;
}

@Controller('cache')
export class CacheController {
  constructor(private readonly cacheService: CacheService) {}

  @Post('set')
  @HttpCode(HttpStatus.CREATED)
  async setCache(@Body() body: SetCacheDto) {
    await this.cacheService.set(body.key, body.value, body.ttl, body.token);
    return { message: 'Data cached successfully' };
  }

  @Get('get/:key')
  async getCache(@Param('key') key: string, @Query('token') token: string) {
    const value = await this.cacheService.get(key, token);
    return value ? { value } : { message: 'Key not found or expired' };
  }

  @Delete('delete/:key')
  @HttpCode(HttpStatus.NO_CONTENT)
  async deleteCache(@Param('key') key: string, @Query('token') token: string) {
    await this.cacheService.delete(key, token);
    return { message: 'Key deleted successfully' };
  }
}

4. Cache Module (CacheModule)

Defines the cache module that connects the service and controller and injects Redis.

// src/cache/cache.module.ts

import { Module } from '@nestjs/common';
import { CacheService } from './cache.service';
import { CacheController } from './cache.controller';
import { RedisModule } from '@nestjs-modules/ioredis';

@Module({
  imports: [
    RedisModule.forRoot({
      config: {
        host: 'localhost',
        port: 6379,
        // Other Redis configurations
      },
    }),
  ],
  providers: [CacheService],
  controllers: [CacheController],
})
export class CacheModule {}

5. Redis Configuration

To use Redis in the NestJS project, we utilize the @nestjs-modules/ioredis package. First, install the package:

npm install @nestjs-modules/ioredis ioredis

Then, configure Redis in the CacheModule as shown above. If you require more advanced configurations, you can use separate configuration files.

6. Token Validation Mechanism

For managing and validating tokens, various strategies can be employed. In this simple implementation, tokens are maintained in a fixed set. For larger projects, it is recommended to use JWT (JSON Web Tokens) or other advanced security methods.

7. Error Handling and Input Validation

In this implementation, DTO (Data Transfer Object) classes are used for input validation and error management. Additionally, the cache service utilizes general error handling to prevent unexpected issues.

8. Main Application Module (AppModule)

Finally, we add the cache module to the main application module.

// src/utils/avltree.ts

export class AVLNode {
  key: string;
  value: any;
  ttl: number; // Expiration time in milliseconds
  height: number;
  left: AVLNode | null;
  right: AVLNode | null;

  constructor(key: string, value: any, ttl: number) {
    this.key = key;
    this.value = value;
    this.ttl = Date.now() + ttl;
    this.height = 1;
    this.left = null;
    this.right = null;
  }

  isExpired(): boolean {
    return Date.now() > this.ttl;
  }
}

export class AVLTree {
  private root: AVLNode | null;

  constructor() {
    this.root = null;
  }

  private getHeight(node: AVLNode | null): number {
    return node ? node.height : 0;
  }

  private updateHeight(node: AVLNode): void {
    node.height = 1 + Math.max(this.getHeight(node.left), this.getHeight(node.right));
  }

  private rotateRight(y: AVLNode): AVLNode {
    const x = y.left!;
    y.left = x.right;
    x.right = y;
    this.updateHeight(y);
    this.updateHeight(x);
    return x;
  }

  private rotateLeft(x: AVLNode): AVLNode {
    const y = x.right!;
    x.right = y.left;
    y.left = x;
    this.updateHeight(x);
    this.updateHeight(y);
    return y;
  }

  private getBalance(node: AVLNode): number {
    return node ? this.getHeight(node.left) - this.getHeight(node.right) : 0;
  }

  insert(key: string, value: any, ttl: number): void {
    this.root = this.insertNode(this.root, key, value, ttl);
  }

  private insertNode(node: AVLNode | null, key: string, value: any, ttl: number): AVLNode {
    if (!node) return new AVLNode(key, value, ttl);

    if (key  node.key) {
      node.right = this.insertNode(node.right, key, value, ttl);
    } else {
      node.value = value;
      node.ttl = Date.now() + ttl;
      return node;
    }

    this.updateHeight(node);
    const balance = this.getBalance(node);

    // Balancing the tree
    if (balance > 1 && key  node.right!.key) return this.rotateLeft(node);
    if (balance > 1 && key > node.left!.key) {
      node.left = this.rotateLeft(node.left!);
      return this.rotateRight(node);
    }
    if (balance  node.key) {
      node.right = this.deleteNode(node.right, key);
    } else {
      if (!node.left || !node.right) return node.left || node.right;
      let minLargerNode = node.right;
      while (minLargerNode.left) minLargerNode = minLargerNode.left;
      node.key = minLargerNode.key;
      node.value = minLargerNode.value;
      node.ttl = minLargerNode.ttl;
      node.right = this.deleteNode(node.right, minLargerNode.key);
    }

    this.updateHeight(node);
    const balance = this.getBalance(node);

    if (balance > 1 && this.getBalance(node.left!) >= 0) return this.rotateRight(node);
    if (balance  1 && this.getBalance(node.left!)  0) {
      node.right = this.rotateRight(node.right!);
      return this.rotateLeft(node);
    }

    return node;
  }
}

9. Main Application File (main.ts)

The main application file that bootstraps NestJS.

// src/cache/cache.service.ts

import { Injectable, UnauthorizedException, InternalServerErrorException } from '@nestjs/common';
import { AVLTree } from '../utils/avltree';
import { InjectRedis, Redis } from '@nestjs-modules/ioredis';

@Injectable()
export class CacheService {
  private avlTree: AVLTree;
  private authorizedTokens: Set<string> = new Set(['your_authorized_token']); // Authorized tokens

  constructor(@InjectRedis() private readonly redis: Redis) {
    this.avlTree = new AVLTree();
  }

  validateToken(token: string): void {
    if (!this.authorizedTokens.has(token)) {
      throw new UnauthorizedException('Invalid access token');
    }
  }

  async set(key: string, value: any, ttl: number, token: string): Promise<void> {
    this.validateToken(token);
    try {
      // Store in Redis
      await this.redis.set(key, JSON.stringify(value), 'PX', ttl);
      // Store in AVL Tree
      this.avlTree.insert(key, value, ttl);
    } catch (error) {
      throw new InternalServerErrorException('Failed to set cache');
    }
  }

  async get(key: string, token: string): Promise<any> {
    this.validateToken(token);
    try {
      // First, attempt to retrieve from Redis
      const redisValue = await this.redis.get(key);
      if (redisValue) {
        return JSON.parse(redisValue);
      }

      // If not found in Redis, retrieve from AVL Tree
      const avlValue = this.avlTree.search(key);
      if (avlValue) {
        // Re-store in Redis for faster access next time
        // Assuming the remaining TTL is maintained in AVL Tree
        // For simplicity, we set a new TTL
        const newTtl = 60000; // 60 seconds as an example
        await this.redis.set(key, JSON.stringify(avlValue), 'PX', newTtl);
        return avlValue;
      }

      return null;
    } catch (error) {
      throw new InternalServerErrorException('Failed to get cache');
    }
  }

  async delete(key: string, token: string): Promise<void> {
    this.validateToken(token);
    try {
      // Remove from Redis
      await this.redis.del(key);
      // Remove from AVL Tree
      this.avlTree.delete(key);
    } catch (error) {
      throw new InternalServerErrorException('Failed to delete cache');
    }
  }
}
</void></any></void></string>

10. Testing and Running the Application

After implementing all components, you can run the application to ensure its functionality.

// src/cache/cache.controller.ts

import { Controller, Get, Post, Delete, Body, Param, Query, HttpCode, HttpStatus } from '@nestjs/common';
import { CacheService } from './cache.service';

class SetCacheDto {
  key: string;
  value: any;
  ttl: number; // milliseconds
  token: string;
}

@Controller('cache')
export class CacheController {
  constructor(private readonly cacheService: CacheService) {}

  @Post('set')
  @HttpCode(HttpStatus.CREATED)
  async setCache(@Body() body: SetCacheDto) {
    await this.cacheService.set(body.key, body.value, body.ttl, body.token);
    return { message: 'Data cached successfully' };
  }

  @Get('get/:key')
  async getCache(@Param('key') key: string, @Query('token') token: string) {
    const value = await this.cacheService.get(key, token);
    return value ? { value } : { message: 'Key not found or expired' };
  }

  @Delete('delete/:key')
  @HttpCode(HttpStatus.NO_CONTENT)
  async deleteCache(@Param('key') key: string, @Query('token') token: string) {
    await this.cacheService.delete(key, token);
    return { message: 'Key deleted successfully' };
  }
}

11. Sample Requests

Set Cache:

// src/cache/cache.module.ts

import { Module } from '@nestjs/common';
import { CacheService } from './cache.service';
import { CacheController } from './cache.controller';
import { RedisModule } from '@nestjs-modules/ioredis';

@Module({
  imports: [
    RedisModule.forRoot({
      config: {
        host: 'localhost',
        port: 6379,
        // Other Redis configurations
      },
    }),
  ],
  providers: [CacheService],
  controllers: [CacheController],
})
export class CacheModule {}

Get Cache:

npm install @nestjs-modules/ioredis ioredis

Delete Cache:

// src/app.module.ts

import { Module } from '@nestjs/common';
import { CacheModule } from './cache/cache.module';

@Module({
  imports: [CacheModule],
  controllers: [],
  providers: [],
})
export class AppModule {}

Suitable Use Cases for Combining Redis and AVL Tree-Based Caching Systems

  1. Banking and Financial Systems:

    • Managing Sensitive Sessions and Transactions: High security and precise TTL management are essential for sensitive financial data. Combining token security and intelligent TTL management is highly beneficial in this domain.
  2. High-Traffic E-commerce Platforms:

    • Storing Product Data and Managing Shopping Carts: Optimizing memory and increasing data access speed are crucial for enhancing user experience in large online stores like Amazon.
  3. Messaging and Social Networking Applications:

    • Storing Real-Time User Statuses: Fast access and precise data management are required to display users' online/offline statuses and messages.
  4. Weather and Currency Exchange Applications:

    • API Caching to Reduce Request Load: Storing results of complex computations and live data with precise expiration management to provide up-to-date and fast information to users.
  5. Content Management Systems and Media Platforms:

    • Caching High-Traffic Pages and Content: Optimizing access to highly viewed content and reducing server load to provide a smoother user experience.
  6. Analytical Applications and Real-Time Dashboards:

    • Storing Immediate Analysis Results: Providing fast and up-to-date analytical data using multiple caches to enhance performance and result accuracy.

Conclusion

In this article, we implemented an advanced caching system using AVL trees and Redis within the NestJS framework. This system, offering advanced TTL management, token-based security, and Redis integration, provides a robust and flexible solution for high-demand applications. The combination of these two technologies leverages the strengths of both, addressing Redis's weaknesses and enhancing overall caching performance.

The above is the detailed content of Boosting Speed and Performance with Advanced Caching in NestJS: How to Use AVL Trees and Redis. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.