---
title: Table
summary: null
url: https://www.fastly.com/documentation/reference/vcl/declarations/table
---

A `table` declaration creates a key-value store in VCL code.
Table keys are always of type `STRING`.
Values may be one of a number of VCL types,
but must all be of the same type within the same table.

Tables may be declared using VCL, but can also be created via an
[API call](https://www.fastly.com/documentation/reference/api/dictionaries/dictionary/#create-dictionary),
using the CLI, or using the
[web interface](https://www.fastly.com/documentation/guides/full-site-delivery/dictionaries/working-with-dictionaries).

Tables created via API, CLI, or web interface are called
[dictionaries](https://www.fastly.com/documentation/guides/concepts/edge-state/dynamic-config/#dictionaries)
and are dynamic (i.e., the items in the dictionary may be changed without
cloning and activating a new version of your service).

## Literal keys

The most common kind of table has literal `STRING` keys and literal `STRING` values.
The syntax looks like this:

```vcl
table redirects {
  "/old/path": "https://other.hostname/new/path",
  "/another/path": "/new/path",
}
```

See the documentation on [types](https://www.fastly.com/documentation/reference/vcl/types)
for details on the syntax for `STRING` literals.

A trailing comma after the final table entry is optional, but supported.

The `table.contains` function will return a `BOOL` indicating whether a specified
key is in the table.
The `table.lookup` function will return the corresponding `STRING` value.

## Regex keys

You can also have regex keys in a table.
You can freely mix regex keys with literal string keys.
Regex keys are indicated with `~` before the key.

```vcl
table extensions {
    # a literal string key
    ".epub": "application/epub+zip",

    # some regex keys
    # notice escaping the \. in regex syntax to match a literal dot character
  ~ "\.html?": "text/html", # matches ".htm", ".html"
  ~ "\.jpe?g": "image/jpeg", # matches ".jpg", ".jpeg"

    # a mix of both
    ".mp3":    "audio/mpeg",
    ".gif":    "image/gif",
    ".svg":    "image/svg+xml",
  ~ "\.tiff?": "image/tiff", # matches ".tif", ".tiff"
    ".webp":   "image/webp",
}
```

Table lookup behaves as if you had written every possibility for each key out explicitly,
in the above example `table.lookup` and `table.contains` behave as if `.jpg` and `.jpeg` are
both present as literal keys in the table.

The regex syntax permitted in table keys is a strict subset of the
[PCRE](https://www.pcre.org/current/doc/html/) syntax used for the `~` operator in general.
Several features are not supported.
These include the PCRE lazy and greedy quantifiers, word boundary specifiers,
conditional patterns, control over backtracking, and backreferences.
These will result in a compile time error.

Duplicate literal keys are not permitted.
Likewise regex keys may not have ambigious matches.
For example the regex keys `~"a(b?)"` and `~"a?b+"` would both match the string `ab`,
and this is disallowed.

A key matching the empty string is allowed, this is a `STRING` value like any other.

Lookup for regex keys is still done using `table.contains`
and whichever lookup function suits the value type,
the same as for literal keys.

There is no performance cost for regex keys compared to string literal keys.
They both run in _O(1)_ time relative to the number of entries in the table.

## Anchoring for regex keys

Regex keys in VCL tables are implicitly anchored as if by writing `^` `$` at each end.
For example, `if (req.http.x ~ "abc") { }` will match `"xxxabc"`
but the regex key `~ "ab+c*": ...` will not match `"xxxabc"` in a table.

Another example:

```vcl
table t INTEGER {
    "/abc":          1, # equivalent to ^/abc$
  ~ "/def":          2, # anchored: only the exact string "/def" matches
  ~ "[^/]*/hi[^/]*": 3, # want unanchored behavior? add an explicit prefix/suffix
  ~ "^(?:/x?y|z)$":  4,
}
```

In this example a lookup of `"/def"` returns `2`, but a lookup of `"xx/defxx"` does not match that key.

If you do want to match a substring, match the prefix and suffix explicitly.
Usually you don't want `.*` for this, because it would also match part of the middle section too.
What to write here exactly depends on the string you're matching.
In the example here we're matching path-style filenames, and so the prefix and suffix have `[^/]*`
to match any number of characters up to a following `/`.
See our advice on [best practices for regexes](https://www.fastly.com/documentation/reference/vcl/regex/#best-practices-and-common-mistakes).

This behavior differs from the `~` operator for regex conditionals.
When you match with the `~` operator, the pattern is unanchored.
For example, `req.url ~ "/def"` matches any URL that contains `/def` anywhere.

## Group capture for keys

You can use group capture in the regex syntax for table keys.
For example:

```vcl
table redirects {
    # some regex keys with group capture
    # not all regexes need to have the same number of capturing groups
  ~ "/images/([^.]*)\.gif": "/img/\1.png",
  ~ "/articles/([0-9]{1,2})-([0-9]{1,2})-([0-9]{4})/([^/]*)/?": "/articles/\3-\2-\1/\4/",

    # a literal key behaves as if there are no captures other than \0
    "/about": "/old-content\0",
}
```

Lookup using `table.lookup_interpolate` behaves as `table.lookup`,
but captured substrings from the matching key can be inserted into the value string
using `\0` through `\9` or `\{0}` through `\{19}`.
`\0` is available for literal keys.

The `\` syntax for interpolation in values is the same as for `reginterpolate`,
see the documentation for `reginterpolate` for details.
See `table.lookup_interpolate` for more examples using group capture in regex keys.

Matching with a regex key does not set `re.group.{N}` as a side effect.
This differs from the `~` operator for regex conditions.
Any existing values for `re.group.{N}` will be preserved after a call to
all the table lookup functions, including `table.lookup_interpolate`,
and none of these behave differently.
This way the behaviour is always the same even if the matched key uses
capturing groups or not.

You can only use capturing groups for regex keys in tables with `STRING` values.
Capturing groups are disallowed for regex keys in tables with other value types,
because without string interpolation in the value there would be no way to
pick up the captured substrings from the key match.

## Values

Values in a table have a `STRING` type by default.
You can override this with an optional explicit type specifier.

A table with an explicit [type](https://www.fastly.com/documentation/reference/vcl/types)
for values is declared like this:

```vcl
table routing_table BACKEND {
  "a.example.com": b0,
  "b.example.com": b1,
  "c.example.com": b2,
}
```

The syntax for a table value differs for each type.
See [the documentation on types](https://www.fastly.com/documentation/reference/vcl/types/)
for examples.

Valid types are listed below.
All values within the same table are of the same type.

The following functions perform table lookup and return values of the appropriate type:

| Value type | Lookup function            | Notes                                              |
| :--------- | :------------------------- | :------------------------------------------------- |
| `STRING`   | `table.lookup`             | The default value type                             |
| `STRING`   | `table.lookup_interpolate` | For group capture interpolation on `STRING` values |
| `ACL`      | `table.lookup_acl`         |                                                    |
| `BACKEND`  | `table.lookup_backend`     |                                                    |
| `BOOL`     | `table.lookup_bool`        |                                                    |
| `FLOAT`    | `table.lookup_float`       |                                                    |
| `INTEGER`  | `table.lookup_integer`     |                                                    |
| `IP`       | `table.lookup_ip`          |                                                    |
| `REGEX`    | `table.lookup_regex`       |                                                    |
| `RTIME`    | `table.lookup_rtime`       |                                                    |

You can use all of these lookup functions for tables with both literal keys and with regex keys,
there isn't a special lookup function for regex keys.

## Example

Redirects are a very common use case for tables:

[Try it in Fastly Fiddle](https://fiddle.fastly.dev/95954fd2)

## Limitations

All tables combined support up to 100,000 items.
If you need a larger limit than the default,
get in touch with [Fastly support](https://support.fastly.com/)
and we'll be glad to assist you.
