# Database

Sometimes you need a backend storage, i.e redis, which will keep your session data on server restarts and scale horizontally.

Registering a session database can be done through a single call of `sessions.UseDatabase(database)`.

Iris implements three (3) builtin session databases for [redis](https://redis.io/), [badger](https://github.com/dgraph-io/badger) and [boltdb](https://github.com/kataras/iris-book/tree/949c38a02420899aa31dbb029b89f63a3b3846af/sessions/github.com/etcd-io/bbolt/README.md). These session databases are implemented via the [iris/sessions/sessiondb](https://github.com/kataras/iris/tree/main/sessions/sessiondb) subpackage which you'll have to import.

1. import the `gitbub.com/kataras/iris/sessions/sessiondb/redis or boltdb or badger`,
2. initialize the database and
3. register it

For example, to register the redis session database:

```go
import (
    "github.com/kataras/iris/v12"
    "github.com/kataras/iris/v12/sessions"

    // 1. Import the session database.
    "github.com/kataras/iris/v12/sessions/sessiondb/redis"
)

// 2. Initialize the database.
// These are the default values,
// you can replace them based on your running redis' server settings:
db := redis.New(redis.Config{
    Network:   "tcp",
    Addr:      "127.0.0.1:6379",
    Timeout:   time.Duration(30) * time.Second,
    MaxActive: 10,
    Password:  "",
    Database:  "",
    Prefix:    "",
    Delim:     "-",
    Driver:    redis.Redigo(), // redis.Radix() can be used instead.
})

// Optionally configure the underline driver:
// driver := redis.Redigo()
// driver.MaxIdle = ...
// driver.IdleTimeout = ...
// driver.Wait = ...
// redis.Config {Driver: driver}

// Close connection when control+C/cmd+C
iris.RegisterOnInterrupt(func() {
    db.Close()
})

sess := sessions.New(sessions.Config{
    Cookie:       "sessionscookieid",
    AllowReclaim: true,
})

// 3. Register it.
sess.UseDatabase(db)

// [...]
```

**boltdb**

```go
import "os"
import "github.com/kataras/iris/v12/sessions/sessiondb/boltdb"

db, err := boltdb.New("./sessions.db", os.FileMode(0750))
```

**badger**

```go
import "github.com/kataras/iris/v12/sessions/sessiondb/badger"

db, err := badger.New("./data")
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://iris-go.gitbook.io/iris/contents/sessions/database.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
