Skip to content

Performance guide

nopCommerce Caching and Redis: When You Need It and How to Set It Up

nopCommerce caches in each server’s memory by default, which is the fastest option for a single server. Stores on several servers turn on the distributed cache in appsettings.json: Redis with synchronised memory is the best choice, plain Redis and SQL Server also work. The settings are the same in 4.90 and 5.00.

By CartenvoPublished Updated

Facts checked against the nopCommerce 4.90.8 and 5.00.0 source code on .

Default
In-memory cache, one server
For several servers
Redis synchronised memory
On this page

Redis is often suggested as the fix for a slow nopCommerce store. Sometimes it is; often it makes no difference, or makes things slower. This guide explains how nopCommerce’s cache works, the four distributed cache options, when each one fits, and how to set up Redis, based on the 4.90 and 5.00 source code.

How nopCommerce caching works

nopCommerce caches the data it reads on almost every page: settings, categories, the menu, URL slugs, prices and permissions. Each entry is kept for 60 minutes by default, and is removed as soon as the data behind it changes in the admin area. That’s why changes appear straight away without clearing anything.

By default the cache lives in the application’s memory. That’s as fast as caching gets, but each server has its own copy. On two servers, a price changed through server A is still cached on server B, so customers see different prices depending on which server answers. The distributed cache fixes that.

The cache options

nopCommerce cache options (DistributedCacheType)
TypeWhere entries liveRead speedServers
Off (default)Each server’s memoryFastestOne server only
memoryEach server’s memory, through the distributed cache codeFastOne server; mainly for testing
redisA Redis serverA network call per readSeveral
redissynchronizedmemoryEach server’s memory, kept in step through RedisClose to in-memorySeveral
sqlserverA table in SQL ServerSlowestSeveral, without Redis

Redis synchronised memory keeps the cache in each server’s memory, as the default does. When a server changes or removes an entry, it tells the others through Redis, in batches every half second, and they drop their copy. Reads never leave the server; only changes cross the network.

Which one to use

  • One server: leave the distributed cache off. Adding Redis only adds network calls.
  • Several servers behind a load balancer: redissynchronizedmemory. It’s the default type once you turn the cache on, and the fastest option for a web farm.
  • Several servers, memory is tight: plain redis. Entries live once, in Redis, instead of on every server, at the cost of a network call for each read.
  • Several servers, no Redis available: sqlserver. It works, but puts more load on the database the cache is meant to protect.

Setting up Redis

  1. Use a Redis server close to the web servers, in the same network or region. A managed service (Azure Cache for Redis, Amazon ElastiCache) saves running it yourself.
  2. Require a password and TLS, and don’t expose Redis to the internet.
  3. Edit DistributedCacheConfig in App_Data/appsettings.json on every server, or in Configuration → App settings in the admin area.
  4. Restart the application on every server. The cache type is read only at startup.
  5. Change a product price in the admin area and check it on each server.

App_Data/appsettings.json · Redis with synchronised memory

"DistributedCacheConfig": {
  "DistributedCacheType": "redissynchronizedmemory",
  "Enabled": true,
  "ConnectionString": "redis.internal:6379,password=YOUR_PASSWORD,ssl=True,abortConnect=False",
  "SchemaName": "dbo",
  "TableName": "DistributedCache",
  "InstanceName": "mystore",
  "PublishIntervalMs": 500
}
DistributedCacheConfig settings
SettingWhat it does
EnabledTurns the distributed cache on. When false, the type is ignored and each server uses its own memory.
DistributedCacheTypememory, redis, redissynchronizedmemory or sqlserver.
ConnectionStringThe Redis connection (host, port, password, ssl) or, for sqlserver, a database connection string.
InstanceNameA prefix for keys in Redis. Give each store its own if several share one Redis server.
PublishIntervalMsSynchronised memory only: how often, in milliseconds, a server tells the others which keys changed. Default 500.
SchemaName, TableNameSQL Server only: where the cache table is. Defaults dbo and DistributedCache.

Keep the password out of the file in source control: every setting can also be set as an environment variable, such as DistributedCacheConfig__ConnectionString. The separate CacheConfig section sets DefaultCacheTime, the cache lifetime in minutes.

Using SQL Server as the cache

nopCommerce doesn’t create the cache table itself. Create it once with Microsoft’s tool, using the schema and table name from your settings, then set the type to sqlserver and the connection string to the database that holds the table:

Create the SQL Server cache table

dotnet tool install --global dotnet-sql-cache
dotnet sql-cache create "Server=...;Database=nopcommerce;..." dbo DistributedCache

A separate database for the cache keeps its writes away from the store’s own database.

A web farm needs more than a shared cache

  • Data protection keys: stored as files in App_Data/DataProtectionKeys. Every server needs the same keys, or customers are signed out when a request moves between servers.
  • Pictures and uploads: must be on shared storage, such as the bundled Azure Blob storage plugin.
  • Scheduled tasks: run on every server. Check that tasks which should run once, like sending queued emails, behave correctly, and see scheduled tasks on web farms if they stop running behind a load balancer.

Our hosting guide covers running nopCommerce on several servers in more detail.

When the cache isn’t the problem

nopCommerce already caches in memory, so a slow store on one server rarely gets faster with Redis. The usual causes are elsewhere:

  • Slow database queries, often from large catalogues, missing indexes or an undersized database server.
  • Plugins or theme code that query the database on every page, outside the cache.
  • Code that clears the whole cache too often, for example an import or integration that saves settings in a loop.
  • Large, uncompressed images and too many scripts in the theme.

Rule out the basics before profiling: our free plugin lets you check task and store health with StorePulse, including overdue scheduled tasks, error spikes and database growth.

Measure first: find the slow pages and what they spend their time on, then fix that. That’s where our performance optimisation service starts.

Frequently asked questions

Does nopCommerce need Redis?

No. On a single server, nopCommerce caches in memory by default, and that’s the fastest option. Redis is for stores running on more than one server, so every server sees the same cache.

How do I enable Redis in nopCommerce?

In App_Data/appsettings.json, or Configuration → App settings in the admin area, set DistributedCacheConfig Enabled to true, DistributedCacheType to redissynchronizedmemory or redis, and ConnectionString to your Redis server. Restart the application.

What is RedisSynchronizedMemory?

A cache type where each server keeps its cache in its own memory, and uses Redis only to tell the other servers which entries changed. Reads are as fast as the default memory cache, and servers stay in step within about half a second.

How do I clear the nopCommerce cache?

Click Clear cache in the admin header. With a distributed cache it clears the shared cache for every server. Restarting the application also clears the default memory cache.

Will Redis make my store faster?

Not on its own. On one server it usually makes reads slower, because each one becomes a network call. Slow stores are more often slow because of database queries, heavy plugins or missing indexes.

Did caching change in nopCommerce 5.0?

No. The cache settings and options are the same in 4.90 and 5.00, so an existing configuration carries over.

Next steps

On one server, leave caching as it is and look at what’s slow. Moving to several servers, use Redis with synchronised memory and share the keys and files too. If you’d like a second opinion on either, send us your setup.

Related

Related guides

Store slow, or outgrowing one server?

Tell us your traffic, hosting and nopCommerce version, and we will find what is slow before changing anything.