DEV Community

mostafa
mostafa

Posted on

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.left = this.insertNode(node.left, key, value, ttl);
    } else 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.left!.key) return this.rotateRight(node);
    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 < -1 && key < node.right!.key) {
      node.right = this.rotateRight(node.right!);
      return this.rotateLeft(node);
    }

    return node;
  }

  search(key: string): any {
    let node = this.root;
    while (node) {
      if (node.isExpired()) {
        this.delete(key);
        return null;
      }
      if (key === node.key) return node.value;
      node = key < node.key ? node.left : node.right;
    }
    return null;
  }

  delete(key: string): void {
    this.root = this.deleteNode(this.root, key);
  }

  private deleteNode(node: AVLNode | null, key: string): AVLNode | null {
    if (!node) return null;

    if (key < node.key) {
      node.left = this.deleteNode(node.left, key);
    } else if (key > 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.right!) <= 0) return this.rotateLeft(node);
    if (balance > 1 && this.getBalance(node.left!) < 0) {
      node.left = this.rotateLeft(node.left!);
      return this.rotateRight(node);
    }
    if (balance < -1 && this.getBalance(node.right!) > 0) {
      node.right = this.rotateRight(node.right!);
      return this.rotateLeft(node);
    }

    return node;
  }
}
Enter fullscreen mode Exit fullscreen mode

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');
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

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' };
  }
}
Enter fullscreen mode Exit fullscreen mode

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 {}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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/app.module.ts

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

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

9. Main Application File (main.ts)

The main application file that bootstraps NestJS.

// src/main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  // Use ValidationPipe for input validation
  app.useGlobalPipes(new ValidationPipe({
    whitelist: true,
    forbidNonWhitelisted: true,
    transform: true,
  }));

  await app.listen(3000);
}
bootstrap();
Enter fullscreen mode Exit fullscreen mode

10. Testing and Running the Application

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

npm run start:dev
Enter fullscreen mode Exit fullscreen mode

11. Sample Requests

Set Cache:

POST http://localhost:3000/cache/set
Content-Type: application/json

{
  "key": "user123",
  "value": { "name": "Ali", "age": 30 },
  "ttl": 60000, // 60 seconds
  "token": "your_authorized_token"
}
Enter fullscreen mode Exit fullscreen mode

Get Cache:

GET http://localhost:3000/cache/get/user123?token=your_authorized_token
Enter fullscreen mode Exit fullscreen mode

Delete Cache:

DELETE http://localhost:3000/cache/delete/user123?token=your_authorized_token
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)