Building Grails applications with feature toggles using Togglz
Feature toggles have become a cornerstone technique for teams that want to ship code continuously without exposing every change to end users. In a Grails application, integrating a mature library like Togglz lets you separate deployment from release, giving you fine-grained control over which features are live in each region, environment, or customer segment. This matters enormously when you operate in a market like Australia, where banks, government agencies, and fintechs often demand evidence of controlled rollouts before changes reach production traffic.
The Togglz library originated in the Java ecosystem and fits naturally into any Spring or Spring Boot context, which means it works inside Grails with very little ceremony. In this guide, we will walk through the full setup, the toggle definition patterns, the runtime management story, and the testing approaches that keep your toggles honest. You will also see how the same code can serve teams running clusters on AWS Sydney regions or in on-premises data centres in Melbourne, with examples that respect the operational realities of Australian teams.
Understanding feature toggles and Togglz
Feature toggles, sometimes called feature flags, are conditional branches in your code that decide whether a particular capability is enabled at runtime. The decision can be static, driven by a configuration file, or dynamic, fetched from an external service. Togglz provides both modes and exposes a clean API for declaring flags, evaluating them, and managing their state across environments without forcing developers to invent their own plumbing.
For Australian teams working in regulated industries — say, a health platform serving patients in Brisbane or a payments processor reconciling transactions in AUD — toggles provide an audit trail that compliance teams genuinely appreciate. Every flag has a name, a description, and a state that can be queried later. That auditability is one reason Atlassian, originally a Sydney company, invested heavily in its own internal flag platform, and why local engineering groups often look for similar discipline when adopting open-source tooling.
Setting up Togglz in a Grails project
The first step is to add the Togglz core dependency and the Spring Boot starter to your build configuration. Because Grails 4 and later rely on Spring Boot under the hood, the integration is largely automatic once the beans are on the classpath. In a typical build.gradle file, you will add a line such as implementation 'org.togglz:togglz-spring-boot-starter:4.4.0' alongside the Grails BOM that you already use, and refresh the dependencies so that Gradle resolves the Togglz artefacts.
After the dependency is in place, you need to register a Spring configuration class that tells Togglz where to find your flag definitions and how to persist their state. The default in-memory state manager is fine for demos, but production deployments — particularly those targeting AWS Asia Pacific (Sydney) — usually point at a JDBC-backed feature state repository so that toggles survive container restarts and are shared across every node behind the load balancer. If you need to decouple long-running work from the request thread while you are reshaping controllers, the asynchronous controller patterns covered at Grails async controllers pair naturally with toggle-driven feature exposure.
Creating feature flag definitions
Togglz expects you to declare an enum that lists every feature your application understands. Each constant represents a flag, and the metadata you attach to it — label, description, and optional feature group — becomes visible in the admin console. For example, a personal finance application might declare flags such as NEW_DASHBOARD, REAL_TIME_NOTIFICATIONS, and EXPERIMENTAL_PRICING_ENGINE, each with a short operator-facing description that explains the business intent behind the change.
The enum also lets you attach default activation logic through the isActive method. You can, for instance, return true only when the JVM argument -Dapp.region=sydney is present, which is a simple way to run canary releases in one AWS region before opening the gate to other locations. For teams with Australian data-residency constraints, you can encode geography directly into the toggle so that certain capabilities never leave the Sydney region, regardless of who is asking or which partner integration has triggered the request.
Wrapping application logic with toggle checks
Once flags are declared, you can guard any piece of business logic with a call to FeatureContext.getFeatureManager().isActive(MyFeatures.NEW_DASHBOARD). In a Grails controller, that typically looks like an if statement around a service invocation, or a GSP <g:if> block around template fragments. The advantage is that disabled features remain compiled and unit-tested, so flipping a switch on never causes a runtime surprise or a NullPointerException buried deep in a legacy code path.
A practical pattern is to wrap domain-level behaviour in a service method that consults the flag before delegating to the legacy or new implementation. This keeps your controllers thin and your tests focused. Imagine a transaction categorisation service in a personal finance app — toggling between a rules-based categoriser and an ML-backed one becomes a single line of code rather than a configuration rewrite, and the older code path remains intact and tested for the day the flag is finally retired.
For UI changes, the togglz:feature tag library renders content only when the flag is on, which is handy when you want to grey out experimental menu items in a self-service portal. Combining this with a per-customer override is a common approach among Australian SaaS vendors who negotiate bespoke feature bundles with enterprise clients in Melbourne and Sydney, since the same binary can ship a different experience per tenant without separate branches.
Managing toggle states at runtime
Static configuration is fine for short-lived flags, but most teams eventually need a way to flip a feature without rebuilding or redeploying. Togglz ships with an admin console that can be enabled behind a secured URL, giving authorised operators a UI to switch states, view flag history, and inspect which code paths reference each flag. The console is essentially a control panel that turns a configuration change into a deliberate, auditable action.
In production, the admin console should sit behind your existing authentication layer. If you are using Spring Security in your Grails project, you can restrict the /togglz-console path to a role such as ROLE_RELEASE_MANAGER and require an additional check that the request originates from an Australian IP range or a corporate VPN. This is a small operational win that many local teams implement to satisfy internal audit requirements, particularly when the same toggle affects a capability that touches personal data.
For more sophisticated workflows, Togglz supports JDBC, Redis, and custom feature state providers. Redis is particularly attractive if you already run a managed instance in ap-southeast-2, since the toggle state then updates in milliseconds across every Grails node behind a load balancer. Teams that have built reactive pipelines on top of Groovy can also publish toggle changes over a websocket, which makes the rollout experience feel like a control room rather than a configuration change buried in a pull request.
Testing strategies for toggled features
Testing toggled code is not as simple as testing a single branch — you have to verify behaviour under both states. The recommended approach is to use TogglzRule in JUnit or Spock specifications, which lets each test specify the active flags before the feature manager is consulted. In a Spock feature method, you would call TogglzRule.enable(MyFeatures.NEW_DASHBOARD) in the setup block and then assert the new behaviour, with a corresponding disable call in the cleanup block.
Integration tests that boot the full Grails context can also override the feature state through a test-only configuration bean, which is useful when you want to assert the admin console wiring works correctly. End-to-end tests, particularly those executed against staging environments in Sydney, should always verify that disabling a flag hides the corresponding UI and rejects the corresponding REST endpoint, since release engineers in Australia are often the last line of defence before a feature hits production traffic during AEST business hours.
Practical recommendations for Togglz rollouts
- Start with a flag lifecycle policy: name flags consistently, attach owners, and retire any toggle once the underlying feature is fully released or removed from the codebase.
- Treat every long-lived toggle as a debt item; review them in your regular backlog grooming and aim to remove flags that have been at 100% for more than two release cycles.
- Default new flags to off in production, even when they are on in development; this avoids accidental exposure during deployment and matches the conservative posture expected by local compliance teams.
- Use per-environment configuration files rather than the admin console during local development; the console is for emergencies and controlled rollouts, not daily developer workflow.
- Mirror your flag names in application logs so that operators can grep for activation events when triaging an incident during the AEST on-call rotation.
- Document the blast radius of each flag, including the services and customer segments affected, in a place that release managers in Sydney and Melbourne can both reach quickly.
- Wire toggles into your observability stack so that on and off transitions emit metrics; this gives product owners real data on adoption rather than guesswork.
Feature toggles are most valuable when they fade into the background and let your team deploy without fear. With Togglz wired into your Grails project, you gain that safety net without leaving the Groovy language and Spring ecosystem that you have already invested in. Spend an afternoon standing up the library in a side branch, ship a single flag through the full pipeline, and you will see how quickly the habit spreads across the rest of your application. When you are ready to dig into complementary techniques, the rest of the Grails Example tutorials walk through controllers, services, packaging, and cloud hosting patterns that fit naturally alongside the toggle workflow you have just built.