# Ticket Registry Locking

Source: https://apereo.github.io/cas/development/ticketing/Ticket-Registry-Locking.html

A number of ticket registries support advanced distributed locking operations for highly concurrent requests. In scenarios where the CAS server is under very heavy load, it is possible for multiple requests to attempt to alter the server state at the same time. For example, you might consider service-ticket requests for two separate applications simultaneously where the resulting tickets and operations would end up changing the state of the single sign-on session, the linked ticket-granting ticket and how it tracks the two applications. In such scenarios, CAS may be configured to acquire and enforce locking operations such that the sequence of executions and requests is handled atomically and proper data and state is preserved such that concurrent requests would not lead to data loss and missing updates.

Locking strategies and options are by default available for the default ticket registry implementation. In the event that the ticket registry technology does support distributed locking, CAS would defer to the distributed lock implementation that is provided and supported by the ticket registry itself. As always, you may begin to design your own locking strategy if the registry technology of your choice does not yet support it.

CAS lock implementations are generally backed by [Spring Integration](https://spring.io/projects/spring-integration), and its behavior can be controlled via CAS settings.

The following settings and properties are available from the CAS configuration catalog:

- `cas.ticket.registry.core.enable-locking` (Optional; type `Boolean`; default `true`): When set to true, registry operations will begin to support distributed locking for ticket operations. If the registry itself supports distributed locking, such as JDBC or Redis, then the lock implementation will defer to that option. Otherwise the default locking solution will be specific to a CAS server node, until replaced with a lock implementation or different locking option separate from the registry technology itself.
- `cas.ticket.registry.core.queue-identifier` (Optional; type `String`): Identifier for this CAS server node that tags the sender/receiver in the queue and avoid processing of inbound calls. If left blank, an identifier is generated automatically and kept in memory.

## Default

The default lock implementation, generally suitable for single-node deployments, is one where the *lock registry* uses *Masked Hashcode* algorithm to obtain and store locks in JVM memory. The default mask is `0xFF` which will create an array consisting of `1024 ReentrantLock` instances. When the lock repository attempts to obtain a lock for a given lock key, (i.e. ticket id), the index of the `Lock` is determined by masking the object’s hash code and the `Lock` is returned.

## Custom

To design your own locking implementation, you may inject the following `@Bean` into your CAS configuration:

```java
@Bean
public LockRepository casTicketRegistryLockRepository() {
    return new MyLockRepository();
}
```

[See this guide](https://apereo.github.io/cas/development/configuration/Configuration-Management-Extensions.html) to learn more about how to register configurations into the CAS runtime.

## Others

Distributed locking support is available for some, but not all, ticket registry implementations. Please refer to the documentation for each ticket registry implementation to learn whether support is available for distributed locking.
