08-cache.md 3.7 KB
Newer Older
1
---
2
title: Caching
D
danielclow 已提交
3 4
sidebar_label: Caching
description: This document describes the caching component of TDengine.
5 6
---

7
TDengine uses various kinds of caching techniques to efficiently write and query data. This document describes the caching component of TDengine.
G
gccgdb1234 已提交
8 9 10

## Write Cache

11
TDengine uses an insert-driven cache management policy, known as first in, first out (FIFO). This policy differs from read-driven "least recently used (LRU)" cache management. A FIFO policy stores the latest data in cache and flushes the oldest data from cache to disk when the cache usage reaches a threshold. In IoT use cases, the most recent data or the current state is most important. The cache policy in TDengine, like much of the design and architecture of TDengine, is based on the nature of IoT data.
12

D
dapan1121 已提交
13
When you create a database, you can configure the size of the write cache on each vnode. The **vgroups** parameter determines the number of vgroups that process data in the database, and the **buffer** parameter determines the size of the write cache for each vnode. The unit of buffer is MB.
G
gccgdb1234 已提交
14 15

```sql
D
dapan1121 已提交
16
create database db0 vgroups 100 buffer 16
G
gccgdb1234 已提交
17 18
```

19
In theory, larger cache sizes are always better. However, at a certain point, it becomes impossible to improve performance by increasing cache size. In most scenarios, you can retain the default cache settings.
G
gccgdb1234 已提交
20 21

## Read Cache
22

W
wade zhang 已提交
23 24 25 26 27
When you create a database, you can configure whether the latest data from every subtable is cached. To do so, set the *cachemodel* parameter as follows:
- none: Caching is disabled.
- last_row: The latest row of data in each subtable is cached. This option significantly improves the performance of the `LAST_ROW` function
- last_value: The latest non-null value in each column of each subtable is cached. This option significantly improves the performance of the `LAST` function in normal situations, such as WHERE, ORDER BY, GROUP BY, and INTERVAL statements.
- both: Rows and columns are both cached. This option is equivalent to simultaneously enabling option last_row and last_value.
28

29
## Metadata Cache
G
gccgdb1234 已提交
30

D
dapan1121 已提交
31
To improve query and write performance, each vnode caches the metadata that it receives. When you create a database, you can configure the size of the metadata cache through the *pages* and *pagesize* parameters. The unit of pagesize is kb.
32 33

```sql
D
dapan1121 已提交
34
create database db0 pages 128 pagesize 16
35
```
G
gccgdb1234 已提交
36

37
The preceding SQL statement creates 128 pages on each vnode in the `db0` database. Each page has a 16 KB metadata cache.
G
gccgdb1234 已提交
38 39 40

## File System Cache

41 42 43
TDengine implements data reliability by means of a write-ahead log (WAL). Writing data to the WAL is essentially writing data to the disk in an ordered, append-only manner. For this reason, the file system cache plays an important role in write performance. When you create a database, you can use the *wal* parameter to choose higher performance or higher reliability.
- 1: This option writes to the WAL but does not enable fsync. New data written to the WAL is stored in the file system cache but not written to disk. This provides better performance.
- 2: This option writes to the WAL and enables fsync. New data written to the WAL is immediately written to disk. This provides better data reliability.
G
gccgdb1234 已提交
44 45 46

## Client Cache

47
In addition to the server-side caching discussed previously, the core client library `libtaos.so` also makes use of caching. TDengine Client caches the metadata of all databases, supertables, and subtables that it has accessed, as well as the cluster topology.
G
gccgdb1234 已提交
48

49
If a client modifies certain metadata while multiple clients are simultaneously accessing a TDengine cluster, the metadata caches on each client may fail or become out of sync. If this occurs, run the `reset query cache` command on the affected clientsto force them to obtain fresh metadata and reset their caches.