Skip to content
Stand with Ukraine flag

Caching

ThingsBoard Edge caches frequently accessed data — device credentials, entity profiles, attributes, and active sessions — to avoid a database query on every incoming message. Because Edge validates device credentials on every MQTT publish or HTTP POST, the cache is the primary factor in per-message throughput.

Set the cache backend with the CACHE_TYPE environment variable:

BackendCACHE_TYPEStorageBest for
CaffeinecaffeineJVM heap on the Edge nodeSingle-node deployments (default)
Redis / ValkeyredisExternal shared storeMulti-node Edge clusters (4.0+)

With Caffeine, the cache lives in the JVM heap of the Edge process. Because Edge typically runs on hardware with limited RAM — 512 MB to 4 GB — size each cache namespace conservatively. Every entry stays in memory until it expires or is evicted by the LRU policy.

ThingsBoard Edge maintains separate cache namespaces, each with its own TTL and size limit.

Cache namespaceDefault TTL (min)Default max sizeWhy it matters
Device credentials1440 (24 h)10,000Checked on every incoming message — the most frequently hit cache on Edge.
Sessions144010,000Active device session lookups for persistent MQTT connections.
Device profiles144010,000Alarm rules, transport configuration, and provisioning settings.
Tenants144010,000Rate limits and feature flags for the Edge tenant.
Devices144010,000Device metadata — name, type, and label.
Attributes1440100,000Server, client, and shared attributes.
TS latest1440100,000Latest time-series values for dashboards.
Relations144010,000Entity hierarchy links used in rule chains.
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 (2 h)200,000Per-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 read these values on every refresh. Disable them with CACHE_ATTRIBUTES_ENABLED=false and CACHE_TS_LATEST_ENABLED=false if you need to reclaim heap memory, at the cost of more frequent PostgreSQL reads.

Every cache namespace exposes two environment variables:

  • CACHE_SPECS_{NAME}_TTL — time-to-live in minutes. An entry is evicted from cache this many minutes after it was written.
  • CACHE_SPECS_{NAME}_MAX_SIZE — maximum entry count. When full, the least-recently-used entry is evicted.

For example, to double the device credentials cache capacity:

CACHE_SPECS_DEVICE_CREDENTIALS_TTL=1440
CACHE_SPECS_DEVICE_CREDENTIALS_MAX_SIZE=20000

Sizing guidance for Edge hardware:

  • Set MAX_SIZE for device credentials and sessions to at least the total number of devices connected to this Edge instance.
  • On hardware with under 1 GB RAM, reduce the Attributes and TS latest max sizes from 100,000 to 10,000–20,000 to limit heap pressure.
  • Monitor the cache hit ratio in Edge logs. A hit ratio below 90% on the credentials or sessions cache means entries are being evicted before they can be reused — increase the cache size or reduce the number of active devices per Edge instance.

When CACHE_TYPE=redis, configure the connection to your Redis or Valkey server.

VariableDefaultDescription
REDIS_HOSTlocalhostRedis or Valkey server hostname.
REDIS_PORT6379Redis or Valkey server port.
REDIS_DB0Database index.
REDIS_PASSWORD(empty)Authentication password.
REDIS_USERNAME(empty)Authentication username (ACL-based auth).

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

VariableDefaultDescription
REDIS_MASTER(empty)Name of the master node as configured in sentinel.
REDIS_SENTINELS(empty)Comma-separated host:port pairs of sentinel nodes.
REDIS_SENTINEL_PASSWORD(empty)Sentinel authentication password.
VariableDefaultDescription
REDIS_NODES(empty)Comma-separated host:port pairs of cluster nodes.
REDIS_MAX_REDIRECTS12Maximum cluster redirects before failing a request.
VariableDefaultDescription
TB_REDIS_SSL_ENABLEDfalseEnables TLS for the Redis connection.
TB_REDIS_SSL_PEM_CERT(empty)Path to the client certificate file.
TB_REDIS_SSL_PEM_KEY(empty)Path to the client private key file.
TB_REDIS_SSL_PEM_KEY_PASSWORD(empty)Password for the private key file.
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 in milliseconds when the pool is exhausted.
REDIS_POOL_CONFIG_TEST_WHILE_IDLEtrueValidates idle connections with a PING command.
REDIS_CLIENT_CONNECT_TIMEOUT30000Connection timeout in milliseconds.
REDIS_CLIENT_READ_TIMEOUT60000Read timeout in milliseconds.