# Caching

OrientDB has several caching mechanisms that act at different levels. Look at this picture:

• Local cache is one per database instance (and per thread in multi-thread environment)
• Storage, depending by the implementation could cache. This is the case for the Local Storage (disk based) that caches file reads to reduce I/O requests

# How cache works?

## Local Mode (embedded database)

When the client application asks for a record OrientDB checks:

• if a transaction is begun searches it inside the transaction changed records and returns it if found
• if the Local cache cache is enabled and contains the requested record then return it
• at this point the record is not in cache, then ask it to the Storage (disk, memory)

## Client-Server Mode (remote database)

When the client application asks for a record OrientDB checks:

• if a transaction is begun searches it inside the transaction changed records and returns it if found
• if the Local cache cache is enabled and contains the requested record then return it
• at this point the record is not in cache, then ask it to the Server through a TCP/IP call
• in the server if the Local cache cache is enabled and contains the requested record then return it
• at this point the record is not in cache in the server too, then ask it to the Storage (disk, memory)

# Record cache

## Local cache

Local cache acts at database level. Each database instance has a Local cache enabled by default. This cache keeps the used records. Records will be removed from heap if 2 conditions will be satisfied:

1. There are no links to these records from outside of database
2. The Java Virtual Machine doesn't have enough memory to allocate for new data

## Empty Local cache

To remove all the records in Local cache you can invoke the invalidate() method:

db.getLocalCache().invalidate();


### Disable Local cache

Disabling of local cache may lead to situation when 2 different instances of the same record will be loaded and OConcurrentModificationException may be thrown during record update even in single thread mode.

To disable it use the system property cache.local.enabled by setting it at startup:

java ... -Dcache.local.enabled=false ...


or via code before to open the database:

OGlobalConfiguration.CACHE_LOCAL_ENABLED.setValue(false);