Skip to content

Conversation

@ns212
Copy link
Contributor

@ns212 ns212 commented Aug 20, 2025

This pull request introduces a Redis-based caching layer to the Tari Explorer backend, refactoring multiple routes to utilize Redis for improved performance and reliability. It adds a centralized cacheService and standardized cache key management, and updates dependencies and configuration to support these features. Additionally, there are minor workflow and configuration improvements.

Redis Caching Integration and Refactoring:

  • Added a new cacheService singleton (utils/cacheService.ts) providing get/set/delete and utility methods for Redis-backed caching, with robust error handling and logging. ([utils/cacheService.tsR1-R177](https://github.com/tari-project/tari-explorer/pull/130/files#diff-d8caa00321810af66fa2f55d1a9af44542d288156e72fe3ab45d01e543a0405eR1-R177))
  • Introduced cacheKeys.ts to standardize cache key naming and organization, including keys for blocks, mempool, mining stats, tip, and network stats. ([utils/cacheKeys.tsR1-R36](https://github.com/tari-project/tari-explorer/pull/130/files#diff-8156283b6b8b9227b6b1286db6ac95b1cd9fab4314d7e9bbef1a5ae06a68b5e4R1-R36))
  • Refactored routes (routes/blocks.ts, routes/mempool.ts, routes/miners.ts) to use cacheService and CacheKeys for data retrieval, falling back to gRPC if cache is unavailable. This reduces direct gRPC calls and centralizes cache logic. ([[1]](https://github.com/tari-project/tari-explorer/pull/130/files#diff-1a413a9edee0323a69d674367c7fe5ef3ecc46582c578cd769b95eb7530c63c8R27-L30), [[2]](https://github.com/tari-project/tari-explorer/pull/130/files#diff-1a413a9edee0323a69d674367c7fe5ef3ecc46582c578cd769b95eb7530c63c8L42-R43), [[3]](https://github.com/tari-project/tari-explorer/pull/130/files#diff-1a413a9edee0323a69d674367c7fe5ef3ecc46582c578cd769b95eb7530c63c8L280-R266), [[4]](https://github.com/tari-project/tari-explorer/pull/130/files#diff-1a413a9edee0323a69d674367c7fe5ef3ecc46582c578cd769b95eb7530c63c8L295-L301), [[5]](https://github.com/tari-project/tari-explorer/pull/130/files#diff-811f09b98bed0f3309f7697a6364522b7e2c89042f902d710c3f5c48b0a6025aL25-R27), [[6]](https://github.com/tari-project/tari-explorer/pull/130/files#diff-811f09b98bed0f3309f7697a6364522b7e2c89042f902d710c3f5c48b0a6025aL35-R46), [[7]](https://github.com/tari-project/tari-explorer/pull/130/files#diff-707097915f5b05c9300dea17fa4b4e9fdf1181cbf87bae716530711842bf9ae0L25-R40))
  • Updated health check (routes/healthz.ts) to include Redis availability as part of the health status, and added logging. ([routes/healthz.tsR24-R47](https://github.com/tari-project/tari-explorer/pull/130/files#diff-549c7838519e16356be39b36c88cbd3b382c8fe24d539ddd4b4be6a41771c05dR24-R47))
  • The server now initializes the Redis client on startup, with error handling and fallback to gRPC if Redis is unavailable. (index.ts index.tsR10-R25)

Configuration and Dependency Updates:

  • Added ioredis and @types/ioredis for Redis support, and @eslint/compat for improved ESLint compatibility. (package.json [1] [2] [3]
  • Updated ESLint configuration to import .gitignore patterns for file ignoring, improving linting accuracy. (eslint.config.js [1] [2]

Workflow and Miscellaneous Improvements:

  • Simplified Docker build workflow by temporarily disabling ARM64 builds and removing unused manifest annotations. (.github/workflows/build_docker.yml [1] [2]
  • Minor cleanups: removed unused cache headers and improved error handling in routes. (routes/index.ts [1] [2] [3]

These changes collectively improve backend performance, reliability, and maintainability by leveraging Redis caching and centralizing cache logic.

@ns212 ns212 requested a review from Copilot August 20, 2025 14:33
@ns212 ns212 marked this pull request as ready for review August 20, 2025 14:34

This comment was marked as outdated.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request introduces comprehensive Redis-based caching to the Tari Explorer backend, replacing direct gRPC calls with cached data retrieval for improved performance. The implementation includes distributed locking for background updates and standardized cache key management across the application.

  • Implements Redis caching infrastructure with distributed locking for coordinated background updates
  • Refactors route handlers to use cached data with gRPC fallbacks when cache is unavailable
  • Adds health monitoring for Redis connectivity and updates CI/CD configuration

Reviewed Changes

Copilot reviewed 14 out of 16 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
utils/updater.ts Enhanced background updater with distributed locking and Redis cache population
utils/redisClient.ts New Redis client singleton with connection management and error handling
utils/distributedLock.ts New distributed locking mechanism using Redis for coordinating background processes
utils/cacheService.ts New centralized caching service with Redis operations and BigInt serialization
utils/cacheKeys.ts Standardized cache key definitions and lock key management
routes/miners.ts Updated to use cached network stats with gRPC fallback
routes/mempool.ts Updated to use cached mempool data with gRPC fallback
routes/index.ts Simplified cache header handling and improved error flow
routes/healthz.ts Enhanced health check to include Redis connectivity status
routes/blocks.ts Updated to use cached mining stats and tip data
package.json Added Redis dependencies and ESLint compatibility package
index.ts Added Redis client initialization on server startup
eslint.config.js Updated to import .gitignore patterns for better linting
.github/workflows/build_docker.yml Temporarily disabled ARM64 builds and simplified manifest annotations

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

if (connecting) {
// If we're in the middle of connecting, wait a bit and return the client
// ioredis handles queuing commands until connection is ready
return client!;
Copy link

Copilot AI Aug 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using non-null assertion operator on client when it might be null. If connecting is true but client is still null, this will throw a runtime error. The logic should ensure client exists before returning it.

Suggested change
return client!;
// If we're in the middle of connecting, we cannot return a client yet
// because it has not been created. Throw an explicit error.
throw new Error('Redis client is still connecting. Please retry shortly.');

Copilot uses AI. Check for mistakes.
for (let j = 0; j < (mempool[i]?.transaction?.body?.kernels?.length || 0); j++) {
sum += mempool[i]?.transaction?.body?.kernels[j]?.fee || 0n;
}
(mempool[i]?.transaction?.body as any).total_fees = sum;
Copy link

Copilot AI Aug 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using any type assertion bypasses type safety. Consider defining a proper type or interface for the transaction body with the total_fees property to maintain type safety.

Suggested change
(mempool[i]?.transaction?.body as any).total_fees = sum;
(mempool[i]?.transaction?.body as TransactionBodyWithTotalFees).total_fees = sum;

Copilot uses AI. Check for mistakes.
if (ping && result.value && typeof result.value === "string") {
res.status(200).send(json);
} else {
res.status(500);
Copy link

Copilot AI Aug 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The health check logic is incomplete. If Redis ping succeeds but the gRPC version check fails, the response status is never set, potentially causing the handler to hang. The else branch should explicitly handle this case.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants