Skip to content
Stand with Ukraine flag

Caching

ThingsBoard caches frequently accessed data — device credentials, entity profiles, attributes, sessions — to avoid hitting the database on every message. Since the platform validates device credentials on every incoming telemetry message, the cache is critical for throughput: without it, every MQTT publish or HTTP POST triggers a database query.

Set the cache backend with the CACHE_TYPE environment variable:

BackendCACHE_TYPEStorageBest for
CaffeinecaffeineLocal JVM heap, per-nodeSingle node, monolithic deployments
RedisredisExternal shared storeClusters, microservices, multi-node setups

With Caffeine, each node maintains its own independent cache. Cache invalidations are broadcast between cluster nodes via the message queue, but there’s a brief window where different nodes may serve stale data. Redis eliminates this issue — all nodes read from the same shared store.

ThingsBoard maintains separate cache namespaces, each with its own TTL and size limit. The most performance-critical caches are listed first:

Cache NamespaceDefault TTL (min)Default Max SizeWhy it matters
Device Credentials1440 (24h)10,000Checked on every incoming message — the hottest cache. See Connectivity Guide.
Sessions144010,000Active device session lookups
Device Profiles144010,000Alarm rules, transport config, provisioning settings
Tenant Profiles144010,000Rate limits, feature flags per tenant
Devices144010,000Device metadata (name, type, label)
Attributes1440100,000Server/client/shared attributes
TS Latest1440100,000Latest time-series values for dashboard widgets
Relations144010,000Entity relations (parent-child, contains, manages)
Assets144010,000Asset metadata
Customers144010,000Customer metadata
Users144010,000User accounts
Entity Views144010,000Entity view definitions
Asset Profiles144010,000Asset profile configurations
Security Settings144010,000Platform security configuration
Rate Limits120 (2h)200,000Per-tenant and per-device rate limit counters
Notification Settings101,000Notification delivery configuration

The Attributes and TS Latest caches have the largest default size (100,000 entries) because dashboards and rule chains query these values constantly. Enable or disable them independently with CACHE_ATTRIBUTES_ENABLED and CACHE_TS_LATEST_ENABLED (both true by default).

Every cache namespace has two environment variables:

  • CACHE_SPECS_{NAME}_TTL — time-to-live in minutes (how long an entry stays in cache after being written)
  • CACHE_SPECS_{NAME}_MAX_SIZE — maximum number of entries (evicts least-recently-used when full)

For example, to adjust the device credentials cache:

CACHE_SPECS_DEVICE_CREDENTIALS_TTL=2880
CACHE_SPECS_DEVICE_CREDENTIALS_MAX_SIZE=50000

Sizing rules of thumb:

  • Set MAX_SIZE to at least the number of active entities of that type. For device credentials, this should be ≥ total device count.
  • TTL of 1440 minutes (24 hours) works for most deployments. Reduce TTL if you frequently update device credentials or profiles and need changes to take effect faster.
  • Monitor cache hit ratios in logs. Low hit ratios on the credentials or sessions cache mean the cache is too small and entities are being evicted before they’re accessed again.

When using CACHE_TYPE=redis, configure the Redis connection based on your deployment topology:

A single Redis instance. Suitable for development and small production setups.

VariableDefaultDescription
REDIS_HOSTlocalhostRedis server hostname
REDIS_PORT6379Redis server port

Redis Sentinel provides automatic failover. ThingsBoard connects to the sentinel nodes, which direct it to the current master.

VariableDefaultDescription
REDIS_MASTERName of the master node
REDIS_SENTINELSComma-separated host:port pairs of sentinel nodes
REDIS_SENTINEL_PASSWORDSentinel authentication password

Redis Cluster shards data across multiple nodes. Use for large deployments where a single Redis instance cannot hold the entire cache.

VariableDefaultDescription
REDIS_NODESComma-separated host:port pairs of cluster nodes
REDIS_MAX_REDIRECTS12Maximum cluster redirects before failing a request

All modes support SSL/TLS via TB_REDIS_SSL_ENABLED=true and optional client certificate authentication.

ThingsBoard uses a connection pool to manage Redis connections efficiently. The pool is configured per ThingsBoard node:

VariableDefaultDescription
REDIS_POOL_CONFIG_MAX_TOTAL128Maximum total connections in the pool
REDIS_POOL_CONFIG_MAX_IDLE128Maximum idle connections
REDIS_POOL_CONFIG_MIN_IDLE16Minimum idle connections kept ready
REDIS_POOL_CONFIG_MAX_WAIT_MS60000Maximum wait time (ms) when pool is exhausted
REDIS_POOL_CONFIG_TEST_WHILE_IDLEtrueValidate idle connections with PING

For the complete list of cache and Redis environment variables, see Configuration Reference.