Spring Boot Aerospike

What is Aerospike

Aerospike is a key value datastore Or Distributed Hashtable Or NoSQL Database written in C language. It also provides features of in-memory database so its also called in-memory NoSQL database. It basically merges the concept of in memory and No SQL database.

Support for Hybrid Architecture.

Aerospikes’ hybrid architecture helps us harness the power of,

Layers

Aerospike consists of 3 layers

Aerospike Installation

MAC Installation

For vagrant installation follow this URL

Docker Installation

I’ve used docker to install Aerospike on my machine

Commands

docker run -d --name aerospike -p 3000:3000 aerospike/aerospike-server

To bring up Management Console, run

docker run -d -p 8081:8081 mrbar42/aerospike-amc

URL: http://localhost:8081/
Then enter the IP of Aerospike container. To get IP use following command

docker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" container_id

Aerospike Spring Integration

Configuration

@Configuration
@EnableAerospikeRepositories(basePackages = "in.kamranali.aerospike.aerospike.repositories")
@EnableTransactionManagement
public class AerospikeConfig {

    @Bean
    AerospikeTemplate aerospikeTemplate(){
        return new AerospikeTemplate(aerospikeClient(), "test"); // test is namespace
    }

    @Bean
    AerospikeClient aerospikeClient() {

        ClientPolicy clientPolicy = new ClientPolicy();
        clientPolicy.failIfNotConnected = true;
        return new AerospikeClient(clientPolicy, "localhost", 3000);
    }
}

UserResource

@RestController
@RequestMapping("/rest/users")
public class UserResource {

    @Autowired
    private UserService userService;

    @GetMapping
    public List<User> getAllUsers(){

        return userService.getAllUsers();
    }

    @PostMapping
    public List<User> create(@RequestBody final User user){

        userService.create(user);
        return userService.getAllUsers();
    }
}

UserService

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public List<User> getAllUsers(){

        List<User> users = new ArrayList<>();
        userRepository.findAll()
                .forEach(users::add);
        return users;
    }

    public void create(User user) {

        userRepository.save(user);
    }
}

Repository

public interface UserRepository extends AerospikeRepository<User, Integer> {
}

User Model

public class User {

    @Id
    private Integer id;
    private String name;
    private Long salary;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getSalary() {
        return salary;
    }

    public void setSalary(Long salary) {
        this.salary = salary;
    }
}

Endpoints

GET http://localhost:8080/rest/users

POST http://localhost:8080/rest/users { “id”: 1, “name”: “Kamran”, “salary”: 1234 }

To access the full working code sample, click here

References

What is Aerospike? | Hybrid Architecture used by Flipkart | Tech Primers

SUBSCRIBE TO NEW CONTENT

Tutorials on System Design, BlockChain, Design Patterns and more sent out once a week.

Loading comments...