Express Application Publishing and Scaling: Performance Boost with Memcached
What will you learn in this guide?
In this guide, you will learn how to publish an Express.js application in a PaaS environment.
Then you will scale the application using Memcached.
You will apply computational, view, and session caching techniques.
🧠 Technical Summary
The topic is Express application deployment and caching.
The problem is that CPU-intensive processes reduce performance.
The solution is caching strategies with Memcached.
Prerequisites
Before continuing, the following is required:
- A system with Node.js installed
- Basic Express.js knowledge
- Git and GitHub account
- Basic awareness about using PaaS
1. Template Engine Setup (EJS)
npm install ejs
- This command installs the EJS template engine for Express.
app.set("view engine", "ejs");
- This setting tells Express to use EJS.
2. Prime Number Calculation Logic
- The application finds the largest prime number up to the entered number.
- This operation is CPU intensive and cache friendly.
module.exports = function (n) {
let prime = 2;
for (let i = n; i > 1; i--) {
let isPrime = true;
for (let j = 2; j < i; j++) {
if (i % j === 0) {
isPrime = false;
break;
}
}
if (isPrime) {
prime = i;
break;
}
}
return prime;
};
- This function calculates the largest prime number.
3. Like Mechanism
- A temporary like counter is kept for each issue.
- This construct is used for view cache invalidation.
const likesMap = {};
- This object keeps the number of likes in RAM.
4. Code Management via GitHub
git init
git add .
git commit -m "Initial commit"
git push -u origin main
- These steps will push the project on GitHub.
5. Publishing in a PaaS Environment
- The application connects to a PaaS platform.
- GitHub repository is selected as the source.
- Port information is read from the environment variable.
const port = process.env.PORT || 3000;
app.listen(port);
- This structure adapts to different environments.
6. Memcached Service Installation
- Memcached is used as the cache service.
- The service must be in the same region as the application.
npm install memjs
- This command installs the Memcached client.
const { Client } = require("memjs");
module.exports = Client.create(process.env.MEMCACHIER_SERVERS);
- This code creates the Memcached connection.
7. Calculation Result Caching
memcache.get(key, (err, val) => {
if (val) {
prime = parseInt(val.toString());
} else {
prime = findPrime(n);
memcache.set(key, prime.toString());
}
});
- This structure reduces CPU cost.
8. Rendered View Caching
- HTML output is saved to Memcached.
- The same requests cannot be re-rendered.
const key = `view_${req.url}`;
- A separate cache key is generated for each URL.
9. Cache Invalidation (After Like)
- When the like changes, the view cache is cleared.
memcache.delete(`view_/?n=${n}`);
- This process blocks stale data.
10. Session Caching
npm install express-session connect-memjs
- These packages keep sessions in Memcached.
store: new MemcacheStore({
servers: [process.env.MEMCACHIER_SERVERS],
});
- This structure provides scalable session management.
❓ Frequently Asked Questions
1. What does Memcached do? Provides fast RAM-based caching.
2. Which data is suitable for cache? CPU intensive and frequently used data.
3. What happens if the cache is corrupted? The application continues to work.
4. Is session cache safe? Suitable for short-lived sessions.
5. Is it suitable for a production environment? Yes, it is ideal for horizontal scaling.
Result
You have published the Express application in this guide. You've dramatically improved performance with Memcached. You've implemented measure, view, and session caching.
You can run this architecture with high performance on the GenixNode infrastructure.

